From 8ec162afec5a16073ea0cff99a0c79062615530c Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 6 Oct 2025 09:44:13 -0400 Subject: [PATCH] chore(tracing): Initialize tracing early, before wire (#112007) Signed-off-by: Dave Henderson --- pkg/cmd/grafana-server/commands/cli.go | 9 +++++++-- pkg/infra/tracing/tracing.go | 23 ++++++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/grafana-server/commands/cli.go b/pkg/cmd/grafana-server/commands/cli.go index 8850132105f..f13b3bada3e 100644 --- a/pkg/cmd/grafana-server/commands/cli.go +++ b/pkg/cmd/grafana-server/commands/cli.go @@ -11,9 +11,7 @@ import ( "syscall" "time" - "github.com/grafana/grafana/pkg/services/featuremgmt" _ "github.com/grafana/pyroscope-go/godeltaprof/http/pprof" - "github.com/urfave/cli/v2" "github.com/grafana/grafana/pkg/api" @@ -21,8 +19,10 @@ import ( "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/infra/process" + "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/server" "github.com/grafana/grafana/pkg/services/apiserver/standalone" + "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/setting" ) @@ -111,6 +111,11 @@ func RunServer(opts standalone.BuildInfo, cli *cli.Context) error { return err } + // Initialize tracing early to ensure it's always available for other services + if err := tracing.InitTracing(cfg); err != nil { + return err + } + s, err := server.Initialize( cli.Context, cfg, diff --git a/pkg/infra/tracing/tracing.go b/pkg/infra/tracing/tracing.go index f8b340d12df..c015569cf8a 100644 --- a/pkg/infra/tracing/tracing.go +++ b/pkg/infra/tracing/tracing.go @@ -11,6 +11,8 @@ import ( "sync" "time" + "github.com/go-kit/log/level" + "github.com/grafana/dskit/services" jaegerpropagator "go.opentelemetry.io/contrib/propagators/jaeger" "go.opentelemetry.io/contrib/samplers/jaegerremote" "go.opentelemetry.io/otel" @@ -27,11 +29,9 @@ import ( "go.opentelemetry.io/otel/trace/noop" "google.golang.org/grpc/credentials" - "github.com/go-kit/log/level" - - "github.com/grafana/dskit/services" "github.com/grafana/grafana/pkg/apimachinery/errutil" "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/setting" ) const ( @@ -105,6 +105,23 @@ func ProvideService(tracingCfg *TracingConfig) (*TracingService, error) { return ots, nil } +// InitTracing initializes the tracing service with the provided configuration. +// Used to initialize tracing early to ensure it's always available for other +// services, outside of the wire context. +func InitTracing(cfg *setting.Cfg) error { + tracingCfg, err := ParseTracingConfig(cfg) + if err != nil { + return fmt.Errorf("parse tracing config: %w", err) + } + + _, err = ProvideService(tracingCfg) + if err != nil { + return fmt.Errorf("initialize tracing: %w", err) + } + + return nil +} + func NewNoopTracerService() *TracingService { tp := &noopTracerProvider{TracerProvider: noop.NewTracerProvider()} otel.SetTracerProvider(tp)