Skip to content

Commit

Permalink
Add metric for international PreKey requests
Browse files Browse the repository at this point in the history
  • Loading branch information
eager-signal committed Mar 29, 2021
1 parent 91bd061 commit c10d960
Showing 1 changed file with 33 additions and 17 deletions.
Expand Up @@ -4,8 +4,27 @@
*/
package org.whispersystems.textsecuregcm.controllers;

import static com.codahale.metrics.MetricRegistry.name;

import com.codahale.metrics.annotation.Timed;
import io.dropwizard.auth.Auth;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import io.micrometer.core.instrument.Metrics;
import org.whispersystems.textsecuregcm.auth.AmbiguousIdentifier;
import org.whispersystems.textsecuregcm.auth.Anonymous;
import org.whispersystems.textsecuregcm.auth.DisabledPermittedAccount;
Expand All @@ -22,23 +41,7 @@
import org.whispersystems.textsecuregcm.storage.AccountsManager;
import org.whispersystems.textsecuregcm.storage.Device;
import org.whispersystems.textsecuregcm.storage.KeysDynamoDb;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.whispersystems.textsecuregcm.util.Util;

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Path("/v2/keys")
Expand All @@ -49,6 +52,11 @@ public class KeysController {
private final AccountsManager accounts;
private final DirectoryQueue directoryQueue;

private static final String INTERNATIONAL_PREKEY_REQUEST_COUNTER_NAME =
name(KeysController.class, "internationalPreKeyGet");

private static final String SOURCE_COUNTRY_TAG_NAME = "sourceCountry";

public KeysController(RateLimiters rateLimiters, KeysDynamoDb keysDynamoDb, AccountsManager accounts, DirectoryQueue directoryQueue) {
this.rateLimiters = rateLimiters;
this.keysDynamoDb = keysDynamoDb;
Expand Down Expand Up @@ -119,6 +127,14 @@ public Optional<PreKeyResponse> getDeviceKeys(@Auth

if (account.isPresent()) {
rateLimiters.getPreKeysLimiter().validate(account.get().getNumber() + "." + account.get().getAuthenticatedDevice().get().getId() + "__" + target.get().getNumber() + "." + deviceId);

final String accountCountryCode = Util.getCountryCode(account.get().getNumber());
final String targetCountryCode = Util.getCountryCode(target.get().getNumber());

if (!accountCountryCode.equals(targetCountryCode)) {
Metrics.counter(INTERNATIONAL_PREKEY_REQUEST_COUNTER_NAME, SOURCE_COUNTRY_TAG_NAME, accountCountryCode)
.increment();
}
}

Map<Long, PreKey> preKeysByDeviceId = getLocalKeys(target.get(), deviceId);
Expand Down

0 comments on commit c10d960

Please sign in to comment.