Search code examples
javaprometheusmicrometerspring-micrometer

Templated URLs in Prometheus End Point with Jersey and Micrometer


I am trying to integrate micrometer in a spring application built using Jersey REST. I am using Spring Boot 1.5.

When I have a pure Spring REST APIs (using Spring Web Annotations), prometheus results are displayed as below

http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/v2/api/greeting/{name}",} 18.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/v2/api/greeting/{name}",} 0.39309228

But for the APIs built using Jersey, the prometheus metrics are not templated.

http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_1",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_1",} 0.043685372
http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_2",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_2",} 0.006280183
http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_3",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_3",} 0.006258448
http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_4",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_4",} 0.006635791
http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_5",} 1.0
http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/v1/api/greeting/Jersey_5",} 0.006313993

Looks like there is an open issue with micrometer listed at https://github.com/micrometer-metrics/micrometer/issues/486 which causes this issue with jersey. A remedy is mentioned at https://github.com/micrometer-metrics/micrometer/issues/486#issuecomment-372446968. When I add spring.jersey.type=filter, none of the actuator APIs work. Neither prometheus end point works.

I do not want un-templated URLs appearing in Prometheus end point. Since my API can have infinitely many combinations (many names in above examples), it will create large metrics data and in turn prometheus server will have tones of metrics.

Any help here is highly appreciated.


Solution

  • I created a sample to reproduce the problem from your description but failed to reproduce it. I can see URI templates are being used in the Prometheus scrape endpoint (/management/prometheus) as follows:

    # HELP http_server_requests_seconds  
    # TYPE http_server_requests_seconds summary
    http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/prometheus",} 1.0
    http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/prometheus",} 0.062559545
    http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/hello/{name}",} 1.0
    http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/hello/{name}",} 0.02323862
    http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="UNKNOWN",} 1.0
    http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="UNKNOWN",} 0.050339909
    # HELP http_server_requests_seconds_max  
    # TYPE http_server_requests_seconds_max gauge
    http_server_requests_seconds_max{exception="None",method="GET",status="200",uri="/prometheus",} 0.062559545
    http_server_requests_seconds_max{exception="None",method="GET",status="200",uri="/hello/{name}",} 0.02323862
    http_server_requests_seconds_max{exception="None",method="GET",status="200",uri="UNKNOWN",} 0.050339909
    

    I guess there might be some misconfiguration in your project or I might miss some requirement(s) from your description.

    UPDATE:

    Based on the comment, disabling Web MVC metrics will provide cleaner result as follows:

    # HELP http_server_requests_seconds  
    # TYPE http_server_requests_seconds summary
    http_server_requests_seconds_count{exception="None",method="GET",status="200",uri="/hello/{name}",} 1.0
    http_server_requests_seconds_sum{exception="None",method="GET",status="200",uri="/hello/{name}",} 0.032691223
    # HELP http_server_requests_seconds_max  
    # TYPE http_server_requests_seconds_max gauge
    http_server_requests_seconds_max{exception="None",method="GET",status="200",uri="/hello/{name}",} 0.032691223
    

    I also updated the sample in the following commit: https://github.com/izeye/sample-micrometer-spring-boot/commit/14ff838c48cea0c38cf12eeec8e7b211dffe2c50