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 7 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<?>> dynamoInstrumenter() {
DynamoAttributesExtractor dynamoAttributesExtractor = new DynamoAttributesExtractor();

return createInstrumenter(
openTelemetry,
spanName,
SpanKindExtractor.alwaysClient(),
attributesExtractors(),
singletonList(dynamoAttributesExtractor),
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<?>> dynamoInstrumenter;

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

/**
Expand All @@ -76,6 +78,7 @@ public RequestHandler2 newRequestHandler() {
requestInstrumenter,
consumerReceiveInstrumenter,
consumerProcessInstrumenter,
producerInstrumenter);
producerInstrumenter,
dynamoInstrumenter);
}
}
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 DynamoAttributesExtractor implements AttributesExtractor<Request<?>, Response<?>> {

private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
private static final AttributeKey<List<String>> AWS_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_TABLE_NAMES, Collections.singletonList(tableName));
}

@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";

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<?>> dynamoInstrumenter;

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

@Override
Expand Down Expand Up @@ -151,14 +157,16 @@ 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 dynamoInstrumenter;
} else if (className.equals(SEND_MESSAGE_REQUEST)) {
return producerInstrumenter;
}
return requestInstrumenter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.handlers.RequestHandler2;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension;
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 +75,7 @@ public void assertRequestWithMockedResponse(
String service,
String operation,
String method,
Map<String, String> additionalAttributes)
List<AttributeKeyPair<?>> additionalAttributes)
throws Exception {

assertThat(response).isNotNull();
Expand Down Expand Up @@ -114,7 +114,14 @@ public void assertRequestWithMockedResponse(
}

additionalAttributes.forEach(
(k, v) -> attributes.add(equalTo(stringKey(k), v)));
(att) -> {
if (att.getType() == AttributeType.STRING) {
attributes.add(equalTo(att.getStringKey(), att.getStringVal()));
} else if (att.getType() == AttributeType.STRING_ARRAY) {
attributes.add(
equalTo(att.getStringArrayKey(), att.getStringArrayVal()));
}
});

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,18 @@

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringArrayKeyPair;
import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;

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.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.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;

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

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

List<String> tableList = Collections.singletonList("sometable");

List<AttributeKeyPair<?>> additionalAttributes =
Arrays.asList(
createStringKeyPair("aws.table.name", "sometable"),
createStringKeyPair("db.system", "dynamodb"),
createStringArrayKeyPair("aws.dynamodb.table_names", tableList));

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,16 @@

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;

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.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.Arrays;
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 +44,8 @@ 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<AttributeKeyPair<?>> additionalAttributes =
Arrays.asList(createStringKeyPair("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 @@ -7,6 +7,7 @@

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;
import static io.opentelemetry.instrumentation.test.utils.PortUtils.UNUSABLE_PORT;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
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.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.Collections;
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<AttributeKeyPair<?>> 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")),
Collections.singletonList(createStringKeyPair("aws.bucket.name", "testbucket"))),
Arguments.of(
"GetObject",
"GET",
(Function<AmazonS3, Object>) c -> c.getObject("someBucket", "someKey"),
ImmutableMap.of("aws.bucket.name", "someBucket")));
Collections.singletonList(createStringKeyPair("aws.bucket.name", "someBucket"))));
}

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

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME;

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.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.Arrays;
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 +53,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<AttributeKeyPair<?>> additionalAttributes =
Arrays.asList(createStringKeyPair(MESSAGING_DESTINATION_NAME.toString(), "somearn"));

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