Skip to content

Commit

Permalink
add db client metrics for JDBC, vertx, r2dbc, cassandra, add tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger authored Dec 2, 2024
1 parent 965e886 commit 3fd27aa
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.datastax.driver.core.ExecutionInfo;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -40,6 +41,7 @@ public final class CassandraSingletons {
.addAttributesExtractor(
NetworkAttributesExtractor.create(new CassandraNetworkAttributesGetter()))
.addAttributesExtractor(new CassandraAttributesExtractor())
.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 com.datastax.oss.driver.api.core.cql.ExecutionInfo;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -39,6 +40,7 @@ public final class CassandraSingletons {
.addAttributesExtractor(
NetworkAttributesExtractor.create(new CassandraNetworkAttributesGetter()))
.addAttributesExtractor(new CassandraAttributesExtractor())
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -65,6 +66,7 @@ protected Instrumenter<CassandraRequest, ExecutionInfo> createInstrumenter(
.addAttributesExtractor(
NetworkAttributesExtractor.create(new CassandraNetworkAttributesGetter()))
.addAttributesExtractor(new CassandraAttributesExtractor())
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcInstrumenterFactory.createDataSourceInstrumenter;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor;
Expand Down Expand Up @@ -50,6 +51,7 @@ public final class JdbcSingletons {
.addAttributesExtractor(
PeerServiceAttributesExtractor.create(
netAttributesGetter, AgentCommonConfig.get().getPeerServiceResolver()))
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
package io.opentelemetry.javaagent.instrumentation.jdbc.test;

import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
import static io.opentelemetry.instrumentation.testing.junit.db.DbClientMetricsTestUtil.assertDurationMetric;
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_COLLECTION_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAMESPACE;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SQL_TABLE;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -379,6 +383,19 @@ public void testBasicStatement(
equalTo(maybeStable(DB_STATEMENT), sanitizedQuery),
equalTo(maybeStable(DB_OPERATION), "SELECT"),
equalTo(maybeStable(DB_SQL_TABLE), table))));

if (table != null) {
assertDurationMetric(
testing,
"io.opentelemetry.jdbc",
DB_SYSTEM,
DB_COLLECTION_NAME,
DB_NAMESPACE,
DB_OPERATION_NAME);
} else {
assertDurationMetric(
testing, "io.opentelemetry.jdbc", DB_SYSTEM, DB_OPERATION_NAME, DB_NAMESPACE);
}
}

static Stream<Arguments> preparedStatementStream() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -52,6 +53,7 @@ public static Instrumenter<DbRequest, Void> createStatementInstrumenter(
.setStatementSanitizationEnabled(statementSanitizationEnabled)
.build())
.addAttributesExtractor(ServerAttributesExtractor.create(netAttributesGetter))
.addOperationMetrics(DbClientMetrics.get())
.setEnabled(enabled)
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@

package io.opentelemetry.instrumentation.jdbc.datasource;

import static io.opentelemetry.instrumentation.testing.junit.db.DbClientMetricsTestUtil.assertDurationMetric;
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAMESPACE;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection;
Expand Down Expand Up @@ -44,6 +50,15 @@ void buildWithDefaults() throws SQLException {
span ->
span.hasName("SELECT dbname")
.hasAttribute(equalTo(maybeStable(DB_STATEMENT), "SELECT ?;"))));

assertDurationMetric(
testing,
"io.opentelemetry.jdbc",
DB_NAMESPACE,
DB_OPERATION_NAME,
DB_SYSTEM,
SERVER_ADDRESS,
SERVER_PORT);
}

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

package io.opentelemetry.javaagent.instrumentation.jedis.v3_0;

import static io.opentelemetry.instrumentation.testing.junit.db.DbClientMetricsTestUtil.assertDurationMetric;
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
Expand All @@ -14,6 +15,7 @@
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -88,6 +90,16 @@ void setCommand() {
equalTo(NETWORK_TYPE, "ipv4"),
equalTo(NETWORK_PEER_ADDRESS, ip),
satisfies(NETWORK_PEER_PORT, AbstractLongAssert::isNotNegative))));

assertDurationMetric(
testing,
"io.opentelemetry.jedis-3.0",
DB_OPERATION_NAME,
DB_SYSTEM,
SERVER_ADDRESS,
SERVER_PORT,
NETWORK_PEER_ADDRESS,
NETWORK_PEER_PORT);
}

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

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
Expand Down Expand Up @@ -58,6 +59,7 @@ public Instrumenter<DbExecution, Void> build(
.build())
.addAttributesExtractor(ServerAttributesExtractor.create(R2dbcNetAttributesGetter.INSTANCE))
.addAttributesExtractors(additionalExtractors)
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor;
Expand Down Expand Up @@ -47,7 +48,8 @@ public final class VertxSqlClientSingletons {
.addAttributesExtractor(
PeerServiceAttributesExtractor.create(
VertxSqlClientNetAttributesGetter.INSTANCE,
AgentCommonConfig.get().getPeerServiceResolver()));
AgentCommonConfig.get().getPeerServiceResolver()))
.addOperationMetrics(DbClientMetrics.get());

INSTRUMENTER = builder.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.testing.junit.db;

import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;

public class DbClientMetricsTestUtil {

private DbClientMetricsTestUtil() {}

public static void assertDurationMetric(
InstrumentationExtension testing,
String instrumentationName,
AttributeKey<?>... expectedKeys) {
if (!emitStableDatabaseSemconv()) {
return;
}
testing.waitAndAssertMetrics(
instrumentationName,
metrics ->
metrics
.hasName("db.client.operation.duration")
.hasUnit("s")
.hasDescription("Duration of database client operations.")
.hasHistogramSatisfying(
histogram ->
histogram.hasPointsSatisfying(
point ->
point.hasAttributesSatisfying(
attributes ->
assertThat(attributes.asMap())
.containsOnlyKeys(expectedKeys)))));
}
}

0 comments on commit 3fd27aa

Please sign in to comment.