-
Notifications
You must be signed in to change notification settings - Fork 0
Micrometer Integration
Frody edited this page Sep 3, 2026
·
1 revision
The scopeflow-micrometer module provides a ThreadLocalAccessor bridge that enables ScopeFlow context to propagate through Project Reactor pipelines, WebFlux, and any framework using Micrometer's context propagation.
<dependency>
<groupId>io.github.frodygr</groupId>
<artifactId>scopeflow-micrometer</artifactId>
</dependency>Register the accessor with Micrometer's ContextRegistry:
import io.micrometer.context.ContextRegistry;
ScopeFlow scopeFlow = ...; // injected or built manually
ContextRegistry.getInstance()
.registerThreadLocalAccessor(new ScopeFlowThreadLocalAccessor(scopeFlow));@Configuration
public class MicrometerConfig {
@Bean
public ApplicationRunner registerScopeFlowAccessor(ScopeFlow scopeFlow) {
return args -> {
ContextRegistry.getInstance()
.registerThreadLocalAccessor(new ScopeFlowThreadLocalAccessor(scopeFlow));
};
}
}The ScopeFlowThreadLocalAccessor implements three operations:
| Method | Description |
|---|---|
getValue() |
Captures the current ScopeFlow context as an immutable Map<String, Object>
|
setValue(Map) |
Opens a scope named "micrometer.restore" with the captured values |
setValue() |
Closes the restore scope, cleaning up MDC and context |
Mono.deferContextual(ctx -> ...)
│
├── subscribe on Thread A:
│ └── getValue() → captures {request.id=abc-123}
│
├── execute on Thread B (Reactor scheduler):
│ ├── setValue({request.id=abc-123}) → opens scope
│ ├── user code runs with context available
│ └── setValue() → closes scope, MDC cleaned up
│
└── back on Thread A (or another):
└── context properly cleaned up
// Enable automatic context propagation (Reactor 3.5.3+)
Hooks.enableAutomaticContextPropagation();
Mono.just("order-123")
.flatMap(orderId -> {
// This runs on a Reactor scheduler thread
// ScopeFlow context is automatically restored!
log.info("Processing {}", orderId); // MDC has request.id
return processOrder(orderId);
})
.subscribeOn(Schedulers.boundedElastic())
.subscribe();For Spring WebFlux applications, the Micrometer bridge enables ScopeFlow context to flow through the reactive pipeline:
@RestController
public class ReactiveOrderController {
@GetMapping("/orders/{id}")
public Mono<Order> getOrder(@PathVariable String id) {
// ScopeFlow context established by WebFilter or similar
return orderRepository.findById(id)
.doOnNext(order -> {
// Context propagated here via Micrometer accessor
log.info("Found order: {}", order.getId());
});
}
}ScopeFlow • Distributed Context & Tracing Propagation • Licensed under Apache-2.0