micrometer-metrics / micrometer

An application observability facade for the most popular observability tools. Think SLF4J, but for observability.
https://micrometer.io
Apache License 2.0
4.46k stars 985 forks source link

Configure a custom JmsProcessObservationConvention or JmsPublishObservationConvention #5492

Open mihca opened 3 weeks ago

mihca commented 3 weeks ago

Please describe the feature request. It should be possible to configure a custom JmsProcessObservationConvention. Currently a DefaultJmsProcessObservationConvention ist hardcoded in the MessageConsumerInvocationHandler.

Rationale I want to write custom information from the JMS header to the observation (trace info).

Additional context Its possible to define your own JmsProcessObservationConvention, but I found no possibilty to activate/configure it instead of the DefaultJmsProcessObservationConvention.

shakuzen commented 2 weeks ago

Thank you for opening the issue. I agree it would be better to allow passing a custom convention when configuring the instrumentation. In the meantime, perhaps you could use a GlobalObservationConvention to achieve what you want until the instrumentation is updated - see this part of the documentation.

Would you be interested in submitting a pull request for this enhancement?

mihca commented 2 weeks ago

With a GlobalObservationConvention I was able to get it to work, thanks!

PR: I have no idea how to make it configurable in context of micrometer. Any hints?

shakuzen commented 2 weeks ago

PR: I have no idea how to make it configurable in context of micrometer. Any hints?

The way to use the instrumentation now is via JmsInstrumentation. I think adding another static method that takes custom conventions would be one way of making it configurable to users. Maybe @bclozel has other ideas.

bclozel commented 2 weeks ago

Initially, I did not provide a way to customize the observation convention directly on JmsInstrumentation as in my experience, this instrumentation can happen quite deep into libraries and frameworks (it is the case in Spring) so it's not easy to provide an obvious hook point for that.

I guess you're instrumenting JMS sessions manually and not through a library or framework? In this case, another static method as suggested by @shakuzen would work well.

mihca commented 1 week ago

We are using Spring Boot, which uses the Spring Framework, which uses Micrometer. You have to understand all three libs...

bclozel commented 1 week ago

Thanks for your feedback @mihca - this is the main feature behind global observation conventions: you don't need to know where things are instrumented and how, you just need to know which convention to use and override globally.

Admittedly, the JMS processing observability section of the Spring Framework reference documentation is a bit light as it doesn't mention the relevant convention, unlike other instrumentations.

Back to your use case, I believe a global convention is probably the easiest path. We could allow custom conventions here in Micrometer, but we would also need another API in Spring Framework and even maybe something else in Spring Boot. In the end, you would probably provide your custom convention as a bean, which is exactly the same thing as a global one.

Here's my proposal:

Let me know what you think @mihca !

shakuzen commented 1 week ago

I'm not sure there is a dedicated section for this in the Micrometer docs @shakuzen ?

The closest we have is the section I linked earlier which has sample code and JavaDoc comments in it describing the precedence of conventions and gives an example of a GlobalObservationConvention.

mihca commented 1 week ago

Back to your use case, I believe a global convention is probably the easiest path. We could allow custom conventions here in Micrometer, but we would also need another API in Spring Framework and even maybe something else in Spring Boot. In the end, you would probably provide your custom convention as a bean, which is exactly the same thing as a global one.

I expected that it works like org.springframework.http.server.observation.DefaultServerRequestObservationConvention, which is documented in Spring Framework Observability Support#Using custom observation conventions. Just provide the bean and its insrtumented automatically. But that class is defined in Spring Framework and not Micrometer.

I can improve this section of the Framework docs and review others to always mention the convention and context classes I can add another section under "configuring observations" to explain how to use global conventions. Or maybe the Spring Boot docs would be a better fit? I'm not sure there is a dedicated section for this in the Micrometer docs @shakuzen ?

Yes, would be nice that there is a hint in the Spring Framework Documentation#JMS message instrumentation to use a global convention.