Skip to content

Commit 7207885

Browse files
committed
feat: Add remaining unit tests and clean up logic for CFN Primary Ids of existing resources
1 parent cd43a9c commit 7207885

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsAttributeKeys.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ private AwsAttributeKeys() {}
6767
static final AttributeKey<String> AWS_LAMBDA_NAME =
6868
AttributeKey.stringKey("aws.lambda.function.name");
6969

70+
static final AttributeKey<String> AWS_LAMBDA_ARN =
71+
AttributeKey.stringKey("aws.lambda.function.arn");
72+
7073
static final AttributeKey<String> AWS_LAMBDA_RESOURCE_ID =
7174
AttributeKey.stringKey("aws.lambda.resource_mapping.id");
7275

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGenerator.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@
7777
import static software.amazon.opentelemetry.javaagent.providers.AwsSpanProcessingUtil.isKeyPresent;
7878

7979
import com.amazonaws.arn.Arn;
80-
import com.amazonaws.services.lambda.AWSLambda;
81-
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
82-
import com.amazonaws.services.lambda.model.GetFunctionRequest;
83-
import com.amazonaws.services.lambda.model.GetFunctionResult;
8480
import io.opentelemetry.api.common.AttributeKey;
8581
import io.opentelemetry.api.common.Attributes;
8682
import io.opentelemetry.api.common.AttributesBuilder;
@@ -510,46 +506,37 @@ private static void setRemoteResourceTypeAndIdentifier(SpanData span, Attributes
510506
} else if (isKeyPresent(span, AWS_LAMBDA_NAME)) {
511507
remoteResourceType = Optional.of(NORMALIZED_LAMBDA_SERVICE_NAME + "::Function");
512508
remoteResourceIdentifier =
513-
getLambdaNameFromArbitraryName(
514-
Optional.ofNullable(escapeDelimiters(span.getAttributes().get(AWS_LAMBDA_NAME))));
515-
cloudformationPrimaryIdentifier =
516-
getLambdaArnFromArbitraryName(
509+
getLambdaResourceNameFromAribitraryName(
517510
Optional.ofNullable(escapeDelimiters(span.getAttributes().get(AWS_LAMBDA_NAME))));
518511
} else if (isKeyPresent(span, AWS_LAMBDA_RESOURCE_ID)) {
519512
remoteResourceType = Optional.of(NORMALIZED_LAMBDA_SERVICE_NAME + "::EventSourceMapping");
520513
remoteResourceIdentifier =
521514
Optional.ofNullable(escapeDelimiters(span.getAttributes().get(AWS_LAMBDA_RESOURCE_ID)));
522-
cloudformationPrimaryIdentifier = remoteResourceIdentifier;
523515
}
524516
} else if (isDBSpan(span)) {
525517
remoteResourceType = Optional.of(DB_CONNECTION_RESOURCE_TYPE);
526518
remoteResourceIdentifier = getDbConnection(span);
527519
}
528520

521+
if (cloudformationPrimaryIdentifier.isEmpty()) {
522+
cloudformationPrimaryIdentifier = remoteResourceIdentifier;
523+
}
524+
529525
if (remoteResourceType.isPresent() && remoteResourceIdentifier.isPresent()) {
530526
builder.put(AWS_REMOTE_RESOURCE_TYPE, remoteResourceType.get());
531527
builder.put(AWS_REMOTE_RESOURCE_IDENTIFIER, remoteResourceIdentifier.get());
532-
}
533-
if (cloudformationPrimaryIdentifier.isPresent()) {
534528
builder.put(AWS_CLOUDFORMATION_PRIMARY_IDENTIFIER, cloudformationPrimaryIdentifier.get());
535529
}
536530
}
537531

538-
private static Optional<String> getLambdaNameFromArbitraryName(Optional<String> arbitraryName) {
539-
AWSLambda lambdaClient = AWSLambdaClientBuilder.defaultClient();
540-
GetFunctionRequest getFunctionRequest =
541-
new GetFunctionRequest().withFunctionName(arbitraryName.get());
542-
GetFunctionResult getFunctionResult = lambdaClient.getFunction(getFunctionRequest);
543-
return Optional.of(getFunctionResult.getConfiguration().getFunctionName());
544-
}
545-
546532
// NOTE: "name" in this case can be either the lambda name or lambda arn
547-
private static Optional<String> getLambdaArnFromArbitraryName(Optional<String> arbitraryName) {
548-
AWSLambda lambdaClient = AWSLambdaClientBuilder.defaultClient();
549-
GetFunctionRequest getFunctionRequest =
550-
new GetFunctionRequest().withFunctionName(arbitraryName.get());
551-
GetFunctionResult getFunctionResult = lambdaClient.getFunction(getFunctionRequest);
552-
return Optional.of(getFunctionResult.getConfiguration().getFunctionArn());
533+
private static Optional<String> getLambdaResourceNameFromAribitraryName(
534+
Optional<String> arbitraryName) {
535+
if (arbitraryName != null && arbitraryName.get().startsWith("arn:aws:lambda:")) {
536+
Arn resourceArn = Arn.fromString(arbitraryName.get());
537+
return Optional.of(resourceArn.getResource().toString().split(":")[1]);
538+
}
539+
return arbitraryName;
553540
}
554541

555542
private static Optional<String> getSecretsManagerResourceNameFromArn(Optional<String> stringArn) {

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsMetricAttributeGeneratorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_DATA_SOURCE_ID;
2727
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_GUARDRAIL_ID;
2828
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_KNOWLEDGE_BASE_ID;
29+
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_LAMBDA_NAME;
30+
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_LAMBDA_RESOURCE_ID;
2931
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_LOCAL_OPERATION;
3032
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_LOCAL_SERVICE;
3133
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_QUEUE_NAME;
@@ -787,6 +789,16 @@ public void testSdkClientSpanWithRemoteResourceAttributes() {
787789
validateRemoteResourceAttributes("AWS::SecretsManager::Secret", "secretName");
788790
mockAttribute(AWS_SECRET_ARN, null);
789791

792+
// Validate behaviour of AWS_LAMBDA_NAME, then remove it.
793+
mockAttribute(AWS_LAMBDA_NAME, "arn:aws:lambda:us-east-1:123456789012:function:functionName");
794+
validateRemoteResourceAttributes("AWS::Lambda::Function", "functionName");
795+
mockAttribute(AWS_LAMBDA_NAME, null);
796+
797+
// Validate behaviour of AWS_LAMBDA_RESOURCE_ID
798+
mockAttribute(AWS_LAMBDA_RESOURCE_ID, "eventSourceId");
799+
validateRemoteResourceAttributes("AWS::Lambda::EventSourceMapping", "eventSourceId");
800+
mockAttribute(AWS_LAMBDA_RESOURCE_ID, null);
801+
790802
mockAttribute(RPC_SYSTEM, "null");
791803
}
792804

0 commit comments

Comments
 (0)