17
17
import io .opentelemetry .instrumentation .api .incubator .semconv .db .SqlStatementInfo ;
18
18
import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
19
19
import io .opentelemetry .instrumentation .api .instrumenter .SpanNameExtractor ;
20
+ import io .opentelemetry .instrumentation .testing .internal .AutoCleanupExtension ;
20
21
import io .opentelemetry .semconv .incubating .DbIncubatingAttributes ;
21
22
import java .util .Collection ;
22
23
import java .util .Collections ;
23
24
import java .util .Map ;
24
25
import org .junit .jupiter .api .Test ;
26
+ import org .junit .jupiter .api .extension .RegisterExtension ;
25
27
26
28
class InstrumenterContextTest {
29
+ @ RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension .create ();
27
30
28
31
@ SuppressWarnings ({"unchecked" , "deprecation" }) // using deprecated DB_SQL_TABLE
29
32
@ Test
@@ -41,39 +44,35 @@ public Collection<String> getRawQueryTexts(Object request) {
41
44
AttributesExtractor <Object , Void > attributesExtractor =
42
45
SqlClientAttributesExtractor .create (getter );
43
46
44
- assertThat (InstrumenterContext .isActive ()).isFalse ();
45
- InstrumenterContext .withContext (
46
- () -> {
47
- assertThat (InstrumenterContext .isActive ()).isTrue ();
48
- assertThat (InstrumenterContext .get ()).isEmpty ();
49
- assertThat (spanNameExtractor .extract (null )).isEqualTo ("SELECT test" );
50
- // verify that sanitized statement was cached, see SqlStatementSanitizerUtil
51
- assertThat (InstrumenterContext .get ()).containsKey ("sanitized-sql-map" );
52
- Map <String , SqlStatementInfo > sanitizedMap =
53
- (Map <String , SqlStatementInfo >) InstrumenterContext .get ().get ("sanitized-sql-map" );
54
- assertThat (sanitizedMap ).containsKey (testQuery );
47
+ InstrumenterContext .reset ();
48
+ cleanup .deferCleanup (InstrumenterContext ::reset );
55
49
56
- // replace cached sanitization result to verify it is used
57
- sanitizedMap .put (
58
- testQuery ,
59
- SqlStatementInfo .create ("SELECT name2 FROM test2 WHERE id = ?" , "SELECT" , "test2" ));
60
- {
61
- AttributesBuilder builder = Attributes .builder ();
62
- attributesExtractor .onStart (builder , Context .root (), null );
63
- assertThat (builder .build ().get (maybeStable (DbIncubatingAttributes .DB_SQL_TABLE )))
64
- .isEqualTo ("test2" );
65
- }
50
+ assertThat (InstrumenterContext .get ()).isEmpty ();
51
+ assertThat (spanNameExtractor .extract (null )).isEqualTo ("SELECT test" );
52
+ // verify that sanitized statement was cached, see SqlStatementSanitizerUtil
53
+ assertThat (InstrumenterContext .get ()).containsKey ("sanitized-sql-map" );
54
+ Map <String , SqlStatementInfo > sanitizedMap =
55
+ (Map <String , SqlStatementInfo >) InstrumenterContext .get ().get ("sanitized-sql-map" );
56
+ assertThat (sanitizedMap ).containsKey (testQuery );
66
57
67
- // clear cached value to see whether it gets recomputed correctly
68
- sanitizedMap .clear ();
69
- {
70
- AttributesBuilder builder = Attributes .builder ();
71
- attributesExtractor .onStart (builder , Context .root (), null );
72
- assertThat (builder .build ().get (maybeStable (DbIncubatingAttributes .DB_SQL_TABLE )))
73
- .isEqualTo ("test" );
74
- }
58
+ // replace cached sanitization result to verify it is used
59
+ sanitizedMap .put (
60
+ testQuery ,
61
+ SqlStatementInfo .create ("SELECT name2 FROM test2 WHERE id = ?" , "SELECT" , "test2" ));
62
+ {
63
+ AttributesBuilder builder = Attributes .builder ();
64
+ attributesExtractor .onStart (builder , Context .root (), null );
65
+ assertThat (builder .build ().get (maybeStable (DbIncubatingAttributes .DB_SQL_TABLE )))
66
+ .isEqualTo ("test2" );
67
+ }
75
68
76
- return null ;
77
- });
69
+ // clear cached value to see whether it gets recomputed correctly
70
+ sanitizedMap .clear ();
71
+ {
72
+ AttributesBuilder builder = Attributes .builder ();
73
+ attributesExtractor .onStart (builder , Context .root (), null );
74
+ assertThat (builder .build ().get (maybeStable (DbIncubatingAttributes .DB_SQL_TABLE )))
75
+ .isEqualTo ("test" );
76
+ }
78
77
}
79
78
}
0 commit comments