Skip to content

Commit

Permalink
[#3190] Use OpenTelemetry for tracing (via OpenTracing shim).
Browse files Browse the repository at this point in the history
Signed-off-by: Carsten Lohmann <[email protected]>
  • Loading branch information
calohmn committed Apr 12, 2022
1 parent 2ebb2b4 commit d8d8013
Show file tree
Hide file tree
Showing 49 changed files with 761 additions and 384 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, 2021 Contributors to the Eclipse Foundation
# Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
Expand Down Expand Up @@ -59,4 +59,4 @@ jobs:
-Dhono.deviceregistry.type=${{ matrix.device-registry }} \
-Dhono.messaging-infra.type=${{ matrix.messaging-type }} \
-Dhono.commandrouting.cache=${{ matrix.commandrouting-cache }} \
-Pbuild-docker-image,jaeger,run-tests
-Pbuild-docker-image,opentelemetry,run-tests
6 changes: 3 additions & 3 deletions .github/workflows/native-images-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ jobs:
run: |
mvn install -B -e -DskipTests -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
-Dquarkus.native.remote-container-build=false -Dquarkus.native.container-build=true \
-Pjaeger,build-native-image -am \
-Popentelemetry,build-native-image -am \
-pl :hono-service-auth-quarkus,:hono-service-command-router-quarkus,:hono-service-device-registry-mongodb-quarkus,\
:hono-adapter-mqtt-vertx-quarkus,:hono-adapter-amqp-vertx-quarkus,:hono-adapter-coap-vertx-quarkus,:hono-adapter-http-vertx-quarkus,\
:hono-tests
- name: "Build JDBC registry image"
run: |
mvn install -B -e -DskipTests -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
-Pjaeger,build-docker-image -am -pl :hono-service-device-registry-jdbc-quarkus
-Popentelemetry,build-docker-image -am -pl :hono-service-device-registry-jdbc-quarkus
- name: "Run integration tests with Mongo DB and Kafka"
run: |
mvn verify -pl :hono-tests -B -e -DCI=$CI \
-Dhono.components.type=quarkus-native -Dhono.deviceregistry.type=mongodb \
-Dhono.messaging-infra.type=kafka -Dhono.commandrouting.cache=server \
-Prun-tests,jaeger
-Prun-tests,opentelemetry
- name: "Run integration tests with Sandbox-like configuration"
run: |
mvn verify -pl :hono-tests -B -e -DCI=$CI \
Expand Down
45 changes: 5 additions & 40 deletions adapters/base-quarkus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>quarkus.jaeger.metrics.enabled</name>
<value>${jaeger.metrics.enabled}</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -256,30 +236,15 @@
</profile>

<profile>
<id>jaeger</id>
<id>opentelemetry</id>
<activation>
<property>
<name>jaeger</name>
<name>opentelemetry</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
</dependency>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-micrometer</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jaeger</artifactId>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
</dependency>
</dependencies>
<properties>
<quarkus.opentelemetry.enabled>true</quarkus.opentelemetry.enabled>
</properties>
</profile>
</profiles>
</project>
14 changes: 12 additions & 2 deletions adapters/coap-vertx-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<groupId>org.eclipse.californium</groupId>
<artifactId>scandium</artifactId>
</dependency>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
Expand All @@ -44,8 +49,13 @@

<!-- testing -->
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-extension-trace-propagators</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020 Contributors to the Eclipse Foundation
* Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -15,13 +15,19 @@
package org.eclipse.hono.adapter.coap;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.californium.core.coap.Option;
import org.eclipse.californium.core.coap.OptionSet;

import io.jaegertracing.internal.JaegerSpanContext;
import io.jaegertracing.internal.propagation.BinaryCodec;
import io.jaegertracing.internal.propagation.TextMapCodec;
import io.opentracing.propagation.Binary;
import io.opentracing.propagation.TextMap;
import io.opentracing.propagation.TextMapAdapter;

/**
* An adapter for injecting/extracting an OpenTracing {@code SpanContext} to/from a CoAP option.
Expand Down Expand Up @@ -52,7 +58,7 @@ private CoapOptionInjectExtractAdapter() {
}

/**
* Creates a new carrier for extracting a trace context from CoAP options.
* Creates a new binary-format carrier for extracting a trace context from CoAP options.
*
* @param options The CoAP options to extract the context from.
* @throws NullPointerException if options is {@code null}.
Expand All @@ -70,7 +76,29 @@ public static Optional<CoapOptionInjectExtractAdapter> forExtraction(final Optio
}

/**
* Creates a new carrier for injecting a trace context into CoAP options.
* Creates a new text-format carrier from this extraction carrier.
* <p>
* Can be used with an OpenTelemetry TracerShim (OpenTelemetry doesn't support binary
* propagation yet - see https:/open-telemetry/opentelemetry-specification/issues/437).
*
* @return The carrier.
* @throws IllegalStateException If this adapter is not suitable for extraction.
*/
public TextMap asTextMapCarrierForExtraction() {
if (optionToExtractFrom == null) {
throw new IllegalStateException("this carrier is not suitable for extraction");
}
final BinaryCodec jaegerBinaryCodec = new BinaryCodec();
final JaegerSpanContext jaegerSpanContext = jaegerBinaryCodec.extract(this);

final TextMapCodec textMapCodec = new TextMapCodec(false);
final TextMap textMap = new TextMapAdapter(new HashMap<>(1));
textMapCodec.inject(jaegerSpanContext, textMap);
return textMap;
}

/**
* Creates a new binary-format carrier for injecting a trace context into CoAP options.
*
* @param options The CoAP options to inject the context to.
* @throws NullPointerException if options is {@code null}.
Expand All @@ -96,9 +124,6 @@ private static Optional<Option> getTraceContextOption(final OptionSet optionSet)
.findFirst();
}

/**
* {@inheritDoc}
*/
@Override
public ByteBuffer injectionBuffer(final int length) {
if (options == null) {
Expand All @@ -110,9 +135,6 @@ public ByteBuffer injectionBuffer(final int length) {
}
}

/**
* {@inheritDoc}
*/
@Override
public ByteBuffer extractionBuffer() {
if (optionToExtractFrom == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019, 2021 Contributors to the Eclipse Foundation
* Copyright (c) 2019, 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -149,9 +149,12 @@ public static String getAuthId(final CoapExchange exchange) {
return extPrincipal.getExtendedInfo().get(DeviceInfoSupplier.EXT_INFO_KEY_HONO_AUTH_ID, String.class);
}

private SpanContext extractSpanContextFromRequest(final OptionSet requestOptions) {
SpanContext extractSpanContextFromRequest(final OptionSet requestOptions) {
return CoapOptionInjectExtractAdapter.forExtraction(requestOptions)
.map(carrier -> tracer.extract(Format.Builtin.BINARY, carrier))
.map(carrier -> {
// have to adapt the carrier because the binary format isn't supported with the OpenTelemetry TracerShim yet
return tracer.extract(Format.Builtin.TEXT_MAP, carrier.asTextMapCarrierForExtraction());
})
.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020 Contributors to the Eclipse Foundation
* Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -20,6 +20,11 @@
import org.junit.jupiter.api.Test;

import io.jaegertracing.Configuration;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.JaegerPropagator;
import io.opentelemetry.opentracingshim.OpenTracingShim;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
Expand All @@ -39,17 +44,46 @@ class CoapOptionInjectExtractAdapterTest {
@Test
public void testJaegerTracerInjectsAndExtractsSpanContext() {

final Configuration config = new Configuration("test");
final Tracer tracer = config.getTracer();
final Span span = tracer.buildSpan("do").start();
// explicitly using Jaeger tracer here - the OpenTelemetry TracerShim doesn't support BINARY propagation yet (see https:/open-telemetry/opentelemetry-specification/issues/437)
final Configuration jaegerConfig = new Configuration("test");
final Tracer jaegerTracer = jaegerConfig.getTracer();
final Span span = jaegerTracer.buildSpan("do").start();

final OptionSet optionSet = new OptionSet();
final CoapOptionInjectExtractAdapter injectAdapter = CoapOptionInjectExtractAdapter.forInjection(optionSet);
tracer.inject(span.context(), Format.Builtin.BINARY, injectAdapter);
jaegerTracer.inject(span.context(), Format.Builtin.BINARY, injectAdapter);

final SpanContext context = CoapOptionInjectExtractAdapter.forExtraction(optionSet)
.map(carrier -> tracer.extract(Format.Builtin.BINARY, carrier))
.map(carrier -> jaegerTracer.extract(Format.Builtin.BINARY, carrier))
.orElse(null);
assertThat(context.toSpanId()).isEqualTo(span.context().toSpanId());
}

/**
* Verifies that an injected span can be successfully extracted using the text-format carrier
* returned by {@link CoapOptionInjectExtractAdapter#asTextMapCarrierForExtraction()}.
*/
@Test
public void testExtractionUsingTextFormatCarrier() {
// prepare OptionSet with injected span
final Configuration jaegerConfig = new Configuration("test");
final Tracer jaegerTracer = jaegerConfig.getTracer();
final Span span = jaegerTracer.buildSpan("do").start();

final OptionSet optionSet = new OptionSet();
final CoapOptionInjectExtractAdapter injectAdapter = CoapOptionInjectExtractAdapter.forInjection(optionSet);
jaegerTracer.inject(span.context(), Format.Builtin.BINARY, injectAdapter);

// init tracerShim used for span extraction
final OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
.setPropagators(ContextPropagators.create(JaegerPropagator.getInstance()))
.build();
final Tracer tracerShim = OpenTracingShim.createTracerShim(openTelemetry);

final SpanContext context = CoapOptionInjectExtractAdapter.forExtraction(optionSet)
.map(CoapOptionInjectExtractAdapter::asTextMapCarrierForExtraction)
.map(textFormatCarrier -> tracerShim.extract(Format.Builtin.TEXT_MAP, textFormatCarrier))
.orElse(null);
assertThat(context.toSpanId()).isEqualTo(span.context().toSpanId());
}
}
Loading

0 comments on commit d8d8013

Please sign in to comment.