Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenTracingShimBuilder class and test #3129

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ more details.
- The shim now supports methods that take a timestamp as a parameter.
- You can now specify both the `TEXT_MAP` and the `HTTP_HEADER` type propagators for the shim.
See `io.opentelemetry.opentracingshim.OpenTracingPropagators` for details.
- Add `OpenTracingShimBuilder` class to create the shim.

### Extensions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import io.opentelemetry.api.trace.TracerProvider;

/**
* This class is deprecated. Please Use {@link OpenTracingShimBuilder} instead to create an
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I wonder if it would be good to keep this class, just with the builder() method on it, to keep the patterns the same as they are in the rest of the code. It does make this class a bit anemic, but it was already that way. :)

* OpenTracing {@link io.opentracing.Tracer} instance.
*
* Factory for creating an OpenTracing {@link io.opentracing.Tracer} that is implemented using the
* OpenTelemetry APIs.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.opentracingshim;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.TracerProvider;
import java.util.Objects;

/**
* Builder for creating an OpenTracing {@link io.opentracing.Tracer} that is implemented using the
* OpenTelemetry APIs.
*/
public class OpenTracingShimBuilder{
private Tracer tracer = getTracer(GlobalOpenTelemetry.getTracerProvider());
private OpenTracingPropagators propagators = OpenTracingPropagators.builder().build();

/** Returns a new {@code OpenTracingShimBuilder} instance. */
public static OpenTracingShimBuilder builder(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our pattern for this is to have this method live in the OpenTracingShim class, rather than here.

return new OpenTracingShimBuilder();
}

/** Set a {@code Tracer} instance to create this shim. */
public OpenTracingShimBuilder setTracer(Tracer tracer){
Objects.requireNonNull(tracer, "tracer");
this.tracer = tracer;
return this;
}

/** Set a {@code OpenTracingPropagators} instance to create this shim. */
public OpenTracingShimBuilder setPropagators(OpenTracingPropagators propagators){
Objects.requireNonNull(propagators, "propagators");
this.propagators = propagators;
return this;
}

/**
* Set a {@code OpenTelemetry} instance to create this shim.
* The {@code Tracer} and {@code OpenTracingPropagators} instances required to
* create the shim will be obtained from the {@code OpenTelemetry}.
*/
public OpenTracingShimBuilder setOpentelemetry(OpenTelemetry openTelemetry){
setTracer(getTracer(openTelemetry.getTracerProvider()));
setPropagators(OpenTracingPropagators.builder()
.setTextMap(openTelemetry.getPropagators().getTextMapPropagator())
.setHttpHeaders(openTelemetry.getPropagators().getTextMapPropagator())
.build());
return this;
}

/**
* Constructs a new instance of OpenTracing {@link io.opentracing.Tracer} based on the
* builder's values.
*
* @return a {@code io.opentracing.Tracer}.
*/
public io.opentracing.Tracer build(){
return new TracerShim(new TelemetryInfo(tracer, propagators));
}

private static Tracer getTracer(TracerProvider tracerProvider) {
return tracerProvider.get("opentracingshim");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.opentracingshim;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

class OpenTracingShimBuilderTest {

@AfterEach
void tearDown() {
GlobalOpenTelemetry.resetForTest();
}

@Test
void openTracingShimBuilder_default() {
TracerShim tracerShim = (TracerShim) OpenTracingShimBuilder.builder().build();
assertThat(tracerShim.tracer()).isEqualTo(GlobalOpenTelemetry.getTracer("opentracingshim"));
}

@Test
void openTracingShimBuilder_fromOpenTelemetryInstance() {
OpenTelemetry openTelemetry = mock(OpenTelemetry.class);
SdkTracerProvider sdk = SdkTracerProvider.builder().build();
when(openTelemetry.getTracerProvider()).thenReturn(sdk);
ContextPropagators contextPropagators = mock(ContextPropagators.class);
when(contextPropagators.getTextMapPropagator()).thenReturn(mock(TextMapPropagator.class));
when(openTelemetry.getPropagators()).thenReturn(contextPropagators);

TracerShim tracerShim = (TracerShim) OpenTracingShimBuilder.builder().setOpentelemetry(openTelemetry).build();
assertThat(tracerShim.tracer()).isEqualTo(sdk.get("opentracingshim"));
}

@Test
void openTracingShimBuilder_withPropagators() {
Tracer tracer = mock(Tracer.class);

TextMapPropagator textMapPropagator = new CustomTextMapPropagator();
TextMapPropagator httpHeadersPropagator = new CustomTextMapPropagator();

TracerShim tracerShim =
(TracerShim)
OpenTracingShimBuilder
.builder()
.setTracer(tracer)
.setPropagators(
OpenTracingPropagators
.builder()
.setTextMap(textMapPropagator)
.setHttpHeaders(httpHeadersPropagator)
.build())
.build();

assertThat(tracerShim.propagators().textMapPropagator()).isSameAs(textMapPropagator);
assertThat(tracerShim.propagators().httpHeadersPropagator()).isSameAs(httpHeadersPropagator);
}

}