Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 117474f

Browse files
agocshghotra
andauthored
Add lambda function url trigger and inferred span (#197)
* Merge trigger code from feature branch only * fix dockerized tests * UNKNOWN event types should return None string * black * black * Make integration tests output valid JSON * update integration tests * Fix misspelling in comment * Actually update snapshots * fix a goof * black * black * Get http status code for lambda function url invocations * update docstring * Set the service on a lambda function url to aws.lambda * Update integration tests and samples * span->service * fix operation name for fn urls * Remove unused import * Add some newlines because I guess we're all running Apple ][s here * Remove span.name from the function execution span Co-authored-by: Harvinder Ghotra <[email protected]> * remove blank line Co-authored-by: Harvinder Ghotra <[email protected]> * change upstream to parent_span part 1 Co-authored-by: Harvinder Ghotra <[email protected]> * change upstream to parent_span part 2 Co-authored-by: Harvinder Ghotra <[email protected]> * change upstream to parent_span part 3 Co-authored-by: Harvinder Ghotra <[email protected]> * Update snapshots * Update datadog_lambda/trigger.py Co-authored-by: Harvinder Ghotra <[email protected]> * Update datadog_lambda/trigger.py Co-authored-by: Harvinder Ghotra <[email protected]> * Remove extension check for inferred spans Co-authored-by: Harvinder Ghotra <[email protected]>
1 parent ce534c1 commit 117474f

15 files changed

+549
-213
lines changed

datadog_lambda/tracing.py

+52
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TraceContextSource,
1414
XrayDaemon,
1515
)
16+
from datadog_lambda.trigger import parse_event_source, EventTypes
1617
from datadog_lambda.xray import (
1718
send_segment,
1819
parse_xray_header,
@@ -377,13 +378,62 @@ def set_dd_trace_py_root(trace_context_source, merge_xray_traces):
377378
)
378379

379380

381+
def create_inferred_span(event, context, make_inferred_spans):
382+
event_source = parse_event_source(event)
383+
try:
384+
if event_source.equals(EventTypes.LAMBDA_FUNCTION_URL):
385+
logger.debug("Function URL event detected. Inferring a span")
386+
return create_inferred_span_from_lambda_function_url_event(event, context)
387+
if not make_inferred_spans:
388+
return None
389+
# the rest of the inferred span logic goes here
390+
except Exception as e:
391+
logger.debug(
392+
"Unable to infer span. Detected type: {}. Reason: {}",
393+
event_source.to_string(),
394+
e,
395+
)
396+
return None
397+
logger.debug("Unable to infer a span: unknown event type")
398+
return None
399+
400+
401+
def create_inferred_span_from_lambda_function_url_event(event, context):
402+
domain = event["requestContext"]["domainName"]
403+
path = event["rawPath"]
404+
tags = {
405+
"operation_name": "aws.lambda.url",
406+
"http.url": domain + path,
407+
"endpoint": path,
408+
"http.method": event["requestContext"]["http"]["method"],
409+
"resource_names": domain + path,
410+
"request_id": context.aws_request_id,
411+
"service": "aws.lambda",
412+
}
413+
request_time_epoch = event["requestContext"]["timeEpoch"]
414+
args = {
415+
"resource": domain + path,
416+
"span_type": "http",
417+
}
418+
tracer.set_tags(
419+
{"_dd.origin": "lambda"}
420+
) # function urls don't count as lambda_inferred,
421+
# because they're in the same service as the inferring lambda function
422+
span = tracer.trace("aws.lambda.url", **args)
423+
if span:
424+
span.set_tags(tags)
425+
span.start = request_time_epoch / 1000
426+
return span
427+
428+
380429
def create_function_execution_span(
381430
context,
382431
function_name,
383432
is_cold_start,
384433
trace_context_source,
385434
merge_xray_traces,
386435
trigger_tags,
436+
parent_span=None,
387437
):
388438
tags = {}
389439
if context:
@@ -415,4 +465,6 @@ def create_function_execution_span(
415465
span = tracer.trace("aws.lambda", **args)
416466
if span:
417467
span.set_tags(tags)
468+
if parent_span:
469+
span.parent_id = parent_span.span_id
418470
return span

0 commit comments

Comments
 (0)