spring-bootmicrometerdistributed-tracingspring-boot-3

How to extend micrometer observation context in Spring Boot


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?


Solution

  • 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:

    1. Attach the data inline, similarly what you do right now: currentObservation.highCardinalityKeyValue("userId", userId), least flexible/convenient
    2. You can utilize an ObservationFilter to do the same: more flexible but has a limitation: it runs once, before onStop
    3. You can write your own Observation.Convention to do the same: most flexible

    Here's a conference talk that demos some of these things: https://www.youtube.com/watch?v=Qyku6cR6ADY