3636import com .google .api .gax .tracing .OpenTelemetryTracingRecorder ;
3737import com .google .api .gax .tracing .OpenTelemetryTracingTracer ;
3838import com .google .api .gax .tracing .OpenTelemetryTracingTracerFactory ;
39+ import com .google .common .collect .ImmutableList ;
3940import com .google .common .collect .ImmutableMap ;
4041import com .google .rpc .Status ;
4142import com .google .showcase .v1beta1 .EchoClient ;
@@ -83,7 +84,7 @@ void tearDown() {
8384 }
8485
8586 @ Test
86- void testTracing_successfulEcho () throws Exception {
87+ void testTracing_successfulEcho_grpc () throws Exception {
8788 OpenTelemetryTracingTracerFactory tracingFactory =
8889 new OpenTelemetryTracingTracerFactory (new OpenTelemetryTracingRecorder (openTelemetrySdk ));
8990
@@ -95,16 +96,6 @@ void testTracing_successfulEcho() throws Exception {
9596 List <SpanData > spans = spanExporter .getFinishedSpanItems ();
9697 assertThat (spans ).isNotEmpty ();
9798
98- // Verify operation span (low-cardinality)
99- // SpanData operationSpan =
100- // spans.stream()
101- // .filter(span -> span.getName().equals("Echo.Echo/operation"))
102- // .findFirst()
103- // .orElseThrow(() -> new AssertionError("Operation span 'Echo.Echo/operation'
104- // not found"));
105- // assertThat(operationSpan.getStatus().getStatusCode()).isEqualTo(StatusCode.OK);
106-
107- // Verify attempt span (RPC convention)
10899 SpanData attemptSpan =
109100 spans .stream ()
110101 .filter (span -> span .getName ().equals ("Echo/Echo/attempt" ))
@@ -122,40 +113,91 @@ void testTracing_successfulEcho() throws Exception {
122113 .getAttributes ()
123114 .get (AttributeKey .stringKey (OpenTelemetryTracingTracerFactory .PORT_ATTRIBUTE )))
124115 .isEqualTo (SHOWCASE_SERVER_PORT );
116+ assertThat (
117+ attemptSpan
118+ .getAttributes ()
119+ .get (
120+ AttributeKey .stringKey (
121+ OpenTelemetryTracingTracerFactory .RPC_SYSTEM_ATTRIBUTE )))
122+ .isEqualTo ("grpc" );
125123 }
126124 }
127125
128126 @ Test
129- void testTracing_failedEcho () throws Exception {
127+ void testTracing_successfulEcho_httpjson () throws Exception {
130128 OpenTelemetryTracingTracerFactory tracingFactory =
131129 new OpenTelemetryTracingTracerFactory (new OpenTelemetryTracingRecorder (openTelemetrySdk ));
132130
133131 try (EchoClient client =
134- TestClientInitializer .createGrpcEchoClientOpentelemetry (tracingFactory )) {
132+ TestClientInitializer .createHttpJsonEchoClientOpentelemetry (tracingFactory )) {
135133
136- // Content is empty, which should trigger an error in Showcase Echo
137- Assertions .assertThrows (
138- Exception .class ,
139- () ->
140- client .echo (
141- EchoRequest .newBuilder ()
142- .setContent ("" )
143- .setError (
144- Status .newBuilder ().setCode (StatusCode .Code .UNKNOWN .ordinal ()).build ())
145- .build ()));
134+ client .echo (EchoRequest .newBuilder ().setContent ("tracing-test" ).build ());
146135
147136 List <SpanData > spans = spanExporter .getFinishedSpanItems ();
148137 assertThat (spans ).isNotEmpty ();
149- assertThat (spans .size () == 10 ); // 10 retires
150138
151- // Verify operation span recorded the error
152- assertThat (
139+ SpanData attemptSpan =
153140 spans .stream ()
154- .allMatch (
155- s ->
156- s .getStatus ()
157- .getStatusCode ()
158- .equals (io .opentelemetry .api .trace .StatusCode .ERROR )));
141+ .filter (span -> span .getName ().equals ("google.showcase.v1beta1/Echo/Echo/attempt" ))
142+ .findFirst ()
143+ .orElseThrow (() -> new AssertionError ("Attempt span 'Echo/Echo/attempt' not found" ));
144+ assertThat (
145+ attemptSpan
146+ .getAttributes ()
147+ .get (
148+ AttributeKey .stringKey (
149+ OpenTelemetryTracingTracerFactory .RPC_SYSTEM_ATTRIBUTE )))
150+ .isEqualTo ("http" );
151+ }
152+ }
153+
154+ @ Test
155+ void testTracing_errorRecording () throws Exception {
156+ List <StatusCode .Code > errorCodes =
157+ ImmutableList .of (
158+ StatusCode .Code .UNAVAILABLE ,
159+ StatusCode .Code .INVALID_ARGUMENT ,
160+ StatusCode .Code .NOT_FOUND ,
161+ StatusCode .Code .DEADLINE_EXCEEDED ,
162+ StatusCode .Code .PERMISSION_DENIED );
163+
164+ OpenTelemetryTracingTracerFactory tracingFactory =
165+ new OpenTelemetryTracingTracerFactory (new OpenTelemetryTracingRecorder (openTelemetrySdk ));
166+
167+ try (EchoClient client =
168+ TestClientInitializer .createGrpcEchoClientOpentelemetry (tracingFactory )) {
169+
170+ for (StatusCode .Code code : errorCodes ) {
171+ spanExporter .reset ();
172+ Assertions .assertThrows (
173+ Exception .class ,
174+ () ->
175+ client .echo (
176+ EchoRequest .newBuilder ()
177+ .setContent ("error-test-" + code )
178+ .setError (Status .newBuilder ().setCode (code .ordinal ()).build ())
179+ .build ()));
180+
181+ List <SpanData > spans = spanExporter .getFinishedSpanItems ();
182+ assertThat (spans ).isNotEmpty ();
183+
184+ SpanData attemptSpan =
185+ spans .stream ()
186+ .filter (span -> span .getName ().equals ("Echo/Echo/attempt" ))
187+ .findFirst ()
188+ .orElseThrow (
189+ () ->
190+ new AssertionError (
191+ "Attempt span 'Echo/Echo/attempt' not found for code: " + code ));
192+
193+ assertThat (
194+ attemptSpan
195+ .getAttributes ()
196+ .get (AttributeKey .stringKey (OpenTelemetryTracingTracer .ERROR_TYPE_ATTRIBUTE )))
197+ .isEqualTo (code .toString ());
198+ assertThat (attemptSpan .getStatus ().getStatusCode ())
199+ .isEqualTo (io .opentelemetry .api .trace .StatusCode .ERROR );
200+ }
159201 }
160202 }
161203
@@ -174,15 +216,6 @@ void testTracing_withCustomAttributes() throws Exception {
174216
175217 List <SpanData > spans = spanExporter .getFinishedSpanItems ();
176218
177- // SpanData operationSpan =
178- // spans.stream()
179- // .filter(span -> span.getName().equals("Echo.Echo/operation"))
180- // .findFirst()
181- // .orElseThrow(() -> new AssertionError("Operation span 'Echo.Echo/operation'
182- // not found"));
183- // assertThat(operationSpan.getAttributes().get(AttributeKey.stringKey("op-key")))
184- // .isEqualTo("op-value");
185-
186219 SpanData attemptSpan =
187220 spans .stream ()
188221 .filter (span -> span .getName ().equals ("Echo/Echo/attempt" ))
@@ -220,81 +253,4 @@ void testTracing_customStubSettings_overridesServiceName() throws Exception {
220253 .isEqualTo (customServiceName );
221254 }
222255 }
223-
224- @ Test
225- void testTracing_unavailableError () throws Exception {
226- OpenTelemetryTracingTracerFactory tracingFactory =
227- new OpenTelemetryTracingTracerFactory (new OpenTelemetryTracingRecorder (openTelemetrySdk ));
228-
229- try (EchoClient client =
230- TestClientInitializer .createGrpcEchoClientOpentelemetry (tracingFactory )) {
231-
232- Assertions .assertThrows (
233- Exception .class ,
234- () ->
235- client .echo (
236- EchoRequest .newBuilder ()
237- .setContent ("unavailable-test" )
238- .setError (
239- Status .newBuilder ()
240- .setCode (StatusCode .Code .UNAVAILABLE .ordinal ())
241- .build ())
242- .build ()));
243-
244- List <SpanData > spans = spanExporter .getFinishedSpanItems ();
245- assertThat (spans ).isNotEmpty ();
246-
247- // Verify that at least one attempt span recorded the UNAVAILABLE error
248- SpanData attemptSpan =
249- spans .stream ()
250- .filter (span -> span .getName ().equals ("Echo/Echo/attempt" ))
251- .findFirst ()
252- .orElseThrow (() -> new AssertionError ("Attempt span 'Echo/Echo/attempt' not found" ));
253-
254- assertThat (
255- attemptSpan
256- .getAttributes ()
257- .get (AttributeKey .stringKey (OpenTelemetryTracingTracer .ERROR_TYPE_ATTRIBUTE )))
258- .isEqualTo (StatusCode .Code .UNAVAILABLE .toString ());
259- assertThat (attemptSpan .getStatus ().getStatusCode ())
260- .isEqualTo (io .opentelemetry .api .trace .StatusCode .ERROR );
261- }
262- }
263-
264- @ Test
265- void testTracing_invalidArgumentError () throws Exception {
266- OpenTelemetryTracingTracerFactory tracingFactory =
267- new OpenTelemetryTracingTracerFactory (new OpenTelemetryTracingRecorder (openTelemetrySdk ));
268-
269- try (EchoClient client =
270- TestClientInitializer .createGrpcEchoClientOpentelemetry (tracingFactory )) {
271-
272- Assertions .assertThrows (
273- Exception .class ,
274- () ->
275- client .echo (
276- EchoRequest .newBuilder ()
277- .setContent ("invalid-argument-test" )
278- .setError (
279- Status .newBuilder ()
280- .setCode (StatusCode .Code .INVALID_ARGUMENT .ordinal ())
281- .build ())
282- .build ()));
283-
284- List <SpanData > spans = spanExporter .getFinishedSpanItems ();
285- assertThat (spans ).isNotEmpty ();
286-
287- SpanData attemptSpan =
288- spans .stream ()
289- .filter (span -> span .getName ().equals ("Echo/Echo/attempt" ))
290- .findFirst ()
291- .orElseThrow (() -> new AssertionError ("Attempt span 'Echo/Echo/attempt' not found" ));
292-
293- assertThat (
294- attemptSpan
295- .getAttributes ()
296- .get (AttributeKey .stringKey (OpenTelemetryTracingTracer .ERROR_TYPE_ATTRIBUTE )))
297- .isEqualTo (StatusCode .Code .INVALID_ARGUMENT .toString ());
298- }
299- }
300256}
0 commit comments