2626import io .opentelemetry .api .common .Attributes ;
2727import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
2828import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
29- import io .opentelemetry .sdk .testing .assertj .SpanDataAssert ;
3029import io .opentelemetry .semconv .incubating .DbIncubatingAttributes ;
31- import java .util .ArrayList ;
32- import java .util .List ;
33- import java .util .function .Consumer ;
34- import org .hibernate .Version ;
3530import org .junit .jupiter .api .AfterAll ;
3631import org .junit .jupiter .api .BeforeAll ;
3732import org .junit .jupiter .api .Test ;
@@ -61,7 +56,6 @@ static void tearDown() {
6156 @ SuppressWarnings ("deprecation" ) // DbIncubatingAttributes.DB_NAME has been deprecated
6257 @ Test
6358 void testCurd () {
64- boolean isHibernate4 = Version .getVersionString ().startsWith ("4." );
6559 Customer customer = new Customer ("Bob" , "Anonymous" );
6660
6761 assertNull (customer .getId ());
@@ -123,117 +117,68 @@ void testCurd() {
123117
124118 assertThat (customer .getId ()).isNotNull ();
125119 testing .waitAndAssertTraces (
126- trace -> {
127- List <Consumer <SpanDataAssert >> consumers =
128- new ArrayList <>(
129- asList (
130- span ->
131- span .hasName ("parent" )
132- .hasKind (INTERNAL )
133- .hasNoParent ()
134- .hasAttributes (Attributes .empty ()),
135- span ->
136- span .hasName ("Session.persist spring.jpa.Customer" )
137- .hasKind (INTERNAL )
138- .hasParent (trace .getSpan (0 ))
139- .hasAttributesSatisfyingExactly (
140- satisfies (
141- AttributeKey .stringKey ("hibernate.session_id" ),
142- val -> val .isInstanceOf (String .class )))));
143- if (!isHibernate4 ) {
144- consumers .addAll (
145- asList (
146- span ->
147- span .hasName ("CALL test" )
148- .hasKind (CLIENT )
149- .hasParent (trace .getSpan (1 ))
150- .hasAttributesSatisfyingExactly (
151- equalTo (
152- DB_SYSTEM ,
153- DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
154- equalTo (maybeStable (DB_NAME ), "test" ),
155- equalTo (
156- DbIncubatingAttributes .DB_USER ,
157- emitStableDatabaseSemconv () ? null : "sa" ),
158- equalTo (
159- maybeStable (DB_STATEMENT ), "call next value for Customer_SEQ" ),
160- equalTo (
161- DbIncubatingAttributes .DB_CONNECTION_STRING ,
162- emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
163- equalTo (maybeStable (DB_OPERATION ), "CALL" )),
164- span ->
165- span .hasName ("Transaction.commit" )
166- .hasKind (INTERNAL )
167- .hasParent (trace .getSpan (0 ))
168- .hasAttributesSatisfyingExactly (
169- equalTo (
170- AttributeKey .stringKey ("hibernate.session_id" ),
171- trace
172- .getSpan (1 )
173- .getAttributes ()
174- .get (stringKey ("hibernate.session_id" )))),
175- span ->
176- span .hasName ("INSERT test.Customer" )
177- .hasKind (CLIENT )
178- .hasParent (trace .getSpan (3 ))
179- .hasAttributesSatisfyingExactly (
180- equalTo (
181- DB_SYSTEM ,
182- DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
183- equalTo (maybeStable (DB_NAME ), "test" ),
184- equalTo (
185- DbIncubatingAttributes .DB_USER ,
186- emitStableDatabaseSemconv () ? null : "sa" ),
187- equalTo (
188- DbIncubatingAttributes .DB_CONNECTION_STRING ,
189- emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
190- satisfies (
191- maybeStable (DB_STATEMENT ),
192- val ->
193- val .matches (
194- "insert into Customer \\ (.*\\ ) values \\ (.*\\ )" )),
195- equalTo (maybeStable (DB_OPERATION ), "INSERT" ),
196- equalTo (maybeStable (DB_SQL_TABLE ), "Customer" ))));
197-
198- } else {
199- consumers .addAll (
200- asList (
201- span ->
202- span .hasName ("INSERT test.Customer" )
203- .hasKind (CLIENT )
204- .hasParent (trace .getSpan (1 ))
205- .hasAttributesSatisfyingExactly (
206- equalTo (
207- DB_SYSTEM ,
208- DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
209- equalTo (maybeStable (DB_NAME ), "test" ),
210- equalTo (
211- DbIncubatingAttributes .DB_USER ,
212- emitStableDatabaseSemconv () ? null : "sa" ),
213- equalTo (
214- DbIncubatingAttributes .DB_CONNECTION_STRING ,
215- emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
216- satisfies (
217- maybeStable (DB_STATEMENT ),
218- val ->
219- val .matches (
220- "insert into Customer \\ (.*\\ ) values \\ (.*\\ )" )),
221- equalTo (maybeStable (DB_OPERATION ), "INSERT" ),
222- equalTo (maybeStable (DB_SQL_TABLE ), "Customer" )),
223- span ->
224- span .hasName ("Transaction.commit" )
225- .hasKind (INTERNAL )
226- .hasParent (trace .getSpan (0 ))
227- .hasAttributesSatisfyingExactly (
228- equalTo (
229- AttributeKey .stringKey ("hibernate.session_id" ),
230- trace
231- .getSpan (1 )
232- .getAttributes ()
233- .get (stringKey ("hibernate.session_id" ))))));
234- }
235- trace .hasSpansSatisfyingExactly (consumers );
236- });
120+ trace ->
121+ trace .hasSpansSatisfyingExactly (
122+ span ->
123+ span .hasName ("parent" )
124+ .hasKind (INTERNAL )
125+ .hasNoParent ()
126+ .hasAttributes (Attributes .empty ()),
127+ span ->
128+ span .hasName ("Session.persist spring.jpa.Customer" )
129+ .hasKind (INTERNAL )
130+ .hasParent (trace .getSpan (0 ))
131+ .hasAttributesSatisfyingExactly (
132+ satisfies (
133+ AttributeKey .stringKey ("hibernate.session_id" ),
134+ val -> val .isInstanceOf (String .class ))),
135+ span ->
136+ span .hasName ("CALL test" )
137+ .hasKind (CLIENT )
138+ .hasParent (trace .getSpan (1 ))
139+ .hasAttributesSatisfyingExactly (
140+ equalTo (
141+ DB_SYSTEM , DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
142+ equalTo (maybeStable (DB_NAME ), "test" ),
143+ equalTo (
144+ DbIncubatingAttributes .DB_USER ,
145+ emitStableDatabaseSemconv () ? null : "sa" ),
146+ equalTo (maybeStable (DB_STATEMENT ), "call next value for Customer_SEQ" ),
147+ equalTo (
148+ DbIncubatingAttributes .DB_CONNECTION_STRING ,
149+ emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
150+ equalTo (maybeStable (DB_OPERATION ), "CALL" )),
151+ span ->
152+ span .hasName ("Transaction.commit" )
153+ .hasKind (INTERNAL )
154+ .hasParent (trace .getSpan (0 ))
155+ .hasAttributesSatisfyingExactly (
156+ equalTo (
157+ AttributeKey .stringKey ("hibernate.session_id" ),
158+ trace
159+ .getSpan (1 )
160+ .getAttributes ()
161+ .get (stringKey ("hibernate.session_id" )))),
162+ span ->
163+ span .hasName ("INSERT test.Customer" )
164+ .hasKind (CLIENT )
165+ .hasParent (trace .getSpan (3 ))
166+ .hasAttributesSatisfyingExactly (
167+ equalTo (
168+ DB_SYSTEM , DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
169+ equalTo (maybeStable (DB_NAME ), "test" ),
170+ equalTo (
171+ DbIncubatingAttributes .DB_USER ,
172+ emitStableDatabaseSemconv () ? null : "sa" ),
173+ equalTo (
174+ DbIncubatingAttributes .DB_CONNECTION_STRING ,
175+ emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
176+ satisfies (
177+ maybeStable (DB_STATEMENT ),
178+ val ->
179+ val .matches ("insert into Customer \\ (.*\\ ) values \\ (.*\\ )" )),
180+ equalTo (maybeStable (DB_OPERATION ), "INSERT" ),
181+ equalTo (maybeStable (DB_SQL_TABLE ), "Customer" ))));
237182
238183 testing .clearData ();
239184
@@ -359,17 +304,13 @@ void testCurd() {
359304 testing .runWithSpan ("parent" , () -> repo .delete (anonymousCustomer ));
360305
361306 testing .waitAndAssertTraces (
362- trace -> {
363- List <Consumer <SpanDataAssert >> consumers =
364- new ArrayList <>(
365- asList (
366- span ->
367- span .hasName ("parent" )
368- .hasKind (INTERNAL )
369- .hasNoParent ()
370- .hasAttributes (Attributes .empty ())));
371- if (!isHibernate4 ) {
372- consumers .add (
307+ trace ->
308+ trace .hasSpansSatisfyingExactly (
309+ span ->
310+ span .hasName ("parent" )
311+ .hasKind (INTERNAL )
312+ .hasNoParent ()
313+ .hasAttributes (Attributes .empty ()),
373314 span ->
374315 span .satisfies (
375316 val ->
@@ -380,9 +321,7 @@ void testCurd() {
380321 .hasAttributesSatisfyingExactly (
381322 satisfies (
382323 AttributeKey .stringKey ("hibernate.session_id" ),
383- val -> val .isInstanceOf (String .class ))));
384-
385- consumers .add (
324+ val -> val .isInstanceOf (String .class ))),
386325 span ->
387326 span .hasName ("SELECT test.Customer" )
388327 .hasKind (CLIENT )
@@ -403,23 +342,34 @@ void testCurd() {
403342 val .matches (
404343 "select ([^.]+)\\ .id([^,]*),([^.]+)\\ .firstName([^,]*),([^.]+)\\ .lastName (.*)from Customer (.*)(where ([^.]+)\\ .lastName( ?)=( ?)\\ ?|)" )),
405344 equalTo (maybeStable (DB_OPERATION ), "SELECT" ),
406- equalTo (maybeStable (DB_SQL_TABLE ), "Customer" )));
407- }
408- consumers .add (
409- span ->
410- span .hasName ("Session.merge spring.jpa.Customer" )
411- .hasKind (INTERNAL )
412- .hasParent (trace .getSpan (0 ))
413- .hasAttributesSatisfyingExactly (
414- satisfies (
415- AttributeKey .stringKey ("hibernate.session_id" ),
416- val -> val .isInstanceOf (String .class ))));
417- if (isHibernate4 ) {
418- consumers .add (
345+ equalTo (maybeStable (DB_SQL_TABLE ), "Customer" )),
419346 span ->
420- span .hasName ("SELECT test.Customer" )
347+ span .hasName ("Session.merge spring.jpa.Customer" )
348+ .hasKind (INTERNAL )
349+ .hasParent (trace .getSpan (0 ))
350+ .hasAttributesSatisfyingExactly (
351+ satisfies (
352+ AttributeKey .stringKey ("hibernate.session_id" ),
353+ val -> val .isInstanceOf (String .class ))),
354+ span ->
355+ span .hasName ("Session.delete spring.jpa.Customer" )
356+ .hasKind (INTERNAL )
357+ .hasParent (trace .getSpan (0 ))
358+ .hasAttributesSatisfyingExactly (
359+ satisfies (
360+ AttributeKey .stringKey ("hibernate.session_id" ),
361+ val -> val .isInstanceOf (String .class ))),
362+ span ->
363+ span .hasName ("Transaction.commit" )
364+ .hasKind (INTERNAL )
365+ .hasParent (trace .getSpan (0 ))
366+ .hasAttributesSatisfyingExactly (
367+ satisfies (
368+ AttributeKey .stringKey ("hibernate.session_id" ),
369+ val -> val .isInstanceOf (String .class ))),
370+ span ->
371+ span .hasName ("DELETE test.Customer" )
421372 .hasKind (CLIENT )
422- .hasParent (trace .getSpan (1 ))
423373 .hasAttributesSatisfyingExactly (
424374 equalTo (
425375 DB_SYSTEM , DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
@@ -430,50 +380,8 @@ void testCurd() {
430380 equalTo (
431381 DbIncubatingAttributes .DB_CONNECTION_STRING ,
432382 emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
433- satisfies (
434- maybeStable (DB_STATEMENT ),
435- val ->
436- val .matches (
437- "select ([^.]+)\\ .id([^,]*),([^.]+)\\ .firstName([^,]*),([^.]+)\\ .lastName (.*)from Customer (.*)where ([^.]+)\\ .id( ?)=( ?)\\ ?" )),
438- equalTo (maybeStable (DB_OPERATION ), "SELECT" ),
439- equalTo (maybeStable (DB_SQL_TABLE ), "Customer" )));
440- }
441- consumers .add (
442- span ->
443- span .hasName ("Session.delete spring.jpa.Customer" )
444- .hasKind (INTERNAL )
445- .hasParent (trace .getSpan (0 ))
446- .hasAttributesSatisfyingExactly (
447- satisfies (
448- AttributeKey .stringKey ("hibernate.session_id" ),
449- val -> val .isInstanceOf (String .class ))));
450- consumers .add (
451- span ->
452- span .hasName ("Transaction.commit" )
453- .hasKind (INTERNAL )
454- .hasParent (trace .getSpan (0 ))
455- .hasAttributesSatisfyingExactly (
456- satisfies (
457- AttributeKey .stringKey ("hibernate.session_id" ),
458- val -> val .isInstanceOf (String .class ))));
459- consumers .add (
460- span ->
461- span .hasName ("DELETE test.Customer" )
462- .hasKind (CLIENT )
463- .hasAttributesSatisfyingExactly (
464- equalTo (
465- DB_SYSTEM , DbIncubatingAttributes .DbSystemIncubatingValues .HSQLDB ),
466- equalTo (maybeStable (DB_NAME ), "test" ),
467- equalTo (
468- DbIncubatingAttributes .DB_USER ,
469- emitStableDatabaseSemconv () ? null : "sa" ),
470- equalTo (
471- DbIncubatingAttributes .DB_CONNECTION_STRING ,
472- emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
473- equalTo (maybeStable (DB_STATEMENT ), "delete from Customer where id=?" ),
474- equalTo (maybeStable (DB_OPERATION ), "DELETE" ),
475- equalTo (maybeStable (DB_SQL_TABLE ), "Customer" )));
476- trace .hasSpansSatisfyingExactly (consumers );
477- });
383+ equalTo (maybeStable (DB_STATEMENT ), "delete from Customer where id=?" ),
384+ equalTo (maybeStable (DB_OPERATION ), "DELETE" ),
385+ equalTo (maybeStable (DB_SQL_TABLE ), "Customer" ))));
478386 }
479387}
0 commit comments