✨ feat(telemetry): ReconfigureTracerProvider for sampler hot-reload (ADR-0023 Phase 3, sub-phase 3.1) (#45)
Co-authored-by: Gabriel Radureau <arcodange@gmail.com> Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
This commit was merged in pull request #45.
This commit is contained in:
@@ -74,6 +74,36 @@ func Shutdown(ctx context.Context, tp *sdktrace.TracerProvider) error {
|
||||
return tp.Shutdown(ctx)
|
||||
}
|
||||
|
||||
// ReconfigureTracerProvider rebuilds the global tracer provider with the
|
||||
// updated sampler settings (ADR-0023 Phase 3 hot-reload). The previous
|
||||
// provider is gracefully shut down so in-flight spans are flushed.
|
||||
//
|
||||
// No-op if oldTP is nil — telemetry was disabled at startup, hot-reloading
|
||||
// it on would require a different code path (out of scope for Phase 3).
|
||||
//
|
||||
// Returns the new TracerProvider so the caller can track it for the next
|
||||
// shutdown / reconfigure cycle. On error the old TP is left in place.
|
||||
func (s *Setup) ReconfigureTracerProvider(ctx context.Context, oldTP *sdktrace.TracerProvider) (*sdktrace.TracerProvider, error) {
|
||||
if oldTP == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Build the new provider first — if anything fails we keep the old TP active.
|
||||
newTP, err := s.InitializeTracing(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// InitializeTracing already swapped the global provider via otel.SetTracerProvider,
|
||||
// so the new one is now active. Drain the old one so no spans are lost.
|
||||
if shutdownErr := oldTP.Shutdown(ctx); shutdownErr != nil {
|
||||
// Log via the standard logger — zerolog isn't imported in this package.
|
||||
log.Printf("ReconfigureTracerProvider: old TP shutdown failed: %v (new TP is active)", shutdownErr)
|
||||
}
|
||||
|
||||
return newTP, nil
|
||||
}
|
||||
|
||||
// getSampler returns the appropriate sampler based on configuration
|
||||
func (s *Setup) getSampler() sdktrace.Sampler {
|
||||
switch s.SamplerType {
|
||||
|
||||
Reference in New Issue
Block a user