traceloop / traceloop/openllmetry

๐Ÿ› Bug Report: OpenAI spans are not always started as current span

Open
#1,543 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.4k
Forks
1.1k
Avg merge
8d 14h
Merged PRs (30d)
2

Description

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
  • Use opentelemetry-instrumentation-httpx and opentelemetry-instrumentation-openai
  • Send a chat completions request
๐Ÿ‘ 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?
  • I checked and didn't find similar issue
Are you willing to submit PR?

Yes I am willing to submit a PR!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up โ€” it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced chat_wrappers.py and embeddings_wrappers.py sections, comparing how each span is made the current active span. Reproduce the issue with httpx and OpenAI instrumentation while sending a chat completions request. Done means the httpx POST span has openai.chat as its parent, rather than sharing its parent.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.