|
8 | 8 | import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
9 | 9 |
|
10 | 10 | import com.datadoghq.agent.instrumentation.Instrumenter; |
| 11 | +import com.datadoghq.trace.DDTags; |
11 | 12 | import com.google.auto.service.AutoService; |
12 | 13 | import io.opentracing.ActiveSpan; |
13 | 14 | import io.opentracing.NoopActiveSpanSource; |
@@ -38,40 +39,43 @@ public static class PreparedStatementAdvice { |
38 | 39 |
|
39 | 40 | @Advice.OnMethodEnter |
40 | 41 | public static ActiveSpan startSpan(@Advice.This final PreparedStatement statement) { |
41 | | - // TODO: Should this happen always instead of just inside an existing tracer? |
42 | | - if (GlobalTracer.get().activeSpan() == null) { |
| 42 | + final String sql = ConnectionInstrumentation.preparedStatements.get(statement); |
| 43 | + final Connection connection; |
| 44 | + try { |
| 45 | + connection = statement.getConnection(); |
| 46 | + } catch (final Throwable e) { |
| 47 | + // Had some problem getting the connection. |
43 | 48 | return NoopActiveSpanSource.NoopActiveSpan.INSTANCE; |
44 | 49 | } |
45 | 50 |
|
46 | | - final String sql = ConnectionInstrumentation.preparedStatements.get(statement); |
| 51 | + final DriverInstrumentation.DBInfo dbInfo = |
| 52 | + DriverInstrumentation.connectionInfo.get(connection); |
47 | 53 |
|
48 | | - final ActiveSpan span = GlobalTracer.get().buildSpan("sql.prepared_statement").startActive(); |
49 | | - Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT); |
50 | | - Tags.COMPONENT.set(span, "java-jdbc"); |
| 54 | + final ActiveSpan span = |
| 55 | + GlobalTracer.get().buildSpan(dbInfo.getType() + ".query").startActive(); |
| 56 | + Tags.DB_TYPE.set(span, dbInfo.getType()); |
51 | 57 | Tags.DB_STATEMENT.set(span, sql); |
52 | | - span.setTag("span.origin.type", statement.getClass().getName()); |
| 58 | + Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT); |
| 59 | + Tags.COMPONENT.set(span, "java-jdbc-prepared_statement"); |
53 | 60 |
|
| 61 | + span.setTag(DDTags.SERVICE_NAME, dbInfo.getType()); |
| 62 | + span.setTag("span.origin.type", statement.getClass().getName()); |
| 63 | + span.setTag("db.jdbc.url", dbInfo.getUrl()); |
54 | 64 | try { |
55 | | - final Connection connection = statement.getConnection(); |
56 | | - final DriverInstrumentation.DBInfo dbInfo = |
57 | | - DriverInstrumentation.connectionInfo.get(connection); |
58 | | - |
59 | | - span.setTag("db.jdbc.url", dbInfo.getUrl()); |
60 | 65 | span.setTag("db.schema", connection.getSchema()); |
| 66 | + } catch (final Throwable e) { |
| 67 | + // Ignore... |
| 68 | + } |
61 | 69 |
|
62 | | - Tags.DB_TYPE.set(span, dbInfo.getType()); |
63 | | - if (dbInfo.getUser() != null) { |
64 | | - Tags.DB_USER.set(span, dbInfo.getUser()); |
65 | | - } |
66 | | - } finally { |
67 | | - return span; |
| 70 | + if (dbInfo.getUser() != null) { |
| 71 | + Tags.DB_USER.set(span, dbInfo.getUser()); |
68 | 72 | } |
| 73 | + return span; |
69 | 74 | } |
70 | 75 |
|
71 | 76 | @Advice.OnMethodExit(onThrowable = Throwable.class) |
72 | 77 | public static void stopSpan( |
73 | | - @Advice.Enter final ActiveSpan activeSpan, |
74 | | - @Advice.Thrown(readOnly = false) final Throwable throwable) { |
| 78 | + @Advice.Enter final ActiveSpan activeSpan, @Advice.Thrown final Throwable throwable) { |
75 | 79 | if (throwable != null) { |
76 | 80 | Tags.ERROR.set(activeSpan, true); |
77 | 81 | activeSpan.log(Collections.singletonMap("error.object", throwable)); |
|
0 commit comments