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

feat(aws-lambda): added eventContextExtractor config option #566

Merged

Conversation

prsnca
Copy link
Contributor

@prsnca prsnca commented Jul 8, 2021

Which problem is this PR solving?

  • Today, Lambda instrumentation supports trace propagation only for X-Ray or HTTP headers. This enhancement allows to add a custom configuration for extracting the context from incoming events to the Lambda function. This will allow the instrumentation to support more event types, such as SQS, CloudWatch or Kinesis.

Short description of the changes

  • Added eventContextExtractor config option to allow custom handling of events which carry the context in different ways. If not provided, a default context extractor is used and tries to pull the context from the HTTP headers - same as the behavior today.

@prsnca prsnca requested a review from a team as a code owner July 8, 2021 08:31
@codecov
Copy link

codecov bot commented Jul 8, 2021

Codecov Report

Merging #566 (297e26d) into main (902c91f) will decrease coverage by 0.04%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main     #566      +/-   ##
==========================================
- Coverage   94.77%   94.72%   -0.05%     
==========================================
  Files         179      174       -5     
  Lines       10921    10822      -99     
  Branches     1083     1057      -26     
==========================================
- Hits        10350    10251      -99     
  Misses        571      571              
Impacted Files Coverage Δ
...-instrumentation-aws-lambda/src/instrumentation.ts 91.60% <0.00%> (-0.19%) ⬇️
...entation-document-load/src/enums/AttributeNames.ts
...strumentation-document-load/src/instrumentation.ts
...lemetry-instrumentation-document-load/src/utils.ts
...metry-instrumentation-document-load/src/version.ts
...trumentation-document-load/src/enums/EventNames.ts
...ws-lambda/test/integrations/lambda-handler.test.ts 97.97% <0.00%> (+0.09%) ⬆️

Copy link
Member

@vmarchaud vmarchaud left a comment

Choose a reason for hiding this comment

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

lgtm, i tagged AWS people so they can check too

@prsnca prsnca force-pushed the add-lambda-custom-context-handler branch from 9d9f22f to f596404 Compare July 11, 2021 08:18
Copy link
Contributor

@willarmiros willarmiros left a comment

Choose a reason for hiding this comment

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

Thanks for the addition! Generally LGTM as well, but can you explain a bit more how context would be extracted from those event types other than an HTTP header? I assume it's for context propagation for vendors other than X-Ray since we don't have any special context stored in e.g. Kinesis/CW Events. But perhaps a small example code in the readme or unit test for what the custom extractors would look like would help me greatly.

Copy link
Member

@obecny obecny left a comment

Choose a reason for hiding this comment

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

2 things, with the rest will wait for creator of this instrumentation

Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

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

LGTM with others' comments, thanks!

@prsnca
Copy link
Contributor Author

prsnca commented Jul 13, 2021

Thanks for the addition! Generally LGTM as well, but can you explain a bit more how context would be extracted from those event types other than an HTTP header? I assume it's for context propagation for vendors other than X-Ray since we don't have any special context stored in e.g. Kinesis/CW Events. But perhaps a small example code in the readme or unit test for what the custom extractors would look like would help me greatly.

Sure!
One example could be a SQS message which triggers the Lambda function, where the trace header is found under the message attributes.
The event payload for the Lambda invocation would look as follows:

{
    "Records":
    [
        {
            "body": "someBody",
            "receiptHandle": "someReceiptHandler",
            "md5OfBody": "somemd5value",
            "eventSourceARN": "arn:aws:sqs:us-east-1:000000000000:0a1b2c3e4d5f",
            "eventSource": "aws:sqs",
            "awsRegion": "us-east-1",
            "messageId": "2c45653f-43ad-87e0-947e-074da0a1b45d",
            "attributes":
            {},
            "messageAttributes":
            {
                "traceparent":
                {
                    "stringValue": "00-c0f3ad06b7d55d5f91e1b5cdf32a63aa-002e02c5e90f1e34-01",
                    "dataType": "String"
                }
            },
            "md5OfMessageAttributes": "somemd5hash",
            "sqs": true
        }
    ]
}

The custom context extractor would be able to catch such cases and extract the context from the event.

@prsnca prsnca force-pushed the add-lambda-custom-context-handler branch from f596404 to 297e26d Compare July 13, 2021 15:07
Copy link
Contributor

@willarmiros willarmiros left a comment

Choose a reason for hiding this comment

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

lgtm as well, thanks for the walkthrough @prsnca!

@obecny
Copy link
Member

obecny commented Jul 13, 2021

Please don't force push after any first review / comment

@dyladan dyladan added the enhancement New feature or request label Jul 13, 2021
@prsnca
Copy link
Contributor Author

prsnca commented Jul 15, 2021

Please don't force push after any first review / comment

Sure! Thanks for the comment.
Could you please merge this PR?

@obecny obecny merged commit 4bf3ad5 into open-telemetry:main Jul 15, 2021
@nozik nozik deleted the add-lambda-custom-context-handler branch September 23, 2021 05:41
@NikunjShah
Copy link

Have a question regarding this.How will the context be propagated in terms of SQS batching or other services which support batching. As the context can be fetched for a single message at a single point of time.

@nozik
Copy link
Contributor

nozik commented Jun 4, 2023

@NikunjShah That's a great question. You could handle that in several ways:

  1. Iterate the Records array and create a consumer span for each message (but the handling of the message will appear as a separate trace)
  2. Patch the iteration functions of the Records array (forEach / map / filter) and so that each iteration handler creates a span with the context of the handled message. This is possible in Node, and not really in other languages (and does have limitations, like no support for a "regular" for loop which cannot be patched
  3. You could provide a decorator function that's inserted manually to each iteration in the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants