Skip to content

Commit de826b8

Browse files
authored
Merge branch 'main' into declarative-config-resource-providers
2 parents 7c5b17b + eb8720f commit de826b8

File tree

36 files changed

+970
-95
lines changed

36 files changed

+970
-95
lines changed

instrumentation/aws-sdk/aws-sdk-1.11/javaagent/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies {
4444
testLibrary("com.amazonaws:aws-java-sdk-dynamodb:1.11.106")
4545
testLibrary("com.amazonaws:aws-java-sdk-ec2:1.11.106")
4646
testLibrary("com.amazonaws:aws-java-sdk-kinesis:1.11.106")
47+
testLibrary("com.amazonaws:aws-java-sdk-lambda:1.11.106")
4748
testLibrary("com.amazonaws:aws-java-sdk-rds:1.11.106")
4849
testLibrary("com.amazonaws:aws-java-sdk-s3:1.11.106")
4950
testLibrary("com.amazonaws:aws-java-sdk-sns:1.11.106")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.awssdk.v1_11;
7+
8+
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
9+
import io.opentelemetry.instrumentation.awssdk.v1_11.AbstractLambdaClientTest;
10+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
11+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
12+
import org.junit.jupiter.api.extension.RegisterExtension;
13+
14+
class LambdaClientTest extends AbstractLambdaClientTest {
15+
@RegisterExtension
16+
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
17+
18+
@Override
19+
protected InstrumentationExtension testing() {
20+
return testing;
21+
}
22+
23+
@Override
24+
public AWSLambdaClientBuilder configureClient(AWSLambdaClientBuilder clientBuilder) {
25+
return clientBuilder;
26+
}
27+
}

instrumentation/aws-sdk/aws-sdk-1.11/library/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
testLibrary("com.amazonaws:aws-java-sdk-dynamodb:1.11.106")
1515
testLibrary("com.amazonaws:aws-java-sdk-ec2:1.11.106")
1616
testLibrary("com.amazonaws:aws-java-sdk-kinesis:1.11.106")
17+
testLibrary("com.amazonaws:aws-java-sdk-lambda:1.11.106")
1718
testLibrary("com.amazonaws:aws-java-sdk-rds:1.11.106")
1819
testLibrary("com.amazonaws:aws-java-sdk-s3:1.11.106")
1920
testLibrary("com.amazonaws:aws-java-sdk-sns:1.11.106")

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsExperimentalAttributes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ final class AwsExperimentalAttributes {
1717
static final AttributeKey<String> AWS_STREAM_NAME = stringKey("aws.stream.name");
1818
static final AttributeKey<String> AWS_TABLE_NAME = stringKey("aws.table.name");
1919

20+
// Work is underway to add these two keys to the SemConv AWS registry, in line with other AWS
21+
// resources.
22+
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/aws.md#amazon-lambda-attributes
23+
static final AttributeKey<String> AWS_LAMBDA_ARN = stringKey("aws.lambda.function.arn");
24+
static final AttributeKey<String> AWS_LAMBDA_NAME = stringKey("aws.lambda.function.name");
25+
2026
private AwsExperimentalAttributes() {}
2127
}

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkAttributesExtractor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class AwsSdkAttributesExtractor implements AttributesExtractor<Request<?>, Respo
2626
// Copied from AwsIncubatingAttributes
2727
private static final AttributeKey<String> AWS_SECRETSMANAGER_SECRET_ARN =
2828
stringKey("aws.secretsmanager.secret.arn");
29+
private static final AttributeKey<String> AWS_LAMBDA_RESOURCE_MAPPING_ID =
30+
stringKey("aws.lambda.resource_mapping.id");
2931
private static final AttributeKey<String> AWS_SNS_TOPIC_ARN = stringKey("aws.sns.topic.arn");
3032
private static final AttributeKey<String> AWS_STEP_FUNCTIONS_ACTIVITY_ARN =
3133
stringKey("aws.step_functions.activity.arn");
@@ -46,6 +48,11 @@ private static boolean canGetResponseMetadata() {
4648
@Override
4749
public void onStart(AttributesBuilder attributes, Context parentContext, Request<?> request) {
4850
Object originalRequest = request.getOriginalRequest();
51+
setAttribute(
52+
attributes,
53+
AWS_LAMBDA_RESOURCE_MAPPING_ID,
54+
originalRequest,
55+
RequestAccess::getLambdaResourceMappingId);
4956
setAttribute(attributes, AWS_SNS_TOPIC_ARN, originalRequest, RequestAccess::getSnsTopicArn);
5057
setAttribute(
5158
attributes,
@@ -69,6 +76,11 @@ public void onEnd(
6976
Object awsResp = getAwsResponse(response);
7077
if (awsResp != null) {
7178
setAttribute(attributes, AWS_SECRETSMANAGER_SECRET_ARN, awsResp, RequestAccess::getSecretArn);
79+
setAttribute(
80+
attributes,
81+
AWS_LAMBDA_RESOURCE_MAPPING_ID,
82+
awsResp,
83+
RequestAccess::getLambdaResourceMappingId);
7284
setAttribute(attributes, AWS_SNS_TOPIC_ARN, awsResp, RequestAccess::getSnsTopicArn);
7385
setAttribute(
7486
attributes,

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkExperimentalAttributesExtractor.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_AGENT;
99
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_BUCKET_NAME;
10+
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_LAMBDA_ARN;
11+
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_LAMBDA_NAME;
1012
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_QUEUE_NAME;
1113
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_QUEUE_URL;
1214
import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttributes.AWS_STREAM_NAME;
@@ -35,6 +37,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, Request
3537
setRequestAttribute(attributes, AWS_QUEUE_NAME, originalRequest, RequestAccess::getQueueName);
3638
setRequestAttribute(attributes, AWS_STREAM_NAME, originalRequest, RequestAccess::getStreamName);
3739
setRequestAttribute(attributes, AWS_TABLE_NAME, originalRequest, RequestAccess::getTableName);
40+
setRequestAttribute(attributes, AWS_LAMBDA_NAME, originalRequest, RequestAccess::getLambdaName);
3841
}
3942

4043
private static void setRequestAttribute(
@@ -54,5 +57,17 @@ public void onEnd(
5457
Context context,
5558
Request<?> request,
5659
@Nullable Response<?> response,
57-
@Nullable Throwable error) {}
60+
@Nullable Throwable error) {
61+
Object awsResponse = getAwsResponse(response);
62+
if (awsResponse != null) {
63+
setRequestAttribute(attributes, AWS_LAMBDA_ARN, awsResponse, RequestAccess::getLambdaArn);
64+
}
65+
}
66+
67+
private static Object getAwsResponse(Response<?> response) {
68+
if (response == null) {
69+
return null;
70+
}
71+
return response.getAwsResponse();
72+
}
5873
}

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/RequestAccess.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.annotation.Nullable;
1212

1313
final class RequestAccess {
14+
private static final String LAMBDA_REQUEST_CLASS_PREFIX = "com.amazonaws.services.lambda.model.";
1415
private static final String SECRETS_MANAGER_REQUEST_CLASS_PREFIX =
1516
"com.amazonaws.services.secretsmanager.model.";
1617
private static final String STEP_FUNCTIONS_REQUEST_CLASS_PREFIX =
@@ -24,6 +25,30 @@ protected RequestAccess computeValue(Class<?> type) {
2425
}
2526
};
2627

28+
@Nullable
29+
static String getLambdaArn(Object request) {
30+
RequestAccess access = REQUEST_ACCESSORS.get(request.getClass());
31+
if (access.getLambdaConfiguration == null) {
32+
return null;
33+
}
34+
Object config = invokeOrNull(access.getLambdaConfiguration, request, Object.class);
35+
return config != null
36+
? invokeOrNull(LambdaFunctionConfigurationAccess.getLambdaArnFromConfiguration, config)
37+
: null;
38+
}
39+
40+
@Nullable
41+
static String getLambdaName(Object request) {
42+
RequestAccess access = REQUEST_ACCESSORS.get(request.getClass());
43+
return invokeOrNull(access.getLambdaName, request);
44+
}
45+
46+
@Nullable
47+
static String getLambdaResourceMappingId(Object request) {
48+
RequestAccess access = REQUEST_ACCESSORS.get(request.getClass());
49+
return invokeOrNull(access.getLambdaResourceMappingId, request);
50+
}
51+
2752
@Nullable
2853
static String getSecretArn(Object request) {
2954
RequestAccess access = REQUEST_ACCESSORS.get(request.getClass());
@@ -86,17 +111,26 @@ static String getTargetArn(Object request) {
86111

87112
@Nullable
88113
private static String invokeOrNull(@Nullable MethodHandle method, Object obj) {
114+
return invokeOrNull(method, obj, String.class);
115+
}
116+
117+
@Nullable
118+
private static <T> T invokeOrNull(
119+
@Nullable MethodHandle method, Object obj, Class<T> returnType) {
89120
if (method == null) {
90121
return null;
91122
}
92123
try {
93-
return (String) method.invoke(obj);
124+
return returnType.cast(method.invoke(obj));
94125
} catch (Throwable t) {
95126
return null;
96127
}
97128
}
98129

99130
@Nullable private final MethodHandle getBucketName;
131+
@Nullable private final MethodHandle getLambdaConfiguration;
132+
@Nullable private final MethodHandle getLambdaName;
133+
@Nullable private final MethodHandle getLambdaResourceMappingId;
100134
@Nullable private final MethodHandle getQueueUrl;
101135
@Nullable private final MethodHandle getQueueName;
102136
@Nullable private final MethodHandle getSecretArn;
@@ -116,6 +150,10 @@ private RequestAccess(Class<?> clz) {
116150
getTopicArn = findAccessorOrNull(clz, "getTopicArn");
117151
getTargetArn = findAccessorOrNull(clz, "getTargetArn");
118152

153+
boolean isLambda = clz.getName().startsWith(LAMBDA_REQUEST_CLASS_PREFIX);
154+
getLambdaConfiguration = isLambda ? findLambdaGetConfigurationMethod(clz) : null;
155+
getLambdaName = isLambda ? findAccessorOrNull(clz, "getFunctionName") : null;
156+
getLambdaResourceMappingId = isLambda ? findAccessorOrNull(clz, "getUUID") : null;
119157
boolean isSecretsManager = clz.getName().startsWith(SECRETS_MANAGER_REQUEST_CLASS_PREFIX);
120158
getSecretArn = isSecretsManager ? findAccessorOrNull(clz, "getARN") : null;
121159
boolean isStepFunction = clz.getName().startsWith(STEP_FUNCTIONS_REQUEST_CLASS_PREFIX);
@@ -125,11 +163,43 @@ private RequestAccess(Class<?> clz) {
125163

126164
@Nullable
127165
private static MethodHandle findAccessorOrNull(Class<?> clz, String methodName) {
166+
return findAccessorOrNull(clz, methodName, String.class);
167+
}
168+
169+
@Nullable
170+
private static MethodHandle findAccessorOrNull(
171+
Class<?> clz, String methodName, Class<?> returnType) {
128172
try {
129173
return MethodHandles.publicLookup()
130-
.findVirtual(clz, methodName, MethodType.methodType(String.class));
174+
.findVirtual(clz, methodName, MethodType.methodType(returnType));
131175
} catch (Throwable t) {
132176
return null;
133177
}
134178
}
179+
180+
@Nullable
181+
private static MethodHandle findLambdaGetConfigurationMethod(Class<?> clz) {
182+
try {
183+
Class<?> returnType =
184+
Class.forName("com.amazonaws.services.lambda.model.FunctionConfiguration");
185+
return findAccessorOrNull(clz, "getConfiguration", returnType);
186+
} catch (Throwable t) {
187+
return null;
188+
}
189+
}
190+
191+
private static class LambdaFunctionConfigurationAccess {
192+
static final MethodHandle getLambdaArnFromConfiguration = findGetLambdaArnMethod();
193+
194+
@Nullable
195+
private static MethodHandle findGetLambdaArnMethod() {
196+
try {
197+
Class<?> lambdaConfigurationClass =
198+
Class.forName("com.amazonaws.services.lambda.model.FunctionConfiguration");
199+
return findAccessorOrNull(lambdaConfigurationClass, "getFunctionArn");
200+
} catch (Throwable t) {
201+
return null;
202+
}
203+
}
204+
}
135205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.awssdk.v1_11;
7+
8+
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
9+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
10+
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
class LambdaClientTest extends AbstractLambdaClientTest {
14+
@RegisterExtension
15+
private static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();
16+
17+
@Override
18+
protected InstrumentationExtension testing() {
19+
return testing;
20+
}
21+
22+
@Override
23+
public AWSLambdaClientBuilder configureClient(AWSLambdaClientBuilder clientBuilder) {
24+
return clientBuilder.withRequestHandlers(
25+
AwsSdkTelemetry.builder(testing().getOpenTelemetry())
26+
.setCaptureExperimentalSpanAttributes(true)
27+
.build()
28+
.newRequestHandler());
29+
}
30+
}

instrumentation/aws-sdk/aws-sdk-1.11/testing/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies {
1010
compileOnly("com.amazonaws:aws-java-sdk-dynamodb:1.11.106")
1111
compileOnly("com.amazonaws:aws-java-sdk-ec2:1.11.106")
1212
compileOnly("com.amazonaws:aws-java-sdk-kinesis:1.11.106")
13+
compileOnly("com.amazonaws:aws-java-sdk-lambda:1.11.106")
1314
compileOnly("com.amazonaws:aws-java-sdk-rds:1.11.106")
1415
compileOnly("com.amazonaws:aws-java-sdk-s3:1.11.106")
1516
compileOnly("com.amazonaws:aws-java-sdk-secretsmanager:1.12.80")

0 commit comments

Comments
 (0)