push stuff

This commit is contained in:
Colin McDonald 2016-05-06 09:58:42 -04:00
parent 979b82e90e
commit 065c55e0b4
6 changed files with 45 additions and 47 deletions

View File

@ -80,13 +80,8 @@
</dependency>
<dependency>
<groupId>com.librato.metrics</groupId>
<artifactId>metrics-librato</artifactId>
<version>4.1.2.5</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.2</version>
<artifactId>librato-java</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>com.bugsnag</groupId>

View File

@ -1,20 +1,18 @@
package net.frozenorb.apiv3;
import com.bugsnag.Client;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.librato.metrics.LibratoReporter;
import com.librato.metrics.*;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.filters.*;
import net.frozenorb.apiv3.routes.GETDump;
import net.frozenorb.apiv3.routes.GETWhoAmI;
import net.frozenorb.apiv3.routes.NotFound;
import net.frozenorb.apiv3.routes.POSTMetrics;
import net.frozenorb.apiv3.routes.servers.POSTServerHeartbeat;
import net.frozenorb.apiv3.routes.announcements.GETAnnouncements;
import net.frozenorb.apiv3.routes.auditLog.GETAuditLog;
import net.frozenorb.apiv3.routes.chatFilterList.GETChatFilterList;
@ -48,16 +46,19 @@ import redis.clients.jedis.JedisPool;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import static spark.Spark.*;
@Slf4j
public final class APIv3 {
@Getter private static Datastore datastore;
@Getter private static Properties config = new Properties();
@Getter private static JedisPool redisPool;
@Getter private static MetricRegistry metrics = new MetricRegistry();
@Getter private static HttpPoster libratoPoster;
@Getter private static Client bugsnagClient;
@Getter private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter())
@ -70,9 +71,11 @@ public final class APIv3 {
setupDatabase();
setupRedis();
setupMetrics();
setupLibrato();
setupBugsnag();
setupHttp();
log.info("APIv3 booted.");
}
private void setupConfig() {
@ -113,23 +116,36 @@ public final class APIv3 {
);
}
private void setupMetrics() {
Gauge memoryUsageGauge = () -> Runtime.getRuntime().totalMemory() / (1024 * 1024);
Gauge memoryMaxGauge = () -> Runtime.getRuntime().maxMemory() / (1024 * 1024);
metrics.register(MetricRegistry.name("apiv3", "memory", "usage"), memoryUsageGauge);
metrics.register(MetricRegistry.name("apiv3", "memory", "max"), memoryMaxGauge);
LibratoReporter.enable(
LibratoReporter.builder(
metrics,
private void setupLibrato() {
try {
libratoPoster = new DefaultHttpPoster(
"https://metrics-api.librato.com/v1/metrics",
config.getProperty("librato.email"),
config.getProperty("librato.apiKey"),
config.getProperty("general.id")
),
1,
TimeUnit.MINUTES
config.getProperty("librato.apiKey")
);
} catch (Exception e) {
e.printStackTrace();
}
new Timer("Librato Post Task").scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
LibratoBatch batch = new LibratoBatch(300, Sanitizer.NO_OP, 10, TimeUnit.SECONDS, "qLib", libratoPoster);
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));
}
private void setupBugsnag() {

View File

@ -1,7 +1,5 @@
package net.frozenorb.apiv3.actors;
import net.frozenorb.apiv3.models.Server;
public final class BungeeCordActor implements Actor {
public boolean isAuthorized() {

View File

@ -1,25 +1,19 @@
package net.frozenorb.apiv3.filters;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import net.frozenorb.apiv3.APIv3;
import spark.Filter;
import spark.Request;
import spark.Response;
import java.util.concurrent.TimeUnit;
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"));
/*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) {
responseLengthMetric.update(req.contentLength());
/*responseLengthMetric.update(req.contentLength());
long started = req.attribute("requestStarted");
responseTimesMetric.update(System.currentTimeMillis() - started, TimeUnit.MILLISECONDS);
responseTimesMetric.update(System.currentTimeMillis() - started, TimeUnit.MILLISECONDS);*/
}
}

View File

@ -1,10 +1,6 @@
package net.frozenorb.apiv3.routes;
import com.google.common.collect.ImmutableMap;
import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.utils.ErrorUtils;
import spark.Request;
import spark.Response;
import spark.Route;

View File

@ -1,6 +1,5 @@
package net.frozenorb.apiv3.unsorted;
import com.codahale.metrics.Timer;
import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.utils.ErrorUtils;
@ -13,8 +12,8 @@ import spark.Response;
public final class LoggingExceptionHandler implements ExceptionHandler {
public void handle(Exception ex, Request req, Response res) {
Timer.Context timerMetric = req.attribute("timerMetric");
timerMetric.stop();
//Timer.Context timerMetric = req.attribute("timerMetric");
//timerMetric.stop();
String code = new ObjectId().toHexString();