Skip to content

Commit d35c6d4

Browse files
committed
Generate root trace id if it doesn't exist
1 parent 98a6464 commit d35c6d4

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

localstack-core/localstack/aws/handlers/tracing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __call__(self, chain: HandlerChain, context: RequestContext, response: Respo
1515
trace_header_str = context.request.headers.get("X-Amzn-Trace-Id")
1616
# Naming aws_trace_header inspired by AWSTraceHeader convention for SQS:
1717
# https://docs.aws.amazon.com/xray/latest/devguide/xray-services-sqs.html
18-
context.trace_context["aws_trace_header"] = TraceHeader.from_header_str(trace_header_str)
18+
aws_trace_header = TraceHeader.from_header_str(trace_header_str).ensure_root_exists()
19+
context.trace_context["aws_trace_header"] = aws_trace_header
1920
# NOTE: X-Ray sampling might require service-specific decisions:
2021
# https://docs.aws.amazon.com/xray/latest/devguide/xray-console-sampling.html

localstack-core/localstack/services/lambda_/invocation/event_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SQSInvocation:
5959

6060
def encode(self) -> str:
6161
# Encode TraceHeader as string
62-
aws_trace_header = self.invocation.trace_context.get("aws_trace_header") or TraceHeader()
62+
aws_trace_header = self.invocation.trace_context.get("aws_trace_header")
6363
aws_trace_header_str = aws_trace_header.to_header_str()
6464
self.invocation.trace_context["aws_trace_header"] = aws_trace_header_str
6565
return json.dumps(

localstack-core/localstack/services/lambda_/invocation/execution_environment.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from typing import Callable, Dict, Optional
99

1010
from localstack import config
11-
from localstack.aws.api.lambda_ import TracingMode
1211
from localstack.aws.connect import connect_to
1312
from localstack.services.lambda_.invocation.lambda_models import (
1413
Credentials,
@@ -26,7 +25,6 @@
2625
is_lambda_debug_timeout_enabled_for,
2726
)
2827
from localstack.utils.strings import to_str
29-
from localstack.utils.xray.trace_header import TraceHeader
3028

3129
STARTUP_TIMEOUT_SEC = config.LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT
3230
HEX_CHARS = [str(num) for num in range(10)] + ["a", "b", "c", "d", "e", "f"]
@@ -342,11 +340,8 @@ def get_prefixed_logs(self) -> str:
342340

343341
def invoke(self, invocation: Invocation) -> InvocationResult:
344342
assert self.status == RuntimeStatus.RUNNING
345-
aws_trace_header = invocation.trace_context.get("aws_trace_header") or TraceHeader()
346-
# TODO: test and implement passive tracing and sampling decisions. Using a simple heuristic for now:
347-
# If TracingMode is "Active", we always sample the request.
348-
if self.function_version.config.tracing_config_mode == TracingMode.Active:
349-
aws_trace_header.sampled = 1
343+
aws_trace_header = invocation.trace_context.get("aws_trace_header")
344+
# TODO: test and implement Active and PassThrough tracing and sampling decisions.
350345
# TODO: implement Lambda lineage: https://docs.aws.amazon.com/lambda/latest/dg/invocation-recursion.html
351346
invoke_payload = {
352347
"invoke-id": invocation.request_id, # TODO: rename to request-id (requires change in lambda-init)

localstack-core/localstack/utils/xray/trace_header.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
# It is adapted from aws-xray-sdk-python licensed under the Apache License 2.0.
33
# You may obtain a copy of the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0
44
# Original source: https://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/core/models/trace_header.py
5-
# Modifications: Add optional lineage field for https://docs.aws.amazon.com/lambda/latest/dg/invocation-recursion.html
5+
# Modifications:
6+
# * Add optional lineage field for https://docs.aws.amazon.com/lambda/latest/dg/invocation-recursion.html
7+
# * Add ensure_root_exists() to generate root trace id
68

79
import logging
810

11+
from localstack.utils.xray.traceid import TraceId
12+
913
log = logging.getLogger(__name__)
1014

1115
ROOT = "Root"
@@ -103,6 +107,15 @@ def to_header_str(self):
103107

104108
return HEADER_DELIMITER.join(h_parts)
105109

110+
def ensure_root_exists(self):
111+
"""
112+
Ensures that a root trace id exists by generating one if None.
113+
Return self to allow for chaining.
114+
"""
115+
if self._root is None:
116+
self._root = TraceId().to_id()
117+
return self
118+
106119
@property
107120
def root(self):
108121
"""

0 commit comments

Comments
 (0)