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 for aws sdk2 dynamodb #13283

Merged
merged 1 commit into from
Feb 13, 2025
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,8 +28,8 @@
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 java.util.function.Consumer;
import javax.annotation.Nullable;

final class AwsSdkInstrumenterFactory {
Expand Down Expand Up @@ -189,14 +189,16 @@ Instrumenter<Request<?>, Response<?>> producerInstrumenter() {
}

Instrumenter<Request<?>, Response<?>> dynamoDbInstrumenter() {
DynamoDbAttributesExtractor dynamoDbAttributesExtractor = new DynamoDbAttributesExtractor();

return Instrumenter.<Request<?>, Response<?>>builder(
openTelemetry, INSTRUMENTATION_NAME, spanName)
.addAttributesExtractors(attributesExtractors())
.addAttributesExtractors(Collections.singletonList(dynamoDbAttributesExtractor))
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
return createInstrumenter(
openTelemetry,
spanName,
SpanKindExtractor.alwaysClient(),
attributesExtractors(),
builder ->
builder
.addAttributesExtractor(new DynamoDbAttributesExtractor())
.addOperationMetrics(DbClientMetrics.get()),
true);
}

private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrumenter(
Expand All @@ -206,11 +208,29 @@ private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrum
List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributeExtractors,
List<AttributesExtractor<REQUEST, RESPONSE>> additionalAttributeExtractors,
boolean enabled) {
return Instrumenter.<REQUEST, RESPONSE>builder(
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.addAttributesExtractors(attributeExtractors)
.addAttributesExtractors(additionalAttributeExtractors)
.setEnabled(enabled)
.buildInstrumenter(spanKindExtractor);
return createInstrumenter(
openTelemetry,
spanNameExtractor,
spanKindExtractor,
attributeExtractors,
builder -> builder.addAttributesExtractors(additionalAttributeExtractors),
enabled);
}

private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrumenter(
OpenTelemetry openTelemetry,
SpanNameExtractor<REQUEST> spanNameExtractor,
SpanKindExtractor<REQUEST> spanKindExtractor,
List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributeExtractors,
Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> customizer,
boolean enabled) {
InstrumenterBuilder<REQUEST, RESPONSE> builder =
Instrumenter.<REQUEST, RESPONSE>builder(
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.addAttributesExtractors(attributeExtractors)
.setEnabled(enabled);
customizer.accept(builder);

return builder.buildInstrumenter(spanKindExtractor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import java.util.List;
import javax.annotation.Nullable;

public class DynamoDbAttributesExtractor implements AttributesExtractor<Request<?>, Response<?>> {
class DynamoDbAttributesExtractor implements AttributesExtractor<Request<?>, Response<?>> {

// copied from DbIncubatingAttributes
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
private static final AttributeKey<String> DB_OPERATION_NAME =
AttributeKey.stringKey("db.operation.name");
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
private static final AttributeKey<String> DB_SYSTEM_NAME =
AttributeKey.stringKey("db.system.name");
Expand All @@ -36,14 +39,35 @@ public class DynamoDbAttributesExtractor implements AttributesExtractor<Request<
public void onStart(AttributesBuilder attributes, Context parentContext, Request<?> request) {
if (SemconvStability.emitStableDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_SYSTEM_NAME, AWS_DYNAMODB);
} else {
}
if (SemconvStability.emitOldDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_SYSTEM, DYNAMODB);
}

String operation = getOperationName(request.getOriginalRequest());
if (operation != null) {
if (SemconvStability.emitStableDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_OPERATION_NAME, operation);
}
if (SemconvStability.emitOldDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_OPERATION, operation);
}
}

String tableName = RequestAccess.getTableName(request.getOriginalRequest());
AttributesExtractorUtil.internalSet(
attributes, AWS_DYNAMODB_TABLE_NAMES, Collections.singletonList(tableName));
}

private static String getOperationName(Object request) {
String name = request.getClass().getSimpleName();
if (!name.endsWith("Request")) {
return null;
}

return name.substring(0, name.length() - "Request".length());
}

@Override
public void onEnd(
AttributesBuilder attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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_OPERATION;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM_NAME;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemNameIncubatingValues.AWS_DYNAMODB;
Expand Down Expand Up @@ -59,13 +61,19 @@ public void sendRequestWithMockedResponse() throws Exception {
SemconvStability.emitStableDatabaseSemconv()
? AWS_DYNAMODB
: DbIncubatingAttributes.DbSystemIncubatingValues.DYNAMODB),
equalTo(maybeStable(DB_OPERATION), "CreateTable"),
equalTo(AWS_DYNAMODB_TABLE_NAMES, singletonList("sometable")));

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_NAME, SERVER_ADDRESS, SERVER_PORT);
testing(),
"io.opentelemetry.aws-sdk-1.11",
DB_SYSTEM_NAME,
DB_OPERATION_NAME,
SERVER_ADDRESS,
SERVER_PORT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static AwsSdkTelemetryBuilder builder(OpenTelemetry openTelemetry) {
private final Instrumenter<SqsReceiveRequest, Response> consumerReceiveInstrumenter;
private final Instrumenter<SqsProcessRequest, Response> consumerProcessInstrumenter;
private final Instrumenter<ExecutionAttributes, Response> producerInstrumenter;
private final Instrumenter<ExecutionAttributes, Response> dynamoDbInstrumenter;
private final boolean captureExperimentalSpanAttributes;
@Nullable private final TextMapPropagator messagingPropagator;
private final boolean useXrayPropagator;
Expand Down Expand Up @@ -84,6 +85,7 @@ public static AwsSdkTelemetryBuilder builder(OpenTelemetry openTelemetry) {
this.consumerReceiveInstrumenter = instrumenterFactory.consumerReceiveInstrumenter();
this.consumerProcessInstrumenter = instrumenterFactory.consumerProcessInstrumenter();
this.producerInstrumenter = instrumenterFactory.producerInstrumenter();
this.dynamoDbInstrumenter = instrumenterFactory.dynamoDbInstrumenter();
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
this.recordIndividualHttpError = recordIndividualHttpError;
}
Expand All @@ -98,6 +100,7 @@ public ExecutionInterceptor newExecutionInterceptor() {
consumerReceiveInstrumenter,
consumerProcessInstrumenter,
producerInstrumenter,
dynamoDbInstrumenter,
captureExperimentalSpanAttributes,
messagingPropagator,
useXrayPropagator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator;
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;
Expand All @@ -27,6 +28,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
Expand Down Expand Up @@ -204,6 +206,19 @@ public Instrumenter<ExecutionAttributes, Response> producerInstrumenter() {
true);
}

public Instrumenter<ExecutionAttributes, Response> dynamoDbInstrumenter() {
return createInstrumenter(
openTelemetry,
AwsSdkInstrumenterFactory::spanName,
SpanKindExtractor.alwaysClient(),
attributesExtractors(),
builder ->
builder
.addAttributesExtractor(new DynamoDbAttributesExtractor())
.addOperationMetrics(DbClientMetrics.get()),
true);
}

private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrumenter(
OpenTelemetry openTelemetry,
SpanNameExtractor<REQUEST> spanNameExtractor,
Expand All @@ -212,12 +227,31 @@ private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrum
List<AttributesExtractor<REQUEST, RESPONSE>> additionalAttributeExtractors,
boolean enabled) {

return Instrumenter.<REQUEST, RESPONSE>builder(
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.addAttributesExtractors(attributeExtractors)
.addAttributesExtractors(additionalAttributeExtractors)
.setEnabled(enabled)
.buildInstrumenter(spanKindExtractor);
return createInstrumenter(
openTelemetry,
spanNameExtractor,
spanKindExtractor,
attributeExtractors,
builder -> builder.addAttributesExtractors(additionalAttributeExtractors),
enabled);
}

private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrumenter(
OpenTelemetry openTelemetry,
SpanNameExtractor<REQUEST> spanNameExtractor,
SpanKindExtractor<REQUEST> spanKindExtractor,
List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributeExtractors,
Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> customizer,
boolean enabled) {

InstrumenterBuilder<REQUEST, RESPONSE> builder =
Instrumenter.<REQUEST, RESPONSE>builder(
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.addAttributesExtractors(attributeExtractors)
.setEnabled(enabled);
customizer.accept(builder);

return builder.buildInstrumenter(spanKindExtractor);
}

private static String spanName(ExecutionAttributes attributes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.awssdk.v2_2.internal;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import javax.annotation.Nullable;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;

class DynamoDbAttributesExtractor implements AttributesExtractor<ExecutionAttributes, Response> {

// copied from DbIncubatingAttributes
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
private static final AttributeKey<String> DB_OPERATION_NAME =
AttributeKey.stringKey("db.operation.name");
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
private static final AttributeKey<String> DB_SYSTEM_NAME =
AttributeKey.stringKey("db.system.name");

// copied from DbIncubatingAttributes.DbSystemIncubatingValues
private static final String DYNAMODB = "dynamodb";
// copied from DbIncubatingAttributes.DbSystemNameIncubatingValues
private static final String AWS_DYNAMODB = "aws.dynamodb";

@Override
public void onStart(
AttributesBuilder attributes,
Context parentContext,
ExecutionAttributes executionAttributes) {
if (SemconvStability.emitStableDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_SYSTEM_NAME, AWS_DYNAMODB);
}
if (SemconvStability.emitOldDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_SYSTEM, DYNAMODB);
}
String operation = executionAttributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME);
if (operation != null) {
if (SemconvStability.emitStableDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_OPERATION_NAME, operation);
}
if (SemconvStability.emitOldDatabaseSemconv()) {
AttributesExtractorUtil.internalSet(attributes, DB_OPERATION, operation);
}
}
}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
ExecutionAttributes executionAttributes,
@Nullable Response response,
@Nullable Throwable error) {}
}
Loading
Loading