Add timed metrics to accounts manager
This commit is contained in:
parent
bb0d26e116
commit
768b52e517
|
@ -17,6 +17,9 @@
|
|||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.SharedMetricRegistries;
|
||||
import com.codahale.metrics.Timer;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
|
@ -29,6 +32,7 @@ import org.slf4j.LoggerFactory;
|
|||
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
||||
import org.whispersystems.textsecuregcm.hystrix.GroupKeys;
|
||||
import org.whispersystems.textsecuregcm.redis.ReplicatedJedisPool;
|
||||
import org.whispersystems.textsecuregcm.util.Constants;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
|
||||
|
@ -36,12 +40,24 @@ import java.io.IOException;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.codahale.metrics.MetricRegistry.name;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
public class AccountsManager {
|
||||
|
||||
private static final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||
private static final Timer createTimer = metricRegistry.timer(name(AccountsManager.class, "create" ));
|
||||
private static final Timer updateTimer = metricRegistry.timer(name(AccountsManager.class, "update" ));
|
||||
private static final Timer getTimer = metricRegistry.timer(name(AccountsManager.class, "get" ));
|
||||
|
||||
private static final Timer redisSetTimer = metricRegistry.timer(name(AccountsManager.class, "redisSet" ));
|
||||
private static final Timer redisGetTimer = metricRegistry.timer(name(AccountsManager.class, "redisGet" ));
|
||||
|
||||
private static final Timer databaseCreateTimer = metricRegistry.timer(name(AccountsManager.class, "databaseCreate"));
|
||||
private static final Timer databaseGetTimer = metricRegistry.timer(name(AccountsManager.class, "databaseGet" ));
|
||||
private static final Timer databaseUpdateTimer = metricRegistry.timer(name(AccountsManager.class, "databaseUpdate"));
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(AccountsManager.class);
|
||||
|
||||
private final Accounts accounts;
|
||||
|
@ -69,20 +85,25 @@ public class AccountsManager {
|
|||
}
|
||||
|
||||
public boolean create(Account account) {
|
||||
try (Timer.Context context = createTimer.time()) {
|
||||
boolean freshUser = databaseCreate(account);
|
||||
redisSet(account.getNumber(), account, false);
|
||||
updateDirectory(account);
|
||||
|
||||
return freshUser;
|
||||
}
|
||||
}
|
||||
|
||||
public void update(Account account) {
|
||||
try (Timer.Context context = updateTimer.time()) {
|
||||
redisSet(account.getNumber(), account, false);
|
||||
databaseUpdate(account);
|
||||
updateDirectory(account);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> get(String number) {
|
||||
try (Timer.Context context = getTimer.time()) {
|
||||
Optional<Account> account = redisGet(number);
|
||||
|
||||
if (!account.isPresent()) {
|
||||
|
@ -92,6 +113,7 @@ public class AccountsManager {
|
|||
|
||||
return account;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDirectory(Account account) {
|
||||
new HystrixCommand<Void>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GroupKeys.DIRECTORY_SERVICE))
|
||||
|
@ -117,9 +139,9 @@ public class AccountsManager {
|
|||
}
|
||||
|
||||
private void redisSet(String number, Account account, boolean optional) {
|
||||
try (Timer.Context context = redisSetTimer.time()) {
|
||||
new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GroupKeys.REDIS_CACHE))
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(AccountsManager.class.getSimpleName() + ".redisSet")))
|
||||
{
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(AccountsManager.class.getSimpleName() + ".redisSet"))) {
|
||||
@Override
|
||||
protected Boolean run() {
|
||||
try (Jedis jedis = cacheClient.getWriteResource()) {
|
||||
|
@ -138,8 +160,10 @@ public class AccountsManager {
|
|||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<Account> redisGet(String number) {
|
||||
try (Timer.Context context = redisGetTimer.time()) {
|
||||
return new HystrixCommand<Optional<Account>>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GroupKeys.REDIS_CACHE))
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(AccountsManager.class.getSimpleName() + ".redisGet")))
|
||||
{
|
||||
|
@ -162,8 +186,10 @@ public class AccountsManager {
|
|||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<Account> databaseGet(String number) {
|
||||
try (Timer.Context context = databaseGetTimer.time()) {
|
||||
return new HystrixCommand<Optional<Account>>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GroupKeys.DATABASE_ACCOUNTS))
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(AccountsManager.class.getSimpleName() + ".databaseGet")))
|
||||
{
|
||||
|
@ -173,8 +199,10 @@ public class AccountsManager {
|
|||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean databaseCreate(Account account) {
|
||||
try (Timer.Context context = databaseCreateTimer.time()) {
|
||||
return new HystrixCommand<Boolean>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GroupKeys.DATABASE_ACCOUNTS))
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(AccountsManager.class.getSimpleName() + ".databaseCreate")))
|
||||
{
|
||||
|
@ -184,8 +212,10 @@ public class AccountsManager {
|
|||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
|
||||
private void databaseUpdate(Account account) {
|
||||
try (Timer.Context context = databaseUpdateTimer.time()) {
|
||||
new HystrixCommand<Void>(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GroupKeys.DATABASE_ACCOUNTS))
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(AccountsManager.class.getSimpleName() + ".databaseUpdate")))
|
||||
{
|
||||
|
@ -197,3 +227,4 @@ public class AccountsManager {
|
|||
}.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue