1

I have a spring mvc web application with REST endpoints. I am integrating with prometheus for generating metrics. I have exposed "/prometheus" endpoint and am able to capture jvm matrics and log4j2 matrcis but stuck on how to generate http request metrics.

    public static PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);


    @Autowired
    public PrometheusController(Environment env) {
        
        ImmutableTag tag = new ImmutableTag(env.getProperty("promotheus.metric.key.name"), env.getProperty("promotheus.metric.key.value"));
        List<Tag> tags = new ArrayList<>();
        tags.add(tag);
        
        new JvmThreadMetrics(tags).bindTo(registry);
        new JvmGcMetrics(tags).bindTo(registry);
        new JvmMemoryMetrics(tags).bindTo(registry);
        new JvmHeapPressureMetrics(tags, Duration.ofMinutes(5), 
                Duration.ofMinutes(1)).bindTo(registry);
        
        new DiskSpaceMetrics(new File("/"), tags).bindTo(registry);
        new ProcessorMetrics(tags).bindTo(registry);
        new UptimeMetrics(tags).bindTo(registry);
        new Log4j2Metrics(tags).bindTo(registry);
        new ClassLoaderMetrics(tags).bindTo(registry);
        new JvmCompilationMetrics(tags).bindTo(registry);
        new TomcatMetrics(null, tags).bindTo(registry);
        
    }
    
    @RequestMapping(value = { "/prometheus" }, method = RequestMethod.GET)
    public ResponseEntity<?> getPrometheusMetric() {
        
        return new ResponseEntity<>(registry.scrape(TextFormat.CONTENT_TYPE_OPENMETRICS_100), HttpStatus.OK);
    }

I could not find any code that would relevant to get these metrics. Can someone help me on this ?

1 Answers1

0

I would suggest that you create a Filter (same approach as spring-boot-actuator uses) and wrap the request process with a Timer.Sample and add the tags you would like when stopping the sample

Take a look how spring-boot-actuator has implemented the filter here and here

Hans-Christian
  • 542
  • 4
  • 6