Skip to content

CAMEL-23225: Propagate OpenTelemetry context across thread boundaries in camel-opentelemetry2#22190

Merged
gnodet merged 2 commits intomainfrom
CAMEL-23225
Mar 25, 2026
Merged

CAMEL-23225: Propagate OpenTelemetry context across thread boundaries in camel-opentelemetry2#22190
gnodet merged 2 commits intomainfrom
CAMEL-23225

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Mar 23, 2026

JIRA: CAMEL-23225

Summary

  • Port thread pool instrumentation from the deprecated camel-opentelemetry module to camel-opentelemetry2
  • Add OpenTelemetryInstrumentedThreadPoolFactory — wraps Camel-managed ExecutorService and ScheduledExecutorService with Context.taskWrapping() to propagate OTel context
  • Add OpenTelemetryInstrumentedThreadFactoryListener — wraps ThreadFactory to propagate OTel context via Context.current().wrap()
  • Both classes are registered as @JdkService so they are automatically discovered by Camel

This prevents OpenTelemetry context leaks when Camel routes use async processing, thread pools, or other multi-threaded patterns. The deprecated camel-opentelemetry module had these classes, but they were not carried over when camel-opentelemetry2 was created.

Test plan

  • All 12 existing camel-opentelemetry2 tests pass
  • JDK service files generated correctly for thread-pool-factory and thread-factory-listener
  • Verified same pattern as deprecated camel-opentelemetry module

… 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>
@github-actions
Copy link
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using build-all, build-dependents, skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

…emetry2 thread-factory-listener

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added the dsl label Mar 23, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 23, 2026

🧪 CI tested the following changed modules:

  • components/camel-opentelemetry2
  • dsl/camel-kamelet-main
All tested modules (74 modules)
  • Camel :: All Components Sync point
  • Camel :: All Components Sync point [pom]
  • Camel :: Assembly
  • Camel :: Assembly [pom]
  • Camel :: Catalog :: CSimple Maven Plugin (deprecated) [maven-plugin]
  • Camel :: Catalog :: CSimple Maven Plugin (deprecated) SUCCESS [ 1.463 s]
  • Camel :: Catalog :: Camel Catalog
  • Camel :: Catalog :: Camel Catalog [jar]
  • Camel :: Catalog :: Camel Report Maven Plugin
  • Camel :: Catalog :: Camel Report Maven Plugin [maven-plugin]
  • Camel :: Catalog :: Camel Route Parser
  • Camel :: Catalog :: Camel Route Parser [jar]
  • Camel :: Catalog :: Console
  • Camel :: Catalog :: Console [jar]
  • Camel :: Catalog :: Dummy Component
  • Camel :: Catalog :: Dummy Component [jar]
  • Camel :: Catalog :: Lucene (deprecated)
  • Camel :: Catalog :: Lucene (deprecated) [jar]
  • Camel :: Catalog :: Maven
  • Camel :: Catalog :: Maven [jar]
  • Camel :: Catalog :: Suggest
  • Camel :: Catalog :: Suggest [jar]
  • Camel :: Component DSL
  • Camel :: Component DSL [jar]
  • Camel :: Coverage
  • Camel :: Coverage [pom]
  • Camel :: Docs
  • Camel :: Docs [pom]
  • Camel :: Endpoint DSL
  • Camel :: Endpoint DSL [jar]
  • Camel :: Endpoint DSL :: Support
  • Camel :: Endpoint DSL :: Support [jar]
  • Camel :: Integration Tests
  • Camel :: Integration Tests [jar]
  • Camel :: JBang :: Core
  • Camel :: JBang :: Core [jar]
  • Camel :: JBang :: Integration tests
  • Camel :: JBang :: Integration tests [jar]
  • Camel :: JBang :: MCP
  • Camel :: JBang :: MCP [jar]
  • Camel :: JBang :: Main
  • Camel :: JBang :: Main [jar]
  • Camel :: JBang :: Plugin :: Edit
  • Camel :: JBang :: Plugin :: Edit [jar]
  • Camel :: JBang :: Plugin :: Generate
  • Camel :: JBang :: Plugin :: Generate [jar]
  • Camel :: JBang :: Plugin :: Kubernetes
  • Camel :: JBang :: Plugin :: Kubernetes [jar]
  • Camel :: JBang :: Plugin :: Route Parser
  • Camel :: JBang :: Plugin :: Route Parser [jar]
  • Camel :: JBang :: Plugin :: Testing
  • Camel :: JBang :: Plugin :: Testing [jar]
  • Camel :: JBang :: Plugin :: Validate
  • Camel :: JBang :: Plugin :: Validate [jar]
  • Camel :: Kamelet Main
  • Camel :: Kamelet Main [jar]
  • Camel :: Launcher
  • Camel :: Launcher [jar]
  • Camel :: Launcher :: Container
  • Camel :: Launcher :: Container [pom]
  • Camel :: Observability Services
  • Camel :: Observability Services [jar]
  • Camel :: Opentelemetry 2
  • Camel :: Opentelemetry 2 [jar]
  • Camel :: YAML DSL
  • Camel :: YAML DSL [jar]
  • Camel :: YAML DSL :: Deserializers
  • Camel :: YAML DSL :: Deserializers [jar]
  • Camel :: YAML DSL :: Maven Plugins
  • Camel :: YAML DSL :: Maven Plugins [maven-plugin]
  • Camel :: YAML DSL :: Validator
  • Camel :: YAML DSL :: Validator [jar]
  • Camel :: YAML DSL :: Validator Maven Plugin
  • Camel :: YAML DSL :: Validator Maven Plugin [maven-plugin]

@apupier apupier requested a review from johnpoth March 24, 2026 12:14
Copy link
Member

@johnpoth johnpoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@gnodet gnodet merged commit 5810a74 into main Mar 25, 2026
5 checks passed
@gnodet gnodet deleted the CAMEL-23225 branch March 25, 2026 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants