traceloop / openllmetry

Open-source observability for your LLM application, based on OpenTelemetry
https://www.traceloop.com/openllmetry
Apache License 2.0
1.71k stars 138 forks source link

🐛 Bug Report: OpenAI spans are not always started as current span #1543

Open bastbu opened 1 month ago

bastbu commented 1 month ago

Which component is this bug for?

OpenAI Instrumentation

📜 Description

When having httpx instrumentation enabled, the POST span to the chat completions endpoint does not have openai.chat as a parent.

1541 reports a similar behavior for the Langchain instrumentation.

👟 Reproduction steps

👍 Expected behavior

The POST request should have openai.chat as a parent.

👎 Actual Behavior with Screenshots

The httpx span and the openai.chat span have the same parent.

{
    "name": "POST",
    "context": {
        "trace_id": "0x8252aa8e6eab7da39677aeb684257982",
        "span_id": "0xbbb358e3bb731dda",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": "0x174f1749b1e29d48",
    "start_time": "2024-07-12T05:54:09.501887Z",
    "end_time": "2024-07-12T05:54:10.174165Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.method": "POST",
        "http.url": "<redacted>",
        "http.status_code": 200
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.25.0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}
{
    "name": "openai.chat",
    "context": {
        "trace_id": "0x8252aa8e6eab7da39677aeb684257982",
        "span_id": "0x7a611da25eb9b760",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": "0x174f1749b1e29d48",
    "start_time": "2024-07-12T05:54:09.490627Z",
    "end_time": "2024-07-12T05:54:10.246270Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "llm.request.type": "chat",
        "gen_ai.system": "OpenAI",
        "gen_ai.request.model": "gpt-35-turbo",
        "llm.headers": "None",
        "llm.is_streaming": true,
        "gen_ai.openai.api_base": "<redacted>",
        "gen_ai.openai.api_version": "2023-07-01-preview",
        "gen_ai.response.model": "gpt-35-turbo"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.25.0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}

🤖 Python Version

3.11

📃 Provide any additional context for the Bug.

The spans appear not to be consistently set as the currently active span when starting:

https://github.com/traceloop/openllmetry/blob/fe2393a31cdb307d3638c8995a907f846e8befec/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/chat_wrappers.py#L70-L74

versus

https://github.com/traceloop/openllmetry/blob/fe2393a31cdb307d3638c8995a907f846e8befec/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/embeddings_wrappers.py#L58-L62

👀 Have you spent some time to check if this bug has been raised before?

Are you willing to submit PR?

Yes I am willing to submit a PR!

nirga commented 1 month ago

Thanks for reporting @bastbu! You mentioned you'd like to issue a fix - would love that if you have time! If not let me know and we'll take care of it. Let me know if you need guidance / assistance.

bastbu commented 1 month ago

Thanks @nirga! I realized that the fix is a little bit more involved than I originally anticipated, due to the differences in the non-streaming and streaming cases.

It'd probably take me a while before I get the time to attempt a proper fix for this. Hence, if you'd want to take care of it, that may be better.