diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClient.java b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClient.java index 080518de5..dca1bb633 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClient.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClient.java @@ -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); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreManager.java index 5a8fcab3c..fafc1ef7f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreManager.java @@ -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 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)); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClientTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClientTest.java index fe01adfd8..83e60395f 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClientTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/AppleAppStoreClientTest.java @@ -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))