Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add db client metrics #12806

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
// copied from DbIncubatingAttributes
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
private static final AttributeKey<String> DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text");

private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
private static final AttributeKey<String> DB_OPERATION_NAME =
AttributeKey.stringKey("db.operation.name");
static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
static final AttributeKey<String> DB_OPERATION_NAME = AttributeKey.stringKey("db.operation.name");
static final AttributeKey<Long> DB_RESPONSE_STATUS_CODE =
AttributeKey.longKey("db.response.status_code");

/** Creates the database client attributes extractor with default configuration. */
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ abstract class DbClientCommonAttributesExtractor<

// copied from DbIncubatingAttributes
private static final AttributeKey<String> DB_NAME = AttributeKey.stringKey("db.name");
private static final AttributeKey<String> DB_NAMESPACE = AttributeKey.stringKey("db.namespace");
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
static final AttributeKey<String> DB_NAMESPACE = AttributeKey.stringKey("db.namespace");
static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
private static final AttributeKey<String> DB_USER = AttributeKey.stringKey("db.user");
private static final AttributeKey<String> DB_CONNECTION_STRING =
AttributeKey.stringKey("db.connection_string");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics;
import io.opentelemetry.instrumentation.api.internal.OperationMetricsUtil;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
* {@link OperationListener} which keeps track of <a
* href="https://opentelemetry.io/docs/specs/semconv/database/database-metrics/#metric-dbclientoperationduration">Database
* client metrics</a>.
*
* @since 2.11.0
*/
public final class DbClientMetrics implements OperationListener {

private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1);

private static final ContextKey<State> DB_CLIENT_OPERATION_METRICS_STATE =
ContextKey.named("db-client-metrics-state");

private static final Logger logger = Logger.getLogger(DbClientMetrics.class.getName());

/**
* Returns an {@link OperationMetrics} instance which can be used to enable recording of {@link
* DbClientMetrics}.
*
* @see InstrumenterBuilder#addOperationMetrics(OperationMetrics)
*/
public static OperationMetrics get() {
if (SemconvStability.emitStableDatabaseSemconv()) {
return OperationMetricsUtil.create("database client", DbClientMetrics::new);
}
return meter -> OperationMetricsUtil.NOOP_OPERATION_LISTENER;
}

private final DoubleHistogram duration;

private DbClientMetrics(Meter meter) {
DoubleHistogramBuilder stableDurationBuilder =
meter
.histogramBuilder("db.client.operation.duration")
.setUnit("s")
.setDescription("Duration of database client operations.")
.setExplicitBucketBoundariesAdvice(DbClientMetricsAdvice.DURATION_SECONDS_BUCKETS);
DbClientMetricsAdvice.applyClientDurationAdvice(stableDurationBuilder);
duration = stableDurationBuilder.build();
}

@Override
public Context onStart(Context context, Attributes startAttributes, long startNanos) {
return context.with(
DB_CLIENT_OPERATION_METRICS_STATE,
new AutoValue_DbClientMetrics_State(startAttributes, startNanos));
}

@Override
public void onEnd(Context context, Attributes endAttributes, long endNanos) {
State state = context.get(DB_CLIENT_OPERATION_METRICS_STATE);
if (state == null) {
logger.log(
FINE,
"No state present when ending context {0}. Cannot record database operation metrics.",
context);
return;
}

Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build();

duration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context);
}

@AutoValue
abstract static class State {

abstract Attributes startAttributes();

abstract long startTimeNanos();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;

import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogramBuilder;
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.semconv.ErrorAttributes;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
import java.util.List;

final class DbClientMetricsAdvice {

static final List<Double> DURATION_SECONDS_BUCKETS =
unmodifiableList(
asList(0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0));

static void applyClientDurationAdvice(DoubleHistogramBuilder builder) {
if (!(builder instanceof ExtendedDoubleHistogramBuilder)) {
return;
}
((ExtendedDoubleHistogramBuilder) builder)
.setAttributesAdvice(
asList(
DbClientCommonAttributesExtractor.DB_SYSTEM,
SqlClientAttributesExtractor.DB_COLLECTION_NAME,
DbClientCommonAttributesExtractor.DB_NAMESPACE,
DbClientAttributesExtractor.DB_OPERATION_NAME,
// will be implemented in
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12804
DbClientAttributesExtractor.DB_RESPONSE_STATUS_CODE,
// will be implemented in
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12804
ErrorAttributes.ERROR_TYPE,
NetworkAttributes.NETWORK_PEER_ADDRESS,
NetworkAttributes.NETWORK_PEER_PORT,
ServerAttributes.SERVER_ADDRESS,
ServerAttributes.SERVER_PORT));
}

private DbClientMetricsAdvice() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
AttributeKey.stringKey("db.operation.name");
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
private static final AttributeKey<String> DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text");
private static final AttributeKey<String> DB_COLLECTION_NAME =
static final AttributeKey<String> DB_COLLECTION_NAME =
AttributeKey.stringKey("db.collection.name");

/** Creates the SQL client attributes extractor with default configuration. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.semconv.ErrorAttributes;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class DbClientMetricsTest {

static final double[] DURATION_BUCKETS =
DbClientMetricsAdvice.DURATION_SECONDS_BUCKETS.stream().mapToDouble(d -> d).toArray();

@Test
void collectsMetrics() {
assumeTrue(emitStableDatabaseSemconv());

InMemoryMetricReader metricReader = InMemoryMetricReader.create();
SdkMeterProvider meterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader).build();

OperationListener listener = DbClientMetrics.get().create(meterProvider.get("test"));

Attributes operationAttributes =
Attributes.builder()
.put(DbClientCommonAttributesExtractor.DB_SYSTEM, "myDb")
.put(SqlClientAttributesExtractor.DB_COLLECTION_NAME, "table")
.put(DbClientCommonAttributesExtractor.DB_NAMESPACE, "potatoes")
.put(DbClientAttributesExtractor.DB_OPERATION_NAME, "SELECT")
.put(ServerAttributes.SERVER_ADDRESS, "localhost")
.put(ServerAttributes.SERVER_PORT, 1234)
.build();

Attributes responseAttributes =
Attributes.builder()
.put(DbClientAttributesExtractor.DB_RESPONSE_STATUS_CODE, 200)
.put(ErrorAttributes.ERROR_TYPE, "400")
.put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4")
.put(NetworkAttributes.NETWORK_PEER_PORT, 8080)
.build();

Context parent =
Context.root()
.with(
Span.wrap(
SpanContext.create(
"ff01020304050600ff0a0b0c0d0e0f00",
"090a0b0c0d0e0f00",
TraceFlags.getSampled(),
TraceState.getDefault())));

Context context1 = listener.onStart(parent, operationAttributes, nanos(100));

assertThat(metricReader.collectAllMetrics()).isEmpty();

listener.onEnd(context1, responseAttributes, nanos(250));

assertThat(metricReader.collectAllMetrics())
.satisfiesExactlyInAnyOrder(
metric ->
assertThat(metric)
.hasName("db.client.operation.duration")
.hasUnit("s")
.hasDescription("Duration of database client operations.")
.hasHistogramSatisfying(
histogram ->
histogram.hasPointsSatisfying(
point ->
point
.hasSum(0.15 /* seconds */)
.hasAttributesSatisfying(
equalTo(
DbClientCommonAttributesExtractor.DB_SYSTEM,
"myDb"),
equalTo(
DbClientCommonAttributesExtractor.DB_NAMESPACE,
"potatoes"),
equalTo(
DbClientAttributesExtractor.DB_OPERATION_NAME,
"SELECT"),
equalTo(
SqlClientAttributesExtractor.DB_COLLECTION_NAME,
"table"),
equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"),
equalTo(ServerAttributes.SERVER_PORT, 1234),
equalTo(
DbClientAttributesExtractor.DB_RESPONSE_STATUS_CODE,
200),
equalTo(ErrorAttributes.ERROR_TYPE, "400"),
equalTo(
NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, 8080))
.hasExemplarsSatisfying(
exemplar ->
exemplar
.hasTraceId("ff01020304050600ff0a0b0c0d0e0f00")
.hasSpanId("090a0b0c0d0e0f00"))
.hasBucketBoundaries(DURATION_BUCKETS))));
}

private static long nanos(int millis) {
return TimeUnit.MILLISECONDS.toNanos(millis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class OperationMetricsUtil {
private static final Logger logger = Logger.getLogger(OperationMetricsUtil.class.getName());
private static final OperationListener NOOP_OPERATION_LISTENER =
public static final OperationListener NOOP_OPERATION_LISTENER =
new OperationListener() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
Expand All @@ -27,6 +28,7 @@ public final class ClickHouseSingletons {
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(
ServerAttributesExtractor.create(new ClickHouseNetworkAttributesGetter()))
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
Expand Down Expand Up @@ -34,7 +35,8 @@ public final class CouchbaseSingletons {
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter))
.addContextCustomizer(
(context, couchbaseRequest, startAttributes) ->
CouchbaseRequestInfo.init(context, couchbaseRequest));
CouchbaseRequestInfo.init(context, couchbaseRequest))
.addOperationMetrics(DbClientMetrics.get());

if (AgentInstrumentationConfig.get()
.getBoolean("otel.instrumentation.couchbase.experimental-span-attributes", false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
Expand Down Expand Up @@ -47,6 +48,7 @@ public static Instrumenter<ElasticsearchRestRequest, Response> create(
.addAttributesExtractor(DbClientAttributesExtractor.create(dbClientAttributesGetter))
.addAttributesExtractor(esClientAtrributesExtractor)
.addAttributesExtractors(attributesExtractors)
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -34,7 +35,8 @@ public static Instrumenter<ElasticTransportRequest, ActionResponse> create(
instrumentationName,
DbClientSpanNameExtractor.create(dbClientAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbClientAttributesGetter))
.addAttributesExtractor(netAttributesExtractor);
.addAttributesExtractor(netAttributesExtractor)
.addOperationMetrics(DbClientMetrics.get());

if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) {
instrumenterBuilder.addAttributesExtractor(experimentalAttributesExtractor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
Expand All @@ -25,6 +26,7 @@ public final class GeodeSingletons {
INSTRUMENTATION_NAME,
DbClientSpanNameExtractor.create(dbClientAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbClientAttributesGetter))
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Loading