TKM
TKM

Reputation: 43

Can't get Prometheus to work with Spring Boot 2.0.3

I am using Spring Boot 2.0.3.RELEASE with following dependencies:

spring-boot-starter-actuator:2.0.3.RELEASE
micrometer-core:1.0.6
micrometer-registry-prometheus:1.0.6

But when I invoke Prometheus all I keep getting is

{
    "timestamp": 1532426317772,
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/actuator/prometheus"
}

*and/or from browser*
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

I have also tried previous Prometheus releases of the range 1.0.X with Spring Boot 2 but with no luck. Could someone please suggest some insights? Many thanks.

Upvotes: 4

Views: 4229

Answers (5)

Kenny
Kenny

Reputation: 101

We recently had the same problem. While debugging Spring Boot I discovered that we had no HttpMessageConverter registered that could handle the "text/plain" MediaType, only "application/json". Because the Prometheus endpoint returns the "text/plain" MediaType we added some configuration as a temporary workaround. We added a StringHttpMessageConverter linked to the specific MediaType.

@Configuration
public class ApplicationConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN));
            converters.add(converter);
    }
}

Hope this helps you

Upvotes: 10

Aleks D
Aleks D

Reputation: 381

If other answers didn't help, it possible to add configuration to ContentNegotiationConfigurer. You can set defaultContentType as "MediaType.TEXT_PLAIN".

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
   
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.TEXT_PLAIN);
        configurer.favorParameter(false);
        configurer.favorPathExtension(false);
        configurer.parameterName("mediaType");
        configurer.useJaf(false);
    }
}

Upvotes: 0

SimonBillingsley
SimonBillingsley

Reputation: 31

Another reason for this error is an invalid Spring XML configuration file that contains something like this:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>

This above XML overrides the list of messageConverters in the RequestMappingHandlerAdapter so only the listed converters are available, which in my case removed the string converter causing the 406 error response when /actuator/prometheus was requested.

Upvotes: 1

Se ven
Se ven

Reputation: 405

May It's 406 status code which also can solved by client side with specified accept header. like this: curl -v -H "Accept: text/plain" url

Upvotes: 1

Kongmingtwo
Kongmingtwo

Reputation: 105

Probably not a clear answer, but hopefully this is some help. I was integrating prometheus into a service I didn't develop and had this same issue. I narrowed it to this code block (removal makes it work)

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
    converter.setGson(GSON);
    converters.add(converter);
}

So checking your conversion methods might help a bit. The repos I had made were using jackson instead of gson for this sort of thing, so that's the route I'm going to take. Not an ideal solution, but lacking anything better...

Upvotes: 0

Related Questions