Readd metrics integration with Librato (and small redis metrics)

This commit is contained in:
Colin McDonald 2016-07-10 12:45:13 -04:00
parent a61ad36151
commit b6f987e75a
6 changed files with 89 additions and 1 deletions

View File

@ -6,6 +6,9 @@ mongo.password=
redis.address=209.222.96.50 redis.address=209.222.96.50
redis.port=6379 redis.port=6379
http.port=80 http.port=80
librato.email=cmcdonald.main@gmail.com
librato.apiToken=a818c3eca8a59d6d9cf76dc9f0d237c6aa97f257c482ce3363cf55a5431bc153
librato.sourceIdentifier=apiv3-dev-01
mandrill.apiKey=0OYtwymqJP6oqvszeJu0vQ mandrill.apiKey=0OYtwymqJP6oqvszeJu0vQ
maxMind.userId=66817 maxMind.userId=66817
maxMind.maxMindLicenseKey=8Aw9NsOUeOp7 maxMind.maxMindLicenseKey=8Aw9NsOUeOp7

12
pom.xml
View File

@ -84,6 +84,11 @@
<artifactId>vertx-circuit-breaker</artifactId> <artifactId>vertx-circuit-breaker</artifactId>
<version>3.3.0</version> <version>3.3.0</version>
</dependency> </dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dropwizard-metrics</artifactId>
<version>3.3.0</version>
</dependency>
<!-- Google Libs --> <!-- Google Libs -->
<dependency> <dependency>
@ -114,6 +119,13 @@
<version>2.7.0</version> <version>2.7.0</version>
</dependency> </dependency>
<!-- Metrics -->
<dependency>
<groupId>com.librato.metrics</groupId>
<artifactId>metrics-librato</artifactId>
<version>4.1.2.5</version>
</dependency>
<!-- Totp --> <!-- Totp -->
<dependency> <dependency>
<groupId>com.warrenstrange</groupId> <groupId>com.warrenstrange</groupId>

View File

