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

AzureMonitorTraceExporter not creating Exception envelopes #20281

Closed
tonybaloney opened this issue Aug 14, 2021 · 3 comments · Fixed by #23708
Closed

AzureMonitorTraceExporter not creating Exception envelopes #20281

tonybaloney opened this issue Aug 14, 2021 · 3 comments · Fixed by #23708
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. Monitor - Exporter Monitor OpenTelemetry Exporter Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@tonybaloney
Copy link
Contributor

This example FastAPI application --

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (BatchSpanProcessor,
                                            ConsoleSpanExporter,
                                            SimpleSpanProcessor)

from example_app.models import Settings

app = FastAPI()

settings = Settings(_env_file=".env")
tracer = TracerProvider()


@app.get('/bad_page')
def bad_page():
    total = 0
    for i in range(-5, 10):
        total += 5 / i  # will hit a ZeroDivisionError
    return total 


@app.on_event("startup")
def startup_event():
    if settings.verbose_tracing:
        tracer.add_span_processor(
            SimpleSpanProcessor(ConsoleSpanExporter())
        )

    if settings.app_insights_connection_string:
        exporter = AzureMonitorTraceExporter.from_connection_string(
            settings.app_insights_connection_string
        )
        tracer.add_span_processor(
            BatchSpanProcessor(exporter)
        )

    FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer)

A request to /bad_page will raise a ZeroDivisionError, which is then added to the request span under the exception attribute.

{
    "name": "/bad_page",
    "context": {
        "trace_id": "0xcbd32ecdaaee270eb09a5cf4bdb3677e",
        "span_id": "0xf80cc259c0d35a97",
        "trace_state": "[]"
    },
    "kind": "SpanKind.SERVER",
    "parent_id": null,
    "start_time": "2021-08-14T04:31:13.590721Z",
    "end_time": "2021-08-14T04:31:13.602652Z",
    "status": {
        "status_code": "ERROR",
        "description": "ZeroDivisionError: division by zero"
    },
    "attributes": {
        "http.scheme": "http",
        "http.host": "127.0.0.1:8000",
        "net.host.port": 8000,
        "http.flavor": "1.1",
        "http.target": "/bad_page",
        "http.url": "http://127.0.0.1:8000/bad_page",
        "http.method": "GET",
        "http.server_name": "127.0.0.1:8000",
        "http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15",
        "net.peer.ip": "127.0.0.1",
        "net.peer.port": 52005,
        "http.route": "/bad_page"
    },
    "events": [
        {
            "name": "exception",
            "timestamp": "2021-08-14T04:31:13.602515Z",
            "attributes": {
                "exception.type": "ZeroDivisionError",
                "exception.message": "division by zero",
                "exception.stacktrace": "Traceback (most recent call last):\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/opentelemetry/trace/__init__.py\", line 525, in use_span\n    yield span\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/opentelemetry/sdk/trace/__init__.py\", line 961, in start_as_current_span\n    yield span_context\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/opentelemetry/instrumentation/asgi/__init__.py\", line 245, in __call__\n    await self.app(scope, wrapped_receive, wrapped_send)\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/starlette/exceptions.py\", line 82, in __call__\n    raise exc from None\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/starlette/exceptions.py\", line 71, in __call__\n    await self.app(scope, receive, sender)\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/starlette/routing.py\", line 580, in __call__\n    await route.handle(scope, receive, send)\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/starlette/routing.py\", line 241, in handle\n    await self.app(scope, receive, send)\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/starlette/routing.py\", line 52, in app\n    response = await func(request)\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/fastapi/routing.py\", line 220, in app\n    dependant=dependant, values=values, is_coroutine=is_coroutine\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/fastapi/routing.py\", line 154, in run_endpoint_function\n    return await run_in_threadpool(dependant.call, **values)\n  File \"/Users/anthonyshaw/Library/Caches/pypoetry/virtualenvs/09-opentelemetry-fastapi-yQ0OWPAy-py3.7/lib/python3.7/site-packages/starlette/concurrency.py\", line 40, in run_in_threadpool\n    return await loop.run_in_executor(None, func, *args)\n  File \"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py\", line 57, in run\n    result = self.fn(*self.args, **self.kwargs)\n  File \"./example_app/routes.py\", line 12, in bad_page\n    total += 5 / i  # will hit a ZeroDivisionError\nZeroDivisionError: division by zero\n",
                "exception.escaped": "False"
            }
        }
    ],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.4.1",
        "service.name": "unknown_service"
    }
}

However, the Azure Exporter only send the bad request to App Insights, there is no Exception record in the portal

screenshot 2021-08-14 at 14 39 48

This previously worked in open census.

FYI @lzchen

@ghost ghost added the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Aug 14, 2021
@xiangyan99 xiangyan99 added the Monitor - Exporter Monitor OpenTelemetry Exporter label Aug 16, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Aug 16, 2021
@xiangyan99 xiangyan99 added Client This issue points to a problem in the data-plane of the library. Service Attention Workflow: This issue is responsible by Azure service team. labels Aug 16, 2021
@lzchen
Copy link
Member

lzchen commented Aug 16, 2021

Related to: #16413

@johnchildren
Copy link

The way that this seems to be handled in a similar library for Rust is to have multiple Envelopes per Span and record each event in a span as a separate Envelope: https://github.com/frigus02/opentelemetry-application-insights/blob/edcea396750b4232eaf645941bc3f47556824fa9/src/trace.rs#L53

Would this be desirable? It's a more intrusive change but would make the exceptions visible I believe.

@lzchen
Copy link
Member

lzchen commented Oct 4, 2021

@johnchildren
Yes this feature would be desirable. It will be implemented when #16413 is addressed.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. Monitor - Exporter Monitor OpenTelemetry Exporter Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants