javaspring-bootmicrometerspring-micrometermicrometer-tracing

Tracing per endpoint using Spring Boot and micrometer


I have a REST API which exposes two endpoints.

@RestController
class ReportController {

  private static final Logger LOGGER = LoggerFactory.getLogger(ReportController.class);

  @Autowired
  ReportService reportService;

  @GetMapping("/businessCriticalFLow")
  String businessCriticalFLow(@RequestBody String string) {
    LOGGER.info("would like to see traces like this INFO [app,bbe3aea006077640b66d40f3e62f04b9,93b7a150b7e293ef]");
    Report report = reportService.getReport(string); //will see traces from the repository as well
    return ...;
  }

  @GetMapping("/notSoImportant")
  String notSoImportant(@RequestParam(value = "id", required = true) int id) {
      return ...;
  }

}

We have the current solutions:

  1. disable tracing entirely (removing micrometer, or setting false to the micrometer property in application properties)

    Result -> We lose everything, no observability on the endpoint 1 (important flow)

  2. enable tracing for everything (default behavior when bringing micrometer, actuator and co)

    Result -> We waste money sending traces we will not use for flow 2 just to see traces of flow 1

  3. refactor and break the app into two apps, one endpoint each, one that has tracing enabled, and one that does not.

Is there a way we can achieve tracing per-flow, per endpoint using micrometer and Spring Boot?


Solution

  • You might want to check the docs, there is a component for this: ObservationPredicate, see: https://docs.micrometer.io/micrometer/reference/observation/components.html#micrometer-observation-predicates-filters

    Here's an example that disables observations for certain paths for http server, http clients and their children: https://github.com/jonatan-ivanov/teahouse/blob/1052673cb137b59d304a5db6296581158808849b/core/src/main/java/org/example/teahouse/core/actuator/config/CommonActuatorConfig.java#L31-L67

    Also, here's a more general Spring Boot support change for this: https://github.com/jonatan-ivanov/spring-boot/commit/fb2a0f601f154418225155fdc867506a3436ed17