OTel: add trace_flags to JSON log records so unsampled trace ids are recognizable

未关闭 适合新手
#7,729 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@ShaanGS 已经在做这个了。

开始于 2026年11月4日。

  • #7734 来自 @ShaanGS —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
observability

调研方向

先阅读 otel.trace_fields() 以及测试 test_trace_fields_inside_span 和 test_json_mode_adds_trace_ids;issue 还提到了 docs/api-reference/observability.md。检查 span context 如何提供 trace flags,然后运行相关测试。完成标准是:JSON 日志记录包含两位小写的 W3C flag,ALWAYS_OFF 测试期望 "00",并且 Logs 文档解释该值。

由索引模型根据 Issue 内容生成。

描述

enhancement

Follow-up to #7668 (fixes #6903).

Problem

#7668 adds trace_id and span_id to JSON log records written inside an OpenTelemetry span. It does not add trace_flags.

A span that is not sampled still has a valid span context. So its log records get ids, but the tracing backend never stores that trace. A reader then searches for a trace that does not exist, and the log line gives no sign of why.

This case is normal in Reflex. The event span uses a parent-based sampler by default, so a client traceparent with the sampled flag off gives an unsampled span. A ratio sampler (OTEL_TRACES_SAMPLER=traceidratio) does the same for most spans.

Repro

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF
from reflex_base import otel

otel.enable(tracer_provider=TracerProvider(sampler=ALWAYS_OFF))
with otel._tracer.start_as_current_span("x") as span:
    print(span.is_recording())  # False
    print(otel.trace_fields())  # {'trace_id': '...', 'span_id': '...'}, no sampled flag

Proposed change

Add trace_flags to otel.trace_fields(), as the OpenTelemetry spec "Trace Context in Non-OTLP Log Formats" recommends. Use the W3C format: two lowercase hex digits, "01" when sampled, "00" when not.

"trace_flags": f"{span_context.trace_flags:02x}",
  • Extend test_trace_fields_inside_span and test_json_mode_adds_trace_ids to check the field.
  • Add a test with an ALWAYS_OFF sampler that expects "trace_flags": "00".
  • Add the field to the "Logs" section of docs/api-reference/observability.md, with one sentence on what "00" means.
主要语言
Python
星标
29k
派生
1.8k
平均合并
2 天 22 小时
30 天内合并 PR
143

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

reflex-dev/reflex 的其他 Issue

查看 reflex-dev/reflex 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。