Skip to content

Commit

Permalink
Apache camel enable sqs propagation (#2102) -- 0.15.x cherrypick (#2200)
Browse files Browse the repository at this point in the history
* Apache camel enable sqs propagation (#2102)

* adding AWS SQS tests to Apache Camel instrumentation

* code review changes

Co-authored-by: Anuraag Agrawal <[email protected]>

* SQS context propagation for Apache Camel instrumentation

* code review - removed not needed dep

Co-authored-by: Anuraag Agrawal <[email protected]>

* Bumped testcontainers version to make smoke tests pass

Co-authored-by: Jakub Wach <[email protected]>
Co-authored-by: Anuraag Agrawal <[email protected]>
  • Loading branch information
3 people authored Feb 5, 2021
1 parent 94fd21e commit 0d4f861
Show file tree
Hide file tree
Showing 22 changed files with 664 additions and 272 deletions.
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ext {
],
groovy : "org.codehaus.groovy:groovy-all:${versions.groovy}",
systemLambda : "com.github.stefanbirkner:system-lambda:${versions.systemLambda}",
testcontainers : "org.testcontainers:testcontainers:1.15.0-rc2",
testcontainers : "org.testcontainers:testcontainers:1.15.1",
testLogging : [
dependencies.create(group: 'ch.qos.logback', name: 'logback-classic', version: versions.logback),
dependencies.create(group: 'org.slf4j', name: 'log4j-over-slf4j', version: versions.slf4j),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply from: "$rootDir/gradle/java.gradle"

dependencies {
testImplementation project(':instrumentation:apache-camel-2.20:javaagent')
testImplementation group: 'org.apache.camel', name: 'camel-core', version: '2.20.1'
testImplementation group: 'org.apache.camel', name: 'camel-aws', version: '2.20.1'

testImplementation deps.opentelemetryTraceProps
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.18.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.apachecamel;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import java.util.Collections;
import java.util.Map;
import org.apache.camel.Endpoint;
import org.apache.camel.component.aws.sqs.SqsComponent;
import org.apache.camel.component.aws.sqs.SqsConfiguration;
import org.apache.camel.component.aws.sqs.SqsEndpoint;
import org.junit.jupiter.api.Test;

public class CamelPropagationUtilTest {

@Test
public void shouldExtractAwsParent() {

// given
Endpoint endpoint = new SqsEndpoint("", new SqsComponent(), new SqsConfiguration());
Map<String, Object> exchangeHeaders =
Collections.singletonMap(
"AWSTraceHeader",
"Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\n");

// when
Context parent = CamelPropagationUtil.extractParent(exchangeHeaders, endpoint);

// then
Span parentSpan = Span.fromContext(parent);
SpanContext parentSpanContext = parentSpan.getSpanContext();
assertThat(parentSpanContext.getTraceIdAsHexString())
.isEqualTo("5759e988bd862e3fe1be46a994272793");
assertThat(parentSpanContext.getSpanIdAsHexString()).isEqualTo("53995c3f42cd8ad8");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ muzzle {

dependencies {
library group: 'org.apache.camel', name: 'camel-core', version: '2.20.1'
implementation deps.opentelemetryTraceProps

testLibrary group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.20.1'
testLibrary group: 'org.apache.camel', name: 'camel-jetty-starter', version: '2.20.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new CamelContextInstrumentation());
}

@Override
public String[] additionalHelperClassNames() {
return new String[] {"io.opentelemetry.extension.trace.propagation.AwsXrayPropagator"};
}

public static class CamelContextInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<ClassLoader> classLoaderOptimization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,34 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator.Getter;
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
import io.opentelemetry.extension.trace.propagation.AwsXrayPropagator;
import java.util.Collections;
import java.util.Map;
import org.apache.camel.Endpoint;

final class CamelPropagationUtil {

private CamelPropagationUtil() {}

static Context extractParent(final Map<String, Object> exchangeHeaders) {
private static boolean isAwsPropagated(Endpoint endpoint) {
return endpoint.getClass().getName().endsWith("SqsEndpoint");
}

static Context extractParent(final Map<String, Object> exchangeHeaders, Endpoint endpoint) {
return (isAwsPropagated(endpoint)
? extractAwsPropagationParent(exchangeHeaders)
: extractHttpPropagationParent(exchangeHeaders));
}

private static Context extractAwsPropagationParent(final Map<String, Object> exchangeHeaders) {
return AwsXrayPropagator.getInstance()
.extract(
Context.current(),
Collections.singletonMap("X-Amzn-Trace-Id", exchangeHeaders.get("AWSTraceHeader")),
MapGetter.INSTANCE);
}

private static Context extractHttpPropagationParent(final Map<String, Object> exchangeHeaders) {
return GlobalOpenTelemetry.getPropagators()
.getTextMapPropagator()
.extract(Context.current(), exchangeHeaders, MapGetter.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private Span spanOnExchangeBegin(
SpanBuilder builder = CamelTracer.TRACER.spanBuilder(name);
builder.setSpanKind(spanKind);
if (!activeSpan.getSpanContext().isValid()) {
Context parentContext = CamelPropagationUtil.extractParent(exchange.getIn().getHeaders());
Context parentContext =
CamelPropagationUtil.extractParent(exchange.getIn().getHeaders(), route.getEndpoint());
if (parentContext != null) {
builder.setParent(parentContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import static io.opentelemetry.api.trace.Span.Kind.INTERNAL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import org.apache.camel.LoggingLevel
import org.apache.camel.builder.RouteBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import org.apache.camel.LoggingLevel
import org.apache.camel.builder.RouteBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import static io.opentelemetry.api.trace.Span.Kind.INTERNAL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import static io.opentelemetry.api.trace.Span.Kind.CLIENT
import static io.opentelemetry.api.trace.Span.Kind.INTERNAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import org.apache.camel.LoggingLevel
import org.apache.camel.builder.RouteBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import static io.opentelemetry.api.trace.Span.Kind.SERVER

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package test
package io.opentelemetry.javaagent.instrumentation.apachecamel

import org.apache.camel.LoggingLevel
import org.apache.camel.builder.RouteBuilder
Expand Down
Loading

0 comments on commit 0d4f861

Please sign in to comment.