以编程方式使用文档

有时,您需要在多个服务之间追踪一个请求。

LangSmith 开箱即用支持分布式追踪,使用上下文传播头信息(langsmith-trace 以及可选的 baggage for metadata/tags).

客户端-服务器设置示例:

  • * 追踪从客户端开始
  • * 在服务器上继续

Python 中的分布式追踪

# client.py
from langsmith.run_helpers import get_current_run_tree, traceable


@traceable
async def my_client_function():
    headers = {}
    async with httpx.AsyncClient(base_url="...") as client:
        if run_tree := get_current_run_tree():
            # add langsmith-id to headers
            headers.update(run_tree.to_headers())
        return await client.post("/my-route", headers=headers)

然后服务器(或其他服务)可以通过适当处理头信息来继续追踪。如果您使用的是 ASGI 应用 Starlette 或 FastAPI,您可以使用 LangSmith 的 TracingMiddleware.

使用 FastAPI 的示例:

from langsmith import traceable
from langsmith.middleware import TracingMiddleware
from fastapi import FastAPI, Request

app = FastAPI()  # Or Flask, Django, or any other framework
app.add_middleware(TracingMiddleware)

@traceable
async def some_function():
    ...

@app.post("/my-route")
async def fake_route(request: Request):
    return await some_function()

或在 Starlette 中:

from starlette.applications import Starlette
from starlette.middleware import Middleware
from langsmith.middleware import TracingMiddleware

routes = ...
middleware = [
    Middleware(TracingMiddleware),
]
app = Starlette(..., middleware=middleware)

如果您使用其他服务器框架,您始终可以通过将头信息传入 langsmith_extra:

# server.py

from fastapi import FastAPI, Request

@ls.traceable
async def my_application():
    ...

app = FastAPI()  # Or Flask, Django, or any other framework

@app.post("/my-route")
async def fake_route(request: Request):
    # request.headers:  {"langsmith-trace": "..."}
    # as well as optional metadata/tags in `baggage`
    with ls.tracing_context(parent=request.headers):
        return await my_application()

上面的示例使用了 tracing_context 上下文管理器。您也可以直接在 langsmith_extra 方法的 @traceable.

# ... same as above

@app.post("/my-route")
async def fake_route(request: Request):
    # request.headers:  {"langsmith-trace": "..."}
    my_application(langsmith_extra={"parent": request.headers})

TypeScript 中的分布式追踪

首先,我们从客户端获取当前运行树并将其转换为 langsmith-tracebaggage 头信息值,我们可以将其传递给服务器:

// client.mts


const client = traceable(
    async () => {
        const runTree = getCurrentRunTree();
        return await fetch("...", {
            method: "POST",
            headers: runTree.toHeaders(),
        }).then((a) => a.text());
    },
    { name: "client" }
);

await client();

然后,服务器将头信息转换回运行树,用于继续追踪。

要将新创建的运行树传递给可追踪函数,我们可以使用 withRunTree 辅助函数,它将确保运行树在可追踪调用中被传播。

// server.mts





    const server = traceable(
        (text: string) => `Hello from the server! Received "${text}"`,
        { name: "server" }
    );

    const app = express();
    app.use(bodyParser.text());

app.post("/", async (req, res) => {
    const runTree = RunTree.fromHeaders(req.headers);
    const result = await withRunTree(runTree, () => server(req.body));
    res.send(result);
});
// server.mts




    const server = traceable(
        (text: string) => `Hello from the server! Received "${text}"`,
        { name: "server" }
    );

    const app = new Hono();

app.post("/", async (c) => {
    const body = await c.req.text();
    const runTree = RunTree.fromHeaders(c.req.raw.headers);
    const result = await withRunTree(runTree, () => server(body));
    return c.body(result);
});