5151import static net .bytebuddy .matcher .ElementMatchers .named ;
5252import static net .bytebuddy .matcher .ElementMatchers .not ;
5353import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
54+ import static net .bytebuddy .matcher .ElementMatchers .takesArguments ;
5455
5556/**
5657 * Creates spans for JDBC calls
5758 */
58- public class StatementInstrumentation extends ElasticApmInstrumentation {
59+ public abstract class StatementInstrumentation extends ElasticApmInstrumentation {
5960
61+ @ SuppressWarnings ("WeakerAccess" )
6062 @ Nullable
6163 @ VisibleForAdvice
6264 public static HelperClassManager <JdbcHelper > jdbcHelperManager ;
6365
64- public StatementInstrumentation (ElasticApmTracer tracer ) {
65- jdbcHelperManager = HelperClassManager .ForSingleClassLoader .of (tracer , "co.elastic.apm.agent.jdbc.helper.JdbcHelperImpl" ,
66+ private final ElementMatcher <? super MethodDescription > methodMatcher ;
67+
68+ StatementInstrumentation (ElasticApmTracer tracer , ElementMatcher <? super MethodDescription > methodMatcher ) {
69+ this .methodMatcher = methodMatcher ;
70+ jdbcHelperManager = HelperClassManager .ForSingleClassLoader .of (tracer ,
71+ "co.elastic.apm.agent.jdbc.helper.JdbcHelperImpl" ,
6672 "co.elastic.apm.agent.jdbc.helper.JdbcHelperImpl$1" ,
6773 "co.elastic.apm.agent.jdbc.helper.JdbcHelperImpl$ConnectionMetaData" );
6874 }
6975
70- @ Nullable
71- @ VisibleForAdvice
72- @ Advice .OnMethodEnter (suppress = Throwable .class )
73- public static Span onBeforeExecute (@ Advice .This Statement statement , @ Advice .Argument (0 ) String sql ) throws SQLException {
74- if (tracer != null && jdbcHelperManager != null ) {
75- JdbcHelper helperImpl = jdbcHelperManager .getForClassLoaderOfClass (Statement .class );
76- if (helperImpl != null ) {
77- return helperImpl .createJdbcSpan (sql , statement .getConnection (), tracer .getActive (), false );
78- }
79- }
80- return null ;
81- }
82-
83- @ VisibleForAdvice
84- @ Advice .OnMethodExit (suppress = Throwable .class , onThrowable = Throwable .class )
85- public static void onAfterExecute (@ Advice .Enter @ Nullable Span span , @ Advice .Thrown Throwable t ) {
86- if (span != null ) {
87- span .captureException (t )
88- .deactivate ()
89- .end ();
90- }
91- }
92-
9376 @ Override
9477 public ElementMatcher <? super NamedElement > getTypeMatcherPreFilter () {
9578 return nameContains ("Statement" );
@@ -103,13 +86,98 @@ public ElementMatcher<? super TypeDescription> getTypeMatcher() {
10386
10487 @ Override
10588 public ElementMatcher <? super MethodDescription > getMethodMatcher () {
106- return nameStartsWith ("execute" )
107- .and (takesArgument (0 , String .class ))
108- .and (isPublic ());
89+ return methodMatcher ;
10990 }
11091
11192 @ Override
11293 public Collection <String > getInstrumentationGroupNames () {
11394 return Collections .singleton (JDBC_INSTRUMENTATION_GROUP );
11495 }
96+
97+ public static class ExecuteWithQueryInstrumentation extends StatementInstrumentation {
98+
99+ public ExecuteWithQueryInstrumentation (ElasticApmTracer tracer ) {
100+ super (tracer ,
101+ nameStartsWith ("execute" )
102+ .and (takesArgument (0 , String .class ))
103+ .and (isPublic ())
104+ );
105+ }
106+
107+ @ Nullable
108+ @ VisibleForAdvice
109+ @ Advice .OnMethodEnter (suppress = Throwable .class )
110+ public static Span onBeforeExecute (@ Advice .This Statement statement , @ Advice .Argument (0 ) String sql ) throws SQLException {
111+ if (tracer != null && jdbcHelperManager != null ) {
112+ JdbcHelper helperImpl = jdbcHelperManager .getForClassLoaderOfClass (Statement .class );
113+ if (helperImpl != null ) {
114+ return helperImpl .createJdbcSpan (sql , statement .getConnection (), tracer .getActive (), false );
115+ }
116+ }
117+ return null ;
118+ }
119+
120+ @ VisibleForAdvice
121+ @ Advice .OnMethodExit (suppress = Throwable .class , onThrowable = Throwable .class )
122+ public static void onAfterExecute (@ Advice .Enter @ Nullable Span span , @ Advice .Thrown Throwable t ) {
123+ if (span != null ) {
124+ span .captureException (t )
125+ .deactivate ()
126+ .end ();
127+ }
128+ }
129+ }
130+
131+ public static class AddBatchInstrumentation extends StatementInstrumentation {
132+
133+ public AddBatchInstrumentation (ElasticApmTracer tracer ) {
134+ super (tracer ,
135+ nameStartsWith ("addBatch" )
136+ .and (takesArgument (0 , String .class ))
137+ .and (isPublic ())
138+ );
139+ }
140+
141+ @ Advice .OnMethodEnter (suppress = Throwable .class )
142+ public static void storeSql (@ Advice .This Statement statement , @ Advice .Argument (0 ) String sql ) {
143+ if (jdbcHelperManager != null ) {
144+ JdbcHelper helperImpl = jdbcHelperManager .getForClassLoaderOfClass (Statement .class );
145+ if (helperImpl != null ) {
146+ helperImpl .mapStatementToSql (statement , sql );
147+ }
148+ }
149+ }
150+ }
151+
152+ public static class ExecuteWithoutQueryInstrumentation extends StatementInstrumentation {
153+ public ExecuteWithoutQueryInstrumentation (ElasticApmTracer tracer ) {
154+ super (tracer ,
155+ nameStartsWith ("execute" )
156+ .and (takesArguments (0 ))
157+ .and (isPublic ())
158+ );
159+ }
160+
161+ @ Nullable
162+ @ Advice .OnMethodEnter (suppress = Throwable .class )
163+ public static Span onBeforeExecute (@ Advice .This Statement statement ) throws SQLException {
164+ if (tracer != null && jdbcHelperManager != null ) {
165+ JdbcHelper helperImpl = jdbcHelperManager .getForClassLoaderOfClass (Statement .class );
166+ if (helperImpl != null ) {
167+ final @ Nullable String sql = helperImpl .retrieveSqlForStatement (statement );
168+ return helperImpl .createJdbcSpan (sql , statement .getConnection (), tracer .getActive (), true );
169+ }
170+ }
171+ return null ;
172+ }
173+
174+ @ Advice .OnMethodExit (suppress = Throwable .class , onThrowable = Throwable .class )
175+ public static void onAfterExecute (@ Advice .Enter @ Nullable Span span , @ Advice .Thrown Throwable t ) {
176+ if (span != null ) {
177+ span .captureException (t )
178+ .deactivate ()
179+ .end ();
180+ }
181+ }
182+ }
115183}
0 commit comments