|
6 | 6 | import logging |
7 | 7 |
|
8 | 8 | from aws_xray_sdk.core import xray_recorder |
| 9 | +from aws_xray_sdk.core.lambda_launcher import LambdaContext |
9 | 10 |
|
10 | 11 | from ddtrace import patch, tracer |
11 | 12 | from datadog_lambda.constants import ( |
@@ -94,6 +95,10 @@ def get_dd_trace_context(): |
94 | 95 | automatically, but this function can be used to manually inject the trace |
95 | 96 | context to an outgoing request. |
96 | 97 | """ |
| 98 | + if not is_lambda_context(): |
| 99 | + logger.debug('get_dd_trace_context is only supported in LambdaContext') |
| 100 | + return {} |
| 101 | + |
97 | 102 | global dd_trace_context |
98 | 103 | xray_trace_entity = xray_recorder.get_trace_entity() # xray (sub)segment |
99 | 104 | if dd_trace_context: |
@@ -124,7 +129,12 @@ def set_correlation_ids(): |
124 | 129 |
|
125 | 130 | TODO: Remove me when Datadog tracer is natively supported in Lambda. |
126 | 131 | """ |
| 132 | + if not is_lambda_context(): |
| 133 | + logger.debug('set_correlation_ids is only supported in LambdaContext') |
| 134 | + return |
| 135 | + |
127 | 136 | context = get_dd_trace_context() |
| 137 | + |
128 | 138 | span = tracer.trace('dummy.span') |
129 | 139 | span.trace_id = context[TraceHeader.TRACE_ID] |
130 | 140 | span.span_id = context[TraceHeader.PARENT_ID] |
@@ -154,3 +164,11 @@ def inject_correlation_ids(): |
154 | 164 | patch(logging=True) |
155 | 165 |
|
156 | 166 | logger.debug('logs injection configured') |
| 167 | + |
| 168 | + |
| 169 | +def is_lambda_context(): |
| 170 | + """ |
| 171 | + Return True if the X-Ray context is `LambdaContext`, rather than the |
| 172 | + regular `Context` (e.g., when testing lambda functions locally). |
| 173 | + """ |
| 174 | + return type(xray_recorder.context) == LambdaContext |
0 commit comments