Skip to content

Commit

Permalink
Adds dedicated tests for OC shim
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarkwat committed Dec 30, 2022
1 parent 983b9dd commit 3119a9b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions instrumentation/opencensus-shim/testing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("otel.javaagent-testing")
}

dependencies {
testLibrary("io.opentelemetry:opentelemetry-opencensus-shim")
testCompileOnly("io.opentelemetry:opentelemetry-api")
}
55 changes: 55 additions & 0 deletions instrumentation/opencensus-shim/testing/src/test/java/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;

import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Tracing;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;


public class Test {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@org.junit.jupiter.api.Test
void test() {
Tracer tracer = testing.getOpenTelemetry().getTracer("test");
Span span = tracer.spanBuilder("otel-span").setSpanKind(SpanKind.INTERNAL).startSpan();
Scope scope = span.makeCurrent();
try {
io.opencensus.trace.Tracer ocTracer = Tracing.getTracer();
io.opencensus.trace.Span internal = ocTracer.spanBuilder("oc-span").startSpan();
io.opencensus.common.Scope ocScope = ocTracer.withSpan(internal);
try {
ocTracer
.getCurrentSpan()
.putAttribute("present-on-oc", AttributeValue.booleanAttributeValue(true));
} finally {
ocScope.close();
}
internal.end();
} finally {
scope.close();
}
span.end();

// will pass when the opencensus shim is correctly proxying calls to the ApplicationSpanImpl instance emitted by the javaagent-configured GlobalOpenTelemetry Tracer
testing
.waitAndAssertTraces(
traceAssert ->
traceAssert.hasSpansSatisfyingExactly(
spanAssert -> spanAssert.hasName("otel-span").hasNoParent(),
spanAssert ->
spanAssert
.hasName("oc-span")
.hasParentSpanId(span.getSpanContext().getSpanId())
.hasAttributesSatisfyingExactly(
equalTo(AttributeKey.booleanKey("present-on-oc"), true))));
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ hideFromDependabot(":instrumentation:okhttp:okhttp-2.2:javaagent")
hideFromDependabot(":instrumentation:okhttp:okhttp-3.0:javaagent")
hideFromDependabot(":instrumentation:okhttp:okhttp-3.0:library")
hideFromDependabot(":instrumentation:okhttp:okhttp-3.0:testing")
hideFromDependabot(":instrumentation:opencensus-shim:testing")
hideFromDependabot(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")
hideFromDependabot(":instrumentation:opentelemetry-api:opentelemetry-api-1.4:javaagent")
hideFromDependabot(":instrumentation:opentelemetry-api:opentelemetry-api-1.10:javaagent")
Expand Down

0 comments on commit 3119a9b

Please sign in to comment.