采样
可以通过采样来控制 Litefuse 收集的 trace 量。采样在客户端处理。
你可以通过设置 LANGFUSE_SAMPLE_RATE 环境变量,或使用 sample_rate/sampleRate 构造函数参数来配置采样率。取值必须介于 0 和 1 之间。
默认值为 1,表示收集所有 trace。值为 0.2 表示只收集 20% 的 trace。SDK 在 trace 级别进行采样,也就是说如果某条 trace 被采样,其内部所有的 observation 和分数都会一并被采样。
使用 Python SDK 时,你可以在初始化客户端时配置采样:
from langfuse import Langfuse, get_client
import os
# Method 1: Set environment variable
os.environ["LANGFUSE_SAMPLE_RATE"] = "0.5" # As string in env var
langfuse = get_client()
# Method 2: Initialize with constructor parameter then get client
Langfuse(sample_rate=0.5) # 50% of traces will be sampled
langfuse = get_client()使用 @observe() 装饰器时:
from langfuse import observe, Langfuse, get_client
# Initialize the client with sampling
Langfuse(sample_rate=0.3) # 30% of traces will be sampled
@observe()
def process_data():
# Only ~30% of calls to this function will generate traces
# The decision is made at the trace level (first span)
pass如果某条 trace 没有被采样,它的所有 observation(span 或 generation)以及关联的分数都不会被发送到 Litefuse,对于高流量应用,这能显著降低数据量。
这个页面对你有帮助吗?