Skip to content

Commit b3738cb

Browse files
authored
Merge pull request quarkusio#35694 from ahreurink/opentelemetry-id-generator-test
Add test for integration of a custom IdGenerator Opentelemetry
2 parents 010a306 + 1dc3cbf commit b3738cb

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed
Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package io.quarkus.opentelemetry.deployment;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
35
import java.lang.reflect.InvocationTargetException;
6+
import java.util.concurrent.atomic.AtomicLong;
47

8+
import jakarta.enterprise.context.ApplicationScoped;
9+
import jakarta.enterprise.inject.Produces;
510
import jakarta.inject.Inject;
611

7-
import org.junit.jupiter.api.Disabled;
12+
import org.junit.jupiter.api.MethodOrderer;
13+
import org.junit.jupiter.api.Order;
814
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.TestMethodOrder;
916
import org.junit.jupiter.api.extension.RegisterExtension;
1017

1118
import io.opentelemetry.api.OpenTelemetry;
12-
// import io.opentelemetry.contrib.awsxray.AwsXrayIdGenerator;
19+
import io.opentelemetry.api.trace.Span;
20+
import io.opentelemetry.api.trace.Tracer;
1321
import io.opentelemetry.sdk.trace.IdGenerator;
1422
import io.quarkus.opentelemetry.deployment.common.TestUtil;
1523
import io.quarkus.test.QuarkusUnitTest;
1624

17-
@Disabled("We need to move the AWS dependency testing to an independent module")
25+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1826
public class OpenTelemetryIdGeneratorTest {
1927
@RegisterExtension
2028
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
@@ -24,18 +32,51 @@ public class OpenTelemetryIdGeneratorTest {
2432
OpenTelemetry openTelemetry;
2533

2634
@Test
27-
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
35+
@Order(1)
36+
void testGenerateIds()
37+
throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
2838
IdGenerator idGenerator = TestUtil.getIdGenerator(openTelemetry);
39+
String spanId = idGenerator.generateSpanId();
40+
String traceId = idGenerator.generateTraceId();
41+
42+
assertEquals(String.format("%016d", OtelConfiguration.SPAN_START_NUMBER + 1), spanId);
43+
assertEquals(String.format("%032d", OtelConfiguration.TRACE_START_NUMBER + 1), traceId);
44+
}
45+
46+
@Test
47+
@Order(2)
48+
void testGenerateSpan() {
49+
Tracer testTracer = openTelemetry.getTracer("io.quarkus.opentelemetry.deployment");
50+
Span testSpan = testTracer.spanBuilder("testSpan").startSpan();
51+
testSpan.end();
2952

30-
// assertThat(idGenerator, instanceOf(AwsXrayIdGenerator.class));
53+
String generatedSpanId = testSpan.getSpanContext().getSpanId();
54+
String generatedTraceId = testSpan.getSpanContext().getTraceId();
55+
assertEquals(String.format("%016d", OtelConfiguration.SPAN_START_NUMBER + 2), generatedSpanId);
56+
assertEquals(String.format("%032d", OtelConfiguration.TRACE_START_NUMBER + 2), generatedTraceId);
3157
}
3258

33-
// @ApplicationScoped
34-
// public static class OtelConfiguration {
35-
//
36-
// @Produces
37-
// public IdGenerator idGenerator() {
38-
// return AwsXrayIdGenerator.getInstance();
39-
// }
40-
// }
59+
@ApplicationScoped
60+
public static class OtelConfiguration {
61+
static final long TRACE_START_NUMBER = 42;
62+
static final long SPAN_START_NUMBER = 42;
63+
64+
@Produces
65+
public IdGenerator idGenerator() {
66+
return new IdGenerator() {
67+
final AtomicLong traceId = new AtomicLong(OtelConfiguration.TRACE_START_NUMBER);
68+
final AtomicLong spanId = new AtomicLong(OtelConfiguration.SPAN_START_NUMBER);
69+
70+
@Override
71+
public String generateSpanId() {
72+
return String.format("%016d", spanId.incrementAndGet());
73+
}
74+
75+
@Override
76+
public String generateTraceId() {
77+
return String.format("%032d", traceId.incrementAndGet());
78+
}
79+
};
80+
}
81+
}
4182
}

0 commit comments

Comments
 (0)