Skip to content

Commit 26d4dbf

Browse files
DotSpythisthatvladislav-kiva
authored
test: end to end opentelemetry test with jaeger exporter (#1038)
* test: end to end opentelemetry test with jaeger exporter * fix: add license header * build: circle ci use machine executor type https://www.testcontainers.org/supported_docker_environment/continuous_integration/circle_ci/ * Update exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/JaegerIntegrationTest.java Co-Authored-By: Giovanni Liva <[email protected]> * refactor: apply review suggestions * style: apply google code style * docs: add comment explaining why executor type machine * fix: remove setup as it run via @before Co-authored-by: Giovanni Liva <[email protected]> Co-authored-by: vladislav-kiva <[email protected]>
1 parent 0111714 commit 26d4dbf

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ publish_release_task: &publish_release_task
1919
name: Publish Release Artifacts
2020
command: make publish-release-artifacts
2121

22+
#for test containers https://www.testcontainers.org/supported_docker_environment/continuous_integration/circle_ci/
23+
executorType: machine
24+
2225
jobs:
2326
build:
2427
environment:

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ subprojects {
125125
truth : 'com.google.truth:truth:1.0.1',
126126
slf4jsimple : 'org.slf4j:slf4j-simple:1.7.25', // Compatibility layer
127127
awaitility : 'org.awaitility:awaitility:3.0.0', // Compatibility layer
128+
testcontainers : 'org.testcontainers:testcontainers:1.13.0',
129+
rest_assured : 'io.rest-assured:rest-assured:4.2.0',
128130
]
129131
}
130132

exporters/jaeger/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ dependencies {
2020
libraries.protobuf,
2121
libraries.protobuf_util
2222

23-
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
23+
testImplementation "io.grpc:grpc-testing:${grpcVersion}",
24+
libraries.testcontainers,
25+
libraries.awaitility,
26+
libraries.rest_assured
27+
2428
testRuntime "io.grpc:grpc-netty-shaded:${grpcVersion}"
2529

2630
signature "org.codehaus.mojo.signature:java17:1.0@signature"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.exporters.jaeger;
18+
19+
import static io.restassured.RestAssured.given;
20+
21+
import io.grpc.ManagedChannel;
22+
import io.grpc.ManagedChannelBuilder;
23+
import io.opentelemetry.OpenTelemetry;
24+
import io.opentelemetry.sdk.OpenTelemetrySdk;
25+
import io.opentelemetry.sdk.trace.export.SimpleSpansProcessor;
26+
import io.opentelemetry.trace.Span;
27+
import io.opentelemetry.trace.Tracer;
28+
import io.restassured.http.ContentType;
29+
import io.restassured.response.Response;
30+
import java.util.Map;
31+
import java.util.concurrent.Callable;
32+
import java.util.concurrent.TimeUnit;
33+
import org.awaitility.Awaitility;
34+
import org.junit.Before;
35+
import org.junit.ClassRule;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.junit.runners.JUnit4;
39+
import org.testcontainers.containers.GenericContainer;
40+
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
41+
42+
@RunWith(JUnit4.class)
43+
public class JaegerIntegrationTest {
44+
45+
private static final int QUERY_PORT = 16686;
46+
private static final int COLLECTOR_PORT = 14250;
47+
private static final String JAEGER_VERSION = "1.17";
48+
private static final String SERVICE_NAME = "E2E-test";
49+
private static final String JAEGER_URL = "http://localhost";
50+
private final Tracer tracer =
51+
OpenTelemetry.getTracerProvider().get(getClass().getCanonicalName());
52+
private JaegerGrpcSpanExporter jaegerExporter;
53+
54+
@SuppressWarnings("rawtypes")
55+
@ClassRule
56+
public static GenericContainer jaeger =
57+
new GenericContainer("jaegertracing/all-in-one:" + JAEGER_VERSION)
58+
.withExposedPorts(COLLECTOR_PORT, QUERY_PORT)
59+
.waitingFor(new HttpWaitStrategy().forPath("/"));
60+
61+
@Before
62+
public void setupJaegerExporter() {
63+
ManagedChannel jaegerChannel =
64+
ManagedChannelBuilder.forAddress("127.0.0.1", jaeger.getMappedPort(COLLECTOR_PORT))
65+
.usePlaintext()
66+
.build();
67+
this.jaegerExporter =
68+
JaegerGrpcSpanExporter.newBuilder()
69+
.setServiceName(SERVICE_NAME)
70+
.setChannel(jaegerChannel)
71+
.setDeadlineMs(30000)
72+
.build();
73+
OpenTelemetrySdk.getTracerProvider()
74+
.addSpanProcessor(SimpleSpansProcessor.newBuilder(this.jaegerExporter).build());
75+
}
76+
77+
@Test
78+
public void testJaegerIntegration() {
79+
imitateWork();
80+
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(assertJaegerHaveTrace());
81+
}
82+
83+
private void imitateWork() {
84+
Span span = this.tracer.spanBuilder("Test span").startSpan();
85+
span.addEvent("some event");
86+
try {
87+
Thread.sleep(1000);
88+
} catch (InterruptedException e) {
89+
throw new RuntimeException(e);
90+
}
91+
span.end();
92+
}
93+
94+
private static Callable<Boolean> assertJaegerHaveTrace() {
95+
return new Callable<Boolean>() {
96+
@Override
97+
public Boolean call() {
98+
try {
99+
String url =
100+
String.format(
101+
"%s/api/traces?service=%s",
102+
String.format(JAEGER_URL + ":%d", jaeger.getMappedPort(QUERY_PORT)),
103+
SERVICE_NAME);
104+
Response response =
105+
given()
106+
.headers("Content-Type", ContentType.JSON, "Accept", ContentType.JSON)
107+
.when()
108+
.get(url)
109+
.then()
110+
.contentType(ContentType.JSON)
111+
.extract()
112+
.response();
113+
Map<String, String> path = response.jsonPath().getMap("data[0]");
114+
return path.get("traceID") != null;
115+
} catch (Exception e) {
116+
return false;
117+
}
118+
}
119+
};
120+
}
121+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<!-- encoders are assigned the type
5+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6+
<encoder>
7+
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="STDOUT"/>
13+
</root>
14+
</configuration>

0 commit comments

Comments
 (0)