feat(servicekit): OpenTelemetry tracer provider and RPC propagation

This commit is contained in:
sam committed 2026-10-25 23:11:03 +11:00
1 parent b30cb0b1db
commit bc41094b14
23 files changed
+288 -29

No files matched your search

+9 -6
View File
@@ -223,7 +223,7 @@ func (b *boot) cleanup() {
}
}
func (b *boot) loadConfig(_ context.Context, cfgPath, overlayPath string) error {
func (b *boot) loadConfig(ctx context.Context, cfgPath, overlayPath string) error {
loader, doc, err := servicekit.Resolve(b.log, cfgPath, overlayPath)
if err != nil {
return err
@@ -233,20 +233,23 @@ func (b *boot) loadConfig(_ context.Context, cfgPath, overlayPath string) error
b.cfg = b.doc.Config
b.cfgHolder = config.NewHolder(b.cfg)
b.currentProvenance.Store(&b.doc.Provenance)
b.setupLogging()
return nil
return b.setupLogging(ctx)
}
// setupLogging stays on stderr for an offline command (stderrLog): creating,
// appending to or rotating the deployment's log is a side effect a diagnosis
// must not have.
func (b *boot) setupLogging() {
func (b *boot) setupLogging(ctx context.Context) error {
if b.stderrLog {
return
return nil
}
logs, err := servicekit.Logging(ctx, b.cfg, "daemon")
if err != nil {
return err
}
logs := servicekit.Logging(b.cfg, "daemon")
b.log, b.logFeed, b.taskLogs = logs.Log, logs.Feed, logs.TaskLogs
b.addCleanup(func() { _ = logs.Closer.Close() })
return nil
}
// openStores wires the secret registry and the conversation store. The
+6 -3
View File
@@ -109,7 +109,7 @@ type server struct {
func Run(ctx context.Context, options Options) error {
b := &server{log: slog.New(slog.NewJSONHandler(os.Stderr, nil))}
defer b.cleanup()
if err := b.loadConfig(options.Config, options.Overlay); err != nil {
if err := b.loadConfig(ctx, options.Config, options.Overlay); err != nil {
return err
}
if err := b.openState(ctx); err != nil {
@@ -151,7 +151,7 @@ func (b *server) chatLLM() *runtime.Runtime { return b.llm.Load() }
func (b *server) setLLM(rt *runtime.Runtime) { b.llm.Store(rt) }
func (b *server) announceReady() { sdnotify.Ready(b.log) }
func (b *server) loadConfig(cfgPath, overlayPath string) error {
func (b *server) loadConfig(ctx context.Context, cfgPath, overlayPath string) error {
_, doc, err := servicekit.Resolve(b.log, cfgPath, overlayPath)
if err != nil {
return err
@@ -160,7 +160,10 @@ func (b *server) loadConfig(cfgPath, overlayPath string) error {
b.cfgPath = cfgPath
b.catalog = modelcatalog.NewCatalog(cfgPath)
b.cfgHolder = config.NewHolder(b.cfg)
logs := servicekit.Logging(b.cfg, "gateway")
logs, err := servicekit.Logging(ctx, b.cfg, "gateway")
if err != nil {
return err
}
b.log, b.logFeed = logs.Log, logs.Feed
b.addCleanup(func() { _ = logs.Closer.Close() })
b.secrets = secret.NewRegistry()
+4
View File
@@ -26,6 +26,7 @@ type ResolvedConfig struct {
WorkDir string
BotUser string
ShowToolCalls bool
Telemetry config.Telemetry
}
type projection struct {
@@ -37,6 +38,7 @@ type projection struct {
workDir string
botUser string
showToolCalls bool
telemetry config.Telemetry
}
// Resolve loads the configuration, extracts the messaging projection and
@@ -71,6 +73,7 @@ func Resolve(o Options, log *slog.Logger) (ResolvedConfig, error) {
WorkDir: proj.workDir,
BotUser: proj.botUser,
ShowToolCalls: proj.showToolCalls,
Telemetry: proj.telemetry,
}, nil
}
@@ -95,6 +98,7 @@ func project(cfg config.Config) projection {
workDir: cfg.WorkDir,
botUser: cfg.BotUser,
showToolCalls: cfg.Chat.ShowToolCalls,
telemetry: cfg.Telemetry,
}
}
+17 -6
View File
@@ -10,6 +10,7 @@ import (
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/app/servicekit"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/messaging"
@@ -22,20 +23,30 @@ import (
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
"github.com/samcharles93/archie-core/internal/infrastructure/secretengine"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/releaseupdate"
"github.com/samcharles93/archie-core/internal/secret"
)
// Run runs the standalone Messaging Service process until ctx is cancelled.
func Run(ctx context.Context, o Options) error {
log := slog.Default().With("service", "archie-messaging")
log, feed := servicekit.Diagnostics(log, presence.Messaging)
// resolveTraced resolves the configuration and starts trace export from it.
func resolveTraced(ctx context.Context, o Options, log *slog.Logger) (ResolvedConfig, func(), error) {
cfg, err := Resolve(o, log)
if err != nil {
return fmt.Errorf("resolve messaging options: %w", err)
return ResolvedConfig{}, nil, fmt.Errorf("resolve messaging options: %w", err)
}
stop, err := telemetry.Start(ctx, cfg.Telemetry, "messaging", buildinfo.Version)
return cfg, stop, err
}
// Run runs the standalone Messaging Service process until ctx is cancelled.
func Run(ctx context.Context, o Options) error {
log, feed := servicekit.Diagnostics(slog.Default().With("service", "archie-messaging"), presence.Messaging)
cfg, stopTracing, err := resolveTraced(ctx, o, log)
if err != nil {
return err
}
defer stopTracing()
chat, closeGateway, err := gatewayrpc.Dial(cfg.Options.Gateway.Target, cfg.Identity)
if err != nil {
+3
View File
@@ -27,6 +27,7 @@ func Resolve(o Options, log *slog.Logger) (Options, error) {
o = merge(o, cfg)
tls = cfg.tls
o.instance = cfg.instance
o.telemetry = cfg.telemetry
}
}
if o.instance == "" {
@@ -70,6 +71,7 @@ type projection struct {
tls config.TLS
capture CaptureOptions
instance string
telemetry config.Telemetry
}
func project(cfg config.Config) projection {
@@ -82,6 +84,7 @@ func project(cfg config.Config) projection {
daemon: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameDaemon).Target},
tls: cfg.TLS,
instance: cfg.Instance,
telemetry: cfg.Telemetry,
capture: CaptureOptions{
Retention: cfg.Capture.Retention.Std(),
MaxEvents: cfg.Capture.MaxEvents,
+3
View File
@@ -5,6 +5,7 @@ package archieui
import (
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
)
@@ -78,6 +79,8 @@ type Options struct {
// instance is the deployment name, from the configuration source or
// ARCHIE_INSTANCE.
instance string
// telemetry is the [telemetry] section, read only by Run.
telemetry config.Telemetry
}
// CaptureOptions carries the [capture] settings the capture receiver needs.
+15 -2
View File
@@ -14,6 +14,7 @@ import (
"time"
"github.com/samcharles93/archie-core/internal/app/servicekit"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/presence"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
@@ -23,6 +24,7 @@ import (
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
"github.com/samcharles93/archie-core/internal/infrastructure/harnessrpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
"github.com/samcharles93/archie-core/internal/webui"
)
@@ -34,6 +36,16 @@ func buildAccessChain(ctx context.Context, store *staterpc.Client, log *slog.Log
return infraaccess.StartLive(ctx, store.ListPolicies, store.PoliciesVersion, log)
}
// resolveTraced resolves the options and starts trace export from them.
func resolveTraced(ctx context.Context, options Options, log *slog.Logger) (Options, func(), error) {
opts, err := Resolve(options, log)
if err != nil {
return Options{}, nil, err
}
stop, err := telemetry.Start(ctx, opts.telemetry, "ui", buildinfo.Version)
return opts, stop, err
}
// Run serves the dashboard until ctx is cancelled. It resolves its own
// options, dials the Gateway and State Store contracts itself, composes the
// HTTP surface over them, and unwinds listener then clients in reverse order
@@ -41,10 +53,11 @@ func buildAccessChain(ctx context.Context, store *staterpc.Client, log *slog.Log
func Run(ctx context.Context, options Options) error {
log := slog.New(slog.NewJSONHandler(os.Stderr, nil)).With("component", "ui")
log, feed := servicekit.Diagnostics(log, presence.UI)
opts, err := Resolve(options, log)
opts, stopTracing, err := resolveTraced(ctx, options, log)
if err != nil {
return err
}
defer stopTracing()
warnPlainHTTP(log, opts)
var cleanups []func()
defer func() {
@@ -157,7 +170,7 @@ func wireHarness(opts Options, srv *webui.Server, cleanups *[]func()) error {
// serve runs handler until ctx ends, then shuts down within
// ShutdownTimeout.
func serve(ctx context.Context, listener net.Listener, handler http.Handler, opts Options) error {
server := &http.Server{Handler: handler, ReadHeaderTimeout: opts.ReadHeaderTimeout}
server := &http.Server{Handler: telemetry.Handler(handler), ReadHeaderTimeout: opts.ReadHeaderTimeout}
serveErr := make(chan error, 1)
go func() { serveErr <- server.Serve(listener) }()
select {
+22 -5
View File
@@ -1,14 +1,17 @@
package servicekit
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
"github.com/samcharles93/archie-core/internal/logging"
)
@@ -37,12 +40,18 @@ type Logs struct {
Closer io.Closer
}
// Logging opens the configured log for component. A file log that cannot be
// opened is reported and logging continues on stderr.
func Logging(cfg config.Config, component string) Logs {
// Logging opens the configured log for component and starts trace export. A
// file log that cannot be opened is reported and logging continues on stderr;
// a trace exporter that cannot be built fails startup. Closing flushes the
// exporter, then closes the log, so shutdown records are still written.
func Logging(ctx context.Context, cfg config.Config, component string) (Logs, error) {
stopTracing, err := telemetry.Start(ctx, cfg.Telemetry, component, buildinfo.Version)
if err != nil {
return Logs{}, err
}
feed := logging.NewFeed(1000)
taskLogs := logging.NewTaskRegistry(filepath.Join(cfg.StateDir, "logs", "tasks"), feed, logging.TaskSinkOptions{})
fileLog, closer, err := logging.New(logging.Options{
fileLog, fileCloser, err := logging.New(logging.Options{
File: cfg.Log.File,
MaxSizeMB: cfg.Log.MaxSizeMB,
Keep: cfg.Log.Keep,
@@ -56,9 +65,17 @@ func Logging(cfg config.Config, component string) Logs {
} else if cfg.Log.File != "" {
log.Info("logging to file", "path", cfg.Log.File)
}
return Logs{Log: log, Feed: feed, TaskLogs: taskLogs, Closer: closer}
closer := closerFunc(func() error {
stopTracing()
return fileCloser.Close()
})
return Logs{Log: log, Feed: feed, TaskLogs: taskLogs, Closer: closer}, nil
}
type closerFunc func() error
func (f closerFunc) Close() error { return f() }
// Diagnostics captures a service's stderr log for its recent-log contract.
func Diagnostics(log *slog.Logger, service string) (*slog.Logger, *logging.Feed) {
feed := logging.NewFeed(1000)
+3
View File
@@ -7,6 +7,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
)
// maxConcurrentStreams caps the calls one connection holds open at once, so a
@@ -20,6 +22,7 @@ const maxConcurrentStreams = 256
// the OS gives up on the socket.
func ServerLimits() []grpc.ServerOption {
return []grpc.ServerOption{
telemetry.ServerOption(),
grpc.MaxConcurrentStreams(maxConcurrentStreams),
grpc.ConnectionTimeout(10 * time.Second),
grpc.KeepaliveParams(keepalive.ServerParameters{
+5 -2
View File
@@ -48,7 +48,7 @@ func (b *server) cleanup() {
}
}
func (b *server) loadConfig(_ context.Context, cfgPath, overlayPath string) error {
func (b *server) loadConfig(ctx context.Context, cfgPath, overlayPath string) error {
_, doc, err := servicekit.Resolve(b.log, cfgPath, overlayPath)
if err != nil {
return err
@@ -57,7 +57,10 @@ func (b *server) loadConfig(_ context.Context, cfgPath, overlayPath string) erro
if b.stderrLog {
return nil
}
logs := servicekit.Logging(b.cfg, "state-store")
logs, err := servicekit.Logging(ctx, b.cfg, "state-store")
if err != nil {
return err
}
b.log, b.taskLogs, b.logFeed = logs.Log, logs.TaskLogs, logs.Feed
b.addCleanup(func() { _ = logs.Closer.Close() })
return nil