Files
nvke/cli/internal/telemetry/handler.go
Anisha Gupta US 82c8b7e8a3 feat(cli): time each bootstrap phase with spans and log lines (#1979)
* feat(cli): time each bootstrap phase with spans and log lines

bootstrap site runs in five phases: preflight, bootstrap_cluster,
engine_deploy, site_cluster and finish. Each phase now has a child span
of the run's root span and logs one "phase finished" line with its
outcome and duration_ms, so phase durations are available without a
trace backend.

Every log line carries operation and, once the site spec is loaded,
cluster. While a phase runs, a line also carries phase, and its span_id is
the phase span's. The run tracks the current phase, because the component
loggers are built before a phase starts and are given no context. With
--json-logs the time is in UTC.

The Kind cluster's log attribute is now bootstrap_kind_cluster, so
cluster means the site cluster on every line.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* fix(cli): keep the site cluster name on the lines that already carried it

The handler adds cluster to every line, and a record that names cluster keeps its own value, so these lines carry it once.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* docs(cli): show the new log fields in the existing troubleshooting example

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* test(cli): let the bootstrap e2e allowlist accept the phase fields on its two log lines

The allowlist matches each of those lines from time= to its end, so the operation, cluster and phase fields now on every line made gitleaks flag both again.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* test(cli): cover an interrupted bootstrap and a masked phase error

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* fix(cli): give exported log records the phase span and fields

With OTLP export on, the tee handler put the root span into a record's
context before the log handler ran, so a context-free logger never got
its phase span, and the exported record never got phase, operation or
cluster. Resolve the span and fields once and use them for both the
stderr line and the exported record.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* fix(cli): log trace fields before the run fields again

The previous commit stamped trace_id and span_id after operation, cluster
and phase, which the e2e gitleaks allowlist does not match, so its etcd
key line was reported as a leak. Restore the order and pin it in a test.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* fix(cli): let a bound logger field win over the run's field

A logger made with With("cluster", ...) got the run's cluster field as
well, so the line had the key twice. Track the top-level keys a handler
has bound and skip those run fields.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* refactor(cli): name the phase tracker's error parameter previousErr

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* fix(cli): track a logger's bound fields per group

Attributes bound after WithGroup go in the same group as the record's
attributes, so the run's field collided with them and the line had the
key twice. Start a new set of bound keys at each group, and make the
test assert the group's one value.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* Revert "fix(cli): track a logger's bound fields per group"

This reverts commit 1841921b19.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* Revert "fix(cli): let a bound logger field win over the run's field"

This reverts commit 5669089a50.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* fix(cli): fail the finish phase when the Kind teardown fails

A failed teardown was only logged, so finish reported succeeded even
though the Kind cluster was left behind. Record the teardown error on the
finish phase, joined with any error finish already has, and leave the
command's own result unchanged.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* refactor(cli): drop the lowercase clone methods of the log handler

The tee asserts the logHandler its local handler's WithAttrs and
WithGroup return, so the handler needs one method for each, not two.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* test(cli): drop the teardown case that needs an untaint failure to tear down

On main a failure after the site cluster exists keeps the bootstrap Kind
cluster, so a failed untaint never reaches the teardown and the case
asserts an error that is not produced.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* docs(cli): document the bootstrap phases in the troubleshooting guide

Describe the phase finished line and the five phases, add the logged
phase to the failure table, and put each error under the phase that logs
it: the kubeconfig, SPIRE and encryption key errors are site_cluster, and
only the untaint is finish. The retained-cluster example now has the
fields in the order the log writes them.

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

* docs(cli): name the stack install as part of the finish phase

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>

---------

Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>
2026-10-29 15:59:17 -05:00

153 lines
5.5 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package telemetry
import (
"context"
"log/slog"
"go.opentelemetry.io/otel/trace"
"github.com/dsx-ai-factory/nvke/cli/internal/redact"
)
// logHandler adds trace_id and span_id, and the run's phase, operation and
// cluster when set, to every record.
type logHandler struct {
slog.Handler
root trace.SpanContext
run *Run
}
// LogHandler wraps inner so each record carries trace_id and span_id: those of
// the span in the record's context, or else those of the run's current phase,
// or else those of the run's root span. Records logged without a context
// therefore still carry the run's trace ID. While a phase is active each record
// also carries phase, and operation and cluster once SetLogFields has set them.
// If EnableOTLP was called, each record that inner handles is also exported,
// with the same trace ID and span ID and with its attributes. Export
// failures are reported as warnings through the same handler, with the trace
// ID and span ID. It returns inner unchanged if the run has not started.
func (run *Run) LogHandler(inner slog.Handler) slog.Handler {
if run != nil {
run.handlerBuilt = true
}
root := run.SpanContext()
if !root.IsValid() {
return inner
}
local := &logHandler{Handler: inner, root: root, run: run}
exported := run.otlpHandler()
if exported == nil {
return local
}
run.warnLogger.Store(slog.New(local))
return &teeHandler{local: local, exported: exported}
}
// teeHandler sends each record to the local handler and to the exporter. It
// logs the levels the local handler logs, so the exporter receives the records
// that appear on stderr.
type teeHandler struct {
local *logHandler
exported slog.Handler
}
// Enabled implements slog.Handler.
func (handler *teeHandler) Enabled(ctx context.Context, level slog.Level) bool {
return handler.local.Enabled(ctx, level)
}
// Handle implements slog.Handler. It exports a copy of record and writes record
// to the local handler. Both get the span and fields that the local handler
// resolves, so the exported record matches the one on stderr. An export error is
// not returned.
func (handler *teeHandler) Handle(ctx context.Context, record slog.Record) error {
// The exporter reads a record's trace ID and span ID from its context.
ctx, fields := handler.local.resolve(ctx, record)
exported := record.Clone()
exported.AddAttrs(fields...)
_ = handler.exported.Handle(ctx, redact.Record(exported))
return handler.local.handleResolved(ctx, record, fields)
}
// WithAttrs implements slog.Handler.
func (handler *teeHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
// A logHandler's WithAttrs returns a logHandler.
return &teeHandler{local: handler.local.WithAttrs(attrs).(*logHandler), exported: handler.exported.WithAttrs(redact.Attrs(attrs))}
}
// WithGroup implements slog.Handler.
func (handler *teeHandler) WithGroup(name string) slog.Handler {
return &teeHandler{local: handler.local.WithGroup(name).(*logHandler), exported: handler.exported.WithGroup(name)}
}
// Handle implements slog.Handler.
func (handler *logHandler) Handle(ctx context.Context, record slog.Record) error {
ctx, fields := handler.resolve(ctx, record)
return handler.handleResolved(ctx, record, fields)
}
// resolve returns ctx carrying the span the record belongs to, and the operation,
// cluster and phase fields to add to record. The span is the one already in ctx,
// else the current phase's, else the root span.
func (handler *logHandler) resolve(ctx context.Context, record slog.Record) (context.Context, []slog.Attr) {
phase, operation, cluster := handler.run.logFields()
spanContext := trace.SpanContextFromContext(ctx)
if !spanContext.IsValid() && phase != nil {
spanContext = phase.span
}
if !spanContext.IsValid() {
spanContext = handler.root
}
ctx = trace.ContextWithSpanContext(ctx, spanContext)
var fields []slog.Attr
// A record that already names a field keeps its own value.
fields = appendUnset(fields, record, "operation", operation)
fields = appendUnset(fields, record, "cluster", cluster)
if phase != nil {
fields = appendUnset(fields, record, "phase", phase.name)
}
return ctx, fields
}
// handleResolved adds trace_id and span_id from ctx, as resolve left it, then
// fields, and passes record to the inner handler. The e2e gitleaks allowlist
// depends on this order.
func (handler *logHandler) handleResolved(ctx context.Context, record slog.Record, fields []slog.Attr) error {
spanContext := trace.SpanContextFromContext(ctx)
record.AddAttrs(
slog.String("trace_id", spanContext.TraceID().String()),
slog.String("span_id", spanContext.SpanID().String()),
)
record.AddAttrs(fields...)
return handler.Handler.Handle(ctx, record)
}
// WithAttrs implements slog.Handler.
func (handler *logHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &logHandler{Handler: handler.Handler.WithAttrs(attrs), root: handler.root, run: handler.run}
}
// WithGroup implements slog.Handler.
func (handler *logHandler) WithGroup(name string) slog.Handler {
return &logHandler{Handler: handler.Handler.WithGroup(name), root: handler.root, run: handler.run}
}
// appendUnset appends key=value to attrs unless value is empty or record has key.
func appendUnset(attrs []slog.Attr, record slog.Record, key, value string) []slog.Attr {
if value == "" {
return attrs
}
found := false
record.Attrs(func(attr slog.Attr) bool {
found = attr.Key == key
return !found
})
if found {
return attrs
}
return append(attrs, slog.String(key, value))
}