Even more metrics

This commit is contained in:
Colin McDonald 2016-05-06 21:00:29 -04:00
parent 065c55e0b4
commit 6a1c86ee5a
7 changed files with 25 additions and 47 deletions

View File

@ -1,4 +1,3 @@
general.id=main
general.releaseStage=production general.releaseStage=production
logging.level=info logging.level=info
mongo.address=ds055505.mongolab.com mongo.address=ds055505.mongolab.com
@ -14,8 +13,6 @@ http.workerThreads=6
twillio.accountSID=AC9e2f88c5690134d29a56f698de3cd740 twillio.accountSID=AC9e2f88c5690134d29a56f698de3cd740
twillio.authToken=982592505a171d3be6b0722f5ecacc0e twillio.authToken=982592505a171d3be6b0722f5ecacc0e
mandrill.apiKey=0OYtwymqJP6oqvszeJu0vQ mandrill.apiKey=0OYtwymqJP6oqvszeJu0vQ
librato.email=cmcdonald.main@gmail.com
librato.apiKey=a818c3eca8a59d6d9cf76dc9f0d237c6aa97f257c482ce3363cf55a5431bc153
bugsnag.apiKey=0e47fba8b825416b7cbc839066184509 bugsnag.apiKey=0e47fba8b825416b7cbc839066184509
auth.permittedUserRanks=developer,owner auth.permittedUserRanks=developer,owner
auth.websiteApiKey=RVbp4hY6sCFVaf auth.websiteApiKey=RVbp4hY6sCFVaf

View File

@ -79,9 +79,9 @@
<version>2.6.2</version> <version>2.6.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.librato.metrics</groupId> <groupId>com.indeed</groupId>
<artifactId>librato-java</artifactId> <artifactId>java-dogstatsd-client</artifactId>
<version>1.0.13</version> <version>2.0.12</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.bugsnag</groupId> <groupId>com.bugsnag</groupId>

View File

@ -3,9 +3,10 @@ package net.frozenorb.apiv3;
import com.bugsnag.Client; import com.bugsnag.Client;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.librato.metrics.*;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
import com.timgroup.statsd.NonBlockingStatsDClient;
import com.timgroup.statsd.StatsDClient;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.filters.*; import net.frozenorb.apiv3.filters.*;
@ -58,8 +59,7 @@ public final class APIv3 {
@Getter private static Datastore datastore; @Getter private static Datastore datastore;
@Getter private static Properties config = new Properties(); @Getter private static Properties config = new Properties();
@Getter private static JedisPool redisPool; @Getter private static JedisPool redisPool;
@Getter private static HttpPoster libratoPoster; @Getter private static StatsDClient statsD;
@Getter private static Client bugsnagClient;
@Getter private static final Gson gson = new GsonBuilder() @Getter private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()) .registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter())
.setExclusionStrategies(new FollowAnnotationExclusionStrategy()) .setExclusionStrategies(new FollowAnnotationExclusionStrategy())
@ -71,11 +71,9 @@ public final class APIv3 {
setupDatabase(); setupDatabase();
setupRedis(); setupRedis();
setupLibrato(); setupMetrics();
setupBugsnag(); setupBugsnag();
setupHttp(); setupHttp();
log.info("APIv3 booted.");
} }
private void setupConfig() { private void setupConfig() {
@ -116,42 +114,24 @@ public final class APIv3 {
); );
} }
private void setupLibrato() { private void setupMetrics() {
try { statsD = new NonBlockingStatsDClient(null, "localhost", 8125);
libratoPoster = new DefaultHttpPoster(
"https://metrics-api.librato.com/v1/metrics",
config.getProperty("librato.email"),
config.getProperty("librato.apiKey")
);
} catch (Exception e) {
e.printStackTrace();
}
new Timer("Librato Post Task").scheduleAtFixedRate(new TimerTask() { new Timer("Librato Post Task").scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run() { public void run() {
LibratoBatch batch = new LibratoBatch(300, Sanitizer.NO_OP, 10, TimeUnit.SECONDS, "qLib", libratoPoster); statsD.recordGaugeValue("apiv3.memory.usage", Runtime.getRuntime().totalMemory() / (1024 * 1024));
statsD.recordGaugeValue("apiv3.memory.max", Runtime.getRuntime().maxMemory() / (1024 * 1024));
batch.addGaugeMeasurement("apiv3.memory.usage", Runtime.getRuntime().totalMemory() / (1024 * 1024));
batch.addGaugeMeasurement("apiv3.memory.max", Runtime.getRuntime().maxMemory() / (1024 * 1024));
BatchResult result = batch.post(config.getProperty("general.id"), System.currentTimeMillis() / 1000L);
if (!result.success()) {
for (PostResult post : result.getFailedPosts()) {
log.warn("Could not POST to Librato: " + post);
}
}
} }
}, TimeUnit.SECONDS.toMillis(30), TimeUnit.MINUTES.toMillis(1)); }, TimeUnit.SECONDS.toMillis(5), TimeUnit.SECONDS.toMillis(5));
} }
private void setupBugsnag() { private void setupBugsnag() {
bugsnagClient = new Client(config.getProperty("bugsnag.apiKey")); Client bugsnag = new Client(config.getProperty("bugsnag.apiKey"));
bugsnagClient.setReleaseStage(config.getProperty("general.releaseStage")); bugsnag.setReleaseStage(config.getProperty("general.releaseStage"));
bugsnagClient.setProjectPackages("net.frozenorb.apiv3"); bugsnag.setProjectPackages("net.frozenorb.apiv3");
// TODO: Use .setLogger to use slf4j with this // TODO: Use .setLogger to use slf4j with this
} }

View File

@ -42,7 +42,7 @@ public final class ActorAttributeFilter implements Filter {
} }
res.header("WWW-Authenticate", "Basic realm=\"MineHQ\""); res.header("WWW-Authenticate", "Basic realm=\"MineHQ\"");
Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.error("Failed to authorize."))); Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.error("Failed to authorize as " + credentials[0] + ".")));
return null; return null;
} }

View File

@ -14,6 +14,7 @@ public final class AuthorizationFilter implements Filter {
Actor actor = req.attribute("actor"); Actor actor = req.attribute("actor");
if (!actor.isAuthorized()) { if (!actor.isAuthorized()) {
APIv3.getStatsD().incrementCounter("apiv3.http.unauthorized");
res.header("WWW-Authenticate", "Basic realm=\"MineHQ\""); res.header("WWW-Authenticate", "Basic realm=\"MineHQ\"");
Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.error("Unauthorized access: Please authenticate as either a server, the website, or an authorized user. You're currently authorized as " + actor.getName()))); Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.error("Unauthorized access: Please authenticate as either a server, the website, or an authorized user. You're currently authorized as " + actor.getName())));
} }

View File

@ -1,19 +1,16 @@
package net.frozenorb.apiv3.filters; package net.frozenorb.apiv3.filters;
import net.frozenorb.apiv3.APIv3;
import spark.Filter; import spark.Filter;
import spark.Request; import spark.Request;
import spark.Response; import spark.Response;
public final class MetricsAfterFilter implements Filter { public final class MetricsAfterFilter implements Filter {
/*private Histogram responseLengthMetric = APIv3.getMetrics().histogram(MetricRegistry.name("apiv3", "http", "responseLength"));
private Timer responseTimesMetric = APIv3.getMetrics().timer(MetricRegistry.name("apiv3", "http", "responseTimes"));*/
public void handle(Request req, Response res) { public void handle(Request req, Response res) {
/*responseLengthMetric.update(req.contentLength());
long started = req.attribute("requestStarted"); long started = req.attribute("requestStarted");
responseTimesMetric.update(System.currentTimeMillis() - started, TimeUnit.MILLISECONDS);*/ APIv3.getStatsD().recordExecutionTime("apiv3.http.executionTime", System.currentTimeMillis() - started);
APIv3.getStatsD().incrementCounter("apiv3.http.requests");
} }
} }

View File

@ -12,8 +12,11 @@ import spark.Response;
public final class LoggingExceptionHandler implements ExceptionHandler { public final class LoggingExceptionHandler implements ExceptionHandler {
public void handle(Exception ex, Request req, Response res) { public void handle(Exception ex, Request req, Response res) {
//Timer.Context timerMetric = req.attribute("timerMetric"); long started = req.attribute("requestStarted");
//timerMetric.stop(); APIv3.getStatsD().recordExecutionTime("apiv3.http.executionTime", System.currentTimeMillis() - started);
APIv3.getStatsD().incrementCounter("apiv3.http.requests");
APIv3.getStatsD().incrementCounter("apiv3.http.errors");
String code = new ObjectId().toHexString(); String code = new ObjectId().toHexString();