Skip to content

Commit

Permalink
Add traceProcessors option to OpenTelemetry extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Jun 3, 2024
1 parent 4188a3a commit 82139ea
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 22 deletions.
12 changes: 9 additions & 3 deletions docs/modules/ROOT/pages/reference/extensions/opentelemetry.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:



Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +52,10 @@ public void camelOpenTelemetryTracerRegistryBeanNotNull() {
Set<OpenTelemetryTracer> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -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:
* <p>
Expand All @@ -49,4 +49,11 @@ public final class CamelOpenTelemetryConfig {
*/
@ConfigItem
public Optional<String> 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,8 +87,8 @@ public void testTracedCamelRouteInvokedFromJaxRsService() {
List<Map<String, String>> 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
Expand All @@ -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
Expand Down Expand Up @@ -141,15 +142,4 @@ public void testTracedJdbcQuery() {
assertEquals(spans.get(5).get("parentId"), "0000000000000000");
assertEquals(spans.get(5).get("code.function"), "jdbcQuery");
}

private List<Map<String, String>> getSpans() {
return RestAssured.given()
.get("/opentelemetry/exporter/spans")
.then()
.statusCode(200)
.extract()
.body()
.jsonPath()
.get();
}
}
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> getSpans() {
return RestAssured.given()
.get("/opentelemetry/exporter/spans")
.then()
.statusCode(200)
.extract()
.body()
.jsonPath()
.get();
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> 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"));
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> getConfigOverrides() {
return Map.of("quarkus.camel.opentelemetry.trace-processors", "true");
}
}

0 comments on commit 82139ea

Please sign in to comment.