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

Added dynamodb instrumenter for aws v1_11 sdk #12756

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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 @@ -186,6 +186,18 @@ Instrumenter<Request<?>, Response<?>> producerInstrumenter() {
true);
}

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

return createInstrumenter(
openTelemetry,
spanName,
SpanKindExtractor.alwaysClient(),
attributesExtractors(),
singletonList(dynamoDbAttributesExtractor),
true);
}

private static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> createInstrumenter(
OpenTelemetry openTelemetry,
SpanNameExtractor<REQUEST> spanNameExtractor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static AwsSdkTelemetryBuilder builder(OpenTelemetry openTelemetry) {
private final Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter;
private final Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter;
private final Instrumenter<Request<?>, Response<?>> producerInstrumenter;
private final Instrumenter<Request<?>, Response<?>> dynamoDbInstrumenter;

AwsSdkTelemetry(
OpenTelemetry openTelemetry,
Expand All @@ -65,6 +66,7 @@ public static AwsSdkTelemetryBuilder builder(OpenTelemetry openTelemetry) {
consumerReceiveInstrumenter = instrumenterFactory.consumerReceiveInstrumenter();
consumerProcessInstrumenter = instrumenterFactory.consumerProcessInstrumenter();
producerInstrumenter = instrumenterFactory.producerInstrumenter();
dynamoDbInstrumenter = instrumenterFactory.dynamoDbInstrumenter();
}

/**
Expand All @@ -76,6 +78,7 @@ public RequestHandler2 newRequestHandler() {
requestInstrumenter,
consumerReceiveInstrumenter,
consumerProcessInstrumenter,
producerInstrumenter);
producerInstrumenter,
dynamoDbInstrumenter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.awssdk.v1_11;

import com.amazonaws.Request;
import com.amazonaws.Response;
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 java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

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

private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
private static final AttributeKey<List<String>> AWS_DYNAMODB_TABLE_NAMES =
AttributeKey.stringArrayKey("aws.dynamodb.table_names");

private static final String DYNAMODB = "dynamodb";

@Override
public void onStart(AttributesBuilder attributes, Context parentContext, Request<?> request) {
AttributesExtractorUtil.internalSet(attributes, DB_SYSTEM, DYNAMODB);
String tableName = RequestAccess.getTableName(request.getOriginalRequest());
AttributesExtractorUtil.internalSet(
attributes, AWS_DYNAMODB_TABLE_NAMES, Collections.singletonList(tableName));
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just an observation more than a request for a change - I understand that this attribute is a List<String> based on the spec, but it doesn't look like this code supports multiple table names, so it feels a little strange for it be defined that way

}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
Request<?> request,
@Nullable Response<?> response,
@Nullable Throwable error) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ final class TracingRequestHandler extends RequestHandler2 {
ContextKey.named(TracingRequestHandler.class.getName() + ".Timer");
private static final ContextKey<Boolean> REQUEST_SPAN_SUPPRESSED_KEY =
ContextKey.named(TracingRequestHandler.class.getName() + ".RequestSpanSuppressed");
private static final String SEND_MESSAGE_REQUEST =
"com.amazonaws.services.sqs.model.SendMessageRequest";
private static final String DYNAMODBV2 = "com.amazonaws.services.dynamodbv2.model.";
Comment on lines +34 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slight nitpick: perhaps the usage of these variables provides enough context, but it might be a little more intuitive if the names indicated that they reference the names of classes

Suggested change
private static final String SEND_MESSAGE_REQUEST =
"com.amazonaws.services.sqs.model.SendMessageRequest";
private static final String DYNAMODBV2 = "com.amazonaws.services.dynamodbv2.model.";
private static final String SEND_MESSAGE_REQUEST_CLASS =
"com.amazonaws.services.sqs.model.SendMessageRequest";
private static final String DYNAMODBV2_CLASS = "com.amazonaws.services.dynamodbv2.model.";


private final Instrumenter<Request<?>, Response<?>> requestInstrumenter;
private final Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter;
private final Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter;
private final Instrumenter<Request<?>, Response<?>> producerInstrumenter;
private final Instrumenter<Request<?>, Response<?>> dynamoDbInstrumenter;

TracingRequestHandler(
Instrumenter<Request<?>, Response<?>> requestInstrumenter,
Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter,
Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter,
Instrumenter<Request<?>, Response<?>> producerInstrumenter) {
Instrumenter<Request<?>, Response<?>> producerInstrumenter,
Instrumenter<Request<?>, Response<?>> dynamoDbInstrumenter) {
this.requestInstrumenter = requestInstrumenter;
this.consumerReceiveInstrumenter = consumerReceiveInstrumenter;
this.consumerProcessInstrumenter = consumerProcessInstrumenter;
this.producerInstrumenter = producerInstrumenter;
this.dynamoDbInstrumenter = dynamoDbInstrumenter;
}

@Override
Expand Down Expand Up @@ -151,14 +157,17 @@ private void finish(Request<?> request, Response<?> response, @Nullable Throwabl
}
return;
}

instrumenter.end(context, request, response, error);
}

private Instrumenter<Request<?>, Response<?>> getInstrumenter(Request<?> request) {
boolean isSqsProducer =
"com.amazonaws.services.sqs.model.SendMessageRequest"
.equals(request.getOriginalRequest().getClass().getName());
return isSqsProducer ? producerInstrumenter : requestInstrumenter;
String className = request.getOriginalRequest().getClass().getName();
if (className.startsWith(DYNAMODBV2)) {
return dynamoDbInstrumenter;
}
if (className.equals(SEND_MESSAGE_REQUEST)) {
return producerInstrumenter;
}
return requestInstrumenter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -75,7 +74,7 @@ public void assertRequestWithMockedResponse(
String service,
String operation,
String method,
Map<String, String> additionalAttributes)
List<AttributeAssertion> additionalAttributes)
throws Exception {

assertThat(response).isNotNull();
Expand Down Expand Up @@ -113,8 +112,7 @@ public void assertRequestWithMockedResponse(
stringKey("aws.request_id"), v -> v.isInstanceOf(String.class)));
}

additionalAttributes.forEach(
(k, v) -> attributes.add(equalTo(stringKey(k), v)));
attributes.addAll(additionalAttributes);

span.hasName(service + "." + operation)
.hasKind(operation.equals("SendMessage") ? PRODUCER : CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static java.util.Collections.singletonList;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

public abstract class AbstractDynamoDbClientTest extends AbstractBaseAwsClientTest {
Expand All @@ -34,13 +41,14 @@ public void sendRequestWithMockedResponse() throws Exception {

server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, ""));

List<AttributeAssertion> additionalAttributes =
Arrays.asList(
equalTo(stringKey("aws.table.name"), "sometable"),
equalTo(stringKey("db.system"), "dynamodb"),
equalTo(stringArrayKey("aws.dynamodb.table_names"), singletonList("sometable")));
Comment on lines +46 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit confusing to have both aws.dynamodb.table_names and aws.table.name at the same time, but I don't have a better idea (and aws.table.name requires experimental flag to get anyways)

Copy link
Contributor Author

@akats7 akats7 Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I had the same qualm but I don't want to introduce a breaking change and also wanted to align to the sem convs


Object response = client.createTable(new CreateTableRequest("sometable", null));
assertRequestWithMockedResponse(
response,
client,
"DynamoDBv2",
"CreateTable",
"POST",
ImmutableMap.of("aws.table.name", "sometable"));
response, client, "DynamoDBv2", "CreateTable", "POST", additionalAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public void sendRequestWithMockedResponse() throws Exception {

Object response = client.allocateAddress();
assertRequestWithMockedResponse(
response, client, "EC2", "AllocateAddress", "POST", Collections.emptyMap());
response, client, "EC2", "AllocateAddress", "POST", Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static java.util.Collections.singletonList;

import com.amazonaws.services.kinesis.AmazonKinesis;
import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder;
import com.amazonaws.services.kinesis.model.DeleteStreamRequest;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.util.Map;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -42,7 +46,9 @@ public void testSendRequestWithMockedResponse(

server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, ""));

Map<String, String> additionalAttributes = ImmutableMap.of("aws.stream.name", "somestream");
List<AttributeAssertion> additionalAttributes =
singletonList(equalTo(stringKey("aws.stream.name"), "somestream"));

Object response = call.apply(client);
assertRequestWithMockedResponse(
response, client, "Kinesis", operation, "POST", additionalAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public void sendRequestWithMockedResponse() throws Exception {

Object response = client.deleteOptionGroup(new DeleteOptionGroupRequest());
assertRequestWithMockedResponse(
response, client, "RDS", "DeleteOptionGroup", "POST", Collections.emptyMap());
response, client, "RDS", "DeleteOptionGroup", "POST", Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_METHOD;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SERVICE;
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SYSTEM;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

Expand All @@ -27,14 +28,14 @@
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.time.Duration;
import java.util.Map;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
Expand All @@ -60,7 +61,7 @@ public void testSendRequestWithMockedResponse(
String operation,
String method,
Function<AmazonS3, Object> call,
Map<String, String> additionalAttributes)
List<AttributeAssertion> additionalAttributes)
throws Exception {

AmazonS3 client =
Expand All @@ -82,12 +83,12 @@ private static Stream<Arguments> provideArguments() {
"CreateBucket",
"PUT",
(Function<AmazonS3, Object>) c -> c.createBucket("testbucket"),
ImmutableMap.of("aws.bucket.name", "testbucket")),
singletonList(equalTo(stringKey("aws.bucket.name"), "testbucket"))),
Arguments.of(
"GetObject",
"GET",
(Function<AmazonS3, Object>) c -> c.getObject("someBucket", "someKey"),
ImmutableMap.of("aws.bucket.name", "someBucket")));
singletonList(equalTo(stringKey("aws.bucket.name"), "someBucket"))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME;
import static java.util.Collections.singletonList;

import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.PublishRequest;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.util.Map;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -52,8 +54,8 @@ public void testSendRequestWithMockedResponse(Function<AmazonSNS, Object> call)

server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, body));

Map<String, String> additionalAttributes =
ImmutableMap.of(MESSAGING_DESTINATION_NAME.toString(), "somearn");
List<AttributeAssertion> additionalAttributes =
singletonList(equalTo(MESSAGING_DESTINATION_NAME, "somearn"));

Object response = call.apply(client);
assertRequestWithMockedResponse(
Expand Down
Loading