Skip to content
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

Spans do not have the parent child relationship when created using the decorator : @tracer.start_as_current_span() #35042

Closed
summahto opened this issue Apr 2, 2024 · 7 comments
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Monitor - Exporter Monitor OpenTelemetry Exporter Monitor Monitor, Monitor Ingestion, Monitor Query needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@summahto
Copy link

summahto commented Apr 2, 2024

  • **Package Name and version **: azure-monitor-opentelemetry :1.3.0 , azure-monitor-opentelemetry-exporter :1.0.0b23
  • Operating System: Ubuntu WSL 2.0
  • Python Version: 3.11.8

Describe the bug
Different behavior is seen in Application Insights when a span is created with and without a decorator.

To Reproduce
Steps to reproduce the behavior:

  1. Create a sample FastAPI application and create an endpoint.
  2. Call a function test1() within this endpoint.
  3. Within the function test1() call 2 more functions test2() and test3()

Here is the sample code :

CASE 1:

logger = logging.getLogger(__name__)
logger.setLevel(logger.parent.level)
tracer = trace.get_tracer('doc_generator_api')

configure_azure_monitor(
        connection_string= os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"),
        logger_name=docgen_logger.name
    )

FastAPIInstrumentor.instrument_app(app)


# This generates test2 and test3 span as child spans within test1 span. which is as expected.
@app.get("/healthcheck")
async def healthcheck():
    """Responses with HTTP status code 200 and empty body"""
    await test1()
    return Response()


async def test1():
    with tracer.start_as_current_span("test1"):
        for i in range(10, 20):
            logger.info(i)
        await test2()
        await test3()

@tracer.start_as_current_span("test2")
async def test2():
    for i in range(20, 30):
        logger.info(i)

@tracer.start_as_current_span("test3")
async def test3():
    for i in range(30, 40):
        logger.info(i)

CASE 2:

# This generates 3 separate spans without any parent child relationship between them
@tracer.start_as_current_span("test1")
async def test1():
    for i in range(10, 20):
        logger.info(i)
    await test2()
    await test3()

@tracer.start_as_current_span("test2")
async def test2():
    for i in range(20, 30):
        logger.info(i)

@tracer.start_as_current_span("test3")
async def test3():
    for i in range(30, 40):
        logger.info(i)

Expected behavior
Behavior should be the same in both the CASES 1 and 2. The correct one being the spans seen in CASE 1. Attached the screenshots for the spans as visualized in Application Insights in the following section.

According to Opentelemetry's documentation Create-span-using-decorator :

Creating spans with decorators
It’s common to have a single span track the execution of an entire function. In that scenario, there is a decorator you can use to reduce code:

@tracer.start_as_current_span("do_work")
def do_work():
    print("doing some work...")

Use of the decorator is equivalent to creating the span inside do_work() and ending it when do_work() is finished.
To use the decorator, you must have a tracer instance available global to your function declaration. Based on this definition
of how tracer decorator should create spans, the behavior of CASE1 and CASE2 should be the same.

Screenshots
Spans seen in Application Insights CASE1 (which is fine and expected) :
image

Spans seen in Application Insights CASE2 (which is wrong and should be the same as CASE 1 ) :
image

@summahto summahto changed the title Spans do not have the parent child relationship when created using the decorator : Spans do not have the parent child relationship when created using the decorator : @tracer.start_as_current_span() Apr 2, 2024
@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Apr 2, 2024
@xiangyan99
Copy link
Member

Thanks for reaching out.

Could you share the information which Azure service and Azure SDK do you use?

@xiangyan99 xiangyan99 added needs-author-feedback Workflow: More information is needed from author to address the issue. and removed needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Apr 3, 2024
Copy link

github-actions bot commented Apr 3, 2024

Hi @summahto. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@summahto
Copy link
Author

summahto commented Apr 3, 2024

@xiangyan99 ,

Could you share the information which Azure service and Azure SDK do you use?

Azure SDK used :
azure-monitor-opentelemetry :1.3.0
azure-monitor-opentelemetry-exporter :1.0.0b23

Azure Services used :
azure-cosmos = "^4.3"
azure-identity = "^1.13"
azure-purview-catalog = "1.0.0b4"
azure-storage-blob = "12.18.2"

Here are the dependencies which we are using for our FastAPI application:

[tool.poetry.dependencies]
aiohttp = "^3.8"
aiofiles = "^23.2"
azure-cosmos = "^4.3"
azure-identity = "^1.13"
azure-monitor-opentelemetry = "^1.2.0"
azure-purview-catalog = "1.0.0b4"
azure-storage-blob = "12.18.2"
fastapi = "^0"  # TODO Pin minor version before releasing to PROD
fastapi-sessions = "^0"
httpx = "^0"  # TODO Pin minor version before releasing to PROD
opentelemetry-instrumentation = "^0.44b0"
opentelemetry-instrumentation-aiohttp-client = "^0.44b0"
opentelemetry-instrumentation-httpx = "^0.44b0"
opentelemetry-instrumentation-asyncio = "^0.44b0"
opentelemetry-instrumentation-logging = "^0.44b0"
opentelemetry-distro = "^0.44b0"
uvicorn = { extras = ["standard"], version = ">=0.21, <0.22" }

@github-actions github-actions bot added needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team and removed needs-author-feedback Workflow: More information is needed from author to address the issue. labels Apr 3, 2024
@xiangyan99 xiangyan99 added Monitor Monitor, Monitor Ingestion, Monitor Query Service Attention Workflow: This issue is responsible by Azure service team. Monitor - Exporter Monitor OpenTelemetry Exporter labels Apr 3, 2024
Copy link

github-actions bot commented Apr 3, 2024

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jeremydvoss @lzchen.

@lzchen
Copy link
Member

lzchen commented Apr 3, 2024

@summahto

This seems to be an issue related to the opentelemetry sdk and not specifically azure monitor. Please open up an issue in the opentelemetry repository.

@summahto
Copy link
Author

summahto commented Apr 3, 2024

@lzchen

All right. Can we link the 2 issues once I have created the issue in opentelemetry repository. Issue created in opentelemetry repository : open-telemetry/opentelemetry-python#3832

@xiangyan99
Copy link
Member

Please use the one in open-telemetry repo to track the issue.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Monitor - Exporter Monitor OpenTelemetry Exporter Monitor Monitor, Monitor Ingestion, Monitor Query needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

3 participants