Repository navigation
feat(aws-lambda): Detect Lambda triggers #8222
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
Open
sentry-junior
wants to merge
5
commits into
junior/aws-lambda-invoke-trace-continuation
Choose a base branch
from
junior/aws-lambda-trigger
base: junior/aws-lambda-invoke-trace-continuation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
61bf998
feat(aws-lambda): Detect Lambda triggers
sentry-junior[bot] f864c51
ref(aws-lambda): Don't set messaging.batch.message_count on the Lambd…
sentry-junior[bot] 7118a80
Merge branch 'junior/aws-lambda-invoke-trace-continuation' into junio…
sentry-junior[bot] d877f00
test(aws-lambda): Keep one test_trigger case per code path
sentry-junior[bot] 380eec4
Merge branch 'junior/aws-lambda-invoke-trace-continuation' into junio…
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Optional | ||
|
|
||
| # `faas.trigger` of each `eventSource` of `Records` or of a top-level `eventSource`. | ||
| # https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html | ||
| _TRIGGERS: "dict[Optional[str], str]" = { | ||
| "aws:sqs": "pubsub", | ||
| "aws:sns": "pubsub", | ||
| "aws:kinesis": "pubsub", | ||
| "aws:ses": "pubsub", | ||
| "aws:kafka": "pubsub", | ||
| "SelfManagedKafka": "pubsub", | ||
| "aws:mq": "pubsub", | ||
| "aws:rmq": "pubsub", | ||
| "aws:s3": "datasource", | ||
| "aws:dynamodb": "datasource", | ||
| "aws:docdb": "datasource", | ||
| } | ||
|
|
||
| # https://docs.aws.amazon.com/eventbridge/latest/ref/overiew-event-structure.html | ||
| _EVENTBRIDGE_KEYS = frozenset( | ||
| ( | ||
| "version", | ||
| "id", | ||
| "detail-type", | ||
| "source", | ||
| "account", | ||
| "time", | ||
| "region", | ||
| "resources", | ||
| "detail", | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def _get_records_source(records: "list[Any]") -> "Optional[str]": | ||
| """The records' `eventSource`, one per event; SNS spells it `EventSource`.""" | ||
| return records[0].get("eventSource") or records[0].get("EventSource") | ||
|
|
||
|
|
||
| def _is_appsync_event(aws_event: "Any") -> bool: | ||
| # https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html | ||
| return ( | ||
| isinstance(aws_event, dict) | ||
| and "arguments" in aws_event | ||
| and "fieldName" in aws_event.get("info", {}) | ||
| ) | ||
|
|
||
|
|
||
| def _get_trigger(aws_event: "Any") -> str: | ||
| """`faas.trigger` from the envelope of the delivered event, never from its payload.""" | ||
| if isinstance(aws_event, list): | ||
| # an AppSync batch resolver delivers a list of resolver events. | ||
| if aws_event and all(_is_appsync_event(event) for event in aws_event): | ||
| return "http" | ||
| return "other" | ||
|
|
||
| if not isinstance(aws_event, dict): | ||
| return "other" | ||
|
|
||
| # API Gateway REST APIs and ALB send `httpMethod`, HTTP APIs and function URLs | ||
| # `requestContext.http`. | ||
| request_context = aws_event.get("requestContext") | ||
| if request_context is not None and ( | ||
| "httpMethod" in aws_event or "http" in request_context | ||
| ): | ||
| return "http" | ||
|
|
||
| if _is_appsync_event(aws_event): | ||
| return "http" | ||
|
|
||
| records = aws_event.get("Records") | ||
| if records: | ||
| # CloudFront and Lambda@Edge records have no `eventSource`. | ||
| if "cf" in records[0]: | ||
| return "http" | ||
| return _TRIGGERS.get(_get_records_source(records), "other") | ||
|
|
||
| if _EVENTBRIDGE_KEYS <= aws_event.keys(): | ||
| if ( | ||
| aws_event["source"] == "aws.events" | ||
| and aws_event["detail-type"] == "Scheduled Event" | ||
| ): | ||
| return "timer" | ||
| return "pubsub" | ||
|
|
||
| return _TRIGGERS.get(aws_event.get("eventSource"), "other") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.