mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-10-02 03:24:42 +08:00
Actor logs used `ate.dev/actor_*` while spans and metrics use `ate.*` registry in internal/ateattr. This PR makes ateattr the single source of truth for everything telemetry-related. - Renamed the six actor log labels onto the registry: - `ate.atespace`, `ate.actor.name`, `ate.actor.uid`, `ate.template.namespace`, `ate.template.name`, `ate.actor.container.name` - Logs join traces now. Records set `trace_id`, `span_id` and `trace_flags`, so you can go from Actor restored to the resume that caused it. Our own lines only, not an actor's stdout: one goroutine forwards a whole container stream and can't know which request produced a given line. Per line correlation comes with #853. - Actors can't fake platform labels. They already couldn't overwrite ours, but they could invent new ones like `ate.tenant` that look platform issued downstream. Anything under `ate.` from an actor is now dropped. - Fixed the asymmetry that was actually left: actor supplied label values weren't stringified, and one non string value makes Cloud Logging discard the labels for that whole entry. - Note: the actor_uid bullet in the issue is stale, #841 fixed it earlier. Lifecycle records still set five labels rather than six, on purpose as they're about the actor, so no container produced them. Fixes #886 - [x] Tests pass - [x] Appropriate changes to documentation are included in the PR --------- Signed-off-by: krisztianfekete <git@krisztianfekete.org>
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package contextlogging
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"github.com/agent-substrate/substrate/internal/ateattr"
|
|
)
|
|
|
|
type ContextHandler struct {
|
|
internal slog.Handler
|
|
}
|
|
|
|
func NewHandler(internal slog.Handler) *ContextHandler {
|
|
return &ContextHandler{
|
|
internal: internal,
|
|
}
|
|
}
|
|
|
|
func (h *ContextHandler) Enabled(ctx context.Context, lvl slog.Level) bool {
|
|
return h.internal.Enabled(ctx, lvl)
|
|
}
|
|
|
|
// Handle records the active span on the log record under the field names the OTel
|
|
// spec fixes for non-OTLP log formats, so a collector can lift them onto the log
|
|
// record's own trace fields. Gated on the whole span context being valid: a trace
|
|
// ID without a span ID names a request but not the operation within it.
|
|
func (h *ContextHandler) Handle(ctx context.Context, rec slog.Record) error {
|
|
if sc := trace.SpanContextFromContext(ctx); sc.IsValid() {
|
|
rec.AddAttrs(
|
|
slog.String(ateattr.LogTraceIDField, sc.TraceID().String()),
|
|
slog.String(ateattr.LogSpanIDField, sc.SpanID().String()),
|
|
slog.String(ateattr.LogTraceFlagsField, fmt.Sprintf("%02x", byte(sc.TraceFlags()))),
|
|
)
|
|
}
|
|
|
|
return h.internal.Handle(ctx, rec)
|
|
}
|
|
|
|
func (h *ContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return &ContextHandler{internal: h.internal.WithAttrs(attrs)}
|
|
}
|
|
|
|
func (h *ContextHandler) WithGroup(name string) slog.Handler {
|
|
return &ContextHandler{internal: h.internal.WithGroup(name)}
|
|
}
|