17
17
18
18
import static io .opentelemetry .proto .trace .v1 .Span .SpanKind .SPAN_KIND_CLIENT ;
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .junit .Assert .assertThat ;
20
21
21
22
import io .opentelemetry .proto .common .v1 .KeyValue ;
22
23
import io .opentelemetry .proto .metrics .v1 .ExponentialHistogramDataPoint ;
@@ -33,6 +34,7 @@ public class JdbcContractTestBase extends ContractTestBase {
33
34
protected static final String DB_USER = "sa" ;
34
35
protected static final String DB_PASSWORD = "password" ;
35
36
protected static final String DB_OPERATION = "SELECT" ;
37
+ protected static final String DB_RESOURCE_TYPE = "DB::Connection" ;
36
38
37
39
@ Override
38
40
protected String getApplicationImageName () {
@@ -49,13 +51,16 @@ protected void assertAwsSpanAttributes(
49
51
String method ,
50
52
String path ,
51
53
String dbSystem ,
52
- String dbOperation ) {
54
+ String dbOperation ,
55
+ String type ,
56
+ String identifier ) {
53
57
assertThat (resourceScopeSpans )
54
58
.satisfiesOnlyOnce (
55
59
rss -> {
56
60
assertThat (rss .getSpan ().getKind ()).isEqualTo (SPAN_KIND_CLIENT );
57
61
var attributesList = rss .getSpan ().getAttributesList ();
58
- assertAwsAttributes (attributesList , method , path , dbSystem , dbOperation );
62
+ assertAwsAttributes (
63
+ attributesList , method , path , dbSystem , dbOperation , type , identifier );
59
64
});
60
65
}
61
66
@@ -64,35 +69,52 @@ protected void assertAwsAttributes(
64
69
String method ,
65
70
String endpoint ,
66
71
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
+ }
96
118
}
97
119
98
120
protected void assertSemanticConventionsSpanAttributes (
@@ -181,7 +203,9 @@ protected void assertMetricAttributes(
181
203
String metricName ,
182
204
Double expectedSum ,
183
205
String dbSystem ,
184
- String dbOperation ) {
206
+ String dbOperation ,
207
+ String type ,
208
+ String identifier ) {
185
209
assertThat (resourceScopeMetrics )
186
210
.anySatisfy (
187
211
metric -> {
@@ -193,42 +217,58 @@ protected void assertMetricAttributes(
193
217
dp -> {
194
218
List <KeyValue > attributesList = dp .getAttributesList ();
195
219
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
+ }
232
272
233
273
if (expectedSum != null ) {
234
274
double actualSum = dp .getSum ();
@@ -245,7 +285,13 @@ protected void assertMetricAttributes(
245
285
}
246
286
247
287
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 ) {
249
295
var path = "success" ;
250
296
var method = "GET" ;
251
297
var otelStatusCode = "STATUS_CODE_UNSET" ;
@@ -255,7 +301,7 @@ protected void assertSuccess(
255
301
assertThat (response .status ().isSuccess ()).isTrue ();
256
302
257
303
var traces = mockCollectorClient .getTraces ();
258
- assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation );
304
+ assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation , type , identifier );
259
305
assertSemanticConventionsSpanAttributes (
260
306
traces , otelStatusCode , dbSqlTable , dbSystem , dbOperation , dbUser , dbName , jdbcUrl );
261
307
@@ -266,15 +312,45 @@ protected void assertSuccess(
266
312
AppSignalsConstants .ERROR_METRIC ,
267
313
AppSignalsConstants .FAULT_METRIC ));
268
314
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 );
270
324
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 );
272
334
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 );
274
344
}
275
345
276
346
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 ) {
278
354
var path = "fault" ;
279
355
var method = "GET" ;
280
356
var otelStatusCode = "STATUS_CODE_ERROR" ;
@@ -283,7 +359,7 @@ protected void assertFault(
283
359
assertThat (response .status ().isServerError ()).isTrue ();
284
360
285
361
var traces = mockCollectorClient .getTraces ();
286
- assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation );
362
+ assertAwsSpanAttributes (traces , method , path , dbSystem , dbOperation , type , identifier );
287
363
assertSemanticConventionsSpanAttributes (
288
364
traces , otelStatusCode , dbSqlTable , dbSystem , dbOperation , dbUser , dbName , jdbcUrl );
289
365
@@ -294,10 +370,34 @@ protected void assertFault(
294
370
AppSignalsConstants .ERROR_METRIC ,
295
371
AppSignalsConstants .FAULT_METRIC ));
296
372
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 );
298
382
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 );
300
392
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 );
302
402
}
303
403
}
0 commit comments