open telemetry - create a trace and have remote processes link to the same tracespan - Stack Overflow

admin2025-04-25  2

I have a system that comprises of several openapi servers. these all take requests and store them in a database, I then have a process getting the stored requests and processing them, these processes also talk to the apis

for example we make a request of one api and the process servicing those request picks it up from the api and then contacts 2 other apis and there processors service the request

this is all written in python the api servers are flask.

what I want to see is something like the hotrod example. every api and process that does something relates to the initial call recorded and linked together.

I've tried creating a span context but it does not work

import uuid
from flask import Flask, jsonify
from opentelemetry import trace, context
from opentelemetry.trace import SpanContext, NonRecordingSpan, TraceFlags
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.propagate import propagators, propagator
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry.instrumentation.flask import FlaskInstrumentor

app = Flask(__name__)

# Resource can be required for some backends, e.g. Jaeger
# If resource wouldn't be set - traces wouldn't appears in Jaeger
resource = Resource(attributes={"service.name": "tester 1"})

trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)

otlp_exporter = OTLPSpanExporter(endpoint="http://192.168.178.87:4317", insecure=True)

span_processor = BatchSpanProcessor(otlp_exporter)

trace.get_tracer_provider().add_span_processor(span_processor)
trace.get_tracer_provider().add_span_processor(
    BatchSpanProcessor(ConsoleSpanExporter())
)

FlaskInstrumentor().instrument_app(app)

@app.route("/")
def hello():

    # trace.get_current_span()
    span_context = SpanContext(trace_id=0x4251d1c2561f76b0ba75336d485809ed, span_id=0x1307a493a7655259, is_remote=False)

    ctx = trace.set_span_in_context(span_context)

    print(str(ctx))
    with tracer.start_as_current_span("hello 3", ctx) as span:
        tracking_id = str(uuid.uuid4())
        span.set_attribute("tracking_id", tracking_id)

        return jsonify({"message": "Hello from Flask API!", "tracking_id": tracking_id})

@app.route("/api")
def api():
    with trace.get_tracer(__name__).start_as_current_span("api") as span:
        tracking_id = str(uuid.uuid4())
        span.set_attribute("tracking_id", tracking_id)
        return jsonify({"result": "You are at the API endpoint.", "tracking_id": tracking_id})

if __name__ == "__main__":
   app.run(debug=True)

has anyone got examples of this?

what I would like to see is

what I get it

转载请注明原文地址:http://anycun.com/QandA/1745532096a90851.html