opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

Breaking change in 1.40 to Tracer/Span behavior in opentelemetry-api not documented

Open Oberon00 opened this issue 8 months ago • 9 comments

PR https://github.com/open-telemetry/opentelemetry-python/pull/4861 involved breaking changes to fundamental behavior of the opentelemetry-api package, namely the Span/Tracer behavior that IIUC should be stable. Mainly the span context can differ in cases where it was previously the invalid (all-zero) span context, which may also lead to differing extract/inject behavior. E.g. a service that previously did not propagate any trace/span ID from incoming to outgoing request might now do so. But there are also more subtle differences that might break code that e.g. relied on identity checks for INVALID_SPAN (e.g. https://github.com/open-telemetry/opentelemetry-python-contrib/blob/242cfe1c58d6896144d75c404a4784d4bd16cfed/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py#L152, not that I can think of any likely scenario where this one would actually be hit).

So I don't want to say that it requires a major version bump (although I also don't want to say it wouldn't make sense), but it should IMHO at least be documented more clearly in release notes.

Maybe these changes were even all unintentional, then maybe it still makes sense to revert them, as the main feature that the PR was meant to implement is only experimental.

Oberon00 avatar Mar 24 '26 10:03 Oberon00

We're seeing issues updating to 1.40 as well over at https://github.com/litestar-org/litestar/. A majority of our OTEL integration tests are breaking with that version. We'll pin it for now, but we can't roll out a new release of ours with support for 1.40, since it'd break all downstream applications using our OTEL integration.

provinzkraut avatar Mar 26 '26 14:03 provinzkraut

We've also has been trying to unsuccessfully find the cause of some "memory leaks" after upgrading to 1.40. It turns out we do have a few things instrumented that relies on an identity check for INVALID_SPAN.

cobusc avatar Mar 26 '26 15:03 cobusc

Sorry to hear about this. What do folks think about patching 1.40 to allow for opting out of the new behavior via an environment variable (e.g. OTEL_PYTHON_NOOP_LEGACY_CONTEXT=true)? And to prevent releasing another breaking change, the default could be the new, and to my understanding, spec-compliant, behavior.

Either way, we should probably highlight and explain the breaking change by amending the release notes.

pmcollins avatar Mar 26 '26 18:03 pmcollins

Don't think an env var is going to work for our use case; As a framework, we can't (and should not) override our user's env.

provinzkraut avatar Mar 26 '26 19:03 provinzkraut

I think the purpose of the change was to propagate valid trace context even if the sdk wasn't installed (@xrmx please correct me if I'm wrong). Most likely a bug on our part since other languages have implemented the same way here and here. I don't know the history of this PR but it seems like this was found while implementing the experimental trace configurator. We do treat bug fixes and spec non-compliance as needing to fix and the non-compliant behavior should not be relied on but it is technically a breaking change. For now, we will call that in the release notes and we will discuss how we want to proceed with rolling this change out.

lzchen avatar Mar 26 '26 19:03 lzchen

As @lzchen said that was actually a fix. Should probably have split the fix from the feature to avoid the confusion.

xrmx avatar Mar 30 '26 08:03 xrmx

IMHO it is not so relevant here that it can be considered a fix when considering the shared spec, since the "broken" behavior is not some edge case but actually so commonplace that it was impossible not to rely on it. From perspective of a Python SDK user who should not need to not know the spec details or other technology's SDK, it will be hard to consider this as a bugfix. In practice it seems to be quite breaking.

Oberon00 avatar Mar 30 '26 09:03 Oberon00

@Oberon00 I agree that if things break / change behavior the fact that is was a fix is not a consolation. Can you elaborate a bit more on what broke for you other than the change in the context propagation?

@provinzkraut I've run the litestar tests locally and most failures are around spans not created. AFAIU the litestar otel code is a wrapper around our asgi middleware but we didn't break our asgi instrumentation tests. I've run the following app

from litestar import Litestar, get
from litestar.contrib.opentelemetry import OpenTelemetryConfig, OpenTelemetryPlugin

@get("/")
async def index() -> str:
    return "Hello, world!"

open_telemetry_config = OpenTelemetryConfig()

app = Litestar([index], plugins=[OpenTelemetryPlugin(open_telemetry_config)])

with

opentelemetry-instrument --metrics_exporter console --logs_exporter console --traces_exporter console litestar --app litestarapp:app run

and got

{
    "name": "GET / http send",
    "context": {
        "trace_id": "0xf0441e201671270c6af1d0c90be03bf9",
        "span_id": "0xe776bbead39e34df",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0xf535930fceb8ee9c",
    "start_time": "2026-03-30T10:57:29.918206Z",
    "end_time": "2026-03-30T10:57:29.918252Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "asgi.event.type": "http.response.start",
        "http.status_code": 200
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.40.0",
            "telemetry.auto.version": "0.61b0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}
{
    "name": "GET / http send",
    "context": {
        "trace_id": "0xf0441e201671270c6af1d0c90be03bf9",
        "span_id": "0x051cf01967ed2929",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0xf535930fceb8ee9c",
    "start_time": "2026-03-30T10:57:29.918522Z",
    "end_time": "2026-03-30T10:57:29.918536Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "asgi.event.type": "http.response.body"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.40.0",
            "telemetry.auto.version": "0.61b0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}
{
    "name": "GET /",
    "context": {
        "trace_id": "0xf0441e201671270c6af1d0c90be03bf9",
        "span_id": "0xf535930fceb8ee9c",
        "trace_state": "[]"
    },
    "kind": "SpanKind.SERVER",
    "parent_id": null,
    "start_time": "2026-03-30T10:57:29.917807Z",
    "end_time": "2026-03-30T10:57:29.918575Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.scheme": "http",
        "http.host": "127.0.0.1:8000",
        "net.host.port": 8000,
        "http.flavor": "1.1",
        "http.target": "/",
        "http.url": "http://127.0.0.1:8000/",
        "http.method": "GET",
        "http.server_name": "127.0.0.1:8000",
        "http.user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0",
        "net.peer.ip": "127.0.0.1",
        "net.peer.port": 44422,
        "http.route": "GET /",
        "http.status_code": 200
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.40.0",
            "telemetry.auto.version": "0.61b0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}

Also double checked that we are able to export to an opentelemetry-collector as well. So I guess maybe it's something in the tests? Haven't spotted anything different in the setup code that what we do in our TestBase testcase class though.

@cobusc your leak issue may be #5016 as already spotted by fpiche.

xrmx avatar Mar 30 '26 11:03 xrmx

To be honest, I was mostly referring to other's comments in this thread when saying that things broke.

For our quite special use case at Dynatrace it also breaks but I would not really use it as a general argument. FWIW, we are instrumenting the SDK and API (yes, another layer of instrumentation 😃 and yes, we cannot simply implement an exporter for various reasons) and in case of instrumenting API-only we have to replicate bit of what the SDK does, as far as it is visible to the API user. We had some hard-coded INVALID_SPAN etc there in some cases so now we will have to change to use API's NoopTracer in these cases, which would probably have been the better solution from the start anyways.

Oberon00 avatar Mar 30 '26 12:03 Oberon00