Skip to content

Commit aef70d2

Browse files
authored
Adds dedicated tests for OC shim (#7488)
1 parent fe56784 commit aef70d2

File tree

3 files changed

+336
-0
lines changed

3 files changed

+336
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
id("otel.javaagent-testing")
3+
}
4+
5+
dependencies {
6+
testLibrary("io.opentelemetry:opentelemetry-opencensus-shim")
7+
testCompileOnly("io.opentelemetry:opentelemetry-api")
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.opencensusshim;
7+
8+
import io.opencensus.trace.AttributeValue;
9+
import io.opencensus.trace.Tracing;
10+
import io.opencensus.trace.samplers.Samplers;
11+
import io.opentelemetry.api.common.AttributeKey;
12+
import io.opentelemetry.api.trace.Span;
13+
import io.opentelemetry.api.trace.SpanKind;
14+
import io.opentelemetry.api.trace.Tracer;
15+
import io.opentelemetry.context.Scope;
16+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
17+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
18+
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
19+
import org.assertj.core.api.AbstractBooleanAssert;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
23+
24+
public class JavaagentInstrumentationTest {
25+
26+
@RegisterExtension
27+
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
28+
29+
@BeforeEach
30+
void setup() {
31+
// by default in opencensus, a probability sampling is used which is not 100%;
32+
// we specifically set this configuration here to always sample to ensure traces are emitted for
33+
// our tests
34+
Tracing.getTraceConfig()
35+
.updateActiveTraceParams(
36+
Tracing.getTraceConfig().getActiveTraceParams().toBuilder()
37+
.setSampler(Samplers.alwaysSample())
38+
.build());
39+
}
40+
41+
@Test
42+
void testInterleavedSpansOcFirst() {
43+
io.opencensus.trace.Tracer ocTracer = Tracing.getTracer();
44+
Tracer otelTracer = testing.getOpenTelemetry().getTracer("test");
45+
46+
io.opencensus.trace.Span outerSpan = ocTracer.spanBuilder("outer-span").startSpan();
47+
Span midSpan;
48+
io.opencensus.trace.Span innerSpan;
49+
50+
outerSpan.putAttribute("outer", AttributeValue.booleanAttributeValue(true));
51+
52+
try (io.opencensus.common.Scope outerScope = ocTracer.withSpan(outerSpan)) {
53+
midSpan =
54+
otelTracer
55+
.spanBuilder("mid-span")
56+
.setSpanKind(SpanKind.INTERNAL)
57+
.setAttribute("middle", true)
58+
.startSpan();
59+
try (Scope midScope = midSpan.makeCurrent()) {
60+
innerSpan = ocTracer.spanBuilder("inner-span").startSpan();
61+
innerSpan.putAttribute("inner", AttributeValue.booleanAttributeValue(true));
62+
63+
// make current and immediately close -- avoid empty try block
64+
ocTracer.withSpan(innerSpan).close();
65+
66+
innerSpan.end();
67+
} finally {
68+
midSpan.end();
69+
}
70+
} finally {
71+
outerSpan.end();
72+
}
73+
74+
Tracing.getExportComponent().shutdown();
75+
76+
// expecting 1 trace with 3 spans
77+
testing.waitAndAssertTraces(
78+
ta ->
79+
// ensure each span's attributes haven't seeped into parents or children
80+
ta.hasSpansSatisfyingExactly(
81+
// outer span
82+
sa ->
83+
sa.hasName("outer-span")
84+
.hasNoParent()
85+
.hasAttribute(AttributeKey.booleanKey("outer"), true)
86+
.hasAttributesSatisfying(
87+
OpenTelemetryAssertions.satisfies(
88+
AttributeKey.booleanKey("inner"), AbstractBooleanAssert::isNull),
89+
OpenTelemetryAssertions.satisfies(
90+
AttributeKey.booleanKey("middle"), AbstractBooleanAssert::isNull)),
91+
// middle span
92+
sa ->
93+
sa.hasName("mid-span")
94+
.hasParent(ta.getSpan(0))
95+
.hasAttribute(AttributeKey.booleanKey("middle"), true)
96+
.hasAttributesSatisfying(
97+
OpenTelemetryAssertions.satisfies(
98+
AttributeKey.booleanKey("inner"), AbstractBooleanAssert::isNull),
99+
OpenTelemetryAssertions.satisfies(
100+
AttributeKey.booleanKey("outer"), AbstractBooleanAssert::isNull)),
101+
// inner span
102+
sa ->
103+
sa.hasName("inner-span")
104+
.hasParent(ta.getSpan(1))
105+
.hasAttribute(AttributeKey.booleanKey("inner"), true)
106+
.hasAttributesSatisfying(
107+
OpenTelemetryAssertions.satisfies(
108+
AttributeKey.booleanKey("middle"), AbstractBooleanAssert::isNull),
109+
OpenTelemetryAssertions.satisfies(
110+
AttributeKey.booleanKey("outer"), AbstractBooleanAssert::isNull))));
111+
}
112+
113+
@Test
114+
void testInterleavedSpansOtelFirst() {
115+
io.opencensus.trace.Tracer ocTracer = Tracing.getTracer();
116+
Tracer otelTracer = testing.getOpenTelemetry().getTracer("test");
117+
118+
Span outerSpan =
119+
otelTracer
120+
.spanBuilder("outer-span")
121+
.setSpanKind(SpanKind.INTERNAL)
122+
.setAttribute("outer", true)
123+
.startSpan();
124+
io.opencensus.trace.Span midSpan;
125+
Span innerSpan;
126+
127+
try (Scope outerScope = outerSpan.makeCurrent()) {
128+
midSpan = ocTracer.spanBuilder("mid-span").startSpan();
129+
midSpan.putAttribute("middle", AttributeValue.booleanAttributeValue(true));
130+
try (io.opencensus.common.Scope midScope = ocTracer.withSpan(midSpan)) {
131+
innerSpan =
132+
otelTracer
133+
.spanBuilder("inner-span")
134+
.setSpanKind(SpanKind.INTERNAL)
135+
.setAttribute("inner", true)
136+
.startSpan();
137+
138+
// make current and immediately close -- avoid empty try block
139+
innerSpan.makeCurrent().close();
140+
141+
innerSpan.end();
142+
} finally {
143+
midSpan.end();
144+
}
145+
} finally {
146+
outerSpan.end();
147+
}
148+
149+
Tracing.getExportComponent().shutdown();
150+
151+
// expecting 1 trace with 3 spans
152+
testing.waitAndAssertTraces(
153+
ta ->
154+
// ensure each span's attributes haven't seeped into parents or children
155+
ta.hasSpansSatisfyingExactly(
156+
// outer span
157+
sa ->
158+
sa.hasName("outer-span")
159+
.hasNoParent()
160+
.hasAttribute(AttributeKey.booleanKey("outer"), true)
161+
.hasAttributesSatisfying(
162+
OpenTelemetryAssertions.satisfies(
163+
AttributeKey.booleanKey("inner"), AbstractBooleanAssert::isNull),
164+
OpenTelemetryAssertions.satisfies(
165+
AttributeKey.booleanKey("middle"), AbstractBooleanAssert::isNull)),
166+
// middle span
167+
sa ->
168+
sa.hasName("mid-span")
169+
.hasParent(ta.getSpan(0))
170+
.hasAttribute(AttributeKey.booleanKey("middle"), true)
171+
.hasAttributesSatisfying(
172+
OpenTelemetryAssertions.satisfies(
173+
AttributeKey.booleanKey("inner"), AbstractBooleanAssert::isNull),
174+
OpenTelemetryAssertions.satisfies(
175+
AttributeKey.booleanKey("outer"), AbstractBooleanAssert::isNull)),
176+
// inner span
177+
sa ->
178+
sa.hasName("inner-span")
179+
.hasParent(ta.getSpan(1))
180+
.hasAttribute(AttributeKey.booleanKey("inner"), true)
181+
.hasAttributesSatisfying(
182+
OpenTelemetryAssertions.satisfies(
183+
AttributeKey.booleanKey("middle"), AbstractBooleanAssert::isNull),
184+
OpenTelemetryAssertions.satisfies(
185+
AttributeKey.booleanKey("outer"), AbstractBooleanAssert::isNull))));
186+
}
187+
188+
@Test
189+
void testStartingWithOtelSpan() {
190+
io.opencensus.trace.Tracer ocTracer = Tracing.getTracer();
191+
Tracer otelTracer = testing.getOpenTelemetry().getTracer("test");
192+
193+
Span otelSpan =
194+
otelTracer
195+
.spanBuilder("otel-span")
196+
.setSpanKind(SpanKind.INTERNAL)
197+
.setAttribute("present-on-otel", true)
198+
.startSpan();
199+
200+
io.opencensus.trace.Span ocSpan;
201+
try (Scope scope = otelSpan.makeCurrent()) {
202+
ocSpan = ocTracer.spanBuilder("oc-span").startSpan();
203+
try (io.opencensus.common.Scope ocScope = ocTracer.withSpan(ocSpan)) {
204+
ocTracer
205+
.getCurrentSpan()
206+
.putAttribute("present-on-oc", AttributeValue.booleanAttributeValue(true));
207+
}
208+
ocSpan.end();
209+
}
210+
otelSpan.end();
211+
212+
Tracing.getExportComponent().shutdown();
213+
214+
testing.waitAndAssertTraces(
215+
trace ->
216+
trace.hasSpansSatisfyingExactly(
217+
span ->
218+
span.hasName("otel-span")
219+
.hasNoParent()
220+
.hasAttribute(AttributeKey.booleanKey("present-on-otel"), true),
221+
span ->
222+
span.hasName("oc-span")
223+
.hasParent(trace.getSpan(0))
224+
.hasAttribute(AttributeKey.booleanKey("present-on-oc"), true)));
225+
}
226+
227+
@Test
228+
void testStartingWithOpenCensusSpan() {
229+
io.opencensus.trace.Tracer ocTracer = Tracing.getTracer();
230+
Tracer otelTracer = testing.getOpenTelemetry().getTracer("test");
231+
232+
io.opencensus.trace.Span ocSpan = ocTracer.spanBuilder("oc-span").startSpan();
233+
234+
ocSpan.putAttribute("present-on-oc", AttributeValue.booleanAttributeValue(true));
235+
236+
Span otelSpan;
237+
try (io.opencensus.common.Scope ocScope = ocTracer.withSpan(ocSpan)) {
238+
otelSpan = otelTracer.spanBuilder("otel-span").setSpanKind(SpanKind.INTERNAL).startSpan();
239+
try (Scope scope = otelSpan.makeCurrent()) {
240+
Span.current().setAttribute("present-on-otel", true);
241+
}
242+
otelSpan.end();
243+
}
244+
ocSpan.end();
245+
246+
Tracing.getExportComponent().shutdown();
247+
248+
testing.waitAndAssertTraces(
249+
trace ->
250+
trace.hasSpansSatisfyingExactly(
251+
span ->
252+
span.hasName("oc-span")
253+
.hasNoParent()
254+
.hasAttribute(AttributeKey.booleanKey("present-on-oc"), true),
255+
span ->
256+
span.hasName("otel-span")
257+
.hasParent(trace.getSpan(0))
258+
.hasAttribute(AttributeKey.booleanKey("present-on-otel"), true)));
259+
}
260+
261+
@Test
262+
void testNestedOpenCensusSpans() {
263+
io.opencensus.trace.Tracer ocTracer = Tracing.getTracer();
264+
265+
io.opencensus.trace.Span outerSpan = ocTracer.spanBuilder("outer-span").startSpan();
266+
io.opencensus.trace.Span midSpan;
267+
io.opencensus.trace.Span innerSpan;
268+
269+
outerSpan.putAttribute("outer", AttributeValue.booleanAttributeValue(true));
270+
271+
try (io.opencensus.common.Scope outerScope = ocTracer.withSpan(outerSpan)) {
272+
midSpan = ocTracer.spanBuilder("mid-span").startSpan();
273+
midSpan.putAttribute("middle", AttributeValue.booleanAttributeValue(true));
274+
try (io.opencensus.common.Scope midScope = ocTracer.withSpan(midSpan)) {
275+
innerSpan = ocTracer.spanBuilder("inner-span").startSpan();
276+
innerSpan.putAttribute("inner", AttributeValue.booleanAttributeValue(true));
277+
278+
// make current and immediately close -- avoid empty try block
279+
ocTracer.withSpan(innerSpan).close();
280+
281+
innerSpan.end();
282+
} finally {
283+
midSpan.end();
284+
}
285+
} finally {
286+
outerSpan.end();
287+
}
288+
289+
Tracing.getExportComponent().shutdown();
290+
291+
// expecting 1 trace with 3 spans
292+
testing.waitAndAssertTraces(
293+
ta ->
294+
// ensure each span's attributes haven't seeped into parents or children
295+
ta.hasSpansSatisfyingExactly(
296+
// outer span
297+
sa ->
298+
sa.hasName("outer-span")
299+
.hasNoParent()
300+
.hasAttribute(AttributeKey.booleanKey("outer"), true)
301+
.hasAttributesSatisfying(
302+
OpenTelemetryAssertions.satisfies(
303+
AttributeKey.booleanKey("inner"), AbstractBooleanAssert::isNull),
304+
OpenTelemetryAssertions.satisfies(
305+
AttributeKey.booleanKey("middle"), AbstractBooleanAssert::isNull)),
306+
// middle span
307+
sa ->
308+
sa.hasName("mid-span")
309+
.hasParent(ta.getSpan(0))
310+
.hasAttribute(AttributeKey.booleanKey("middle"), true)
311+
.hasAttributesSatisfying(
312+
OpenTelemetryAssertions.satisfies(
313+
AttributeKey.booleanKey("inner"), AbstractBooleanAssert::isNull),
314+
OpenTelemetryAssertions.satisfies(
315+
AttributeKey.booleanKey("outer"), AbstractBooleanAssert::isNull)),
316+
// inner span
317+
sa ->
318+
sa.hasName("inner-span")
319+
.hasParent(ta.getSpan(1))
320+
.hasAttribute(AttributeKey.booleanKey("inner"), true)
321+
.hasAttributesSatisfying(
322+
OpenTelemetryAssertions.satisfies(
323+
AttributeKey.booleanKey("middle"), AbstractBooleanAssert::isNull),
324+
OpenTelemetryAssertions.satisfies(
325+
AttributeKey.booleanKey("outer"), AbstractBooleanAssert::isNull))));
326+
}
327+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ hideFromDependabot(":instrumentation:okhttp:okhttp-2.2:javaagent")
378378
hideFromDependabot(":instrumentation:okhttp:okhttp-3.0:javaagent")
379379
hideFromDependabot(":instrumentation:okhttp:okhttp-3.0:library")
380380
hideFromDependabot(":instrumentation:okhttp:okhttp-3.0:testing")
381+
hideFromDependabot(":instrumentation:opencensus-shim:testing")
381382
hideFromDependabot(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")
382383
hideFromDependabot(":instrumentation:opentelemetry-api:opentelemetry-api-1.4:javaagent")
383384
hideFromDependabot(":instrumentation:opentelemetry-api:opentelemetry-api-1.10:javaagent")

0 commit comments

Comments
 (0)