Skip to content

Commit 184a1d7

Browse files
committed
feat: switch the otel server for unit test integration
Signed-off-by: chenhuan <xiangyuyu_2024@qq.com>
1 parent 450fce8 commit 184a1d7

File tree

4 files changed

+178
-19
lines changed

4 files changed

+178
-19
lines changed

.github/workflows/java_unit_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
distribution: 'temurin'
3434
- name: setup OpenGemini
3535
uses: hezhangjian/setup-opengemini-action@main
36-
- name: unit tests
37-
run: mvn -B clean test
3836
- name: setup ts-trace
3937
run: go install github.com/openGemini/observability/trace/cmd/ts-trace@latest && ts-trace &
38+
- name: unit tests
39+
run: mvn -B clean test
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.Write;
24+
import io.opengemini.client.impl.OpenGeminiClient;
25+
import io.opentelemetry.api.OpenTelemetry;
26+
import io.opentelemetry.api.common.Attributes;
27+
import io.opentelemetry.api.trace.Tracer;
28+
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
29+
import io.opentelemetry.sdk.OpenTelemetrySdk;
30+
import io.opentelemetry.sdk.resources.Resource;
31+
import io.opentelemetry.sdk.trace.SdkTracerProvider;
32+
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
33+
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.BeforeEach;
36+
import org.junit.jupiter.api.Test;
37+
38+
import java.time.Duration;
39+
import java.util.Collections;
40+
import java.util.concurrent.CompletableFuture;
41+
import java.util.concurrent.ExecutionException;
42+
import java.util.concurrent.TimeUnit;
43+
44+
/**
45+
* Example demonstrating OpenGemini client usage with interceptors.
46+
*/
47+
48+
public class TraceFailureToleranceTest {
49+
50+
private OpenGeminiClient openGeminiClient;
51+
52+
@BeforeEach
53+
void setUp() {
54+
HttpClientConfig httpConfig = new HttpClientConfig.Builder()
55+
.connectTimeout(Duration.ofSeconds(3))
56+
.timeout(Duration.ofSeconds(3))
57+
.build();
58+
Configuration configuration = Configuration.builder()
59+
.addresses(Collections.singletonList(new Address("127.0.0.1", 8086)))
60+
.httpConfig(httpConfig)
61+
.gzipEnabled(false)
62+
.build();
63+
this.openGeminiClient = new OpenGeminiClient(configuration);
64+
65+
OtelInterceptor otelInterceptor = new OtelInterceptor();
66+
67+
otelInterceptor.setTracer(getErrTracer());
68+
openGeminiClient.addInterceptors(otelInterceptor);
69+
}
70+
71+
private Tracer getErrTracer() {
72+
OpenTelemetry openTelemetry;
73+
OtlpGrpcSpanExporter otlpGrpcSpanExporter = OtlpGrpcSpanExporter.builder()
74+
.setEndpoint("http://127.0.0.1:38086") // error endpoiont to test the failure tolerance
75+
.build();
76+
77+
BatchSpanProcessor spanProcessor = BatchSpanProcessor.builder(otlpGrpcSpanExporter)
78+
.setScheduleDelay(100, TimeUnit.MILLISECONDS)
79+
.build();
80+
81+
SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
82+
.addSpanProcessor(spanProcessor)
83+
.setResource(Resource.create(
84+
Attributes.of(ResourceAttributes.SERVICE_NAME, "opengemini-client-java")
85+
))
86+
.build();
87+
88+
openTelemetry = OpenTelemetrySdk.builder()
89+
.setTracerProvider(tracerProvider)
90+
.build();
91+
92+
return openTelemetry.getTracer("opengemini-client-java");
93+
}
94+
95+
@Test
96+
void testTracingIntegration() throws ExecutionException, InterruptedException {
97+
String databaseTestName = "tracing_test_db";
98+
CompletableFuture<Void> createdb = openGeminiClient.createDatabase(databaseTestName);
99+
createdb.get();
100+
101+
Assertions.assertDoesNotThrow(() -> {
102+
Write write = new Write(
103+
"tracing_test_db",
104+
"autogen",
105+
"tracing_measurement,tag=test value=8 " + System.currentTimeMillis(),
106+
"ns"
107+
);
108+
109+
openGeminiClient.executeWrite(
110+
write.getDatabase(),
111+
write.getRetentionPolicy(),
112+
write.getLineProtocol()
113+
).get(10, TimeUnit.SECONDS);
114+
115+
Query query = new Query("SELECT * FROM tracing_measurement");
116+
openGeminiClient.query(query).get(10, TimeUnit.SECONDS);
117+
118+
}, "Tracing integration should not throw an exception");
119+
}
120+
}

