Guest User

Untitled

a guest
Jun 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package com.github.kubernauts.prometheus_example.springboot.instrumented;
  2.  
  3. import org.springframework.boot.*;
  4. import org.springframework.boot.autoconfigure.*;
  5. import org.springframework.web.bind.annotation.*;
  6. // import the Prometheus packages.
  7. import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
  8. import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
  9. // Prometheus counter package.
  10. import io.prometheus.client.Counter;
  11. // Prometheus Histogram package.
  12. import io.prometheus.client.Histogram;
  13.  
  14. @SpringBootApplication
  15. @RestController
  16. // Add a Prometheus metrics enpoint to the route `/prometheus`. `/metrics` is already taken by Actuator.
  17. @EnablePrometheusEndpoint
  18. // Pull all metrics from Actuator and expose them as Prometheus metrics. Need to disable security feature in properties file.
  19. @EnableSpringBootMetricsCollector
  20.  
  21. public class HelloWorld {
  22. // Define a counter metric for /prometheus
  23. static final Counter requests = Counter.build()
  24. .name("requests_total").help("Total number of requests.").register();
  25. // Define a histogram metric for /prometheus
  26. static final Histogram requestLatency = Histogram.build()
  27. .name("requests_latency_seconds").help("Request latency in seconds.").register();
  28. @RequestMapping("/")
  29. String home() {
  30. // Increase the counter metric
  31. requests.inc();
  32. // Start the histogram timer
  33. Histogram.Timer requestTimer = requestLatency.startTimer();
  34. try {
  35. return "Hello World!";
  36. } finally {
  37. // Stop the histogram timer
  38. requestTimer.observeDuration();
  39. }
  40. }
  41. public static void main(String[] args) throws Exception {
  42. SpringApplication.run(HelloWorld.class, args);
  43. }
  44. }
Add Comment
Please, Sign In to add comment