Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
$ANSIBLE_VAULT;1.1;AES256
62383035313961363234303436316238633235343139323264356462393132303962383033623136
3463383832376634343961373932646132666663643732650a333763393432633635303735393163
34623936316439646666303663656462376234646561626635353465396332623933346132386664
3365613034663366660a666133333263326230373235623635633732333661656636383938663863
31643339653664663766303063353062356230313239663030626233323434346631663137623465
65353935346530333734656364656362376234623935636633363638353063653534353031306430
31393233353561616634346231343265663132306366303035313466653036653232306433343564
35323736316462323664666434643938623636373131623635353365376336346538353538616266
33666662343638663464323661366339346364633232333335643464393066363832333830303132
38626130613939373161393266343837343161336130613162333036376562353261313538646666
33306630323664363131303032633131316466333366363465306464626566386336656136663939
36613834653638323463373534376365346131613838366130663630626431643530386434373133
35663763656364363237366666323231386130653365386263623463656232343239373362386530
32393832666333383337336261393332373934343262613066353931393839306639376134623334
61616262353362393931656536346663346164333064313532383035666438626163643733333137
31383263626666316130336536323761646232326532633739623431316134383266623435636134
38346333303664326237636236646161303761326464633635643763336134636566396166356263
64636639323338326366376335636131373532663936623763346565383631366166396332386535
376464376465633439326131623036343763
61313238636666353031616265616533666263613030613261623865386636393664323631366139
6461383234653263656461373164396237313432323866320a653533653164323064613633613234
37373161626138343437333462306463313265633161346665653430373765663632656166373237
6430626430303336340a383231316365633836623661636534303338303338653339353762643934
37376331323864616164643262366334656538643331633935353866616236626165343337323032
33366166383261623039613338373237336332623532616632363363636437383737343461633066
62333733666265643866373130666265656138393163383838633861343766323137616662646631
35386363666430306130323835376534623462613739366431613638653036623361653461303965
63303334373235323931306336316631393830663937393832356437343430616466643664323565
32663638646365303866326161643336633939316237353961303132376665613666396332613938
61346238383366633463623362626637333533323239616663343663633064386536376362666638
62356661623461363834643031376131626536353139353439353734346365343035356463306565
65626465313738626562393866343761636638343066316437373737333136366639316333656431
63383332323137356463303262646533383735636631353065646131323834656237336137386235
31323438656164646531346333613538663061393266323630393530386233336236353731656238
38616535363333336439336531353064623564323464316130633362353131313961613938633339
34363437343766313661636239346165323831333732323232363466663231626236613132373266
32373531323431316534663564353537663061333835336662626463616163303433303833306437
64353337643731343839376566356639333630646164393939653165303636616662393236363362
35643138316661656430646138353630643332653936323861646634663435393638623864623261
64323536303263663361303663376662356634653066376635336464376133356633333632666164
31613561346262343731366133643339613833386133626638613534313265393038313736643964
33613339656562383330306138643634363462633632613331636232633762373134653736353863
65316239333836643734623831396537383563306165616362326231613539626130623762613832
64373834303266366339653738343738346431623562393233323434383931323036373065323434
31653632383535383435393664323833383263613735353365633264396163333334376533393933
37363336643765303936653530383738646662326332643765613365386433383366636331373361
64303432323061646464623539643061363364393338643465613431356461623532623031306633
35333233333734643666386565333831373239356261623465393163663933376637626436393166
61303436306262386365346530333531626635323534633634376665313165396538643632396264
653335353635343964336661643131393839
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
github:
client_id: "{{_vault['github']['client_id']}}"
client_secret: "{{_vault['github']['client_secret']}}"

metrics:
graphite:
host: "{{_vault.metrics.graphite.host}}"
grafana:
api_key: "{{_vault.metrics.grafana.api_key}}"
5 changes: 5 additions & 0 deletions ansible/roles/apps/jvm_app_base/templates/secure.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ github {
clientId="{{github['client_id']}}"
clientSecret="{{github['client_secret']}}"
}