@ -1,5 +1,7 @@
package net.frozenorb.apiv3; package net.frozenorb.apiv3;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
@ -9,6 +11,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.net.MediaType; import com.google.common.net.MediaType;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.librato.metrics.LibratoReporter;
import com.mongodb.ConnectionString; import com.mongodb.ConnectionString;
import com.mongodb.MongoCredential; import com.mongodb.MongoCredential;
import com.mongodb.async.client.MongoClient; import com.mongodb.async.client.MongoClient;
@ -35,8 +38,10 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.handler.ActorAttributeHandler; import net.frozenorb.apiv3.handler.ActorAttributeHandler;
import net.frozenorb.apiv3.handler.AuthorizationHandler; import net.frozenorb.apiv3.handler.AuthorizationHandler;
import net.frozenorb.apiv3.handler.MetricsHandler;
import net.frozenorb.apiv3.model.*; import net.frozenorb.apiv3.model.*;
import net.frozenorb.apiv3.route.GETDumpsType; import net.frozenorb.apiv3.route.GETDumpsType;
import net.frozenorb.apiv3.route.GETMetrics;
import net.frozenorb.apiv3.route.GETWhoAmI; import net.frozenorb.apiv3.route.GETWhoAmI;
import net.frozenorb.apiv3.route.accessTokens.DELETEAccessTokensId; import net.frozenorb.apiv3.route.accessTokens.DELETEAccessTokensId;
import net.frozenorb.apiv3.route.accessTokens.GETAccessTokens; import net.frozenorb.apiv3.route.accessTokens.GETAccessTokens;
@ -121,6 +126,7 @@ public final class APIv3 extends AbstractVerticle {
vertxInstance = vertx; vertxInstance = vertx;
setupConfig(); setupConfig();
setupDatabase(); setupDatabase();
setupMetrics();
setupHttpServer(); setupHttpServer();
/*V2Importer converter = new V2Importer("mongodb://158.69.126.126", "minehq"); /*V2Importer converter = new V2Importer("mongodb://158.69.126.126", "minehq");
@ -233,6 +239,19 @@ public final class APIv3 extends AbstractVerticle {
return mongoJacksonMapper; return mongoJacksonMapper;
} }
private void setupMetrics() {
MetricRegistry registry = SharedMetricRegistries.getOrCreate("apiv3-registry");
LibratoReporter.enable(
LibratoReporter.builder(
registry,
config.getProperty("librato.email"),
config.getProperty("librato.apiToken"),
config.getProperty("librato.sourceIdentifier")),
10,
TimeUnit.SECONDS);
}
private void setupHttpServer() { private void setupHttpServer() {
HttpServer webServer = vertx.createHttpServer( HttpServer webServer = vertx.createHttpServer(
new HttpServerOptions() new HttpServerOptions()
@ -246,6 +265,7 @@ public final class APIv3 extends AbstractVerticle {
http.route().handler(LoggerHandler.create(LoggerFormat.TINY)); http.route().handler(LoggerHandler.create(LoggerFormat.TINY));
http.route().handler(TimeoutHandler.create(TimeUnit.SECONDS.toMillis(5))); http.route().handler(TimeoutHandler.create(TimeUnit.SECONDS.toMillis(5)));
http.route().handler(new ActorAttributeHandler()); http.route().handler(new ActorAttributeHandler());
http.route().handler(new MetricsHandler());
http.route().handler(new AuthorizationHandler()); http.route().handler(new AuthorizationHandler());
http.route().method(HttpMethod.PUT).method(HttpMethod.POST).method(HttpMethod.DELETE).handler(BodyHandler.create()); http.route().method(HttpMethod.PUT).method(HttpMethod.POST).method(HttpMethod.DELETE).handler(BodyHandler.create());
http.exceptionHandler(Throwable::printStackTrace); http.exceptionHandler(Throwable::printStackTrace);
@ -346,6 +366,7 @@ public final class APIv3 extends AbstractVerticle {
http.post("/users/:userId/verifyTotp").handler(new POSTUsersIdVerifyTotp()); http.post("/users/:userId/verifyTotp").handler(new POSTUsersIdVerifyTotp());
http.get("/dumps/:dumpType").handler(new GETDumpsType()); http.get("/dumps/:dumpType").handler(new GETDumpsType());
http.get("/metrics").handler(new GETMetrics());
http.get("/whoami").handler(new GETWhoAmI()); http.get("/whoami").handler(new GETWhoAmI());
int port = Integer.parseInt(config.getProperty("http.port")); int port = Integer.parseInt(config.getProperty("http.port"));

View File

@ -1,6 +1,8 @@
package net.frozenorb.apiv3; package net.frozenorb.apiv3;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.dropwizard.DropwizardMetricsOptions;
final class Main { final class Main {
@ -8,7 +10,9 @@ final class Main {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "error"); System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "error");
System.setProperty("org.slf4j.simpleLogger.showThreadName", "false"); System.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory"); System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory");
Vertx.vertx().deployVerticle(new APIv3()); Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().setEnabled(true).setRegistryName("apiv3-registry")
)).deployVerticle(new APIv3());
} }
} }

View File

@ -0,0 +1,30 @@
package net.frozenorb.apiv3.handler;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
import net.frozenorb.apiv3.APIv3;
public final class MetricsHandler implements Handler<RoutingContext> {
private static final RedisClient redisClient = RedisClient.create(APIv3.getVertxInstance(),
new RedisOptions()
.setAddress(APIv3.getConfig().getProperty("redis.address"))
.setPort(Integer.parseInt(APIv3.getConfig().getProperty("redis.port")))
);
@Override
public void handle(RoutingContext ctx) {
redisClient.incr("apiv3:requests:total", (totalUpdateResult) -> {
if (totalUpdateResult.failed()) {
totalUpdateResult.cause().printStackTrace();
}
});
// we purposely just immediately go on, we don't really care if
// this fails and are fine to just go on without it.
ctx.next();
}
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.route;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.dropwizard.MetricsService;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
public final class GETMetrics implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
MetricsService metricsService = MetricsService.create(APIv3.getVertxInstance());
JsonObject metrics = metricsService.getMetricsSnapshot(APIv3.getVertxInstance());
APIv3.respondJson(ctx, 200, metrics);
}
}