|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.couchbase.v3_1_6; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldDatabaseSemconv; |
| 9 | +import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv; |
| 10 | +import static io.opentelemetry.semconv.DbAttributes.DB_COLLECTION_NAME; |
| 11 | +import static io.opentelemetry.semconv.DbAttributes.DB_NAMESPACE; |
| 12 | +import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_NAME; |
| 13 | +import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_TEXT; |
| 14 | +import static io.opentelemetry.semconv.DbAttributes.DB_SYSTEM_NAME; |
| 15 | +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS; |
| 16 | +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; |
| 17 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME; |
| 18 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION; |
| 19 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT; |
| 20 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; |
| 21 | + |
| 22 | +import com.couchbase.client.core.cnc.RequestSpan; |
| 23 | +import com.couchbase.client.core.cnc.RequestTracer; |
| 24 | +import com.couchbase.client.core.msg.RequestContext; |
| 25 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 26 | +import io.opentelemetry.api.OpenTelemetry; |
| 27 | +import io.opentelemetry.instrumentation.api.incubator.config.internal.DeclarativeConfigUtil; |
| 28 | +import io.opentelemetry.javaagent.instrumentation.couchbase.v3_1_6.shaded.com.couchbase.client.tracing.opentelemetry.OpenTelemetryRequestTracer; |
| 29 | +import java.time.Duration; |
| 30 | +import java.time.Instant; |
| 31 | +import reactor.core.publisher.Mono; |
| 32 | + |
| 33 | +public final class CouchbaseRequestTracer implements RequestTracer { |
| 34 | + |
| 35 | + private static final String DB_COUCHBASE_COLLECTION = "db.couchbase.collection"; |
| 36 | + private static final String NET_PEER_NAME = "net.peer.name"; |
| 37 | + private static final String NET_PEER_PORT = "net.peer.port"; |
| 38 | + |
| 39 | + private static final boolean captureExperimentalAttributes = |
| 40 | + DeclarativeConfigUtil.getInstrumentationConfig(GlobalOpenTelemetry.get(), "couchbase") |
| 41 | + .getBoolean("experimental_span_attributes/development", false); |
| 42 | + |
| 43 | + private final RequestTracer delegate; |
| 44 | + |
| 45 | + public static RequestTracer create(OpenTelemetry openTelemetry) { |
| 46 | + return new CouchbaseRequestTracer(OpenTelemetryRequestTracer.wrap(openTelemetry)); |
| 47 | + } |
| 48 | + |
| 49 | + private CouchbaseRequestTracer(RequestTracer delegate) { |
| 50 | + this.delegate = delegate; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public RequestSpan requestSpan(String name, RequestSpan parent) { |
| 55 | + RequestSpan unwrappedParent = parent; |
| 56 | + if (parent instanceof TranslatingRequestSpan) { |
| 57 | + unwrappedParent = ((TranslatingRequestSpan) parent).delegate; |
| 58 | + } |
| 59 | + return new TranslatingRequestSpan(delegate.requestSpan(name, unwrappedParent)); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Mono<Void> start() { |
| 64 | + return delegate.start(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Mono<Void> stop(Duration timeout) { |
| 69 | + return delegate.stop(timeout); |
| 70 | + } |
| 71 | + |
| 72 | + private static final class TranslatingRequestSpan implements RequestSpan { |
| 73 | + |
| 74 | + private final RequestSpan delegate; |
| 75 | + |
| 76 | + private TranslatingRequestSpan(RequestSpan delegate) { |
| 77 | + this.delegate = delegate; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void attribute(String key, String value) { |
| 82 | + if (emitStableDatabaseSemconv()) { |
| 83 | + String stableKey = stableKey(key); |
| 84 | + if (stableKey != null) { |
| 85 | + delegate.attribute(stableKey, value); |
| 86 | + } else if (captureExperimentalAttribute(key)) { |
| 87 | + delegate.attribute(key, value); |
| 88 | + } |
| 89 | + } |
| 90 | + if (emitOldDatabaseSemconv()) { |
| 91 | + delegate.attribute(key, value); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void attribute(String key, boolean value) { |
| 97 | + if (emitStableDatabaseSemconv()) { |
| 98 | + String stableKey = stableKey(key); |
| 99 | + if (stableKey != null) { |
| 100 | + delegate.attribute(stableKey, value); |
| 101 | + } else if (captureExperimentalAttribute(key)) { |
| 102 | + delegate.attribute(key, value); |
| 103 | + } |
| 104 | + } |
| 105 | + if (emitOldDatabaseSemconv()) { |
| 106 | + delegate.attribute(key, value); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void attribute(String key, long value) { |
| 112 | + if (emitStableDatabaseSemconv()) { |
| 113 | + String stableKey = stableKey(key); |
| 114 | + if (stableKey != null) { |
| 115 | + delegate.attribute(stableKey, value); |
| 116 | + } else if (captureExperimentalAttribute(key)) { |
| 117 | + delegate.attribute(key, value); |
| 118 | + } |
| 119 | + } |
| 120 | + if (emitOldDatabaseSemconv()) { |
| 121 | + delegate.attribute(key, value); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void event(String name, Instant timestamp) { |
| 127 | + delegate.event(name, timestamp); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void end() { |
| 132 | + delegate.end(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void requestContext(RequestContext requestContext) { |
| 137 | + delegate.requestContext(requestContext); |
| 138 | + } |
| 139 | + |
| 140 | + @SuppressWarnings("deprecation") // using deprecated semconv |
| 141 | + private static String stableKey(String key) { |
| 142 | + if (key.equals(DB_COUCHBASE_COLLECTION)) { |
| 143 | + return DB_COLLECTION_NAME.getKey(); |
| 144 | + } |
| 145 | + if (key.equals(DB_NAME.getKey())) { |
| 146 | + return DB_NAMESPACE.getKey(); |
| 147 | + } |
| 148 | + if (key.equals(DB_OPERATION.getKey())) { |
| 149 | + return DB_OPERATION_NAME.getKey(); |
| 150 | + } |
| 151 | + if (key.equals(DB_STATEMENT.getKey())) { |
| 152 | + return DB_QUERY_TEXT.getKey(); |
| 153 | + } |
| 154 | + if (key.equals(DB_SYSTEM.getKey())) { |
| 155 | + return DB_SYSTEM_NAME.getKey(); |
| 156 | + } |
| 157 | + if (key.equals(NET_PEER_NAME)) { |
| 158 | + return NETWORK_PEER_ADDRESS.getKey(); |
| 159 | + } |
| 160 | + if (key.equals(NET_PEER_PORT)) { |
| 161 | + return NETWORK_PEER_PORT.getKey(); |
| 162 | + } |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + private static boolean captureExperimentalAttribute(String key) { |
| 167 | + return captureExperimentalAttributes && key.startsWith("db.couchbase."); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments