Skip to content

Commit

Permalink
fixing possible NPE in Apache Camel instrumentation (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-wu authored and github-actions committed Feb 12, 2021
1 parent a8676e2 commit f9a2928
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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 group: 'org.apache.camel', name: 'camel-http', version: '2.20.1'

testImplementation deps.opentelemetryTraceProps
testImplementation deps.opentelemetryExtAws
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.18.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,86 @@

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

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.JaegerPropagator;
import java.net.URI;
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.apache.camel.component.http.HttpComponent;
import org.apache.camel.component.http.HttpEndpoint;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class CamelPropagationUtilTest {

@BeforeAll
public static void setUp() {
GlobalOpenTelemetry.set(
OpenTelemetry.getPropagating(ContextPropagators.create(JaegerPropagator.getInstance())));
}

@Test
public void shouldExtractHttpParentForHttpEndpoint() throws Exception {

// given
Endpoint endpoint = new HttpEndpoint("", new HttpComponent(), URI.create(""));
Map<String, Object> exchangeHeaders =
Collections.singletonMap(
"uber-trace-id", "1f7f8dab3f0043b1b9cf0a75caf57510:a13825abcb764bd3:0:1");

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

// then
Span parentSpan = Span.fromContext(parent);
SpanContext parentSpanContext = parentSpan.getSpanContext();
assertThat(parentSpanContext.getTraceId()).isEqualTo("1f7f8dab3f0043b1b9cf0a75caf57510");
assertThat(parentSpanContext.getSpanId()).isEqualTo("a13825abcb764bd3");
}

@Test
public void shouldNotFailExtractingNullHttpParentForHttpEndpoint() throws Exception {

// given
Endpoint endpoint = new HttpEndpoint("", new HttpComponent(), URI.create(""));
Map<String, Object> exchangeHeaders = Collections.singletonMap("uber-trace-id", null);

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

// then
Span parentSpan = Span.fromContext(parent);
SpanContext parentSpanContext = parentSpan.getSpanContext();
assertThat(parentSpanContext.isValid()).isEqualTo(false);
}

@Test
public void shouldNotFailExtractingNullAwsParentForSqsEndpoint() {

// given
Endpoint endpoint = new SqsEndpoint("", new SqsComponent(), new SqsConfiguration());
Map<String, Object> exchangeHeaders = Collections.singletonMap("AWSTraceHeader", null);

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

// then
Span parentSpan = Span.fromContext(parent);
SpanContext parentSpanContext = parentSpan.getSpanContext();
assertThat(parentSpanContext.isValid()).isEqualTo(false);
}

@Test
public void shouldExtractAwsParent() {
public void shouldExtractAwsParentForSqsEndpoint() {

// given
Endpoint endpoint = new SqsEndpoint("", new SqsComponent(), new SqsConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public Iterable<String> keys(Map<String, Object> map) {
}

@Override
public String get(Map<String, Object> map, String s) {
return (map.containsKey(s) ? map.get(s).toString() : null);
public String get(Map<String, Object> map, String key) {
Object value = map.get(key);
return (value == null ? null : value.toString());
}
}

Expand Down

0 comments on commit f9a2928

Please sign in to comment.