Skip to content

Instantly share code, notes, and snippets.

@apuravchauhan
Created August 30, 2020 13:29
Show Gist options
  • Save apuravchauhan/123d3640cf65607824754a012af9afa8 to your computer and use it in GitHub Desktop.
Save apuravchauhan/123d3640cf65607824754a012af9afa8 to your computer and use it in GitHub Desktop.
Spring Boot Application Integrated with Prometheus
server.port=8082
management.endpoint.prometheus.enabled=true
management.endpoints.web.exposure.include=info,health,prometheus
package com.apurav;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.micrometer.core.instrument.MeterRegistry;
/**
*
* @author apuravchauhan
*
*/
@SpringBootApplication
@RestController
@RequestMapping("/")
public class PrometheusSpringBoot {
private final MeterRegistry meterRegistry;
public PrometheusSpringBoot(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@GetMapping("/2xx")
public String simulate2xxResponse() {
meterRegistry.counter("orders.2xx","status","OK").increment();
return "Got 2xx Response";
}
@GetMapping("/5xx")
public String simulate5xxResponse() {
meterRegistry.counter("orders.5xx","status","NOTOK").increment();
return "Got 5xx Response";
}
@PostMapping("/alert-hook")
public void receiveAlertHook(@RequestBody Map request) throws Exception {
System.out.println(request);
}
public static void main(String[] args) {
SpringApplication.run(PrometheusSpringBoot.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment