Search code examples
prometheusmetricsmicronautmicrometer

How to change the path of an endpoint in Micronaut?


I have set up micronaut using the cli and want to add an endpoint that provides prometheus metrics. Following the guides (and Micronaut: How to get metrics in the Prometheus format?), I added things to my application.yml the following way:

micronaut:
  application:
    name: foo-service
  metrics:
    enabled: true
    export:
      prometheus:
        enabled: true
        step: PT1M
        descriptions: true
endpoints:
  metrics:
    enabled: true
  prometheus:
    enabled: true
    sensitive: false

Now I have two endpoints, one at /metrics and one at /prometheus. However, I want /metrics to return prometheus metrics. Any idea how I can achieve that?

I know I could go and put all endpoints under a sub-path, such as /endpoints using endpoints.all.path and then proxy to there, but that really is ugly and not that way I want to solve it.


Solution

  • Thanks to james-kleeth I got to the right track, although it basically is a re-implementation. I disabled the prometheus endpoint and added a controller. However, when the endpoint is disabled I cannot inject it anymore. Its implementation was "trivial" (just referencing to the prometheus registry). This is my solution:

    package my.company.service
    
    import io.micrometer.prometheus.PrometheusMeterRegistry
    import io.micronaut.configuration.metrics.annotation.RequiresMetrics
    import io.micronaut.http.annotation.Controller
    import io.micronaut.http.annotation.Get
    import io.micronaut.http.annotation.Produces
    import io.swagger.v3.oas.annotations.Operation
    import javax.inject.Inject
    
    @RequiresMetrics
    @Controller("/metrics")
    class MetricsController @Inject constructor(val prometheusMeterRegistry: PrometheusMeterRegistry) {
        @Operation(summary = "Provide metrics in Prometheus format")
        @Get
        @Produces("text/plain; version=0.0.4")
        fun metrics(): String = prometheusMeterRegistry.scrape()
    }