From 1ea7a588227b57dced8fb7961237616736964d92 Mon Sep 17 00:00:00 2001 From: Nugusbayev Kanagat Date: Tue, 17 Oct 2023 19:13:06 +0600 Subject: [PATCH 1/3] added capturing s3 otel attributes from lamba S3Event --- .../awslambda/helper/S3TransactionHelper.java | 20 +++++++++++++------ .../agent/awslambda/S3EventLambdaTest.java | 13 +++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java index fbfda924cd..f3b52c4640 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java @@ -78,12 +78,20 @@ protected void setTransactionTriggerData(Transaction transaction, S3Event s3Even cloudOrigin.withRegion(s3NotificationRecord.getAwsRegion()); - if (null != s3NotificationRecord.getS3() && null != s3NotificationRecord.getS3().getBucket()) { - S3EventNotification.S3BucketEntity bucket = s3NotificationRecord.getS3().getBucket(); - ServiceOrigin serviceOrigin = transaction.getContext().getServiceOrigin(); - serviceOrigin.withId(bucket.getArn()); - serviceOrigin.withName(bucket.getName()); - serviceOrigin.withVersion(s3NotificationRecord.getEventVersion()); + if (null != s3NotificationRecord.getS3()) { + if (null != s3NotificationRecord.getS3().getBucket()) { + S3EventNotification.S3BucketEntity bucket = s3NotificationRecord.getS3().getBucket(); + ServiceOrigin serviceOrigin = transaction.getContext().getServiceOrigin(); + serviceOrigin.withId(bucket.getArn()); + serviceOrigin.withName(bucket.getName()); + serviceOrigin.withVersion(s3NotificationRecord.getEventVersion()); + + transaction.withOtelAttribute("aws.s3.bucket", bucket.getName()); + } + if (null != s3NotificationRecord.getS3().getObject()) { + final S3EventNotification.S3ObjectEntity object = s3NotificationRecord.getS3().getObject(); + transaction.withOtelAttribute("aws.s3.key", object.getKey()); + } } } } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/test/java/co/elastic/apm/agent/awslambda/S3EventLambdaTest.java b/apm-agent-plugins/apm-awslambda-plugin/src/test/java/co/elastic/apm/agent/awslambda/S3EventLambdaTest.java index d7d7d47e21..82c737523c 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/test/java/co/elastic/apm/agent/awslambda/S3EventLambdaTest.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/test/java/co/elastic/apm/agent/awslambda/S3EventLambdaTest.java @@ -66,7 +66,8 @@ protected boolean supportsContextPropagation() { private S3EventNotification.S3EventNotificationRecord createS3NotificationRecord() { S3EventNotification.ResponseElementsEntity responseElements = new S3EventNotification.ResponseElementsEntity("xAmzId2", S3_REQUEST_ID); S3EventNotification.S3BucketEntity bucket = new S3EventNotification.S3BucketEntity(S3_BUCKET_NAME, null, S3_BUCKET_ARN); - S3EventNotification.S3Entity s3 = new S3EventNotification.S3Entity("configId", bucket, null, "3.3"); + S3EventNotification.S3ObjectEntity object = new S3EventNotification.S3ObjectEntity("b21b84d653bb07b05b1e6b33684dc11b", 1305107, "b21b84d653bb07b05b1e6b33684dc11b", "0C0F6F405D6ED209E1"); + S3EventNotification.S3Entity s3 = new S3EventNotification.S3Entity("configId", bucket, object, "3.3"); return new S3EventNotification.S3EventNotificationRecord(EVENT_SOURCE_REGION, S3_EVENT_NAME, S3_EVENT_SOURCE, null, S3_EVENT_VERSION, null, responseElements, s3, null); } @@ -102,6 +103,8 @@ public void testBasicCall() { assertThat(faas.getId()).isEqualTo(TestContext.FUNCTION_ARN); assertThat(faas.getTrigger().getType()).isEqualTo("datasource"); assertThat(faas.getTrigger().getRequestId()).isEqualTo(S3_REQUEST_ID); + + verifyOtelAttributes(transaction); } @Test @@ -174,4 +177,12 @@ private void validateResultsForUnspecifiedRecord() { assertThat(faas.getTrigger().getType()).isEqualTo("datasource"); assertThat(faas.getTrigger().getRequestId()).isNull(); } + + private void verifyOtelAttributes(Transaction transaction) { + Object s3keyAttribute = transaction.getOtelAttributes().get("aws.s3.key"); + assertThat(s3keyAttribute).isInstanceOf(String.class).isEqualTo("b21b84d653bb07b05b1e6b33684dc11b"); + + Object s3bucketAttribute = transaction.getOtelAttributes().get("aws.s3.bucket"); + assertThat(s3bucketAttribute).isInstanceOf(String.class).isEqualTo(S3_BUCKET_NAME); + } } From 6bb3cf2d3a153162e03893c1f4cb8b1087daab6d Mon Sep 17 00:00:00 2001 From: Nugusbayev Kanagat Date: Tue, 17 Oct 2023 19:21:35 +0600 Subject: [PATCH 2/3] added entry to changelog --- CHANGELOG.asciidoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index f5c09b6aa5..de21e20523 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -31,6 +31,10 @@ Use subheadings with the "=====" level for adding notes for unreleased changes: === Unreleased +[float] +===== Features +* Capture bucket and object key to Lambda transaction as OTel attributes - `aws.s3.bueckt`, `aws.s3.key` - {pull}3364[#3364] + [float] ===== Bug fixes * Fixed too many spans being created for `HTTPUrlConnection` requests with method `HEAD` - {pull}3353[#3353] From 48145858b5c69c632b94d0be01dfe9864a096230 Mon Sep 17 00:00:00 2001 From: Nugusbayev Kanagat Date: Wed, 18 Oct 2023 15:46:43 +0600 Subject: [PATCH 3/3] minor polish --- .../apm/agent/awslambda/helper/S3TransactionHelper.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java index f3b52c4640..e2203a665d 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java @@ -79,8 +79,8 @@ protected void setTransactionTriggerData(Transaction transaction, S3Event s3Even cloudOrigin.withRegion(s3NotificationRecord.getAwsRegion()); if (null != s3NotificationRecord.getS3()) { - if (null != s3NotificationRecord.getS3().getBucket()) { - S3EventNotification.S3BucketEntity bucket = s3NotificationRecord.getS3().getBucket(); + S3EventNotification.S3BucketEntity bucket = s3NotificationRecord.getS3().getBucket(); + if (null != bucket) { ServiceOrigin serviceOrigin = transaction.getContext().getServiceOrigin(); serviceOrigin.withId(bucket.getArn()); serviceOrigin.withName(bucket.getName()); @@ -88,8 +88,8 @@ protected void setTransactionTriggerData(Transaction transaction, S3Event s3Even transaction.withOtelAttribute("aws.s3.bucket", bucket.getName()); } - if (null != s3NotificationRecord.getS3().getObject()) { - final S3EventNotification.S3ObjectEntity object = s3NotificationRecord.getS3().getObject(); + S3EventNotification.S3ObjectEntity object = s3NotificationRecord.getS3().getObject(); + if (null != object) { transaction.withOtelAttribute("aws.s3.key", object.getKey()); } }