mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-19 06:28:07 +01:00
Instrument credits spent
This commit is contained in:
committed by
ravi-signal
parent
fb1c20582e
commit
c69abb5200
@@ -20,6 +20,8 @@ import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import javax.annotation.Nullable;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.whispersystems.textsecuregcm.http.FaultTolerantHttpClient;
|
||||
import org.whispersystems.textsecuregcm.metrics.MetricsUtil;
|
||||
@@ -42,6 +44,9 @@ public class HlrLookupCarrierDataProvider implements CarrierDataProvider {
|
||||
private static final ObjectMapper OBJECT_MAPPER = SystemMapper.jsonMapper();
|
||||
|
||||
private static final String REQUEST_COUNTER_NAME = MetricsUtil.name(HlrLookupCarrierDataProvider.class, "request");
|
||||
private static final String RESULT_COUNTER_NAME = MetricsUtil.name(HlrLookupCarrierDataProvider.class, "result");
|
||||
|
||||
private static final String CREDITS_SPENT_TAG_NAME = "creditsSpent";
|
||||
|
||||
public HlrLookupCarrierDataProvider(final String apiKey,
|
||||
final String apiSecret,
|
||||
@@ -126,9 +131,17 @@ public class HlrLookupCarrierDataProvider implements CarrierDataProvider {
|
||||
final HlrLookupResult result = response.results().getFirst();
|
||||
|
||||
if (!result.error().equals("NONE")) {
|
||||
Metrics.counter(RESULT_COUNTER_NAME, Tags.of(
|
||||
Tag.of("error", result.error()),
|
||||
getCreditsSpentTag(result)))
|
||||
.increment();
|
||||
|
||||
throw new CarrierDataException("Received a per-number error: " + result.error());
|
||||
}
|
||||
|
||||
Metrics.counter(RESULT_COUNTER_NAME, Tags.of(getCreditsSpentTag(result)))
|
||||
.increment();
|
||||
|
||||
return getNetworkDetails(result)
|
||||
.map(networkDetails -> new CarrierData(
|
||||
networkDetails.name(),
|
||||
@@ -137,6 +150,14 @@ public class HlrLookupCarrierDataProvider implements CarrierDataProvider {
|
||||
mncFromMccMnc(networkDetails.mccmnc())));
|
||||
}
|
||||
|
||||
private static Tag getCreditsSpentTag(final HlrLookupResult hlrLookupResult) {
|
||||
// HLR Lookup's docs at https://www.hlrlookup.com/knowledge/full-api-result suggest:
|
||||
//
|
||||
// > Please parse the full float to 1 decimal place to allow for future changes (e.g. service which consumes 1.5 or
|
||||
// > 0.5 credits).
|
||||
return Tag.of(CREDITS_SPENT_TAG_NAME, "%.1f".formatted(hlrLookupResult.creditsSpent()));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static Optional<String> mccFromMccMnc(@Nullable final String mccMnc) {
|
||||
// MCCs are always 3 digits
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||
record HlrLookupResult(String error,
|
||||
float creditsSpent,
|
||||
String originalNetwork,
|
||||
NetworkDetails originalNetworkDetails,
|
||||
String currentNetwork,
|
||||
|
||||
Reference in New Issue
Block a user