Description
Event though the observationConvention already includes some good tags in the spans, I need to add some additional custom tags to it. Unfortunately, I did not find an easy way to customize the observation spans, because the usage of MongoHandlerObservationConvention
is hardcoded:
https://github.com/spring-projects/spring-data-mongodb/blob/main/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/observability/MongoObservationCommandListener.java#L110
So the only way would be to implement my own MongoHandlerObservationConvention
(as we are not able to override the DefaultMongoHandlerObservationConvention
) and in addition to override MongoObservationCommandListener
and rewrite the whole commandStarted
method to use my custom observationConvention. In addition, we also need to implement our own SenderContext
as the MongoHandlerContext
is not public. Then I can use it in the bean-definition of MongoClientSettingsBuilderCustomizer
by setting
@Bean
public MongoClientSettingsBuilderCustomizer mongoMetricsSynchronousContextProvider(ObservationRegistry registry) {
return (clientSettingsBuilder) -> {
clientSettingsBuilder.contextProvider(ContextProviderFactory.create(registry))
.addCommandListener(new MyCustomMongoObservationCommandListener(registry));
};
}
But that's actually a lot of effort just to be able to add a custom tag to the spans.
Maybe it would be easier to allow us overriding the DefaultMongoHandlerObservationConvention
and use a bean-definition of MongoHandlerObservationConvention
or provide a setter in MongoObservationCommandListener
to set the actual used MongoHandlerObservationConvention
.