Skip to content

Commit 2ddc3c2

Browse files
committed
feat: add otel integration tests
Signed-off-by: chenhuan <xiangyuyu_2024@qq.com>
1 parent eac4fd3 commit 2ddc3c2

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

.github/workflows/java_unit_tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ jobs:
3535
uses: hezhangjian/setup-opengemini-action@main
3636
- name: unit tests
3737
run: mvn -B clean test
38+
- name: setup ts-trace
39+
run: go install github.com/openGemini/observability/trace/cmd/ts-trace@latest && ts-trace &
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright 2025 openGemini 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.opengemini.client.interceptor;
18+
19+
import io.github.openfacade.http.HttpClientConfig;
20+
import io.opengemini.client.api.Address;
21+
import io.opengemini.client.api.Configuration;
22+
import io.opengemini.client.api.Query;
23+
import io.opengemini.client.api.QueryResult;
24+
import io.opengemini.client.api.Write;
25+
import io.opengemini.client.impl.OpenGeminiClient;
26+
import io.opentelemetry.api.OpenTelemetry;
27+
import io.opentelemetry.api.common.Attributes;
28+
import io.opentelemetry.api.trace.Tracer;
29+
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
30+
import io.opentelemetry.sdk.OpenTelemetrySdk;
31+
import io.opentelemetry.sdk.resources.Resource;
32+
import io.opentelemetry.sdk.trace.SdkTracerProvider;
33+
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
34+
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
35+
import org.junit.jupiter.api.AfterEach;
36+
import org.junit.jupiter.api.Assertions;
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Test;
39+
40+
import java.time.Duration;
41+
import java.util.Collections;
42+
import java.util.concurrent.CompletableFuture;
43+
import java.util.concurrent.ExecutionException;
44+
import java.util.concurrent.TimeUnit;
45+
46+
/**
47+
* Example demonstrating OpenGemini client usage with interceptors.
48+
*/
49+
50+
public class TracingIntegrationTest {
51+
52+
private OpenGeminiClient openGeminiClient;
53+
54+
@BeforeEach
55+
void setUp() {
56+
HttpClientConfig httpConfig = new HttpClientConfig.Builder()
57+
.connectTimeout(Duration.ofSeconds(3))
58+
.timeout(Duration.ofSeconds(3))
59+
.build();
60+
Configuration configuration = Configuration.builder()
61+
.addresses(Collections.singletonList(new Address("127.0.0.1", 8086)))
62+
.httpConfig(httpConfig)
63+
.gzipEnabled(false)
64+
.build();
65+
this.openGeminiClient = new OpenGeminiClient(configuration);
66+
67+
OtelInterceptor otelInterceptor = new OtelInterceptor();
68+
69+
otelInterceptor.setTracer(getTestTracer());
70+
openGeminiClient.addInterceptors(otelInterceptor);
71+
}
72+
73+
@AfterEach
74+
void setDown() throws InterruptedException {
75+
// for the last reporting record, otherwise the test case will not be detected
76+
Thread.sleep(500);
77+
}
78+
79+
private Tracer getTestTracer() {
80+
OpenTelemetry openTelemetry;
81+
try {
82+
JaegerGrpcSpanExporter jaegerExporter = JaegerGrpcSpanExporter.builder()
83+
.setEndpoint("http://localhost:14250")
84+
.build();
85+
86+
BatchSpanProcessor spanProcessor = BatchSpanProcessor.builder(jaegerExporter)
87+
.setScheduleDelay(100, java.util.concurrent.TimeUnit.MILLISECONDS)
88+
.build();
89+
90+
SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
91+
.addSpanProcessor(spanProcessor)
92+
.setResource(Resource.create(
93+
Attributes.of(ResourceAttributes.SERVICE_NAME, "opengemini-client-java")
94+
))
95+
.build();
96+
97+
openTelemetry = OpenTelemetrySdk.builder()
98+
.setTracerProvider(tracerProvider)
99+
.build();
100+
101+
return openTelemetry.getTracer("opengemini-client-java");
102+
} catch (Exception e) {
103+
// Fallback to no-op implementation
104+
openTelemetry = OpenTelemetry.noop();
105+
return openTelemetry.getTracer("opengemini-client-java");
106+
}
107+
108+
}
109+
110+
@Test
111+
void testDatabaseCreation() {
112+
Assertions.assertDoesNotThrow(() -> {
113+
Query createDbQuery = new Query("CREATE DATABASE test_db");
114+
openGeminiClient.query(createDbQuery).get(10, TimeUnit.SECONDS);
115+
}, "Database creation should not throw an exception");
116+
}
117+
118+
@Test
119+
void testQueryOperation() {
120+
Configuration config = new Configuration();
121+
config.setAddresses(java.util.Collections.singletonList(new Address("localhost", 8086)));
122+
if (config.getHttpConfig() == null) {
123+
config.setHttpConfig(new HttpClientConfig.Builder().build());
124+
}
125+
126+
Assertions.assertDoesNotThrow(() -> {
127+
Query createDbQuery = new Query("CREATE DATABASE test_db");
128+
openGeminiClient.query(createDbQuery).get(10, TimeUnit.SECONDS);
129+
130+
Query showDbQuery = new Query("SHOW DATABASES");
131+
QueryResult result = openGeminiClient.query(showDbQuery).get(10, TimeUnit.SECONDS);
132+
Assertions.assertNotNull(result, "Query result should not be null");
133+
}, "Query operation should not throw an exception");
134+
}
135+
136+
@Test
137+
void testWriteOperation() throws InterruptedException {
138+
Configuration config = new Configuration();
139+
config.setAddresses(java.util.Collections.singletonList(
140+
new Address("localhost", 8086)));
141+
142+
if (config.getHttpConfig() == null) {
143+
config.setHttpConfig(new HttpClientConfig.Builder().build());
144+
}
145+
146+
Assertions.assertDoesNotThrow(() -> {
147+
Query createDbQuery = new Query("CREATE DATABASE test_db");
148+
openGeminiClient.query(createDbQuery).get(10, TimeUnit.SECONDS);
149+
150+
Write write = new Write(
151+
"test_db",
152+
"autogen",
153+
"temperature,location=room1 value=25.5 " + System.currentTimeMillis(),
154+
"ns"
155+
);
156+
157+
openGeminiClient.executeWrite(
158+
write.getDatabase(),
159+
write.getRetentionPolicy(),
160+
write.getLineProtocol()
161+
).get(10, TimeUnit.SECONDS);
162+
163+
}, "Write operation should not throw an exception");
164+
}
165+
166+
@Test
167+
void testTracingIntegration() throws ExecutionException, InterruptedException {
168+
String databaseTestName = "tracing_test_db";
169+
CompletableFuture<Void> createdb = openGeminiClient.createDatabase(databaseTestName);
170+
createdb.get();
171+
172+
Assertions.assertDoesNotThrow(() -> {
173+
174+
Write write = new Write(
175+
"tracing_test_db",
176+
"autogen",
177+
"tracing_measurement,tag=test value=8 " + System.currentTimeMillis(),
178+
"ns"
179+
);
180+
181+
openGeminiClient.executeWrite(
182+
write.getDatabase(),
183+
write.getRetentionPolicy(),
184+
write.getLineProtocol()
185+
).get(10, TimeUnit.SECONDS);
186+
187+
Query query = new Query("SELECT * FROM tracing_measurement");
188+
openGeminiClient.query(query).get(10, TimeUnit.SECONDS);
189+
190+
}, "Tracing integration should not throw an exception");
191+
}
192+
}

0 commit comments

Comments
 (0)