telemetry/logging: share the slog setup every service builds by hand

オープン
#360 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
go
領域
observability

調査の方向性

Start with the existing telemetry/logging package and the dependent work in #359, then compare the logging implementations named in the issue: openndx-core internal/logging, nsw-srilanka internal/bootstrap/logging.go, and agency backend/internal/logging. Review openndx-core#708 for its logging rules and tests. Done means a shared setup covers the listed configuration, trace IDs, optional OTLP export with level filtering, common service attributes, and the default logger and correlation ID wiring; several design decisions and consumer migrations remain.

索引モデルが issue の本文から書いたものです。

説明

Type/Improvement
Current Limitation

Every OpenNSW service builds its slog setup by hand. All of them build the same chain: base handler → trace ID decorator → (optional) OTLP export → slog.SetDefault → httputil.CorrelationIDFunc. Each one does it a little differently:

openndx-core internal/logging nsw-srilanka internal/bootstrap/logging.go agency backend/internal/logging
Level from config logging.level server.logLevel Not configurable
Format json or text, configurable text only json only
service / environment / version on every line Yes No No
Trace ID decorator (trace/logging) No Yes Yes
httputil.CorrelationIDFunc wired No Yes Yes
OTLP log export (otelslog + slog.NewMultiHandler) No Yes No
Level filter in front of otelslog — Yes —
AddSource at debug No Yes No

So:

  • A combined log stream can't be filtered by service, because only one of the three adds service, and its value isn't tied to the OTel service.name.
  • Fixes stay where they were found. nsw-srilanka found that otelslog reports every level as enabled, so behind slog.NewMultiHandler the multi-handler's Enabled is true for DEBUG even when the configured level is INFO. DEBUG records were exported and debug-gated payload logging turned on. Its levelFilter fixes this. Any other service that adds OTLP log export will hit the same bug.
  • Config keys and defaults differ (logging.level vs server.logLevel, json vs text), so operators configure each service differently.
Suggested Improvement

Add the logging setup to telemetry/logging, beside the trace_id / span_id decorator from #359.

package logging // github.com/OpenNSW/core/telemetry/logging

// Config is the logging section of a service's config file.
type Config struct {
	// Level is debug, info, warn or error. Default info.
	Level string `yaml:"level"`
	// Format is json or text. Default json.
	Format string `yaml:"format"`
}

// DefaultConfig returns the values used for keys the config file leaves out.
func DefaultConfig() Config

// Validate reports an unknown level or format.
func (c Config) Validate() error

// Setup builds the logger, sets it as slog's default and returns it. cfg must be valid.
func Setup(cfg Config, p *telemetry.Provider) *slog.Logger

// NewHandler adds trace_id and span_id from the span in ctx (#359).
func NewHandler(next slog.Handler) slog.Handler

Setup builds:

  1. slog.JSONHandler or slog.TextHandler on stdout, at cfg.Level, with AddSource at debug
  2. NewHandler around it, for trace_id / span_id
  3. When p exports logs over OTLP: p.SlogHandler() behind a level filter and NewHandler, fanned out with slog.NewMultiHandler
  4. service, environment and version attributes, taken from the same values as the Provider's resource (service.name, deployment.environment, service.version), so the log field and the OTel attribute can't drift
  5. slog.SetDefault, and httputil.CorrelationIDFunc = telemetry.TraceID

Each main then has:

p, err := telemetry.Start(ctx, cfg.Telemetry)
// ...
logging.Setup(cfg.Logging, p)

Service-specific behavior stays in the service, such as nsw-srilanka's warning that audit records are suppressed above info.

Consumers to migrate

  • openndx/openndx-core: internal/logging (openndx/openndx-core#708) is deleted, and cmd/<service>/main.go calls logging.Setup.
  • OpenNSW/nsw-srilanka: internal/bootstrap/logging.go, including levelFilter. server.logLevel either moves to a logging section or is mapped into logging.Config by its config code.
  • OpenNSW/agency: backend/internal/logging.

Decisions to settle in review

  1. Default format: json (openndx-core, agency) or text (nsw-srilanka). Suggest json for production, since log pipelines parse it.
  2. Config key: logging.level / logging.format as one section, or leave each service to map its own keys into Config.
  3. Is Setup usable without telemetry? For example with p == nil it writes stdout only and adds no trace IDs. Or does every service start telemetry first?
Version

github.com/OpenNSW/core/trace v0.2.0

Additional Context
  • Depends on #359 for telemetry.Provider, telemetry.TraceID and telemetry/logging.NewHandler.
  • openndx/openndx-core#708 has a description of openndx's logging rules (one setup, one global logger, ...Context calls, common fields). Its internal/logging package is the starting point for Config, Validate and the common fields.
主要言語
Go
スター
6
フォーク
8
平均マージ
21時間 36分
マージ済み PR(30日)
42

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

OpenNSW/core のほかの issue

OpenNSW/core の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。