open-telemetry / open-telemetry/opentelemetry-python

baggage and context are ignored. setting values is ignored.

Open
#3,783 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
2.6k
Forks
1k
Avg merge
4d 15h
Merged PRs (30d)
19

Description

Describe your environment

$ pip list | grep opentelemetry
opentelemetry-api                        1.23.0
opentelemetry-exporter-otlp-proto-common 1.23.0
opentelemetry-exporter-otlp-proto-http   1.23.0
opentelemetry-proto                      1.23.0
opentelemetry-sdk                        1.23.0
opentelemetry-semantic-conventions       0.44b0
$ python -V
Python 3.10.5

Steps to reproduce

from opentelemetry import baggage, context, trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor


class TestTelemetry:
    def test_it(self):
        # Create provider and get tracer
        resource = Resource.create({'service.name': 'test-python-1'})
        provider = TracerProvider(resource=resource)

        exporter = ConsoleSpanExporter()
        processor = SimpleSpanProcessor(exporter)
        provider.add_span_processor(processor)

        trace.set_tracer_provider(provider)
        tracer = trace.get_tracer('shell-energy-kafka-python')

        # Set context values
        exec_ctx = context.get_current()
        print('exec_ctx:', exec_ctx)

        print('context.set_value("greetings", "hello world")')
        key = 'greetings'
        value = 'hello world'
        context.set_value(key, value)
        print('exec_ctx:', context.get_current())

        value_from_get = context.get_value(key)
        print(f'exec_ctx: key:"{key}", value:"{value_from_get}"')

        # Start span
        attrs = {'extra.attr': 'extra.attr.value'}
        with tracer.start_as_current_span('Testing traces',
                                          kind=trace.SpanKind.PRODUCER,
                                          attributes=attrs):
            # Set baggage
            baggage_key = 'greetings_bag'
            baggage_value = 'hello world bag'

            print(f'set_baggage: key:"{baggage_key}", value="{baggage_value}"')
            baggage.set_baggage(baggage_key, baggage_value)

            print('baggage.get_all():', baggage.get_all())

Output:

exec_ctx: {}
context.set_value("greetings", "hello world")
exec_ctx: {}
exec_ctx: key:"greetings", value:"None"
set_baggage: key:"greetings_bag", value="hello world bag"
baggage: {}
{
    "name": "Testing traces",
    "context": {
        "trace_id": "0x7c50f96a522f16b6633a8ca827a12551",
        "span_id": "0x4eeef4315fee0241",
        "trace_state": "[]"
    },
    "kind": "SpanKind.PRODUCER",
    "parent_id": null,
    "start_time": "2024-03-15T12:04:13.600564Z",
    "end_time": "2024-03-15T12:04:13.600588Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "extra.attr": "extra.attr.value"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.23.0",
            "service.name": "test-python-1"
        },
        "schema_url": ""
    }
}

What is the expected behavior?
I expect baggage.get_all() to return keys and values previously set in baggage. I expect the same from the context.
I also expect the values set in baggage and in context will appear in the trace printed to console.

What is the actual behavior?

  1. baggage.get_all() and context.get_current() return empty objects.
  2. Trace printed in the console contains none of the values set on baggage and on context.

Additional context
None

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 behavior with the provided Python snippet, then read the context.set_value, context.get_current, baggage.set_baggage, and baggage.get_all entry points. Check how values are expected to be propagated into start_as_current_span and exported spans; done means the documented behavior is covered by tests or the issue's expectations are clarified.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.