From e8cb4a981af09629b9d98496a0f355ecb985a912 Mon Sep 17 00:00:00 2001 From: Dan Markwat Date: Thu, 29 Dec 2022 19:50:25 -0800 Subject: [PATCH] Adds dedicated tests for OC shim Per suggestion: https://github.com/open-telemetry/opentelemetry-java/pull/4900#pullrequestreview-1226461666 --- .../opencensus-shim/testing/build.gradle.kts | 8 +++ .../testing/src/test/java/Test.java | 55 +++++++++++++++++++ settings.gradle.kts | 1 + 3 files changed, 64 insertions(+) create mode 100644 instrumentation/opencensus-shim/testing/build.gradle.kts create mode 100644 instrumentation/opencensus-shim/testing/src/test/java/Test.java diff --git a/instrumentation/opencensus-shim/testing/build.gradle.kts b/instrumentation/opencensus-shim/testing/build.gradle.kts new file mode 100644 index 000000000000..d23366c90aa7 --- /dev/null +++ b/instrumentation/opencensus-shim/testing/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + id("otel.javaagent-testing") +} + +dependencies { + testLibrary("io.opentelemetry:opentelemetry-opencensus-shim") + testCompileOnly("io.opentelemetry:opentelemetry-api") +} diff --git a/instrumentation/opencensus-shim/testing/src/test/java/Test.java b/instrumentation/opencensus-shim/testing/src/test/java/Test.java new file mode 100644 index 000000000000..20092c6f8686 --- /dev/null +++ b/instrumentation/opencensus-shim/testing/src/test/java/Test.java @@ -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)))); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index ee126c69c843..f3eab76ef128 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -378,6 +378,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")