-
Notifications
You must be signed in to change notification settings - Fork 45
Send errors metrics for 5xx response from API Gateway, Lambda Function URL, or ALB #229
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
Changes from 1 commit
de5aba0
ad09cc9
f5e80a8
29f4117
59b1f69
d60572b
d02855a
a7cb6ef
f71679c
9730be5
43fad9d
abcf63c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -110,6 +110,7 @@ def __init__(self, func): | |||
os.environ.get("DD_TRACE_MANAGED_SERVICES", "true").lower() == "true" | ||||
) | ||||
self.response = None | ||||
self.already_submitted_errors_metric_before = False | ||||
|
||||
if self.extractor_env: | ||||
extractor_parts = self.extractor_env.rsplit(".", 1) | ||||
|
@@ -143,6 +144,7 @@ def __call__(self, event, context, **kwargs): | |||
return self.response | ||||
except Exception: | ||||
submit_errors_metric(context) | ||||
self.already_submitted_errors_metric_before = True | ||||
if self.span: | ||||
self.span.set_traceback() | ||||
raise | ||||
|
@@ -190,6 +192,9 @@ def _after(self, event, context): | |||
status_code = extract_http_status_code_tag(self.trigger_tags, self.response) | ||||
if status_code: | ||||
self.trigger_tags["http.status_code"] = status_code | ||||
if not self.already_submitted_errors_metric_before: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a better way to solve this problem is to reset There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, in your current implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch.
And when the exception happens, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's initialized to None, but it doesn't get reset to None between invocations. E.g., you have a good invocation, which would set self.response, then you have a failed invocation, self.response won't reset to None. extract_http_status_code_tag would extract the status code from the last good invocation instead of from the current failed invocation. Even putting your use case aside, the state of self.response probably should be reset to None on before every invocation, keeping the invocation response value from the previous invocation in the current invocation is meaningless and can only lead to bugs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe one context you missed here is that Lambda isn't truly stateless, a sandbox/container is reused for many invocations until it gets recycled by AWS. https://pfisterer.dev/posts/aws-lambda-container-reuse might be a good read? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for sharing the post! Just want to be sure (since the post mentioned only about shared folder -- I plan to reset There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, a new container starts up on first invocation, your python app get loaded into memory, that's called cold start, then from there, the container will be reused for subsequent invocations, until AWS recycles it, or AWS needs to launch more containers to handle more requests (one lambda container can only handle one request/invocation at a time) |
||||
if len(status_code) == 3 and status_code.startswith("5"): | ||||
submit_errors_metric(context) | ||||
kimi-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
# Create a new dummy Datadog subsegment for function trigger tags so we | ||||
# can attach them to X-Ray spans when hybrid tracing is used | ||||
if self.trigger_tags: | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replied to the original conversation, but want to point out, in case of invocation fails,
self.response
would still hold the value from the previous good invocation, and you would end up emitting an error metric based on the previous invocation instead of the current one.