我试图让OpenTelemetry跟踪使用FastAPI和请求。目前,我的设置如下所示:
import requests
from opentelemetry.baggage.propagation import W3CBaggagePropagator
from opentelemetry.propagators.composite import CompositePropagator
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3MultiFormat
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
set_global_textmap(CompositePropagator([B3MultiFormat(), TraceContextTextMapPropagator(), W3CBaggagePropagator()]))
app = FastAPI()
FastAPIInstrumentor.instrument_app(app)
RequestsInstrumentor().instrument()
@app.get("/")
async def get_things():
r = requests.get("http://localhost:8081")
return {
"Hello": "world",
"result": r.json()
}
/
端点只需要访问与此类似的另一个服务,只需使用一些中间件来记录传入的标头。
如果我发送这样的请求(httpie格式),
http :8000 'x-b3-traceid: f8c83f4b5806299983da51de66d9a242' 'x-b3-spanid: ba24f165998dfd8f' 'x-b3-sampled: 1'
我希望下游服务(即requests.get("http://localhost:8081")
请求的服务)接收类似于
{
"x-b3-traceid": "f8c83f4b5806299983da51de66d9a242",
"x-b3-spanid": "xxxxxxx", # some generated value from the upstream service
"x-b3-parentspanid": "ba24f165998dfd8f",
"x-b3-sampled": "1"
}
但我得到的基本上就是我发送给上游服务的东西:
{
"x-b3-traceid": "f8c83f4b5806299983da51de66d9a242",
"x-b3-spanid": "ba24f165998dfd8f",
"x-b3-sampled": "1"
}
我一定是错过了一些显而易见的东西,但似乎不知道到底是什么。
发送W3C traceparent
标头会导致相同的情况(在下游接收的标头中只有traceparent
)。如有任何指示,将不胜感激。
编辑-我不使用任何出口商,因为在我们的环境中,Istio被配置为导出跟踪。所以我们现在只关心HTTP跟踪。
发布于 2021-12-06 13:38:50
B3MultiFormat
传播程序在将上下文序列化为header时不考虑父span字段,因为X-B3-ParentSpanId
是一个可选的标头https://github.com/openzipkin/b3-propagation#multiple-headers。您可以期望X-B3-TraceId
和X-B3-SpanId
始终存在,而不是其余的。
编辑:
您是否设置了具体的跟踪器提供程序?从共享片段中看上去不像,但我不知道您是否是实际的应用程序代码。如果不设置sdk跟踪器提供程序,即在FastAPI服务中没有创建记录跨度,则所有操作都是无操作的。请做以下几点。
...
from opentelemetry.trace import set_tracer_provider
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
set_tracer_provider(TracerProvider(
resource=Resource.create({"serice.name": "my-service"})
))
...
另一个编辑:
OpenTelemetry不将父span ID存储在上下文https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#spancontext中。来自OTEL的上下文传播客户端库仅限于序列化和传递此信息。我不认为你能让parentSpanId
传播。
https://stackoverflow.com/questions/70243528
复制相似问题