Add a country code dimension to the non-normalized number counter

This commit is contained in:
Jon Chambers 2022-08-10 14:49:08 -04:00 committed by Jon Chambers
parent 2bfe2c8ff8
commit 4d78437fe4
1 changed files with 17 additions and 7 deletions

View File

@ -5,23 +5,33 @@
package org.whispersystems.textsecuregcm.mappers;
import io.micrometer.core.instrument.Counter;
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import io.micrometer.core.instrument.Metrics;
import org.whispersystems.textsecuregcm.util.NonNormalizedPhoneNumberException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
import org.whispersystems.textsecuregcm.util.NonNormalizedPhoneNumberException;
public class NonNormalizedPhoneNumberExceptionMapper implements ExceptionMapper<NonNormalizedPhoneNumberException> {
private static final Counter NON_NORMALIZED_NUMBER_COUNTER =
Metrics.counter(name(NonNormalizedPhoneNumberExceptionMapper.class, "nonNormalizedNumbers"));
private static final String NON_NORMALIZED_NUMBER_COUNTER_NAME =
name(NonNormalizedPhoneNumberExceptionMapper.class, "nonNormalizedNumbers");
@Override
public Response toResponse(final NonNormalizedPhoneNumberException exception) {
NON_NORMALIZED_NUMBER_COUNTER.increment();
String countryCode;
try {
countryCode =
String.valueOf(PhoneNumberUtil.getInstance().parse(exception.getOriginalNumber(), null).getCountryCode());
} catch (final NumberParseException ignored) {
countryCode = "unknown";
}
Metrics.counter(NON_NORMALIZED_NUMBER_COUNTER_NAME, "countryCode", countryCode).increment();
return Response.status(Status.BAD_REQUEST)
.entity(new NonNormalizedPhoneNumberResponse(exception.getOriginalNumber(), exception.getNormalizedNumber()))