open-telemetry / open-telemetry/opentelemetry-python-contrib

Automatic Instrumentation does not work with pex/zipapp python code

Open
#1,994 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

Describe your environment

Python 3.10.12

Steps to reproduce
Describe exactly how to reproduce the error. Include a code sample if applicable.

Clone this repo: https://github.com/kuza55/pants_debug_otel/tree/main

Then run

OTEL_TRACES_EXPORTER=console OTEL_METRICS_EXPORTER=console OTEL_LOGS_EXPORTER=console OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true opentelemetry-instrument python3 dist/src/no_configuration.pex

What is the expected behavior?
What did you expect to see?

I expected to see the same output as when I run the command on the python file directly

{
    "body": "baz 325",
    "severity_number": "<SeverityNumber.WARN: 13>",
    "severity_text": "WARNING",
    "attributes": {
        "otelSpanID": "71d54e8b2fde38fe",
        "otelTraceID": "fa86a02c2114d8df845e35c4e9817e0b",
        "otelTraceSampled": true,
        "otelServiceName": "unknown_service"
    },
    "dropped_attributes": 0,
    "timestamp": "2023-10-10T19:29:45.543345Z",
    "trace_id": "0xfa86a02c2114d8df845e35c4e9817e0b",
    "span_id": "0x71d54e8b2fde38fe",
    "trace_flags": 1,
    "resource": "BoundedAttributes({'telemetry.sdk.language': 'python', 'telemetry.sdk.name': 'opentelemetry', 'telemetry.sdk.version': '1.20.0', 'telemetry.auto.version': '0.41b0', 'service.name': 'unknown_service'}, maxlen=None)"
}
{
    "resource_metrics": [
        {
            "resource": {
                "attributes": {
                    "telemetry.sdk.language": "python",
                    "telemetry.sdk.name": "opentelemetry",
                    "telemetry.sdk.version": "1.20.0",
                    "telemetry.auto.version": "0.41b0",
                    "service.name": "unknown_service"
                },
                "schema_url": ""
            },
            "scope_metrics": [],
            "schema_url": ""
        }
    ]
}
{
    "name": "bar",
    "context": {
        "trace_id": "0xfa86a02c2114d8df845e35c4e9817e0b",
        "span_id": "0x71d54e8b2fde38fe",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0xd561f1c55aece97e",
    "start_time": "2023-10-10T19:29:45.543308Z",
    "end_time": "2023-10-10T19:29:45.543496Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {},
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.20.0",
            "telemetry.auto.version": "0.41b0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}
{
    "name": "foo",
    "context": {
        "trace_id": "0xfa86a02c2114d8df845e35c4e9817e0b",
        "span_id": "0xd561f1c55aece97e",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2023-10-10T19:29:45.543230Z",
    "end_time": "2023-10-10T19:29:45.543512Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {},
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.20.0",
            "telemetry.auto.version": "0.41b0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}

What is the actual behavior?
What did you see instead?

WARNING:root:baz 425

Note: the logging integration is being executed inside the pex file, which is why we see any output at all

Additional context

It seems like I can configure all the exporters/etc from code inside my python code and it will work, but I would much rather just reuse the existing auto-instrumentation code, either through the binary wrapper as-is, or by calling a python function that does all the setup for me.

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

Reproduce the issue with the provided repository and compare the opentelemetry-instrument commands for src/no_configuration.py and dist/src/no_configuration.pex. Trace the automatic instrumentation setup used by the binary wrapper and determine what prevents it from applying inside the pex file; done means the pex run emits the same telemetry as the direct Python-file run.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.