Skip to content

Bug: Tracing issues #4045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
royassis opened this issue Mar 31, 2024 · 3 comments
Closed

Bug: Tracing issues #4045

royassis opened this issue Mar 31, 2024 · 3 comments
Assignees

Comments

@royassis
Copy link
Contributor

Expected Behaviour

Traceback is not being logged or raised

Current Behaviour

Traceback attached below is being logged and raised, when the pymongo package and tracing is being used in code.

Code snippet

Traceback (most recent call last):
  File "/var/task/pymongo/monitoring.py", line 1519, in publish_command_success
    subscriber.succeeded(event)
  File "/var/task/aws_xray_sdk/ext/pymongo/patch.py", line 37, in succeeded
    subsegment.put_annotation('mongodb_duration_micros', event.duration_micros)
AttributeError: 'NoneType' object has no attribute 'put_annotation'
[WARNING]	2024-03-31T16:26:55.661Z	9a41172c-e61b-4d05-a460-6eed564ec3f7	Subsegment appdata@172.30.3.165:27017 discarded due to Lambda worker still initializing
Traceback (most recent call last):
  File "/var/task/pymongo/monitoring.py", line 1519, in publish_command_success
    subscriber.succeeded(event)
  File "/var/task/aws_xray_sdk/ext/pymongo/patch.py", line 37, in succeeded
    subsegment.put_annotation('mongodb_duration_micros', event.duration_micros)

Possible Solution

No response

Steps to Reproduce

Create a function and initialize connection to mongodb database with pymongo.
Use tracer.capture_lambda_handler and tracer.capture_method.

Where:

from aws_lambda_powertools import Logger, Tracer
tracer = Tracer().

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.11

Packaging format used

PyPi

Debugging logs

No response

@royassis royassis added bug Something isn't working triage Pending triage from maintainers labels Mar 31, 2024
@leandrodamascena
Copy link
Contributor

Hey @royassis! It's great to see you back here! 😊

After examining the exception and reviewing the code, it appears that the error originates from this line. I have a hypothesis regarding the cause, and I'd like to confirm it with you.

1 - Are you running this code locally? I'm asking because I noticed the presence of the [WARNING] 2024-03-31T16:26:55.661Z 9a41172c-e61b-4d05-a460-6eed564ec3f7 Subsegment [email protected]:27017 discarded due to Lambda worker still initializing line, which typically indicates an error when running outside of AWS Lambda.
If this is the case, the error is expected. When running locally with an emulator, there is no X-Ray root segment, and therefore X-Ray cannot obtain the subsegment using xray_recorder.current_subsegment().
Just a quick explanation on this matter: when you activate "X-Ray active tracing" in your Lambda configuration on AWS, then AWS Lambda automatically generates a root segment. All subsequent Tracer information is then added in the form of subsegments.

2 - Are you using a Powertools version prior to 2.36.0? Starting from version 2.36.0, we automatically disable the X-Ray tracer in environments such as Chalice and AWS SAM to prevent this type of error.

I used this code in my Lambda function and it worked as expected:

from aws_lambda_powertools import Tracer
from aws_lambda_powertools.utilities.typing import LambdaContext
from pymongo import MongoClient

tracer = Tracer() 

@tracer.capture_method
def db_conn():
    uri = "mongodb+srv://USER:[email protected]/?retryWrites=true&w=majority&appName=test-powertools"
    mongodb_client = MongoClient(uri)
    return mongodb_client


@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> str:
    conn = db_conn()
    print(conn.server_info())
    return {"test": "ok"}

image

Thanks

@leandrodamascena leandrodamascena added not-a-bug and removed bug Something isn't working triage Pending triage from maintainers labels Apr 1, 2024
@leandrodamascena leandrodamascena self-assigned this Apr 1, 2024
@leandrodamascena leandrodamascena moved this from Triage to Pending customer in Powertools for AWS Lambda (Python) Apr 1, 2024
@royassis
Copy link
Contributor Author

royassis commented Apr 1, 2024

Thanks @leandrodamascena :)

Issue resolved

@royassis royassis closed this as completed Apr 1, 2024
@github-project-automation github-project-automation bot moved this from Pending customer to Coming soon in Powertools for AWS Lambda (Python) Apr 1, 2024
Copy link
Contributor

github-actions bot commented Apr 1, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Shipped
Development

No branches or pull requests

2 participants