Add source tag to appstore API errors

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-01-06 15:47:05 -05:00
1 parent 7f5097bd33
commit 9a43f76663
3 files changed
+20 -15

No files matched your search

@@ -28,6 +28,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import io.micrometer.core.instrument.Tags;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.metrics.MetricsUtil;
import org.whispersystems.textsecuregcm.util.ResilienceUtil;
@@ -122,14 +123,14 @@ public class AppleAppStoreClient {
}
}
public StatusResponse getAllSubscriptions(final String originalTransactionId)
public StatusResponse getAllSubscriptions(final String originalTransactionId, final Tags errorTags)
throws SubscriptionNotFoundException, SubscriptionInvalidArgumentsException, RateLimitExceededException {
try {
return retry.executeCallable(() -> {
try {
return getAllSubscriptionsHelper(defaultEnvironment, originalTransactionId);
} catch (final APIException e) {
Metrics.counter(GET_SUBSCRIPTION_ERROR_COUNTER_NAME, "reason", e.getApiError().name()).increment();
Metrics.counter(GET_SUBSCRIPTION_ERROR_COUNTER_NAME, errorTags.and("reason", e.getApiError().name())).increment();
throw switch (e.getApiError()) {
case TRANSACTION_ID_NOT_FOUND, ORIGINAL_TRANSACTION_ID_NOT_FOUND -> new SubscriptionNotFoundException();
case RATE_LIMIT_EXCEEDED -> new RateLimitExceededException(null);
@@ -16,6 +16,7 @@ import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import io.micrometer.core.instrument.Tags;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
@@ -32,6 +33,8 @@ public class AppleAppStoreManager implements SubscriptionPaymentProcessor {
private static final Logger logger = LoggerFactory.getLogger(AppleAppStoreManager.class);
private static final String LOOKUP_TYPE_TAG = "lookup_type";
private final AppleAppStoreClient appleAppStoreClient;
private final Map<String, Long> productIdToLevel;
private final String subscriptionGroupId;
@@ -63,7 +66,7 @@ public class AppleAppStoreManager implements SubscriptionPaymentProcessor {
*/
public Long validateTransaction(final String originalTransactionId)
throws SubscriptionInvalidArgumentsException, RateLimitExceededException, SubscriptionNotFoundException, SubscriptionPaymentRequiredException {
final AppleAppStoreDecodedTransaction tx = lookupAndValidateTransaction(originalTransactionId);
final AppleAppStoreDecodedTransaction tx = lookupAndValidateTransaction(originalTransactionId, Tags.of(LOOKUP_TYPE_TAG, "validate"));
if (!isSubscriptionActive(tx)) {
throw new SubscriptionPaymentRequiredException();
}
@@ -87,7 +90,7 @@ public class AppleAppStoreManager implements SubscriptionPaymentProcessor {
public void cancelAllActiveSubscriptions(String originalTransactionId)
throws SubscriptionInvalidArgumentsException, RateLimitExceededException {
try {
final AppleAppStoreDecodedTransaction tx = lookup(originalTransactionId);
final AppleAppStoreDecodedTransaction tx = lookup(originalTransactionId, Tags.of(LOOKUP_TYPE_TAG, "cancel"));
if (tx.signedTransaction().getStatus() != Status.EXPIRED &&
tx.signedTransaction().getStatus() != Status.REVOKED &&
tx.renewalInfo().getAutoRenewStatus() != AutoRenewStatus.OFF) {
@@ -102,7 +105,7 @@ public class AppleAppStoreManager implements SubscriptionPaymentProcessor {
@Override
public SubscriptionInformation getSubscriptionInformation(final String originalTransactionId)
throws RateLimitExceededException, SubscriptionNotFoundException {
final AppleAppStoreDecodedTransaction tx = lookup(originalTransactionId);
final AppleAppStoreDecodedTransaction tx = lookup(originalTransactionId, Tags.of(LOOKUP_TYPE_TAG, "info"));
final SubscriptionStatus status = switch (tx.signedTransaction().getStatus()) {
case ACTIVE -> SubscriptionStatus.ACTIVE;
case BILLING_RETRY -> SubscriptionStatus.PAST_DUE;
@@ -128,7 +131,7 @@ public class AppleAppStoreManager implements SubscriptionPaymentProcessor {
@Override
public ReceiptItem getReceiptItem(String originalTransactionId)
throws RateLimitExceededException, SubscriptionNotFoundException, SubscriptionPaymentRequiredException {
final AppleAppStoreDecodedTransaction tx = lookup(originalTransactionId);
final AppleAppStoreDecodedTransaction tx = lookup(originalTransactionId, Tags.of(LOOKUP_TYPE_TAG, "receipt"));
if (!isSubscriptionActive(tx)) {
throw new SubscriptionPaymentRequiredException();
}
@@ -143,19 +146,19 @@ public class AppleAppStoreManager implements SubscriptionPaymentProcessor {
}
private AppleAppStoreDecodedTransaction lookup(final String originalTransactionId)
private AppleAppStoreDecodedTransaction lookup(final String originalTransactionId, final Tags tags)
throws RateLimitExceededException, SubscriptionNotFoundException {
try {
return lookupAndValidateTransaction(originalTransactionId);
return lookupAndValidateTransaction(originalTransactionId, tags);
} catch (SubscriptionInvalidArgumentsException e) {
// Shouldn't happen because we previously validated this transactionId before storing it
throw new UncheckedIOException(new IOException(e));
}
}
private AppleAppStoreDecodedTransaction lookupAndValidateTransaction(final String originalTransactionId)
private AppleAppStoreDecodedTransaction lookupAndValidateTransaction(final String originalTransactionId, final Tags errorTags)
throws SubscriptionInvalidArgumentsException, RateLimitExceededException, SubscriptionNotFoundException {
final StatusResponse statuses = appleAppStoreClient.getAllSubscriptions(originalTransactionId);
final StatusResponse statuses = appleAppStoreClient.getAllSubscriptions(originalTransactionId, errorTags);
final SubscriptionGroupIdentifierItem item = statuses.getData().stream()
.filter(s -> subscriptionGroupId.equals(s.getSubscriptionGroupIdentifier())).findFirst()
.orElseThrow(() -> new SubscriptionInvalidArgumentsException("transaction did not contain a backup subscription", null));
@@ -26,6 +26,7 @@ import com.apple.itunes.storekit.verification.SignedDataVerifier;
import com.apple.itunes.storekit.verification.VerificationException;
import java.io.IOException;
import java.io.UncheckedIOException;
import io.micrometer.core.instrument.Tags;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -58,7 +59,7 @@ class AppleAppStoreClientTest {
when(productionClient.getAllSubscriptionStatuses(ORIGINAL_TX_ID, new Status[]{}))
.thenThrow(new APIException(404, error, "test"));
assertThatException().isThrownBy(() -> apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID));
assertThatException().isThrownBy(() -> apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID, Tags.empty()));
verifyNoInteractions(sandboxClient);
}
@@ -71,7 +72,7 @@ class AppleAppStoreClientTest {
when(sandboxClient.getAllSubscriptionStatuses(ORIGINAL_TX_ID, new Status[]{}))
.thenReturn(new StatusResponse().environment(Environment.SANDBOX));
final StatusResponse allSubscriptions = apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID);
final StatusResponse allSubscriptions = apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID, Tags.empty());
assertThat(allSubscriptions.getEnvironment()).isEqualTo(Environment.SANDBOX);
verify(productionClient).getAllSubscriptionStatuses(ORIGINAL_TX_ID, new Status[]{});
@@ -86,7 +87,7 @@ class AppleAppStoreClientTest {
.thenThrow(new APIException(404, APIError.ORIGINAL_TRANSACTION_ID_NOT_FOUND_RETRYABLE.errorCode(), "test"))
.thenThrow(new APIException(404, APIError.ORIGINAL_TRANSACTION_ID_NOT_FOUND_RETRYABLE.errorCode(), "test"))
.thenReturn(new StatusResponse().environment(Environment.PRODUCTION));
final StatusResponse statusResponse = apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID);
final StatusResponse statusResponse = apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID, Tags.empty());
assertThat(statusResponse.getEnvironment()).isEqualTo(Environment.PRODUCTION);
verifyNoInteractions(sandboxClient);
}
@@ -97,7 +98,7 @@ class AppleAppStoreClientTest {
when(productionClient.getAllSubscriptionStatuses(ORIGINAL_TX_ID, new Status[]{}))
.thenThrow(new APIException(404, APIError.ORIGINAL_TRANSACTION_ID_NOT_FOUND_RETRYABLE.errorCode(), "test"));
assertThatException()
.isThrownBy(() -> apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID))
.isThrownBy(() -> apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID, Tags.empty()))
.isInstanceOf(UncheckedIOException.class)
.withRootCauseInstanceOf(APIException.class);
@@ -116,7 +117,7 @@ class AppleAppStoreClientTest {
.thenThrow(new APIException(404, APIError.ORIGINAL_TRANSACTION_ID_NOT_FOUND_RETRYABLE.errorCode(), "test"))
.thenReturn(new StatusResponse().environment(Environment.SANDBOX));
final StatusResponse statusResponse = apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID);
final StatusResponse statusResponse = apiWrapper.getAllSubscriptions(ORIGINAL_TX_ID, Tags.empty());
assertThat(statusResponse.getEnvironment()).isEqualTo(Environment.SANDBOX);
verify(productionClient, times(3))