Skip to content

Commit

Permalink
Add zipkin compatibility mode
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-valdoleiros-gowithflow-io committed Jul 1, 2021
1 parent ada7316 commit f612f93
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2859,6 +2859,11 @@
<artifactId>jaeger-thrift</artifactId>
<version>${jaeger.version}</version>
</dependency>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-zipkin</artifactId>
<version>${jaeger.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions extensions/jaeger/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<artifactId>quarkus-smallrye-metrics-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.quarkus.jaeger.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import io.jaegertracing.Configuration;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.JaegerTracer.Builder;
import io.jaegertracing.spi.Reporter;
import io.jaegertracing.zipkin.ZipkinV2Reporter;
import io.opentracing.Tracer;
import io.quarkus.jaeger.runtime.QuarkusJaegerTracer;

public class QuarkusJaegerTracerTest {

@Test
public void withzipkinCompatibilityMode() {

try (MockedStatic<Configuration> mockedStaticConfiguration = Mockito.mockStatic(Configuration.class)) {
Configuration mockedInstanceConfiguration = Mockito.mock(Configuration.class);
Builder mockedBuilder = Mockito.mock(Builder.class);
Tracer mockedTracer = Mockito.mock(JaegerTracer.class);

mockedStaticConfiguration.when(() -> Configuration.fromEnv()).thenReturn(mockedInstanceConfiguration);
mockedStaticConfiguration.when(() -> mockedInstanceConfiguration.withMetricsFactory(Mockito.any())).thenReturn(mockedInstanceConfiguration);
mockedStaticConfiguration.when(() -> mockedInstanceConfiguration.getTracerBuilder()).thenReturn(mockedBuilder);
mockedStaticConfiguration.when(() -> mockedBuilder.withScopeManager(Mockito.any())).thenReturn(mockedBuilder);
mockedStaticConfiguration.when(() -> mockedBuilder.withReporter(Mockito.any())).thenReturn(mockedBuilder);
mockedStaticConfiguration.when(() -> mockedBuilder.build()).thenReturn(mockedTracer);

QuarkusJaegerTracer tracer = new QuarkusJaegerTracer();
tracer.setZipkinCompatibilityMode(true);
tracer.setEndpoint("http://localhost");
tracer.toString();
tracer.close();

ArgumentCaptor<Reporter> argument = ArgumentCaptor.forClass(Reporter.class);
Mockito.verify(mockedBuilder).withReporter(argument.capture());
assertEquals(ZipkinV2Reporter.class, argument.getValue().getClass());
}

}

@Test
public void withoutZipkinCompatibilityMode() {
try (MockedStatic<Configuration> mockedStaticConfiguration = Mockito.mockStatic(Configuration.class)) {
Configuration mockedInstanceConfiguration = Mockito.mock(Configuration.class);
Builder mockedBuilder = Mockito.mock(Builder.class);
Tracer mockedTracer = Mockito.mock(JaegerTracer.class);

mockedStaticConfiguration.when(() -> Configuration.fromEnv()).thenReturn(mockedInstanceConfiguration);
mockedStaticConfiguration.when(() -> mockedInstanceConfiguration.withMetricsFactory(Mockito.any())).thenReturn(mockedInstanceConfiguration);
mockedStaticConfiguration.when(() -> mockedInstanceConfiguration.getTracerBuilder()).thenReturn(mockedBuilder);
mockedStaticConfiguration.when(() -> mockedBuilder.withScopeManager(Mockito.any())).thenReturn(mockedBuilder);
mockedStaticConfiguration.when(() -> mockedBuilder.withReporter(Mockito.any())).thenReturn(mockedBuilder);
mockedStaticConfiguration.when(() -> mockedBuilder.build()).thenReturn(mockedTracer);

QuarkusJaegerTracer tracer = new QuarkusJaegerTracer();
tracer.toString();
tracer.close();

ArgumentCaptor<Reporter> argument = ArgumentCaptor.forClass(Reporter.class);
Mockito.verify(mockedBuilder).withReporter(argument.capture());
assertNull(argument.getValue());
}
}

}
14 changes: 14 additions & 0 deletions extensions/jaeger/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-zipkin</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- transitive dependency from jaeger-thrift, consider removing once we move to jaeger 1.x -->
<dependency>
<groupId>jakarta.activation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ public class JaegerConfig {
@ConfigItem(defaultValue = "true")
public Boolean logTraceContext;

/**
* Whether the jaeger should run in zipkin compatibility mode
*/
@ConfigItem
public Optional<Boolean> zipkinCompatibilityMode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ private synchronized void registerTracer(JaegerConfig jaeger, ApplicationConfig

private void initTracerConfig(JaegerConfig jaeger) {
initTracerProperty("JAEGER_ENDPOINT", jaeger.endpoint, uri -> uri.toString());
if (jaeger.endpoint.isPresent()) {
quarkusTracer.setEndpoint(jaeger.endpoint.get().toString());
}
initTracerProperty("JAEGER_AUTH_TOKEN", jaeger.authToken, token -> token);
initTracerProperty("JAEGER_USER", jaeger.user, user -> user);
initTracerProperty("JAEGER_PASSWORD", jaeger.password, pw -> pw);
Expand All @@ -79,6 +82,9 @@ private void initTracerConfig(JaegerConfig jaeger) {
initTracerProperty("JAEGER_PROPAGATION", jaeger.propagation, format -> format.toString());
initTracerProperty("JAEGER_SENDER_FACTORY", jaeger.senderFactory, sender -> sender);
quarkusTracer.setLogTraceContext(jaeger.logTraceContext);
if (jaeger.zipkinCompatibilityMode.isPresent()) {
quarkusTracer.setZipkinCompatibilityMode(jaeger.zipkinCompatibilityMode.get());
}
}

private <T> void initTracerProperty(String property, Optional<T> value, Function<T, String> accessor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
import io.jaegertracing.Configuration;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.spi.MetricsFactory;
import io.jaegertracing.zipkin.ZipkinV2Reporter;
import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.util.ThreadLocalScopeManager;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.urlconnection.URLConnectionSender;

public class QuarkusJaegerTracer implements Tracer {

private volatile JaegerTracer tracer;

private boolean logTraceContext;
private MetricsFactory metricsFactory;
private boolean zipkinCompatibilityMode = false;
private String endpoint = null;

private final ScopeManager scopeManager = new ScopeManager() {

Expand Down Expand Up @@ -71,10 +76,15 @@ private Tracer tracer() {
if (tracer == null) {
synchronized (this) {
if (tracer == null) {
ZipkinV2Reporter reporter = null;
if (zipkinCompatibilityMode) {
reporter = new ZipkinV2Reporter(AsyncReporter.create(URLConnectionSender.create(endpoint)));
}
tracer = Configuration.fromEnv()
.withMetricsFactory(metricsFactory)
.getTracerBuilder()
.withScopeManager(scopeManager)
.withReporter(reporter)
.build();
}
}
Expand Down Expand Up @@ -124,4 +134,12 @@ public Span activeSpan() {
public Scope activateSpan(final Span span) {
return tracer.activateSpan(span);
}

public void setZipkinCompatibilityMode(boolean zipkinCompatibilityMode) {
this.zipkinCompatibilityMode = zipkinCompatibilityMode;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
}

0 comments on commit f612f93

Please sign in to comment.