opengemini-client/src/test/java/io/opengemini/client/interceptor/TracingIntegrationTest.java

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import io.opentelemetry.api.OpenTelemetry;
2727
import io.opentelemetry.api.common.Attributes;
2828
import io.opentelemetry.api.trace.Tracer;
29-
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
29+
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
3030
import io.opentelemetry.sdk.OpenTelemetrySdk;
3131
import io.opentelemetry.sdk.resources.Resource;
3232
import io.opentelemetry.sdk.trace.SdkTracerProvider;
@@ -39,6 +39,7 @@
3939

4040
import java.time.Duration;
4141
import java.util.Collections;
42+
import java.util.List;
4243
import java.util.concurrent.CompletableFuture;
4344
import java.util.concurrent.ExecutionException;
4445
import java.util.concurrent.TimeUnit;
@@ -51,8 +52,14 @@ public class TracingIntegrationTest {
5152

5253
private OpenGeminiClient openGeminiClient;
5354

55+
private String databaseName = "jaeger_storage";
56+
57+
private String rpName = "trace";
58+
59+
private String measurementName = "opengemini-client-java";
60+
5461
@BeforeEach
55-
void setUp() {
62+
void setUp() throws ExecutionException, InterruptedException {
5663
HttpClientConfig httpConfig = new HttpClientConfig.Builder()
5764
.connectTimeout(Duration.ofSeconds(3))
5865
.timeout(Duration.ofSeconds(3))
@@ -68,6 +75,8 @@ void setUp() {
6875

6976
otelInterceptor.setTracer(getTestTracer());
7077
openGeminiClient.addInterceptors(otelInterceptor);
78+
79+
cleanTrace();
7180
}
7281

7382
@AfterEach
@@ -79,12 +88,13 @@ void setDown() throws InterruptedException {
7988
private Tracer getTestTracer() {
8089
OpenTelemetry openTelemetry;
8190
try {
82-
JaegerGrpcSpanExporter jaegerExporter = JaegerGrpcSpanExporter.builder()
83-
.setEndpoint("http://localhost:14250")
91+
OtlpGrpcSpanExporter otlpGrpcSpanExporter = OtlpGrpcSpanExporter.builder()
92+
.setEndpoint("http://127.0.0.1:18086")
93+
.addHeader("Authentication", "")
8494
.build();
8595

86-
BatchSpanProcessor spanProcessor = BatchSpanProcessor.builder(jaegerExporter)
87-
.setScheduleDelay(100, java.util.concurrent.TimeUnit.MILLISECONDS)
96+
BatchSpanProcessor spanProcessor = BatchSpanProcessor.builder(otlpGrpcSpanExporter)
97+
.setScheduleDelay(100, TimeUnit.MILLISECONDS)
8898
.build();
8999

90100
SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
@@ -100,57 +110,79 @@ private Tracer getTestTracer() {
100110

101111
return openTelemetry.getTracer("opengemini-client-java");
102112
} catch (Exception e) {
103-
// Fallback to no-op implementation
104113
openTelemetry = OpenTelemetry.noop();
105114
return openTelemetry.getTracer("opengemini-client-java");
106115
}
107116

108117
}
109118

119+
private void cleanTrace() throws ExecutionException, InterruptedException {
120+
Query dropMeasurement = new Query("DROP MEASUREMENT \"%s\"".formatted(measurementName), databaseName, rpName);
121+
openGeminiClient.query(dropMeasurement).get();
122+
}
123+
124+
private void checkLastTraceCommand(String command) throws ExecutionException, InterruptedException {
125+
String queryTraceCommand = "SELECT command FROM \"%s\"".formatted(measurementName);
126+
Query checkTraceQuery = new Query(queryTraceCommand, databaseName, rpName);
127+
QueryResult checkRst = openGeminiClient.query(checkTraceQuery).get();
128+
List<List<Object>> queryValues = checkRst.getResults().get(0).getSeries().get(0).getValues();
129+
int recordNum = queryValues.size();
130+
Assertions.assertEquals(command, queryValues.get(recordNum - 1).get(1));
131+
}
132+
110133
@Test
111-
void testDatabaseCreation() {
134+
void testDatabaseCreation() throws ExecutionException, InterruptedException {
135+
String command = "CREATE DATABASE test_db";
112136
Assertions.assertDoesNotThrow(() -> {
113-
Query createDbQuery = new Query("CREATE DATABASE test_db");
137+
Query createDbQuery = new Query(command);
114138
openGeminiClient.query(createDbQuery).get(10, TimeUnit.SECONDS);
115139
}, "Database creation should not throw an exception");
140+
141+
Thread.sleep(3000);
142+
checkLastTraceCommand(command);
116143
}
117144

118145
@Test
119-
void testQueryOperation() {
146+
void testQueryOperation() throws InterruptedException, ExecutionException {
120147
Configuration config = new Configuration();
121-
config.setAddresses(java.util.Collections.singletonList(new Address("localhost", 8086)));
148+
config.setAddresses(Collections.singletonList(new Address("localhost", 8086)));
122149
if (config.getHttpConfig() == null) {
123150
config.setHttpConfig(new HttpClientConfig.Builder().build());
124151
}
125152

153+
String command = "SHOW DATABASES";
126154
Assertions.assertDoesNotThrow(() -> {
127155
Query createDbQuery = new Query("CREATE DATABASE test_db");
128156
openGeminiClient.query(createDbQuery).get(10, TimeUnit.SECONDS);
129157

130-
Query showDbQuery = new Query("SHOW DATABASES");
158+
Query showDbQuery = new Query(command);
131159
QueryResult result = openGeminiClient.query(showDbQuery).get(10, TimeUnit.SECONDS);
132160
Assertions.assertNotNull(result, "Query result should not be null");
133161
}, "Query operation should not throw an exception");
162+
163+
Thread.sleep(3000);
164+
checkLastTraceCommand(command);
134165
}
135166

136167
@Test
137-
void testWriteOperation() throws InterruptedException {
168+
void testWriteOperation() throws InterruptedException, ExecutionException {
138169
Configuration config = new Configuration();
139-
config.setAddresses(java.util.Collections.singletonList(
170+
config.setAddresses(Collections.singletonList(
140171
new Address("localhost", 8086)));
141172

142173
if (config.getHttpConfig() == null) {
143174
config.setHttpConfig(new HttpClientConfig.Builder().build());
144175
}
145176

177+
String lineProtocol = "temperature,location=room1 value=25.5";
146178
Assertions.assertDoesNotThrow(() -> {
147179
Query createDbQuery = new Query("CREATE DATABASE test_db");
148180
openGeminiClient.query(createDbQuery).get(10, TimeUnit.SECONDS);
149181

150182
Write write = new Write(
151183
"test_db",
152184
"autogen",
153-
"temperature,location=room1 value=25.5 " + System.currentTimeMillis(),
185+
lineProtocol,
154186
"ns"
155187
);
156188

@@ -161,6 +193,9 @@ void testWriteOperation() throws InterruptedException {
161193
).get(10, TimeUnit.SECONDS);
162194

163195
}, "Write operation should not throw an exception");
196+
197+
Thread.sleep(3000);
198+
checkLastTraceCommand(lineProtocol);
164199
}
165200

166201
@Test
@@ -169,6 +204,7 @@ void testTracingIntegration() throws ExecutionException, InterruptedException {
169204
CompletableFuture<Void> createdb = openGeminiClient.createDatabase(databaseTestName);
170205
createdb.get();
171206

207+
String command = "SELECT * FROM tracing_measurement";
172208
Assertions.assertDoesNotThrow(() -> {
173209

174210
Write write = new Write(
@@ -184,9 +220,12 @@ void testTracingIntegration() throws ExecutionException, InterruptedException {
184220
write.getLineProtocol()
185221
).get(10, TimeUnit.SECONDS);
186222

187-
Query query = new Query("SELECT * FROM tracing_measurement");
223+
Query query = new Query(command);
188224
openGeminiClient.query(query).get(10, TimeUnit.SECONDS);
189225

190226
}, "Tracing integration should not throw an exception");
227+
228+
Thread.sleep(3000);
229+
checkLastTraceCommand(command);
191230
}
192231
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
</dependency>
133133
<dependency>
134134
<groupId>io.opentelemetry</groupId>
135-
<artifactId>opentelemetry-exporter-jaeger</artifactId>
135+
<artifactId>opentelemetry-exporter-otlp</artifactId>
136136
<version>${opentelemetry-exporter.version}</version>
137137
</dependency>
138138
<dependency>

0 commit comments

Comments
 (0)