OpenTelemetry & Reactive Java: how to Tag a Mono with return value

227 Views Asked by At

I am trying to add Observability info to a method:

@GetMapping(value = "/getClient")
public Mono<ResponseEntity<String>> getClient(HttpServletRequest request, final ClientRequest body) {
 return myService
            .getClient(body.getClient_id(), body.getProvider())
            .doOnError(throwable -> log.error("Failed to get client", throwable))
            .name("get_client")
            .tag("client_id", body.getClient_id())
            .tag("provider", body.getProvider())
            .tap(Micrometer.observation(observationRegistry));
}

This gives me some extra info in my traces and also the metrics; all work fine.

But now I also want to know if this call was successful or if it contains an error. For that I would need a mix of .tag(...) and .doOnError(...), but I can't find a solution. I would have the same problem if I wanted to add info about the result of myService.getClient.

I this actually possible ?

1

There are 1 best solutions below

0
On BEST ANSWER

I found the solution:

...
 .doOnError(throwable -> observationRegistry.getCurrentObservation().event(Observation.Event.of("Error", "Whatever"));
 .map(result -> observationRegistry.getCurrentObservation().event(Observation.Event.of("Success", "Whatever") 
...