diff --git a/docs/modules/ROOT/pages/reference/extensions/opentelemetry.adoc b/docs/modules/ROOT/pages/reference/extensions/opentelemetry.adoc index 84982456b641..7fd7861c8b0b 100644 --- a/docs/modules/ROOT/pages/reference/extensions/opentelemetry.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/opentelemetry.adoc @@ -120,15 +120,15 @@ There is more information about CDI instrumentation in the https://quarkus.io/gu | Configuration property | Type | Default -|icon:lock[title=Fixed at build time] [[quarkus.camel.opentelemetry.encoding]]`link:#quarkus.camel.opentelemetry.encoding[quarkus.camel.opentelemetry.encoding]` +| [[quarkus.camel.opentelemetry.encoding]]`link:#quarkus.camel.opentelemetry.encoding[quarkus.camel.opentelemetry.encoding]` Sets whether header names need to be encoded. Can be useful in situations where OpenTelemetry propagators potentially set header name values in formats that are not compatible with the target system. E.g for JMS where the specification mandates header names are valid Java identifiers. | `boolean` | `false` -|icon:lock[title=Fixed at build time] [[quarkus.camel.opentelemetry.exclude-patterns]]`link:#quarkus.camel.opentelemetry.exclude-patterns[quarkus.camel.opentelemetry.exclude-patterns]` +| [[quarkus.camel.opentelemetry.exclude-patterns]]`link:#quarkus.camel.opentelemetry.exclude-patterns[quarkus.camel.opentelemetry.exclude-patterns]` -Sets whether to disable tracing for endpoint URIs that match the given comma separated patterns. The pattern can take the following forms: +Sets whether to disable tracing for endpoint URIs or Processor ids that match the given comma separated patterns. The pattern can take the following forms: @@ -143,6 +143,12 @@ Sets whether to disable tracing for endpoint URIs that match the given comma sep 3. A regular expression matching the endpoint URI. E.g platform-http:/prefix/.++*++ | `string` | + +| [[quarkus.camel.opentelemetry.trace-processors]]`link:#quarkus.camel.opentelemetry.trace-processors[quarkus.camel.opentelemetry.trace-processors]` + +Sets whether to create new OpenTelemetry spans for each Camel Processor. Use the excludePatterns property to filter out Processors. +| `boolean` +| `false` |=== [.configuration-legend] diff --git a/extensions/opentelemetry/deployment/src/test/java/org/apache/camel/quarkus/component/opentelemetry/deployment/OpenTelemetryEnabledTest.java b/extensions/opentelemetry/deployment/src/test/java/org/apache/camel/quarkus/component/opentelemetry/deployment/OpenTelemetryEnabledTest.java index 57ab28369e35..dfbda0612105 100644 --- a/extensions/opentelemetry/deployment/src/test/java/org/apache/camel/quarkus/component/opentelemetry/deployment/OpenTelemetryEnabledTest.java +++ b/extensions/opentelemetry/deployment/src/test/java/org/apache/camel/quarkus/component/opentelemetry/deployment/OpenTelemetryEnabledTest.java @@ -23,18 +23,25 @@ import org.apache.camel.CamelContext; import org.apache.camel.opentelemetry.CamelQuarkusOpenTelemetryTracer; import org.apache.camel.opentelemetry.OpenTelemetryTracer; +import org.apache.camel.opentelemetry.OpenTelemetryTracingStrategy; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertTrue; public class OpenTelemetryEnabledTest { + private static final String EXCLUDE_PATTERNS = "platform-http:*,platform-http:/prefix/.*"; + @RegisterExtension static final QuarkusUnitTest CONFIG = new QuarkusUnitTest() + .overrideConfigKey("quarkus.camel.opentelemetry.encoding", "true") + .overrideConfigKey("quarkus.camel.opentelemetry.exclude-patterns", EXCLUDE_PATTERNS) + .overrideConfigKey("quarkus.camel.opentelemetry.trace-processors", "true") .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); @Inject @@ -45,7 +52,10 @@ public void camelOpenTelemetryTracerRegistryBeanNotNull() { Set tracers = context.getRegistry().findByType(OpenTelemetryTracer.class); assertEquals(1, tracers.size()); - OpenTelemetryTracer factory = tracers.iterator().next(); - assertTrue(factory instanceof CamelQuarkusOpenTelemetryTracer); + OpenTelemetryTracer tracer = tracers.iterator().next(); + assertInstanceOf(CamelQuarkusOpenTelemetryTracer.class, tracer); + assertInstanceOf(OpenTelemetryTracingStrategy.class, tracer.getTracingStrategy()); + assertTrue(tracer.isEncoding()); + assertEquals(EXCLUDE_PATTERNS, tracer.getExcludePatterns()); } } diff --git a/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/CamelOpenTelemetryConfig.java b/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/CamelOpenTelemetryConfig.java index dd0670842f3d..7ea9cb3759fe 100644 --- a/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/CamelOpenTelemetryConfig.java +++ b/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/CamelOpenTelemetryConfig.java @@ -22,7 +22,7 @@ import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; -@ConfigRoot(name = "camel.opentelemetry", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) +@ConfigRoot(name = "camel.opentelemetry", phase = ConfigPhase.RUN_TIME) public final class CamelOpenTelemetryConfig { /** @@ -34,7 +34,7 @@ public final class CamelOpenTelemetryConfig { public boolean encoding; /** - * Sets whether to disable tracing for endpoint URIs that match the given + * Sets whether to disable tracing for endpoint URIs or Processor ids that match the given * comma separated patterns. The pattern can take the following * forms: *

