From 44bea8dacfe864eab73188d44384c275b6000a81 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Wed, 15 Jan 2025 18:34:46 +0100 Subject: [PATCH] add db client metrics for dynamo db (#13033) --- .../aws-sdk-1.11/javaagent/build.gradle.kts | 8 ++++++++ .../aws-sdk/aws-sdk-1.11/library/build.gradle.kts | 10 ++++++++++ .../awssdk/v1_11/AwsSdkInstrumenterFactory.java | 15 ++++++++------- .../awssdk/v1_11/AbstractDynamoDbClientTest.java | 6 ++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/build.gradle.kts b/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/build.gradle.kts index a550982611a2..cfce0eafe4df 100644 --- a/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/build.gradle.kts +++ b/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/build.gradle.kts @@ -140,6 +140,14 @@ tasks { } } + val testStableSemconv by registering(Test::class) { + jvmArgs("-Dotel.semconv-stability.opt-in=database") + } + + check { + dependsOn(testStableSemconv) + } + test { usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service) } diff --git a/instrumentation/aws-sdk/aws-sdk-1.11/library/build.gradle.kts b/instrumentation/aws-sdk/aws-sdk-1.11/library/build.gradle.kts index 37329705b800..639cbd4d99a2 100644 --- a/instrumentation/aws-sdk/aws-sdk-1.11/library/build.gradle.kts +++ b/instrumentation/aws-sdk/aws-sdk-1.11/library/build.gradle.kts @@ -34,3 +34,13 @@ if (!(findProperty("testLatestDeps") as Boolean)) { } } } + +tasks { + val testStableSemconv by registering(Test::class) { + jvmArgs("-Dotel.semconv-stability.opt-in=database") + } + + check { + dependsOn(testStableSemconv) + } +} diff --git a/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkInstrumenterFactory.java b/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkInstrumenterFactory.java index e2ca18d98715..0db35cef3f1b 100644 --- a/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkInstrumenterFactory.java +++ b/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkInstrumenterFactory.java @@ -14,6 +14,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; +import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientMetrics; import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessageOperation; import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessagingAttributesExtractor; import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessagingAttributesGetter; @@ -27,6 +28,7 @@ import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractor; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.annotation.Nullable; @@ -189,13 +191,12 @@ Instrumenter, Response> producerInstrumenter() { Instrumenter, Response> dynamoDbInstrumenter() { DynamoDbAttributesExtractor dynamoDbAttributesExtractor = new DynamoDbAttributesExtractor(); - return createInstrumenter( - openTelemetry, - spanName, - SpanKindExtractor.alwaysClient(), - attributesExtractors(), - singletonList(dynamoDbAttributesExtractor), - true); + return Instrumenter., Response>builder( + openTelemetry, INSTRUMENTATION_NAME, spanName) + .addAttributesExtractors(attributesExtractors()) + .addAttributesExtractors(Collections.singletonList(dynamoDbAttributesExtractor)) + .addOperationMetrics(DbClientMetrics.get()) + .buildInstrumenter(SpanKindExtractor.alwaysClient()); } private static Instrumenter createInstrumenter( diff --git a/instrumentation/aws-sdk/aws-sdk-1.11/testing/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AbstractDynamoDbClientTest.java b/instrumentation/aws-sdk/aws-sdk-1.11/testing/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AbstractDynamoDbClientTest.java index 441a4a3a0b59..683da33f1b5c 100644 --- a/instrumentation/aws-sdk/aws-sdk-1.11/testing/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AbstractDynamoDbClientTest.java +++ b/instrumentation/aws-sdk/aws-sdk-1.11/testing/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AbstractDynamoDbClientTest.java @@ -6,7 +6,10 @@ package io.opentelemetry.instrumentation.awssdk.v1_11; import static io.opentelemetry.api.common.AttributeKey.stringKey; +import static io.opentelemetry.instrumentation.testing.junit.db.DbClientMetricsTestUtil.assertDurationMetric; 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.AwsIncubatingAttributes.AWS_DYNAMODB_TABLE_NAMES; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemIncubatingValues.DYNAMODB; @@ -52,5 +55,8 @@ public void sendRequestWithMockedResponse() throws Exception { Object response = client.createTable(new CreateTableRequest("sometable", null)); assertRequestWithMockedResponse( response, client, "DynamoDBv2", "CreateTable", "POST", additionalAttributes); + + assertDurationMetric( + testing(), "io.opentelemetry.aws-sdk-1.11", DB_SYSTEM, SERVER_ADDRESS, SERVER_PORT); } }