open-telemetry / open-telemetry/opentelemetry-python-contrib

Interpret TraceParent from non-http lambda events

Open
#2,764 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

What problem do you want to solve?

The default implementation of the aws-lambda instrumentation will only determine trace parent from http headers, but lambdas can be invoked through other mechanisms: sqs, event bridge, etc. It would be nice to make a basic attempt to determine trace parent for these other types.

Describe the solution you'd like

I would like the default context extractor method to perform further inspections on the event dictionary to determine if the trace parent can be obtained from other keys in the dictionary.

Describe alternatives you've considered

Currently I am parsing the event in a dummy handler and adding the sqs traceparent to event['headers'] to trick the instrumentation into assigning the correct parent. This solution works for a lambda event source batch size of 1, but doesn't make sense for larger than 1.

def handler(event, context):
    records = event.get("Records")
    if records:
        event_source = records[0].get("EventSource") or records[0].get("eventSource")
        if event_source == "aws:sqs":
            # inject header information for sqs
            traceparent = records[0].get("messageAttributes", {}).get('traceparent', {}).get('stringValue')
            if traceparent:
                headers = event.get("headers", {})
                headers['traceparent'] = traceparent
                event["headers"] = headers

    return real_handler(event, context)
Additional Context

I don't think we have to solve for every scenario, but a better best effort would be appreciated. For example, if the event type is sqs and record size is 1, then attempt to pull the parent from message attributes. If the record size is larger than 1, then parentage can't be automatically determined and the end user should manually instrument parents of spans.

Would you like to implement a fix?

Yes

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the default context extractor in the AWS Lambda instrumentation and inspect how it currently reads HTTP headers. Use the SQS Records and messageAttributes example as the first non-HTTP case to evaluate, including the single-record limitation. Done should include a best-effort parent extraction without breaking existing HTTP handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
cloud
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.