metrics {
graphite.host="{{metrics.graphite.host}}"
grafana.api_key="{{metrics.grafana.api_key}}"
}
3 changes: 2 additions & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ext {
slf4j : '1.7.25', // Logging
logback : '1.2.3', // Logging
undertow : '1.4.20.Final',// Webserver
metrics : '3.2.5', // Metrics
metrics : '4.0.2', // Metrics
guava : '23.3-jre', // Common / Helper libraries
typesafeConfig : '1.3.2', // Configuration
handlebars : '4.0.6', // HTML templating
Expand Down Expand Up @@ -42,6 +42,7 @@ ext {
metricsJson : "io.dropwizard.metrics:metrics-json:$versions.metrics",
metricsLogback : "io.dropwizard.metrics:metrics-logback:$versions.metrics",
metricsHealthchecks : "io.dropwizard.metrics:metrics-healthchecks:$versions.metrics",
metricsGraphite : "io.dropwizard.metrics:metrics-graphite:$versions.metrics",
undertowCore : "io.undertow:undertow-core:$versions.undertow",
slf4j : "org.slf4j:slf4j-api:$versions.slf4j",
slf4jLog4j : "org.slf4j:log4j-over-slf4j:$versions.slf4j",
Expand Down
1 change: 1 addition & 0 deletions stubbornjava-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
compile libs.metricsJson
compile libs.metricsLogback
compile libs.metricsHealthchecks
compile libs.metricsGraphite
compile libs.guava
compile libs.typesafeConfig
compile libs.handlebars
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.stubbornjava.common;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.graphite.GraphiteSender;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;

class GraphiteHttpSender implements GraphiteSender {
private static final Logger log = LoggerFactory.getLogger(GraphiteHttpSender.class);

private final OkHttpClient client;
private final String host;
private final List<GraphiteMetric> metrics = Lists.newArrayList();

public GraphiteHttpSender(OkHttpClient client, String host, String apiKey) {
this.client = client.newBuilder()
.addInterceptor(HttpClient.getHeaderInterceptor("Authorization", "Bearer " + apiKey))
.build();
this.host = host;
}

@Override
public void connect() throws IllegalStateException, IOException {
// Just no op here
}

@Override
public void close() throws IOException {
// no op
}

@Override
public void send(String name, String value, long timestamp) throws IOException {
metrics.add(new GraphiteMetric(name, 10, Double.parseDouble(value), timestamp));
}

@Override
public void flush() throws IOException {
Request request = new Request.Builder()
.url(host + "/metrics")
.post(RequestBody.create(MediaType.parse("application/json"), Json.serializer().toByteArray(metrics)))
.build();
String response = Retry.retryUntilSuccessfulWithBackoff(() -> client.newCall(request).execute());
metrics.clear();
}

@Override
public boolean isConnected() {
// TODO Auto-generated method stub
return false;
}

@Override
public int getFailures() {
// TODO Auto-generated method stub
return 0;
}

private static final HttpLoggingInterceptor getLogger(Level level) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor((msg) -> {
log.debug(msg);
});
loggingInterceptor.setLevel(level);
return loggingInterceptor;
}

private static final class GraphiteMetric {
private final String name;
private final int interval;
private final double value;
private final long time;

public GraphiteMetric(@JsonProperty("name") String name,
@JsonProperty("interval") int interval,
@JsonProperty("value") double value,
@JsonProperty("time") long time) {
this.name = name;
this.interval = interval;
this.value = value;
this.time = time;
}

public String getName() {
return name;
}
public int getInterval() {
return interval;
}
public double getValue() {
return value;
}
public long getTime() {
return time;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.util.EC2MetadataUtils;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.codahale.metrics.jvm.CachedThreadStatesGaugeSet;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.logback.InstrumentedAppender;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import okhttp3.OkHttpClient;
// {{start:metrics}}
public class Metrics {
private static final Logger log = LoggerFactory.getLogger(Metrics.class);

/*
* Use a concurrent map to cache metrics we generate on the fly.
* For example we generate status code metrics on the fly.
Expand All @@ -33,12 +39,29 @@ public class Metrics {

// Logback metrics
final LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger root = factory.getLogger(Logger.ROOT_LOGGER_NAME);
final ch.qos.logback.classic.Logger root = factory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
final InstrumentedAppender metrics = new InstrumentedAppender(registry);
metrics.setContext(root.getLoggerContext());
metrics.start();
root.addAppender(metrics);


// Graphite reporter to Grafana Cloud
OkHttpClient client = new OkHttpClient.Builder()
//.addNetworkInterceptor(HttpClient.getLoggingInterceptor())
.build();

String graphiteHost = Configs.properties().getString("metrics.graphite.host");
String grafanaApiKey = Configs.properties().getString("metrics.grafana.api_key");
final GraphiteHttpSender graphite = new GraphiteHttpSender(client, graphiteHost, grafanaApiKey);
final GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)
.prefixedWith(metricPrefix("stubbornjava"))
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(graphite);
reporter.start(10, TimeUnit.SECONDS);

// Register reporters here.
}

Expand All @@ -63,5 +86,17 @@ public static Meter meter(String first, String... keys) {
return metric;
});
}

private static String metricPrefix(String app) {
Env env = Env.get();
String host = env == Env.LOCAL ? "localhost" : getHost();
String prefix = MetricRegistry.name(app, env.getName(), host);
log.info("Setting Metrics Prefix {}", prefix);
return prefix;
}

private static String getHost() {
return EC2MetadataUtils.getLocalHostName().split("\\.")[0];
}
}
// {{end:metrics}}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static StatusCodeHandler statusCodeMetrics(HttpHandler next) {
}

public static TimingHttpHandler timed(String name, HttpHandler next) {
return new TimingHttpHandler(next, name);
return new TimingHttpHandler(next, "routes." + name);
}

public static void metrics(HttpServerExchange exchange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class StubbornJavaBootstrap {

public static Config getConfig() {
Config config = Configs.newBuilder()
.withOptionalRelativeFile("./secure.conf")
.withResource("application." + Env.get().getName() + ".conf")
.withResource("application.conf")
.withOptionalRelativeFile("/secure.conf")
.withResource("sjweb." + Env.get().getName() + ".conf")
.withResource("sjweb.conf")
.build();
logger.debug(Json.serializer().toPrettyString(Configs.asMap(config)));
return config;
Expand Down