OpenInference 上下文管理器批量附加追踪属性
OpenInference 提供六个上下文管理器,在作用域内创建 span 时自动附加会话、用户、元数据、标签和提示模板等属性,避免逐一手工修改每个 span。
OpenInference提供了一组 **上下文管理器** 将属性附加到在范围中创建的每个跨度上。它们是最容易添加跨领域上下文的方法 — 会话 ID、用户 ID、元数据、标记、快速模板,而无需手动修改每个跨度。 *组* 对于仅适用于一个跨度的属性,将其直接设定在该跨度上。 ## 他们如何工作 每个上下文管理器都会给 OpenTelemetry 上下文附加一个值。 当管理器处于范围时创建一个跨度时, OpenInference-aware tracker 返回 [`register()`](<https://arize.com/docs/phoenix/tracing/concepts-tracing/otel-openinference/phoenix-otel-helpers#what-register-does-under-the-hood>) 读取这些上下文属性,并在创建时将上下文的属性印在上下文上。这就是为什么 `register()` 事项: 它返回 `OITracer` 而不是香草OTel跟踪器。如果绕过 `register()` 这些上下文管理器仍然设置了上下文值, 但追踪器不会自动捡到它们 。 ## 六帮手 六个人都住在这里 `openinference.instrumentation`: | 帮助者 | 设定 | 使用大小写 | | --- | --- | --- | | `using_session` | `session.id` | 组团追踪到多回合对话. | | `using_user` | `user.id` | 请求所属用户的标记跨度 。 | | `using_metadata` | `metadata` | 任意 JSON 字符串数据 。 | | `using_tags` | `tag.tags` | 用于过滤和搜索的标签列表 。 | | `using_prompt_template` | `llm.prompt_template.{template,version,variables}` | 附上提示模板、其版本和所用的变量。 | | `using_attributes` | 以上各项的任何组合 | 在一个区块中设置多个来避免筑巢. | ## `using_session` 设置 `session.id` 属性。这是凤凰城组合对会话的跟踪。 ``` from openinference.instrumentation import using_session with using_session(session_id="user-42-conv-7"): response = client.chat.completions.create(...) # every span created in this block gets session.id = "user-42-conv-7" ``` 实际会话设置见: [设置会话](<https://arize.com/docs/phoenix/tracing/how-to-tracing/setup-tracing/setup-sessions>). ## `using_user` 设置 `user.id` 属性。 ``` from openinference.instrumentation import using_user with using_user(user_id="user-42"): response = client.chat.completions.create(...) ``` ## `using_metadata` 设置任意的 JSON 字符串元数据。 用于不符合其他帮助者的应用程序特定上下文( 租户、 环境、 请求来源 ) 。 ``` from openinference.instrumentation import using_metadata with using_metadata({"tenant": "acme", "feature_flag": "v2"}): response = client.chat.completions.create(...) ``` ## `using_tags` 设置 `tag.tags` 属性 – 用于过滤和搜索的字符串列表,位于凤凰城UI. ``` from openinference.instrumentation import using_tags with using_tags(["experiment-7", "high-priority"]): response = client.chat.completions.create(...) ``` ## `using_prompt_template` 设置三个快速模板属性 - `llm.prompt_template.template`, `llm.prompt_template.version`, `llm.prompt_template.variables` - 马上 ``` from openinference.instrumentation import using_prompt_template template = "You are a helpful assistant. The user's name is {{name}}." with using_prompt_template( template=template, version="v3", variables={"name": "Alice"}, ): response = client.chat.completions.create(...) ``` 附加模板(而非仅是最终的提示),使得直接从凤凰网的UI. ## `using_attributes` 将上面的数个设置在一个块中 —— 当您选择了三四个上下文管理器时有用 。 ``` from openinference.instrumentation import using_attributes with using_attributes( session_id="user-42-conv-7", user_id="user-42", metadata={"tenant": "acme"}, tags=["experiment-7"], prompt_template=template, prompt_template_version="v3", prompt_template_variables={"name": "Alice"}, ): response = client.chat.completions.create(...) ``` 关于犬科的参考文献,见 [自定义你的痕迹](<https://arize.com/docs/phoenix/tracing/how-to-tracing/add-metadata/customize-spans>). ## 手动弹簧和上下文继承 上下文管理器将属性附加到 OTel 上下文。 `OITracer` 当它创造一个跨度时读取这些属性——这就是自动仪器是如何跨度的(并且跨度是随着时间跨度而创建的). `OITracer`自动继承它们。手动跨度用原始OTel跟踪器创建 **没有** 自动拾取这些上下文属性。如果将手动跨度与上下文管理器混合,请使用来自 `openinference.instrumentation`: ``` from contextlib import contextmanager from openinference.instrumentation import get_attributes_from_context @contextmanager def create_span_with_context(tracer, name, **kwargs): with tracer.start_as_current_span(name, **kwargs) as span: span.set_attributes(dict(get_attributes_from_context())) yield span # Usage with using_session(session_id="my-session-id"): with create_span_with_context(tracer, "my-manual-span") as span: # span now has session.id attached ... ``` 这拉动了当前上下文属性并将其应用到新的跨度。 `@contextmanager` 装饰器( 带有 `yield span` 改为 `return span`(b) 保持跨度在下列期间的开放: `with` 块 —— 直接返回时段会在块运行前结束 。 ## 当上下文管理员帮助( 当他们不帮助) | 使用上下文管理器时... | 当... 时, 将属性直接设置在跨度上 。 | | --- | --- | | 该值适用于许多跨度(整段对话,整段请求). | 这个值只适用于这一个跨度。 | | 您正在使用自动工具, 无法访问图书馆代码 。 | 你用手来创造这个跨度。 | | 你想把交叉背景 清晰地看出来 `with` 块。 | 数值根据每秒数据计算。 | --- ## 下一步 您已经覆盖了设置属性的每个方法。 接下来, 能够保持整个过程、线程和服务界限完整保存痕迹的力学 — — 上下文传播: ## 下一步:背景宣传 这页有用吗? [建议编辑](<https://github.com/arize-ai/phoenix/edit/main/docs/phoenix/tracing/concepts-tracing/otel-openinference/context-managers.mdx>)提出问题
查看原始来源