1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ * SPDX-License-Identifier: Apache-2.0
4+ */
5+
16package spring .jpa ;
27
8+ import static io .opentelemetry .api .trace .SpanKind .CLIENT ;
9+ import static io .opentelemetry .api .trace .SpanKind .INTERNAL ;
10+ import static io .opentelemetry .instrumentation .api .internal .SemconvStability .emitStableDatabaseSemconv ;
11+ import static io .opentelemetry .instrumentation .testing .junit .db .SemconvStabilityUtil .maybeStable ;
12+ import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
13+ import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .satisfies ;
14+ import static org .junit .jupiter .api .Assertions .assertEquals ;
15+ import static org .junit .jupiter .api .Assertions .assertFalse ;
16+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
17+ import static org .junit .jupiter .api .Assertions .assertNull ;
18+
319import io .opentelemetry .api .common .AttributeKey ;
420import io .opentelemetry .api .common .Attributes ;
5- import io .opentelemetry .instrumentation .test .AgentInstrumentationSpecification ;
621import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
722import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
823import io .opentelemetry .semconv .incubating .DbIncubatingAttributes ;
1328import org .junit .jupiter .api .Test ;
1429import org .junit .jupiter .api .extension .RegisterExtension ;
1530import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
16- import spring .jpa .Customer ;
17- import spring .jpa .CustomerRepository ;
18- import spring .jpa .PersistenceConfig ;
19-
20- import static io .opentelemetry .api .trace .SpanKind .CLIENT ;
21- import static io .opentelemetry .api .trace .SpanKind .INTERNAL ;
22- import static io .opentelemetry .instrumentation .api .internal .SemconvStability .emitStableDatabaseSemconv ;
23- import static io .opentelemetry .instrumentation .testing .junit .db .SemconvStabilityUtil .maybeStable ;
24- import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
25- import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .satisfies ;
26- import static org .junit .jupiter .api .Assertions .assertEquals ;
27- import static org .junit .jupiter .api .Assertions .assertFalse ;
28- import static org .junit .jupiter .api .Assertions .assertNotNull ;
29- import static org .junit .jupiter .api .Assertions .assertNull ;
30- import static org .junit .jupiter .api .Assertions .assertTrue ;
3131
32- class SpringJpaTest {
32+ class SpringJpaTest {
3333 @ RegisterExtension
3434 static final InstrumentationExtension testing = AgentInstrumentationExtension .create ();
3535
@@ -49,53 +49,63 @@ static void tearDown() {
4949 }
5050 }
5151
52+ @ SuppressWarnings ({
53+ "deprecation" ,
54+ "UnusedVariable"
55+ }) // DbIncubatingAttributes.DB_NAME has been deprecated
5256 @ Test
5357 public void testCRUD () {
5458 boolean isHibernate4 = Version .getVersionString ().startsWith ("4." );
55- final Customer customer = new Customer ("Bob" , "Anonymous" );
59+ Customer customer = new Customer ("Bob" , "Anonymous" );
5660
5761 assertNull (customer .getId ());
5862 assertFalse (testing .runWithSpan ("parent" , () -> repo .findAll ().iterator ().hasNext ()));
5963
60- AtomicReference <String > sessionId = new AtomicReference <>();
64+ AtomicReference <String > sessionId = new AtomicReference <>();
6165 testing .waitAndAssertTraces (
62- trace -> trace .hasSpansSatisfyingExactly (
63- span ->
64- span .hasName ("parent" )
65- .hasKind (INTERNAL )
66- .hasNoParent ()
67- .hasAttributes (Attributes .empty ()),
68- span ->
69- span .hasName ("SELECT spring.jpa.Customer" )
70- .hasKind (INTERNAL )
71- .hasParent (trace .getSpan (0 ))
72- .hasAttributesSatisfyingExactly (
73- satisfies (AttributeKey .stringKey ("hibernate.session_id" ),
74- val -> {
75- sessionId .set (String .valueOf (val ));
76- val .isInstanceOf (String .class );
77- }
78- )),
79- span ->
80- span .hasName ("SELECT test.Customer" )
81- .hasKind (CLIENT )
82- .hasParent (trace .getSpan (1 ))
66+ trace ->
67+ trace .hasSpansSatisfyingExactly (
68+ span ->
69+ span .hasName ("parent" )
70+ .hasKind (INTERNAL )
71+ .hasNoParent ()
72+ .hasAttributes (Attributes .empty ()),
73+ span ->
74+ span .hasName ("SELECT spring.jpa.Customer" )
75+ .hasKind (INTERNAL )
76+ .hasParent (trace .getSpan (0 ))
8377 .hasAttributesSatisfyingExactly (
84- equalTo (DbIncubatingAttributes .DB_SYSTEM ,"hsqldb" ),
85- equalTo (maybeStable (DbIncubatingAttributes .DB_NAME ),"test" ),
86- equalTo (DbIncubatingAttributes .DB_USER , emitStableDatabaseSemconv () ? null : "sa" ),
87- equalTo (DbIncubatingAttributes .DB_CONNECTION_STRING , emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
88- equalTo (maybeStable (DbIncubatingAttributes .DB_STATEMENT ), "select ... from Customer ..." ),
78+ satisfies (
79+ AttributeKey .stringKey ("hibernate.session_id" ),
80+ val -> {
81+ sessionId .set (String .valueOf (val ));
82+ val .isInstanceOf (String .class );
83+ })),
84+ span ->
85+ span .hasName ("SELECT test.Customer" )
86+ .hasKind (CLIENT )
87+ .hasParent (trace .getSpan (1 ))
88+ .hasAttributesSatisfyingExactly (
89+ equalTo (DbIncubatingAttributes .DB_SYSTEM , "hsqldb" ),
90+ equalTo (maybeStable (DbIncubatingAttributes .DB_NAME ), "test" ),
91+ equalTo (
92+ DbIncubatingAttributes .DB_USER ,
93+ emitStableDatabaseSemconv () ? null : "sa" ),
94+ equalTo (
95+ DbIncubatingAttributes .DB_CONNECTION_STRING ,
96+ emitStableDatabaseSemconv () ? null : "hsqldb:mem:" ),
97+ equalTo (
98+ maybeStable (DbIncubatingAttributes .DB_STATEMENT ),
99+ "select ... from Customer ..." ),
89100 equalTo (maybeStable (DbIncubatingAttributes .DB_OPERATION ), "SELECT" ),
90- equalTo (maybeStable (DbIncubatingAttributes .DB_SQL_TABLE ), "Customer" )
91- ),
92- span -> span .hasName ("Transaction.commit" )
93- .hasKind (INTERNAL )
94- .hasParent (trace .getSpan (0 ))
95- .hasAttributesSatisfyingExactly (
96- equalTo (AttributeKey .stringKey ("hibernate.session_id" ),sessionId .get ())
97- )
98- ));
101+ equalTo (maybeStable (DbIncubatingAttributes .DB_SQL_TABLE ), "Customer" )),
102+ span ->
103+ span .hasName ("Transaction.commit" )
104+ .hasKind (INTERNAL )
105+ .hasParent (trace .getSpan (0 ))
106+ .hasAttributesSatisfyingExactly (
107+ equalTo (
108+ AttributeKey .stringKey ("hibernate.session_id" ), sessionId .get ()))));
99109
100110 testing .clearData ();
101111
@@ -107,25 +117,23 @@ public void testCRUD() {
107117 // todo assert
108118 testing .clearData ();
109119
110-
111120 customer .setFirstName ("Bill" );
112121 testing .runWithSpan ("parent" , () -> repo .save (customer ));
113122
114123 assertEquals (customer .getId (), savedId );
115124 String sessionId3 = null ;
116125 // todo assert
117126 testing .clearData ();
118- Customer customer1 = testing .runWithSpan ("parent" ,
119- () -> repo .findByLastName ("Anonymous" ).get (0 ));
120-
127+ Customer customer1 =
128+ testing .runWithSpan ("parent" , () -> repo .findByLastName ("Anonymous" ).get (0 ));
121129
122- assertEquals (savedId , customer1 .getId () );
130+ assertEquals (savedId , customer1 .getId ());
123131 assertEquals ("Bill" , customer1 .getFirstName ());
124132 // todo assert
125133 testing .clearData ();
126134
127135 testing .runWithSpan ("parent" , () -> repo .delete (customer1 ));
128136
129- //todo assert
137+ // todo assert
130138 }
131139}
0 commit comments