Skip to content

Instantly share code, notes, and snippets.

@iszlai
Forked from tazjin/Main.java
Created December 17, 2017 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iszlai/bc037f2dab4332003481403d61c15201 to your computer and use it in GitHub Desktop.
Save iszlai/bc037f2dab4332003481403d61c15201 to your computer and use it in GitHub Desktop.
Playing with Prometheus Java client
import io.prometheus.client.*;
import io.prometheus.client.exporter.common.TextFormat;
import io.prometheus.client.hotspot.DefaultExports;
import spark.Request;
import spark.Response;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Random;
import static spark.Spark.get;
public class Main implements Runnable {
/* Prometheus metrics registry for exposing metrics */
final private CollectorRegistry registry = CollectorRegistry.defaultRegistry;
// Simple test counter
final private static Counter testCounter = Counter.build()
.name("inc_counter").help("Amount of calls to /inc").register();
// Test gauge for in-progress requests of various request types
final private static Gauge inProgressRequests = Gauge.build()
.name("in_progress_requests").help("In progress requests")
.labelNames("method").register();
// Test summary for average response latency
final private static Summary avgRequestLatency = Summary.build()
.name("request_latency").help("Average request latency").register();
// Random instance for testing
final Random random = new Random();
public static void main(String[] args) {
// Set up default Prometheus Java metrics (e.g. GC metrics)
// DefaultExports.initialize();
final Main main = new Main();
main.run();
}
@Override
public void run() {
// Test routes to increment counter
get("/counter", (req, res) -> {
testCounter.inc();
return "ok";
});
// Test in progress request metrics
get("/gauge", (req, res) -> {
// Increment in-progress GET requests
try {
inProgressRequests.labels("GET").inc();
Thread.sleep(1000);
return "ok";
} finally {
inProgressRequests.labels("GET").dec();
}
});
// Test random request latency changes
get("/summary", (req, res) -> {
final Summary.Timer requestTimer = avgRequestLatency.startTimer();
try {
Thread.sleep(random.nextInt(5000));
return "ok";
} finally {
requestTimer.observeDuration();
}
});
// Monitoring & Health routes
get("/metrics", this::exposePrometheusMetrics);
get("/heatlhz", (req, res) -> "UP");
}
private String exposePrometheusMetrics(Request request, Response response) throws IOException {
response.status(200);
response.type(TextFormat.CONTENT_TYPE_004);
final StringWriter writer = new StringWriter();
TextFormat.write004(writer, registry.metricFamilySamples());
return writer.toString();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>promethest</groupId>
<artifactId>in.tazj</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<!-- Prometheus instrumentation -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.0.16</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.0.16</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_common</artifactId>
<version>0.0.16</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment