2626import io .opentelemetry .api .OpenTelemetry ;
2727import io .opentelemetry .api .common .Attributes ;
2828import io .opentelemetry .api .trace .Tracer ;
29- import io .opentelemetry .exporter .jaeger . JaegerGrpcSpanExporter ;
29+ import io .opentelemetry .exporter .otlp . trace . OtlpGrpcSpanExporter ;
3030import io .opentelemetry .sdk .OpenTelemetrySdk ;
3131import io .opentelemetry .sdk .resources .Resource ;
3232import io .opentelemetry .sdk .trace .SdkTracerProvider ;
3939
4040import java .time .Duration ;
4141import java .util .Collections ;
42+ import java .util .List ;
4243import java .util .concurrent .CompletableFuture ;
4344import java .util .concurrent .ExecutionException ;
4445import 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}
0 commit comments