-
Notifications
You must be signed in to change notification settings - Fork 829
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
IrisTuntun
wants to merge
3
commits into
open-telemetry:main
from
IrisTuntun:simplifyOpenTracingShim
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/OpenTracingShimBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our pattern for this is to have this method live in the |
||
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"); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...acing-shim/src/test/java/io/opentelemetry/opentracingshim/OpenTracingShimBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. :)