-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
improve X-Amz-Trace-Id for APIGW NG #11327
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 all commits
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import copy | ||
import logging | ||
import re | ||
import time | ||
from secrets import token_hex | ||
from typing import Type, TypedDict | ||
|
||
from moto.apigateway.models import RestAPI as MotoRestAPI | ||
|
@@ -113,3 +115,27 @@ def validate_sub_dict_of_typed_dict(typed_dict: Type[TypedDict], obj: dict) -> b | |
typed_dict_keys = {*typed_dict.__required_keys__, *typed_dict.__optional_keys__} | ||
|
||
return not bool(set(obj) - typed_dict_keys) | ||
|
||
|
||
def generate_trace_id(): | ||
"""https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids""" | ||
original_request_epoch = int(time.time()) | ||
timestamp_hex = hex(original_request_epoch)[2:] | ||
version_number = "1" | ||
unique_id = token_hex(12) | ||
return f"{version_number}-{timestamp_hex}-{unique_id}" | ||
|
||
|
||
def generate_trace_parent(): | ||
return token_hex(8) | ||
|
||
|
||
def parse_trace_id(trace_id: str) -> dict[str, str]: | ||
split_trace = trace_id.split(";") | ||
trace_values = {} | ||
for trace_part in split_trace: | ||
key_value = trace_part.split("=") | ||
if len(key_value) == 2: | ||
trace_values[key_value[0].capitalize()] = key_value[1] | ||
|
||
return trace_values | ||
Comment on lines
+120
to
+141
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. Question: Do you think we should put those method in a shared utils for more services to have access to? I feel like they will mostly be the same for everywhere? 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. That's a good point! Not sure where it should live for now 🤔 we might keep it here for now, and if we hear about more x-ray traces, move it somewhere else? I believe lambda might have a similar 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. I think Lambda is only using I was thinking that it could make sense in I think it is fine with it here for now, but we should stay alert to prevent this behavior to be implemented many times in the code base! |
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.
Lineage is related to detecting recursive Lambda loops (July 2023): https://aws.amazon.com/blogs/compute/detecting-and-stopping-recursive-loops-in-aws-lambda-functions/
They extended support for S3, SQS, and SNS in Aug 2024: https://aws.amazon.com/blogs/compute/aws-lambda-introduces-recursive-loop-detection-apis/