MCPcopy
hub / github.com/redis/go-redis / Init

Method Init

extra/redisotel-native/redisotel.go:71–108  ·  view source on GitHub ↗

Init initializes OpenTelemetry observability globally for all Redis clients. This should be called once at application startup, BEFORE creating any Redis clients. After initialization, all Redis clients will automatically collect and export metrics without needing any additional configuration.

(cfg *Config)

Source from the content-addressed store, hash-verified

69// After initialization, all Redis clients will automatically collect and export
70// metrics without needing any additional configuration.
71func (o *ObservabilityInstance) Init(cfg *Config) error {
72 o.mu.Lock()
73 defer o.mu.Unlock()
74
75 // If already initialized, return error
76 if o.initialized {
77 return errors.New("redisotel: already initialized, call Shutdown() before reinitializing")
78 }
79
80 o.config = cfg
81
82 if !cfg.Enabled {
83 return nil
84 }
85
86 // Get meter provider (use global if not provided)
87 meterProvider := cfg.MeterProvider
88 if meterProvider == nil {
89 meterProvider = otel.GetMeterProvider()
90 }
91
92 meter := meterProvider.Meter(
93 "github.com/redis/go-redis",
94 metric.WithInstrumentationVersion(redis.Version()),
95 )
96
97 internalCfg := o.configToInternal(cfg)
98 recorder, err := o.createRecorder(meter, internalCfg)
99 if err != nil {
100 return fmt.Errorf("failed to create metrics recorder: %w", err)
101 }
102
103 o.recorder = recorder
104 o.initialized = true
105 redis.SetOTelRecorder(recorder)
106
107 return nil
108}
109
110// IsEnabled returns true if observability is initialized and enabled.
111func (o *ObservabilityInstance) IsEnabled() bool {

Callers 3

mainFunction · 0.80
TestMetricsUnderStressFunction · 0.80

Calls 2

configToInternalMethod · 0.95
createRecorderMethod · 0.95

Tested by 2

TestMetricsUnderStressFunction · 0.64