@@ -49,4 +49,11 @@ public final class CamelOpenTelemetryConfig { */ @ConfigItem public Optional excludePatterns; + + /** + * Sets whether to create new OpenTelemetry spans for each Camel Processor. Use the excludePatterns + * property to filter out Processors. + */ + @ConfigItem(defaultValue = "false") + public boolean traceProcessors; } diff --git a/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/OpenTelemetryTracerProducer.java b/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/OpenTelemetryTracerProducer.java index ff44a1aaf677..0e1b61983c3e 100644 --- a/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/OpenTelemetryTracerProducer.java +++ b/extensions/opentelemetry/runtime/src/main/java/org/apache/camel/quarkus/component/opentelemetry/OpenTelemetryTracerProducer.java @@ -23,6 +23,7 @@ import jakarta.inject.Singleton; import org.apache.camel.opentelemetry.CamelQuarkusOpenTelemetryTracer; import org.apache.camel.opentelemetry.OpenTelemetryTracer; +import org.apache.camel.opentelemetry.OpenTelemetryTracingStrategy; @Singleton public class OpenTelemetryTracerProducer { @@ -43,6 +44,13 @@ public OpenTelemetryTracer getOpenTelemetry() { if (config.excludePatterns.isPresent()) { openTelemetryTracer.setExcludePatterns(config.excludePatterns.get()); } + + if (config.traceProcessors) { + OpenTelemetryTracingStrategy tracingStrategy = new OpenTelemetryTracingStrategy(openTelemetryTracer); + tracingStrategy.setPropagateContext(true); + openTelemetryTracer.setTracingStrategy(tracingStrategy); + } + openTelemetryTracer.setEncoding(config.encoding); } return openTelemetryTracer; diff --git a/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTest.java b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTest.java index b4cd23ddf482..91dd3c4fee4e 100644 --- a/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTest.java +++ b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTest.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import static org.apache.camel.quarkus.component.opentelemetry.it.OpenTelemetryTestHelper.getSpans; import static org.awaitility.Awaitility.await; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -86,8 +87,8 @@ public void testTracedCamelRouteInvokedFromJaxRsService() { List> spans = getSpans(); assertEquals(3, spans.size()); assertEquals(spans.get(0).get("parentId"), spans.get(1).get("spanId")); - assertEquals(spans.get(1).get("kind"), SpanKind.CLIENT.name()); - assertEquals(spans.get(2).get("kind"), SpanKind.SERVER.name()); + assertEquals(SpanKind.CLIENT.name(), spans.get(1).get("kind")); + assertEquals(SpanKind.SERVER.name(), spans.get(2).get("kind")); } @Test @@ -104,8 +105,8 @@ public void testTracedBean() { assertEquals(4, spans.size()); assertEquals(spans.get(0).get("parentId"), spans.get(1).get("parentId")); assertEquals(spans.get(1).get("parentId"), spans.get(2).get("spanId")); - assertEquals(spans.get(2).get("kind"), SpanKind.CLIENT.name()); - assertEquals(spans.get(3).get("kind"), SpanKind.SERVER.name()); + assertEquals(SpanKind.CLIENT.name(), spans.get(2).get("kind")); + assertEquals(SpanKind.SERVER.name(), spans.get(3).get("kind")); } @Test @@ -141,15 +142,4 @@ public void testTracedJdbcQuery() { assertEquals(spans.get(5).get("parentId"), "0000000000000000"); assertEquals(spans.get(5).get("code.function"), "jdbcQuery"); } - - private List> getSpans() { - return RestAssured.given() - .get("/opentelemetry/exporter/spans") - .then() - .statusCode(200) - .extract() - .body() - .jsonPath() - .get(); - } } diff --git a/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTestHelper.java b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTestHelper.java new file mode 100644 index 000000000000..85a0f36e4b4f --- /dev/null +++ b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTestHelper.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.opentelemetry.it; + +import java.util.List; +import java.util.Map; + +import io.restassured.RestAssured; + +class OpenTelemetryTestHelper { + private OpenTelemetryTestHelper() { + // Utility class + } + + static List> getSpans() { + return RestAssured.given() + .get("/opentelemetry/exporter/spans") + .then() + .statusCode(200) + .extract() + .body() + .jsonPath() + .get(); + } +} diff --git a/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTraceProcessorsIT.java b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTraceProcessorsIT.java new file mode 100644 index 000000000000..e25053d12ff4 --- /dev/null +++ b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTraceProcessorsIT.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.opentelemetry.it; + +import io.quarkus.test.junit.QuarkusIntegrationTest; + +@QuarkusIntegrationTest +class OpenTelemetryTraceProcessorsIT extends OpenTelemetryTraceProcessorsTest { +} diff --git a/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTraceProcessorsTest.java b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTraceProcessorsTest.java new file mode 100644 index 000000000000..365b1e33b83d --- /dev/null +++ b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/OpenTelemetryTraceProcessorsTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.opentelemetry.it; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import io.opentelemetry.api.trace.SpanKind; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.RestAssured; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.quarkus.component.opentelemetry.it.OpenTelemetryTestHelper.getSpans; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@TestProfile(TraceProcessorsTestProfile.class) +@QuarkusTest +class OpenTelemetryTraceProcessorsTest { + @Test + void traceProcessors() { + RestAssured.get("/opentelemetry/trace") + .then() + .statusCode(200) + .body(equalTo("Traced direct:start")); + + // Verify the span hierarchy is JAX-RS Service -> Direct Endpoint -> SetBody + await().atMost(30, TimeUnit.SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(() -> getSpans().size() == 4); + List> spans = getSpans(); + assertEquals(4, spans.size()); + assertEquals(SpanKind.INTERNAL.name(), spans.get(0).get("kind")); + assertEquals("camel-setBody", spans.get(0).get("component")); + assertEquals(spans.get(1).get("spanId"), spans.get(0).get("parentId")); + assertEquals(SpanKind.INTERNAL.name(), spans.get(1).get("kind")); + assertEquals(spans.get(2).get("spanId"), spans.get(1).get("parentId")); + assertEquals(SpanKind.CLIENT.name(), spans.get(2).get("kind")); + assertEquals(spans.get(3).get("spanId"), spans.get(2).get("parentId")); + assertEquals(SpanKind.SERVER.name(), spans.get(3).get("kind")); + } +} diff --git a/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/TraceProcessorsTestProfile.java b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/TraceProcessorsTestProfile.java new file mode 100644 index 000000000000..20dfeeb52733 --- /dev/null +++ b/integration-tests/opentelemetry/src/test/java/org/apache/camel/quarkus/component/opentelemetry/it/TraceProcessorsTestProfile.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.opentelemetry.it; + +import java.util.Map; + +import io.quarkus.test.junit.QuarkusTestProfile; + +public class TraceProcessorsTestProfile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("quarkus.camel.opentelemetry.trace-processors", "true"); + } +}