【发布时间】:2022-01-11 13:06:41
【问题描述】:
我正在尝试让 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()
}
/ 端点只是对另一个服务执行 GET 操作,基本上看起来像这个服务,只是使用一些中间件来记录传入的标头。
如果我发送这样的请求(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 跟踪。
【问题讨论】:
标签: python fastapi open-telemetry distributed-tracing