{"id":17601242,"url":"https://github.com/sam-martin/aws-xray-lambda-segment-shim","last_synced_at":"2025-04-30T07:49:31.451Z","repository":{"id":47505882,"uuid":"398627268","full_name":"Sam-Martin/aws-xray-lambda-segment-shim","owner":"Sam-Martin","description":"A shim which faciliates the AWS X-Ray tracing of Lambda Functions invoked by triggers which don't have native X-Ray support.","archived":false,"fork":false,"pushed_at":"2021-08-28T10:14:10.000Z","size":169,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-30T07:49:26.268Z","etag":null,"topics":["aws","aws-lambda","aws-lambda-python","aws-xray","tracing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sam-Martin.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-21T18:12:09.000Z","updated_at":"2023-04-24T01:44:47.000Z","dependencies_parsed_at":"2022-09-07T03:02:05.326Z","dependency_job_id":null,"html_url":"https://github.com/Sam-Martin/aws-xray-lambda-segment-shim","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sam-Martin%2Faws-xray-lambda-segment-shim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sam-Martin%2Faws-xray-lambda-segment-shim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sam-Martin%2Faws-xray-lambda-segment-shim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sam-Martin%2Faws-xray-lambda-segment-shim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sam-Martin","download_url":"https://codeload.github.com/Sam-Martin/aws-xray-lambda-segment-shim/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251666226,"owners_count":21624290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","aws-lambda-python","aws-xray","tracing"],"created_at":"2024-10-22T12:09:16.080Z","updated_at":"2025-04-30T07:49:31.389Z","avatar_url":"https://github.com/Sam-Martin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"aws-xray-lambda-segment-shim\n=====================================\n\n|shield1| |shield2|\n\n.. |shield1| image:: https://img.shields.io/github/workflow/status/sam-martin/aws-xray-lambda-segment-shim/Linting%20\u0026%20Testing?style=flat-square\n    :target: https://github.com/Sam-Martin/aws-xray-lambda-segment-shim/actions/workflows/continuous-integration.yml?query=branch%3Amain+\n    :alt: GitHub Workflow Status\n\n.. |shield2|  image:: https://img.shields.io/pypi/v/aws-xray-lambda-segment-shim?style=flat-square\n    :target: https://pypi.org/project/aws-xray-lambda-segment-shim/\n    :alt: PyPI\n\n.. image:: https://github.com/Sam-Martin/aws-xray-lambda-segment-shim/blob/main/images/example.png?raw=true\n\n| **Full Docs:** https://aws-xray-lambda-segment-shim.readthedocs.io\n| **GitHub:** https://github.com/Sam-Martin/aws-xray-sqs-lambda-segment-shim\n\nIt's not currently possibly follow an AWS Xray trace through a Lambda Function triggered by an SQS Queue.\n\nUnless you use ``aws-xray-lambda-segment-shim``!\n\nInstallation\n----------------\n\n.. code-block::\n\n    pip install aws-xray-lambda-segment-shim\n\n\nUsage\n------\n\n.. doctest::\n\n    from aws_xray_lambda_segment_shim import get_sqs_triggered_recorder\n\n\n    def lambda_handler(event, context):\n        for i, record in enumerate(event[\"Records\"]):\n            recorder = get_sqs_triggered_recorder(\n                record=record,\n                lambda_request_id=context.aws_request_id,\n                lambda_arn=context.invoked_function_arn,\n            )\n            with recorder.in_segment():\n                with recorder.in_subsegment(f\"SQS Record {i}\") as subsegment:\n                    print(\n                        \"I'm triggered by an SQS Record and using trace id \",\n                        subsegment.trace_id,\n                    )\n\n\n``TriggeredXrayRecorder`` is a child class of ``aws_xray_sdk.AWSXRayRecorder`` so you can use all the methods you would expect\nfrom following the `aws-xray-sdk documentation \u003chttps://github.com/aws/aws-xray-sdk-python/\u003e`__.\n\n\nCaveats\n----------\n\nThis approach causes all subsegments created with it to appear only in the trace that was passed in by SQS.\nThere will still be a separate Lambda trace that will not contain these subsegments and will not show as\nbeing triggered by SQS.\n\nThis approach is useful if you are using SQS as an intermediary for a process you're already tracing as it\nthen makes logical sense to view the traces from that starting point.\n\nIf you're more likely to view your traces as starting at the lambda function\n(i.e. you do **not** have any tracing prior to the SQS queue) then your mileage may vary with this approach.\n\nWe are also here working outside the scope of what is expected by the aws-xray-sdk.\nWe are pretending to be AWS Lambda when we're initiating a trace, we're using undocumented fields to\npretend to be AWS Lambda, and to allow the correlation of the SQS message and the Lambda Invocation (edge creation).\n\nIf this wasn't the only way to pursue a trace through SQS to lambda I would suggest you avoid it! But given the\ncomplexity involved in automating this from AWS's side, it may be a while before we see native support.\n\n- `Issue on Python SDK \u003chttps://github.com/aws/aws-xray-sdk-python/issues/173\u003e`__\n- `Issue on .NET SDK \u003chttps://github.com/aws/aws-xray-sdk-dotnet/issues/110\u003e`__\n- `Issue on Node SDK \u003chttps://github.com/aws/aws-xray-sdk-node/issues/208\u003e`__\n\nTesting\n---------\n\nYou can run the python tests with:\n\n.. code-block::\n\n    pip install -r requirements.txt\n    make local-test\n\n\nTerraform Tests\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nYou can run the terraform tests with:\n\n.. code-block::\n\n    $ make install-xray-daemon-mac\n    $ make run-xray-daemon-mac\n\nThen in a new terminal run:\n\n.. code-block::\n\n    $ make terraform-init\n    $ make terraform-apply\n    $ make terraform-send-message\n\nOnce that's complete you can click on the xray URL provided at the end of the test to observe the trace is correct.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsam-martin%2Faws-xray-lambda-segment-shim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsam-martin%2Faws-xray-lambda-segment-shim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsam-martin%2Faws-xray-lambda-segment-shim/lists"}