aws-xray-sdk-python: SegmentNotFoundException was raised when AWS_XRAY_SDK_ENABLED=false and use/call any method of xray_recorder

Hello, what is the correct way to disable X-Ray SDK without having to remove from my code the calls to the xray_recorder methods?

I’m using in a Django project that runs on Lambda, however, for local development or in Codebuild, I’d like to deactivate it, but if I disable the SDK by configuring the AWS_XRAY_SDK_ENABLED environment variable to false I get this error:

aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open

My project use

boto3==1.9.144
botocore==1.12.145
aws-xray-sdk==2.4.2
django==2.1.8

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 22 (13 by maintainers)

Commits related to this issue

Most upvoted comments

+1

Meanwhile this is being reviewed, I’ve created the following helper methods in a tracing decorator if that helps anyone:

# Will no longer be needed once #155 is resolved
# https://github.com/aws/aws-xray-sdk-python/issues/155
def _create_subsegment(name=None, client=None):
    if is_trace_disabled:
        segment = DummySegment()
        subsegment = DummySubsegment(segment)
    else:
        subsegment = client.begin_subsegment(name=name)

    return subsegment


def _end_subsegment(subsegment):
    if is_trace_disabled:
        return

    subsegment.end_subsegment()

Hi @mjhanke I’m trying to repro the issue with a sample django app with this configuration. Will get back with an update soon.

In the meantime, can you confirm that when you’re running the app locally or in lambda, the AWS_XRAY_SDK_ENABLED is actually set to false and global_sdk_config.sdk_enabled() indeed returns false. If the SDK is disabled successfully, it should hit this line of code and return a DummySubsegment. Can you debug this and confirm as well?

Thanks.

I’m not sure how to elaborate other than show you all aws-xray-sdk-python related config 😬 let me know if I can be more specific. From our django config.py:

# Note: this are NOQA because they appear unused, but are needed to be patched
# by xray (see `patch_all` call below)
import aiobotocore  # NOQA
import aioboto3  # NOQA
import aiohttp  # NOQA
import botocore  # NOQA
import boto3  # NOQA
import http.client  # NOQA
import requests  # NOQA
from aws_xray_sdk.core import patch_all
...
INSTALLED_APPS = [
   ...,
    "aws_xray_sdk.ext.django"
]
...
MIDDLEWARE = [
    "aws_xray_sdk.ext.django.middleware.XRayMiddleware",
    ...
]
...
# https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-configuration.html#xray-sdk-python-middleware-configuration-django
XRAY_RECORDER = {
    "AUTO_INSTRUMENT": True,  # Record subsegments for built-in database and template rendering operations.
    "AWS_XRAY_TRACING_NAME": os.getenv("AWS_LAMBDA_FUNCTION_NAME"),
}

# X-Ray patches: https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-patching.html
if is_aws():
    patch_all(double_patch=True)

AWS_XRAY_SDK_ENABLED = is_aws()

Hi everyone, this fix was released in the v2.5.0 of the SDK. Hopefully that solves your issue. I’m closing this one. Please feel free to reopen or create a new issue if you’re still having troubles.

Thanks