I have read the Spring blog post Observability with Spring Boot 3 and Micrometer manual. But the examples are too simple, showing how to start a new observation:
ObservationRegistry registry = ObservationRegistry.create();
Observation.createNotStarted("user.name", registry)
As far as I know, Spring Boot is starting observations out of the box and I am happy with that, I do not want to fragment that. I intend to extend the existing observation context with the information I collect during use-case execution. I tried this:
@Autowired
private final ObservationRegistry observationRegistry;
public void myBusinessMethod(String userId) {
// other business code
final Observation currentObservation = observationRegistry.getCurrentObservation();
if (currentObservation != null) {
currentObservation.getContext().put("userId", userId);
}
// other business code
}
I have not seen anything similar in the documentation. So the question is: Is this the preferred way to extend the existing observation context? If not, why and what is the alternative?
Is this the preferred way to extend the existing observation context?
No unless you collect this data somewhere on your own: so you know what you put into the context and you read that later.
What would be your expectation: what should happen in effect of calling put
? Micrometer cannot really figure out what this custom field is so it will ignore it.
But there are multiple ways to tell Micrometer what it is and how it should be used, first, please check the docs: https://docs.micrometer.io/micrometer/reference/observation.html.
You can:
currentObservation.highCardinalityKeyValue("userId", userId)
, least flexible/convenientObservationFilter
to do the same: more flexible but has a limitation: it runs once, before onStop
Observation.Convention
to do the same: most flexibleHere's a conference talk that demos some of these things: https://www.youtube.com/watch?v=Qyku6cR6ADY