Skip to content

Instantly share code, notes, and snippets.

@arush-sal
Created June 16, 2018 22:59
Show Gist options
  • Save arush-sal/f47e8f1f2e65a8ed9ef07763e46a4746 to your computer and use it in GitHub Desktop.
Save arush-sal/f47e8f1f2e65a8ed9ef07763e46a4746 to your computer and use it in GitHub Desktop.
As found at kubernauts/prometheus_example/springboot/instrumented/
package com.github.kubernauts.prometheus_example.springboot.instrumented;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
// import the Prometheus packages.
import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
// Prometheus counter package.
import io.prometheus.client.Counter;
// Prometheus Histogram package.
import io.prometheus.client.Histogram;
@SpringBootApplication
@RestController
// Add a Prometheus metrics enpoint to the route `/prometheus`. `/metrics` is already taken by Actuator.
@EnablePrometheusEndpoint
// Pull all metrics from Actuator and expose them as Prometheus metrics. Need to disable security feature in properties file.
@EnableSpringBootMetricsCollector
public class HelloWorld {
// Define a counter metric for /prometheus
static final Counter requests = Counter.build()
.name("requests_total").help("Total number of requests.").register();
// Define a histogram metric for /prometheus
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
@RequestMapping("/")
String home() {
// Increase the counter metric
requests.inc();
// Start the histogram timer
Histogram.Timer requestTimer = requestLatency.startTimer();
try {
return "Hello World!";
} finally {
// Stop the histogram timer
requestTimer.observeDuration();
}
}
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloWorld.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment