CAMEL-23225: Propagate OpenTelemetry context across thread boundaries in camel-opentelemetry2#22190
CAMEL-23225: Propagate OpenTelemetry context across thread boundaries in camel-opentelemetry2#22190
Conversation
… in camel-opentelemetry2 Port the thread pool instrumentation from the deprecated camel-opentelemetry module to camel-opentelemetry2. This adds OpenTelemetryInstrumentedThreadPoolFactory and OpenTelemetryInstrumentedThreadFactoryListener which wrap Camel-managed thread pools and thread factories with OpenTelemetry Context propagation, preventing context leaks when work crosses thread boundaries. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
…emetry2 thread-factory-listener Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🧪 CI tested the following changed modules:
All tested modules (74 modules)
|
johnpoth
left a comment
There was a problem hiding this comment.
This is great and needed thanks @gnodet !
CAMEL-23225 was more about Spring Boot though. This is still needed IMO and should be merged
CAMEL-23225 was about basically the same thing for Spring Boot's default ThreadPoolTaskExecutor which we use in camel-platform-http-starter which doesn't wrap new threads with the otel context like we do here (which causes a context leak). https://docs.spring.io/spring-boot/reference/actuator/observability.html#actuator.observability.context-propagation seems promising by setting spring.reactor.context-propagation to auto (haven't tried it out yet).
If the property doesn't work we could register a new TaskDecorator in the starters
package jp.co.rakutenbank.ifc.routers.fescores.connectivitytest.connectivitydbcore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskDecorator;
import io.opentelemetry.context.Context;
@Configuration(proxyBeanMethods = false)
public class TracingConfig {
@Bean
public TaskDecorator otelContextPropagatingDecorator() {
return runnable -> {
// Capture the current OTEL context on the submitting thread
Context currentContext = Context.current();
// Wrap the runnable so it activates this context on the worker thread
return () -> {
try (var ignored = currentContext.makeCurrent()) {
runnable.run();
}
};
};
}
}
Hope this helps!
JIRA: CAMEL-23225
Summary
camel-opentelemetrymodule tocamel-opentelemetry2OpenTelemetryInstrumentedThreadPoolFactory— wraps Camel-managedExecutorServiceandScheduledExecutorServicewithContext.taskWrapping()to propagate OTel contextOpenTelemetryInstrumentedThreadFactoryListener— wrapsThreadFactoryto propagate OTel context viaContext.current().wrap()@JdkServiceso they are automatically discovered by CamelThis prevents OpenTelemetry context leaks when Camel routes use async processing, thread pools, or other multi-threaded patterns. The deprecated
camel-opentelemetrymodule had these classes, but they were not carried over whencamel-opentelemetry2was created.Test plan
camel-opentelemetry2tests passthread-pool-factoryandthread-factory-listenercamel-opentelemetrymodule