Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document baggage propagation #3561

Open
veeral-patel opened this issue Nov 29, 2023 · 2 comments · Fixed by open-telemetry/opentelemetry.io#3716
Open

Document baggage propagation #3561

veeral-patel opened this issue Nov 29, 2023 · 2 comments · Fixed by open-telemetry/opentelemetry.io#3716
Labels
doc Documentation-related

Comments

@veeral-patel
Copy link

veeral-patel commented Nov 29, 2023

Thanks for this great project! However, I noticed propagating baggage across network calls is not documented. I needed to go through the code to find a solution.

Here is a example code snippet we could clean up and add to the cookbook:
https://opentelemetry.io/docs/concepts/signals/traces/#context-propagation

Below, api1 calls api2:

api1:

from flask import Flask
import requests
from opentelemetry import trace, propagators, baggage
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.baggage.propagation import W3CBaggagePropagator
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
from opentelemetry.exporter.jaeger.thrift import JaegerExporter

app = Flask(__name__)

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))

jaeger_exporter = JaegerExporter(
    agent_host_name="localhost",  # Set your Jaeger agent host
    agent_port=6831,  # Set your Jaeger agent port
)
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(jaeger_exporter))

tracer = trace.get_tracer(__name__)

@app.route('/')
def hello():
    with tracer.start_as_current_span("api1_span") as span:
        ctx = baggage.set_baggage("hello", "world")

        headers = {}
        W3CBaggagePropagator().inject(headers, ctx)
        TraceContextTextMapPropagator().inject(headers, ctx)
        print(headers)

        response = requests.get('http://127.0.0.1:5001/', headers=headers)
        return f"Hello from API 1! Response from API 2: {response.text}"

if __name__ == '__main__':
    app.run(port=5002)

api2:

from flask import Flask, request
from opentelemetry import trace, baggage
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
import time
from opentelemetry.baggage.propagation import W3CBaggagePropagator

app = Flask(__name__)

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))

jaeger_exporter = JaegerExporter(
    agent_host_name="localhost",  # Set your Jaeger agent host
    agent_port=6831,  # Set your Jaeger agent port
)
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(jaeger_exporter))

tracer = trace.get_tracer(__name__)

@app.route('/')
def hello():
    # Example: Log headers received in the request in API 2
    headers = dict(request.headers)
    print(f"Received headers: {headers}")
    carrier ={'traceparent': headers['Traceparent']}
    ctx = TraceContextTextMapPropagator().extract(carrier=carrier)
    print(f"Received context: {ctx}")

    b2 ={'baggage': headers['Baggage']}
    ctx2 = W3CBaggagePropagator().extract(b2, context=ctx)
    print(f"Received context2: {ctx2}")

    # Start a new span
    with tracer.start_span("api2_span", context=ctx2):
       # Use propagated context
        print(baggage.get_baggage('hello', ctx2))
        return "Hello from API 2!"

if __name__ == '__main__':
    app.run(port=5001)
@veeral-patel veeral-patel added the bug Something isn't working label Nov 29, 2023
@ocelotl
Copy link
Contributor

ocelotl commented Nov 30, 2023

Hello @veeral-patel, it seems like you already have a solution for this issue. Could you open a PR here?

@ocelotl ocelotl added doc Documentation-related and removed bug Something isn't working labels Nov 30, 2023
@pegasas
Copy link
Contributor

pegasas commented Jan 1, 2024

Hello @veeral-patel, it seems like you already have a solution for this issue. Could you open a PR here?

I've open an PR for this, feel free to comments if any concern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation-related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants