55
66package io .opentelemetry .instrumentation .api .incubator .semconv .db ;
77
8+ import static io .opentelemetry .api .common .AttributeKey .stringKey ;
89import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .assertThat ;
910import static org .assertj .core .api .Assertions .entry ;
1011
1314import io .opentelemetry .context .Context ;
1415import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
1516import io .opentelemetry .instrumentation .api .internal .SemconvStability ;
16- import io .opentelemetry .instrumentation .testing .junit .db .SemconvStabilityUtil ;
1717import io .opentelemetry .semconv .incubating .DbIncubatingAttributes ;
1818import java .util .Collections ;
1919import java .util .HashMap ;
@@ -77,7 +77,21 @@ void shouldExtractAllAttributes() {
7777 underTest .onEnd (endAttributes , context , request , null , null );
7878
7979 // then
80- if (SemconvStability .emitOldDatabaseSemconv ()) {
80+ if (SemconvStability .emitStableDatabaseSemconv () && SemconvStability .emitOldDatabaseSemconv ()) {
81+ assertThat (startAttributes .build ())
82+ .containsOnly (
83+ entry (DbIncubatingAttributes .DB_SYSTEM , "myDb" ),
84+ entry (DbIncubatingAttributes .DB_USER , "username" ),
85+ entry (DbIncubatingAttributes .DB_NAME , "potatoes" ),
86+ entry (DbIncubatingAttributes .DB_CONNECTION_STRING , "mydb:///potatoes" ),
87+ entry (DbIncubatingAttributes .DB_STATEMENT , "SELECT * FROM potato WHERE id=?" ),
88+ entry (DbIncubatingAttributes .DB_OPERATION , "SELECT" ),
89+ entry (DbIncubatingAttributes .DB_SQL_TABLE , "potato" ),
90+ entry (stringKey ("db.namespace" ), "potatoes" ),
91+ entry (stringKey ("db.query.text" ), "SELECT * FROM potato WHERE id=?" ),
92+ entry (stringKey ("db.operation.name" ), "SELECT" ),
93+ entry (stringKey ("db.collection.name" ), "potato" ));
94+ } else if (SemconvStability .emitOldDatabaseSemconv ()) {
8195 assertThat (startAttributes .build ())
8296 .containsOnly (
8397 entry (DbIncubatingAttributes .DB_SYSTEM , "myDb" ),
@@ -87,21 +101,14 @@ void shouldExtractAllAttributes() {
87101 entry (DbIncubatingAttributes .DB_STATEMENT , "SELECT * FROM potato WHERE id=?" ),
88102 entry (DbIncubatingAttributes .DB_OPERATION , "SELECT" ),
89103 entry (DbIncubatingAttributes .DB_SQL_TABLE , "potato" ));
90- } else {
104+ } else if ( SemconvStability . emitStableDatabaseSemconv ()) {
91105 assertThat (startAttributes .build ())
92106 .containsOnly (
93107 entry (DbIncubatingAttributes .DB_SYSTEM , "myDb" ),
94- entry (
95- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_NAME ), "potatoes" ),
96- entry (
97- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_STATEMENT ),
98- "SELECT * FROM potato WHERE id=?" ),
99- entry (
100- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_OPERATION ),
101- "SELECT" ),
102- entry (
103- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_SQL_TABLE ),
104- "potato" ));
108+ entry (stringKey ("db.namespace" ), "potatoes" ),
109+ entry (stringKey ("db.query.text" ), "SELECT * FROM potato WHERE id=?" ),
110+ entry (stringKey ("db.operation.name" ), "SELECT" ),
111+ entry (stringKey ("db.collection.name" ), "potato" ));
105112 }
106113
107114 assertThat (endAttributes .build ().isEmpty ()).isTrue ();
@@ -123,14 +130,24 @@ void shouldNotExtractTableIfAttributeIsNotSet() {
123130 underTest .onStart (attributes , context , request );
124131
125132 // then
126- assertThat (attributes .build ())
127- .containsOnly (
128- entry (
129- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_STATEMENT ),
130- "SELECT *" ),
131- entry (
132- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_OPERATION ),
133- "SELECT" ));
133+ if (SemconvStability .emitStableDatabaseSemconv () && SemconvStability .emitOldDatabaseSemconv ()) {
134+ assertThat (attributes .build ())
135+ .containsOnly (
136+ entry (DbIncubatingAttributes .DB_STATEMENT , "SELECT *" ),
137+ entry (DbIncubatingAttributes .DB_OPERATION , "SELECT" ),
138+ entry (stringKey ("db.query.text" ), "SELECT *" ),
139+ entry (stringKey ("db.operation.name" ), "SELECT" ));
140+ } else if (SemconvStability .emitOldDatabaseSemconv ()) {
141+ assertThat (attributes .build ())
142+ .containsOnly (
143+ entry (DbIncubatingAttributes .DB_STATEMENT , "SELECT *" ),
144+ entry (DbIncubatingAttributes .DB_OPERATION , "SELECT" ));
145+ } else if (SemconvStability .emitStableDatabaseSemconv ()) {
146+ assertThat (attributes .build ())
147+ .containsOnly (
148+ entry (stringKey ("db.query.text" ), "SELECT *" ),
149+ entry (stringKey ("db.operation.name" ), "SELECT" ));
150+ }
134151 }
135152
136153 @ Test
@@ -152,17 +169,28 @@ void shouldExtractTableToSpecifiedKey() {
152169 underTest .onStart (attributes , context , request );
153170
154171 // then
155- assertThat (attributes .build ())
156- .containsOnly (
157- entry (
158- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_STATEMENT ),
159- "SELECT * FROM table" ),
160- entry (
161- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_OPERATION ),
162- "SELECT" ),
163- entry (
164- SemconvStabilityUtil .getAttributeKey (DbIncubatingAttributes .DB_CASSANDRA_TABLE ),
165- "table" ));
172+ if (SemconvStability .emitStableDatabaseSemconv () && SemconvStability .emitOldDatabaseSemconv ()) {
173+ assertThat (attributes .build ())
174+ .containsOnly (
175+ entry (DbIncubatingAttributes .DB_STATEMENT , "SELECT * FROM table" ),
176+ entry (DbIncubatingAttributes .DB_OPERATION , "SELECT" ),
177+ entry (DbIncubatingAttributes .DB_CASSANDRA_TABLE , "table" ),
178+ entry (stringKey ("db.query.text" ), "SELECT * FROM table" ),
179+ entry (stringKey ("db.operation.name" ), "SELECT" ),
180+ entry (stringKey ("db.collection.name" ), "table" ));
181+ } else if (SemconvStability .emitOldDatabaseSemconv ()) {
182+ assertThat (attributes .build ())
183+ .containsOnly (
184+ entry (DbIncubatingAttributes .DB_STATEMENT , "SELECT * FROM table" ),
185+ entry (DbIncubatingAttributes .DB_OPERATION , "SELECT" ),
186+ entry (DbIncubatingAttributes .DB_CASSANDRA_TABLE , "table" ));
187+ } else if (SemconvStability .emitStableDatabaseSemconv ()) {
188+ assertThat (attributes .build ())
189+ .containsOnly (
190+ entry (stringKey ("db.query.text" ), "SELECT * FROM table" ),
191+ entry (stringKey ("db.operation.name" ), "SELECT" ),
192+ entry (stringKey ("db.collection.name" ), "table" ));
193+ }
166194 }
167195
168196 @ Test
0 commit comments