1717
1818import static io .opentelemetry .proto .trace .v1 .Span .SpanKind .SPAN_KIND_CLIENT ;
1919import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .junit .Assert .assertThat ;
2021
2122import io .opentelemetry .proto .common .v1 .KeyValue ;
2223import io .opentelemetry .proto .metrics .v1 .ExponentialHistogramDataPoint ;
@@ -33,6 +34,7 @@ public class JdbcContractTestBase extends ContractTestBase {
3334 protected static final String DB_USER = "sa" ;
3435 protected static final String DB_PASSWORD = "password" ;
3536 protected static final String DB_OPERATION = "SELECT" ;
37+ protected static final String DB_RESOURCE_TYPE = "DB::Connection" ;
3638
3739 @ Override
3840 protected String getApplicationImageName () {
@@ -49,13 +51,16 @@ protected void assertAwsSpanAttributes(
4951 String method ,
5052 String path ,
5153 String dbSystem ,
52- String dbOperation ) {
54+ String dbOperation ,
55+ String type ,
56+ String identifier ) {
5357 assertThat (resourceScopeSpans )
5458 .satisfiesOnlyOnce (
5559 rss -> {
5660 assertThat (rss .getSpan ().getKind ()).isEqualTo (SPAN_KIND_CLIENT );
5761 var attributesList = rss .getSpan ().getAttributesList ();
58- assertAwsAttributes (attributesList , method , path , dbSystem , dbOperation );
62+ assertAwsAttributes (
63+ attributesList , method , path , dbSystem , dbOperation , type , identifier );
5964 });
6065 }
6166
@@ -64,35 +69,52 @@ protected void assertAwsAttributes(
6469 String method ,
6570 String endpoint ,
6671 String dbSystem ,
67- String dbOperation ) {
68- assertThat (attributesList )
69- .satisfiesOnlyOnce (
70- attribute -> {
71- assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_LOCAL_OPERATION );
72- assertThat (attribute .getValue ().getStringValue ())
73- .isEqualTo (String .format ("%s /%s" , method , endpoint ));
74- })
75- .satisfiesOnlyOnce (
76- attribute -> {
77- assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_LOCAL_SERVICE );
78- assertThat (attribute .getValue ().getStringValue ())
79- .isEqualTo (getApplicationOtelServiceName ());
80- })
81- .satisfiesOnlyOnce (
82- attribute -> {
83- assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_REMOTE_SERVICE );
84- assertThat (attribute .getValue ().getStringValue ()).isEqualTo (dbSystem );
85- })
86- .satisfiesOnlyOnce (
87- attribute -> {
88- assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_REMOTE_OPERATION );
89- assertThat (attribute .getValue ().getStringValue ()).isEqualTo (dbOperation );
90- })
91- .satisfiesOnlyOnce (
92- attribute -> {
93- assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_SPAN_KIND );
94- assertThat (attribute .getValue ().getStringValue ()).isEqualTo ("CLIENT" );
95- });
72+ String dbOperation ,
73+ String type ,
74+ String identifier ) {
75+ var assertions =
76+ assertThat (attributesList )
77+ .satisfiesOnlyOnce (
78+ attribute -> {
79+ assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_LOCAL_OPERATION );
80+ assertThat (attribute .getValue ().getStringValue ())
81+ .isEqualTo (String .format ("%s /%s" , method , endpoint ));
82+ })
83+ .satisfiesOnlyOnce (
84+ attribute -> {
85+ assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_LOCAL_SERVICE );
86+ assertThat (attribute .getValue ().getStringValue ())
87+ .isEqualTo (getApplicationOtelServiceName ());
88+ })
89+ .satisfiesOnlyOnce (
90+ attribute -> {
91+ assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_REMOTE_SERVICE );
92+ assertThat (attribute .getValue ().getStringValue ()).isEqualTo (dbSystem );
93+ })
94+ .satisfiesOnlyOnce (
95+ attribute -> {
96+ assertThat (attribute .getKey ())
97+ .isEqualTo (AppSignalsConstants .AWS_REMOTE_OPERATION );
98+ assertThat (attribute .getValue ().getStringValue ()).isEqualTo (dbOperation );
99+ })
100+ .satisfiesOnlyOnce (
101+ attribute -> {
102+ assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_SPAN_KIND );
103+ assertThat (attribute .getValue ().getStringValue ()).isEqualTo ("CLIENT" );
104+ });
105+ if (type != null && identifier != null ) {
106+ assertions .satisfiesOnlyOnce (
107+ (attribute ) -> {
108+ assertThat (attribute .getKey ()).isEqualTo (AppSignalsConstants .AWS_REMOTE_RESOURCE_TYPE );
109+ assertThat (attribute .getValue ().getStringValue ()).isEqualTo (type );
110+ });
111+ assertions .satisfiesOnlyOnce (
112+ (attribute ) -> {
113+ assertThat (attribute .getKey ())
114+ .isEqualTo (AppSignalsConstants .AWS_REMOTE_RESOURCE_IDENTIFIER );
115+ assertThat (attribute .getValue ().getStringValue ()).isEqualTo (identifier );
116+ });
117+ }
96118 }
97119
98120 protected void assertSemanticConventionsSpanAttributes (
@@ -181,7 +203,9 @@ protected void assertMetricAttributes(
181203 String metricName ,
182204 Double expectedSum ,
183205 String dbSystem ,
184- String dbOperation ) {
206+ String dbOperation ,
207+ String type ,
208+ String identifier ) {
185209 assertThat (resourceScopeMetrics )
186210 .anySatisfy (
187211 metric -> {
@@ -193,42 +217,58 @@ protected void assertMetricAttributes(
193217 dp -> {
194218 List <KeyValue > attributesList = dp .getAttributesList ();
195219 assertThat (attributesList ).isNotNull ();
196- assertThat (attributesList )
197- .satisfiesOnlyOnce (
198- attribute -> {
199- assertThat (attribute .getKey ())
200- .isEqualTo (AppSignalsConstants .AWS_SPAN_KIND );
201- assertThat (attribute .getValue ().getStringValue ())
202- .isEqualTo ("CLIENT" );
203- })
204- .satisfiesOnlyOnce (
205- attribute -> {
206- assertThat (attribute .getKey ())
207- .isEqualTo (AppSignalsConstants .AWS_LOCAL_OPERATION );
208- assertThat (attribute .getValue ().getStringValue ())
209- .isEqualTo (String .format ("%s /%s" , method , path ));
210- })
211- .satisfiesOnlyOnce (
212- attribute -> {
213- assertThat (attribute .getKey ())
214- .isEqualTo (AppSignalsConstants .AWS_LOCAL_SERVICE );
215- assertThat (attribute .getValue ().getStringValue ())
216- .isEqualTo (getApplicationOtelServiceName ());
217- })
218- .satisfiesOnlyOnce (
219- attribute -> {
220- assertThat (attribute .getKey ())
221- .isEqualTo (AppSignalsConstants .AWS_REMOTE_SERVICE );
222- assertThat (attribute .getValue ().getStringValue ())
223- .isEqualTo (dbSystem );
224- })
225- .satisfiesOnlyOnce (
226- attribute -> {
227- assertThat (attribute .getKey ())
228- .isEqualTo (AppSignalsConstants .AWS_REMOTE_OPERATION );
229- assertThat (attribute .getValue ().getStringValue ())
230- .isEqualTo (dbOperation );
231- });
220+ var assertions =
221+ assertThat (attributesList )
222+ .satisfiesOnlyOnce (
223+ attribute -> {
224+ assertThat (attribute .getKey ())
225+ .isEqualTo (AppSignalsConstants .AWS_SPAN_KIND );
226+ assertThat (attribute .getValue ().getStringValue ())
227+ .isEqualTo ("CLIENT" );
228+ })
229+ .satisfiesOnlyOnce (
230+ attribute -> {
231+ assertThat (attribute .getKey ())
232+ .isEqualTo (AppSignalsConstants .AWS_LOCAL_OPERATION );
233+ assertThat (attribute .getValue ().getStringValue ())
234+ .isEqualTo (String .format ("%s /%s" , method , path ));
235+ })
236+ .satisfiesOnlyOnce (
237+ attribute -> {
238+ assertThat (attribute .getKey ())
239+ .isEqualTo (AppSignalsConstants .AWS_LOCAL_SERVICE );
240+ assertThat (attribute .getValue ().getStringValue ())
241+ .isEqualTo (getApplicationOtelServiceName ());
242+ })
243+ .satisfiesOnlyOnce (
244+ attribute -> {
245+ assertThat (attribute .getKey ())
246+ .isEqualTo (AppSignalsConstants .AWS_REMOTE_SERVICE );
247+ assertThat (attribute .getValue ().getStringValue ())
248+ .isEqualTo (dbSystem );
249+ })
250+ .satisfiesOnlyOnce (
251+ attribute -> {
252+ assertThat (attribute .getKey ())
253+ .isEqualTo (AppSignalsConstants .AWS_REMOTE_OPERATION );
254+ assertThat (attribute .getValue ().getStringValue ())
255+ .isEqualTo (dbOperation );
256+ });
257+ if (type != null && identifier != null ) {
258+ assertions .satisfiesOnlyOnce (
259+ (attribute ) -> {
260+ assertThat (attribute .getKey ())
261+ .isEqualTo (AppSignalsConstants .AWS_REMOTE_RESOURCE_TYPE );
262+ assertThat (attribute .getValue ().getStringValue ()).isEqualTo (type );
263+ });
264+ assertions .satisfiesOnlyOnce (
265+ (attribute ) -> {
266+ assertThat (attribute .getKey ())
267+ .isEqualTo (AppSignalsConstants .AWS_REMOTE_RESOURCE_IDENTIFIER );
268+ assertThat (attribute .getValue ().getStringValue ())
269+ .isEqualTo (identifier );
270+ });
271+ }
232272
233273 if (expectedSum != null ) {
234274 double actualSum = dp .getSum ();
@@ -245,7 +285,13 @@ protected void assertMetricAttributes(
245285 }
246286
247287 protected void assertSuccess (
248- String dbSystem , String dbOperation , String dbUser , String dbName , String jdbcUrl ) {
288+ String dbSystem ,
289+ String dbOperation ,
290+ String dbUser ,
291+ String dbName ,
292+ String jdbcUrl ,
293+ String type ,
294+ String identifier ) {
249295 var path = "success" ;
250296 var method = "GET" ;
251297 var otelStatusCode = "STATUS_CODE_UNSET" ;
@@ -255,7 +301,7 @@ protected void assertSuccess(
255301 assertThat (response .status ().isSuccess ()).isTrue ();
256302
257303 var traces = mockCollectorClient .getTraces ();
258- assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation );
304+ assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation , type , identifier );
259305 assertSemanticConventionsSpanAttributes (
260306 traces , otelStatusCode , dbSqlTable , dbSystem , dbOperation , dbUser , dbName , jdbcUrl );
261307
@@ -266,15 +312,45 @@ protected void assertSuccess(
266312 AppSignalsConstants .ERROR_METRIC ,
267313 AppSignalsConstants .FAULT_METRIC ));
268314 assertMetricAttributes (
269- metrics , method , path , AppSignalsConstants .LATENCY_METRIC , 5000.0 , dbSystem , dbOperation );
315+ metrics ,
316+ method ,
317+ path ,
318+ AppSignalsConstants .LATENCY_METRIC ,
319+ 5000.0 ,
320+ dbSystem ,
321+ dbOperation ,
322+ type ,
323+ identifier );
270324 assertMetricAttributes (
271- metrics , method , path , AppSignalsConstants .ERROR_METRIC , 0.0 , dbSystem , dbOperation );
325+ metrics ,
326+ method ,
327+ path ,
328+ AppSignalsConstants .ERROR_METRIC ,
329+ 0.0 ,
330+ dbSystem ,
331+ dbOperation ,
332+ type ,
333+ identifier );
272334 assertMetricAttributes (
273- metrics , method , path , AppSignalsConstants .FAULT_METRIC , 0.0 , dbSystem , dbOperation );
335+ metrics ,
336+ method ,
337+ path ,
338+ AppSignalsConstants .FAULT_METRIC ,
339+ 0.0 ,
340+ dbSystem ,
341+ dbOperation ,
342+ type ,
343+ identifier );
274344 }
275345
276346 protected void assertFault (
277- String dbSystem , String dbOperation , String dbUser , String dbName , String jdbcUrl ) {
347+ String dbSystem ,
348+ String dbOperation ,
349+ String dbUser ,
350+ String dbName ,
351+ String jdbcUrl ,
352+ String type ,
353+ String identifier ) {
278354 var path = "fault" ;
279355 var method = "GET" ;
280356 var otelStatusCode = "STATUS_CODE_ERROR" ;
@@ -283,7 +359,7 @@ protected void assertFault(
283359 assertThat (response .status ().isServerError ()).isTrue ();
284360
285361 var traces = mockCollectorClient .getTraces ();
286- assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation );
362+ assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation , type , identifier );
287363 assertSemanticConventionsSpanAttributes (
288364 traces , otelStatusCode , dbSqlTable , dbSystem , dbOperation , dbUser , dbName , jdbcUrl );
289365
@@ -294,10 +370,34 @@ protected void assertFault(
294370 AppSignalsConstants .ERROR_METRIC ,
295371 AppSignalsConstants .FAULT_METRIC ));
296372 assertMetricAttributes (
297- metrics , method , path , AppSignalsConstants .LATENCY_METRIC , 5000.0 , dbSystem , dbOperation );
373+ metrics ,
374+ method ,
375+ path ,
376+ AppSignalsConstants .LATENCY_METRIC ,
377+ 5000.0 ,
378+ dbSystem ,
379+ dbOperation ,
380+ type ,
381+ identifier );
298382 assertMetricAttributes (
299- metrics , method , path , AppSignalsConstants .ERROR_METRIC , 0.0 , dbSystem , dbOperation );
383+ metrics ,
384+ method ,
385+ path ,
386+ AppSignalsConstants .ERROR_METRIC ,
387+ 0.0 ,
388+ dbSystem ,
389+ dbOperation ,
390+ type ,
391+ identifier );
300392 assertMetricAttributes (
301- metrics , method , path , AppSignalsConstants .FAULT_METRIC , 1.0 , dbSystem , dbOperation );
393+ metrics ,
394+ method ,
395+ path ,
396+ AppSignalsConstants .FAULT_METRIC ,
397+ 1.0 ,
398+ dbSystem ,
399+ dbOperation ,
400+ type ,
401+ identifier );
302402 }
303403}
0 commit comments