0

So I've been working on exposing /metrics using java client within a Spring Boot Application. I created the counters and histograms and they are all working fine. This is how I did it:

public static void main(String[] args){
   SpringApplication.run(MyApp.class, args);
   try{
     Server server = new Server(port:8888);
     ServletContextHandler context = new ServletContextHandler();
     context.setContextPath("/");
     server.setHandler(context);
     context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
     server.start();
     server.join();
   } catch ...

However, my boss doesn't like that I instantiated a new Server to do this. Somehow, they want me to expose the metrics by writing a @GetMapping("metrics") endpoint and using the server that SpringApplication.run utilizes. I attempted this created an Exporter in one of my classes with a Collector.defaultRegistry.

@GetMapping("metrics")
public Response metrics() throws IOException{
   StringWriter resp = new StringWriter();
   String contentType = "text/plain; version =0.0.4; charset=utf-8";
   
   BufferedWriter writer = new BufferedWriter(resp);
  
   TextFormat.writeFormat(contentType, writer, Collector.defaultRegistry.metricFamilySamples());
   return ok(resp.toString);

}

ANyway, this is showing a blank metrics page. I believe I should be using the exporter I initialized somewhere, but I'm having trouble synthesizing 3 classes into a few functions. Currently my exporter = new Exporter(Collector.defaultRegistry, null);

And no, I don't want to use micrometer

heff
  • 1
  • 1

0 Answers0