trace/logging: write trace_id and span_id as OpenTelemetry names them
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start by reading the trace/logging.Handler implementation and its existing tests; the issue does not name specific file paths. Check how it currently adds traceId, then inspect the available trace context APIs in the Go OpenTelemetry trace package. Done means logs use trace_id, include span_id for a valid active span, and retain the correlation ID fallback when no valid span exists.
索引モデルが issue の本文から書いたものです。
説明
Current Limitation
trace/logging.Handler adds the request's correlation ID to log lines, but the output doesn't follow the OpenTelemetry convention for trace context in logs, and it can't add a span ID:
- Field name. It writes
traceId. The OTel spec for non-OTLP log formats (Trace Context in non-OTLP Log Formats) names the fieldstrace_id,span_idandtrace_flags. Collectors and backends (the Collector'sfilelogreceiver, Loki/Grafana derived fields, OpenObserve) pick up those names when linking logs to traces. WithtraceId, every consumer needs its own parsing rule. - No span ID. The handler reads only
trace.GetTraceID(ctx). When the context carries an active OTel span, its span ID never reaches the log line, so a line can be linked to a trace but not to the span (the hop or operation) that wrote it. - Can disagree with the span's trace ID. The value comes from
TraceMiddleware, not from the OTel span. Unless a service adds something like the customIDGeneratorin OpenNSW/nsw-srilanka#866, thetraceIdin the logs and the trace ID of the spans are different values for the same request.
Suggested Improvement
Make trace/logging.Handler write trace context as the OTel convention describes:
- Rename the field from
traceIdtotrace_id. - Prefer the active OTel span. If
oteltrace.SpanContextFromContext(ctx).IsValid(), add:trace_id:sc.TraceID().String()(32 lowercase hex characters)span_id:sc.SpanID().String()(16 lowercase hex characters)- optionally
trace_flags:sc.TraceFlags()as 2 hex characters
- Fall back to
trace.GetTraceID(ctx)astrace_idwhen there's no valid span, as today.
func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
if sc := oteltrace.SpanContextFromContext(ctx); sc.IsValid() {
record.AddAttrs(
slog.String("trace_id", sc.TraceID().String()),
slog.String("span_id", sc.SpanID().String()),
)
} else if traceID := trace.GetTraceID(ctx); traceID != "" {
record.AddAttrs(slog.String("trace_id", traceID))
}
return h.Handler.Handle(ctx, record)
}
- Services without OTel tracing keep working unchanged, apart from the field name.
- This adds a dependency on
go.opentelemetry.io/otel/trace(API only, no SDK) to thetracemodule. - Renaming the field is a breaking change for anything that queries
traceIdin logs. Release it as a new minor version and note it in the release notes.
Out of scope: the traceId / correlationId names in httputil error responses, and where TraceMiddleware gets its ID from (#349).
Version
github.com/OpenNSW/core/trace v0.2.0
Additional Context
- Needed by openndx/openndx-core#702 (adopt
core/trace,trace_idon log lines) and openndx/openndx-core#711 (OTel tracing,span_idon log lines). openndx-core's logging standard (openndx/openndx-core#708) specifiestrace_id. - Related to #349: once
TraceMiddlewaretakes its ID from the active span, step 3's fallback only applies to services without OTel tracing, and logs and spans always share one trace ID.
- 主要言語
- Go
- スター
- 6
- フォーク
- 8
- 平均マージ
- 21時間 36分
- マージ済み PR(30日)
- 42
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
OpenNSW/core のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
payment: test fixtures hardcode real gateway names (govpay/lankapay) instead of generic placeholdersオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
nsw-sl cleanup Type/Improvement
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
メンテナーはふだん 1 日以内に返信
-
remote: let the host inject a logger, and log with one consistent policy対応中かも @mushrafmim が 5 日前に担当しました。 オープンType/Improvement
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
☢️ Bug v3
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
area/import-export area/projects concern/ux
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
go-vikunja/vikunja#4455 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
Dependabot PRs no longer merge automatically対応中かも @dwisiswant0 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
projectdiscovery/urlfinder#160 ·
メンテナーはふだん 1 日以内に返信
-
database
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
chnm/legal-modernism#450 ·
メンテナーはふだん 1 日以内に返信
-
tech-debt
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信