|
12 | 12 | import io.opentelemetry.context.Context;
|
13 | 13 | import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
|
14 | 14 | import io.opentelemetry.instrumentation.api.internal.SemconvStability;
|
| 15 | +import io.opentelemetry.instrumentation.api.internal.SpanKey; |
| 16 | +import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider; |
| 17 | +import javax.annotation.Nullable; |
15 | 18 |
|
16 | 19 | /**
|
17 | 20 | * Extractor of <a
|
|
22 | 25 | * attribute extraction from request/response objects.
|
23 | 26 | */
|
24 | 27 | public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
|
25 |
| - extends DbClientCommonAttributesExtractor< |
26 |
| - REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST>> { |
| 28 | + implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider { |
27 | 29 |
|
28 | 30 | // copied from DbIncubatingAttributes
|
| 31 | + private static final AttributeKey<String> DB_NAME = AttributeKey.stringKey("db.name"); |
| 32 | + private static final AttributeKey<String> DB_NAMESPACE = AttributeKey.stringKey("db.namespace"); |
| 33 | + private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system"); |
| 34 | + private static final AttributeKey<String> DB_USER = AttributeKey.stringKey("db.user"); |
| 35 | + private static final AttributeKey<String> DB_CONNECTION_STRING = |
| 36 | + AttributeKey.stringKey("db.connection_string"); |
29 | 37 | private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
|
30 | 38 | private static final AttributeKey<String> DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text");
|
31 | 39 |
|
32 | 40 | private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
|
33 | 41 | private static final AttributeKey<String> DB_OPERATION_NAME =
|
34 | 42 | AttributeKey.stringKey("db.operation.name");
|
35 | 43 |
|
| 44 | + private final DbClientAttributesGetter<REQUEST> getter; |
| 45 | + |
36 | 46 | /** Creates the database client attributes extractor with default configuration. */
|
37 | 47 | public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
|
38 | 48 | DbClientAttributesGetter<REQUEST> getter) {
|
39 | 49 | return new DbClientAttributesExtractor<>(getter);
|
40 | 50 | }
|
41 | 51 |
|
42 | 52 | DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST> getter) {
|
43 |
| - super(getter); |
| 53 | + this.getter = getter; |
44 | 54 | }
|
45 | 55 |
|
46 | 56 | @Override
|
| 57 | + @SuppressWarnings("deprecation") // using deprecated semconv |
47 | 58 | public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
|
48 |
| - super.onStart(attributes, parentContext, request); |
49 |
| - |
| 59 | + internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request)); |
50 | 60 | if (SemconvStability.emitStableDatabaseSemconv()) {
|
| 61 | + internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request)); |
51 | 62 | internalSet(attributes, DB_QUERY_TEXT, getter.getDbQueryText(request));
|
52 | 63 | internalSet(attributes, DB_OPERATION_NAME, getter.getDbOperationName(request));
|
53 | 64 | }
|
54 | 65 | if (SemconvStability.emitOldDatabaseSemconv()) {
|
| 66 | + internalSet(attributes, DB_USER, getter.getUser(request)); |
| 67 | + internalSet(attributes, DB_NAME, getter.getDbNamespace(request)); |
| 68 | + internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request)); |
55 | 69 | internalSet(attributes, DB_STATEMENT, getter.getDbQueryText(request));
|
56 | 70 | internalSet(attributes, DB_OPERATION, getter.getDbOperationName(request));
|
57 | 71 | }
|
58 | 72 | }
|
| 73 | + |
| 74 | + @Override |
| 75 | + public void onEnd( |
| 76 | + AttributesBuilder attributes, |
| 77 | + Context context, |
| 78 | + REQUEST request, |
| 79 | + @Nullable RESPONSE response, |
| 80 | + @Nullable Throwable error) {} |
| 81 | + |
| 82 | + /** |
| 83 | + * This method is internal and is hence not for public use. Its API is unstable and can change at |
| 84 | + * any time. |
| 85 | + */ |
| 86 | + @Override |
| 87 | + public SpanKey internalGetSpanKey() { |
| 88 | + return SpanKey.DB_CLIENT; |
| 89 | + } |
59 | 90 | }
|
0 commit comments