4
4
"context"
5
5
"flag"
6
6
"fmt"
7
+ sdktrace "go.opentelemetry.io/otel/sdk/trace"
7
8
"os"
8
9
"runtime/debug"
9
10
"slices"
@@ -26,7 +27,6 @@ import (
26
27
"sigs.k8s.io/controller-runtime/pkg/manager"
27
28
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
28
29
29
- sdktrace "go.opentelemetry.io/otel/sdk/trace"
30
30
corev1 "k8s.io/api/core/v1"
31
31
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
32
32
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -135,74 +135,24 @@ func main() {
135
135
// Get a config to talk to the apiserver
136
136
cfg := ctrl .GetConfigOrDie ()
137
137
138
- log .Infof ("Setting up tracing with ID: %s, Parent ID: %s, Endpoint: %s" , traceIDHex , spanIDHex , endpoint )
138
+ log .Debugf ("Setting up tracing with ID: %s, Parent ID: %s, Endpoint: %s" , traceIDHex , spanIDHex , endpoint )
139
139
traceCtx , tp , err := telemetry .SetupTracing (signalCtx , traceIDHex , spanIDHex , endpoint )
140
140
if err != nil {
141
- log .Errorf ("Failed to setup tracing: %v" , err )
141
+ log .Warnf ("Failed to setup tracing: %v" , err )
142
142
// Continue without tracing instead of failing
143
143
traceCtx = signalCtx
144
- } else if tp != nil {
145
- defer func (tp * sdktrace.TracerProvider , ctx context.Context ) {
146
- log .Info ("Shutting down tracer provider to ensure all spans are exported" )
147
- // Give time for queued spans to be exported before shutdown
148
- shutdownCtx , cancel := context .WithTimeout (ctx , 5 * time .Second )
149
- defer cancel ()
150
-
151
- if err := tp .Shutdown (shutdownCtx ); err != nil {
152
- log .Errorf ("Error shutting down tracer provider: %v" , err )
144
+ } else {
145
+ defer func () {
146
+ if tp != nil {
147
+ shutdownTracerProvider (signalCtx , tp )
153
148
} else {
154
- log .Info ( "Tracer provider successfully shut down " )
149
+ log .Warn ( "No tracer provider created, tracing will be disabled " )
155
150
}
156
- }(tp , signalCtx )
157
- log .Info ("OpenTelemetry tracing successfully configured" )
158
- } else {
159
- log .Warn ("No tracer provider created, tracing will be disabled" )
151
+ }()
160
152
}
161
153
162
- log .Infof ("Starting spans with trace context: %+v" , traceCtx )
163
-
164
- opts := []trace.SpanStartOption {
165
- trace .WithAttributes (
166
- attribute .String ("component" , "operator" ),
167
- attribute .String ("namespace" , currentNamespace ),
168
- attribute .String ("service.name" , "mongodb-kubernetes-operator" ),
169
- // let's ensure that the root span follows the given parent span
170
- attribute .String ("trace.parent_id" , spanIDHex ),
171
- ),
172
- trace .WithSpanKind (trace .SpanKindServer ),
173
- }
174
-
175
- // Use a very distinctive name for the root span to make it easier to find
176
- ctxWithSpan , operatorSpan := telemetry .TRACER .Start (traceCtx , "MONGODB_OPERATOR_ROOT" , opts ... )
177
- log .Infof ("Started root operator span with ID: %s in trace %s" , operatorSpan .SpanContext ().SpanID ().String (), operatorSpan .SpanContext ().TraceID ().String ())
178
-
179
- // We need to manually flush the span when the program ends
180
- defer func () {
181
- // Add more context to the root span before ending it
182
- operatorSpan .SetAttributes (
183
- attribute .String ("operator.shutdown" , "true" ),
184
- attribute .String ("operator.span.status" , "complete" ),
185
- attribute .Bool ("root" , true ),
186
- )
187
-
188
- log .Info ("Ending root operator span and flushing it to exporter" )
189
- operatorSpan .End ()
190
- log .Info ("Root operator span ended" )
191
-
192
- if tp != nil {
193
- // Give enough time for the root span to be exported
194
- log .Info ("exporting spans..." )
195
-
196
- // Force a flush of any pending spans
197
- shutdownCtx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
198
- defer cancel ()
199
- if err := tp .Shutdown (shutdownCtx ); err != nil {
200
- log .Errorf ("Error flushing spans: %v" , err )
201
- } else {
202
- log .Info ("Spans successfully flushed" )
203
- }
204
- }
205
- }()
154
+ ctxWithSpan , operatorSpan := startRootSpan (currentNamespace , spanIDHex , traceCtx )
155
+ defer operatorSpan .End ()
206
156
207
157
managerOptions := ctrl.Options {
208
158
Scheme : scheme ,
@@ -377,6 +327,33 @@ func main() {
377
327
}
378
328
}
379
329
330
+ func startRootSpan (currentNamespace string , spanIDHex string , traceCtx context.Context ) (context.Context , trace.Span ) {
331
+ opts := []trace.SpanStartOption {
332
+ trace .WithAttributes (
333
+ attribute .String ("component" , "operator" ),
334
+ attribute .String ("namespace" , currentNamespace ),
335
+ attribute .String ("service.name" , "mongodb-kubernetes-operator" ),
336
+ // let's ensure that the root span follows the given parent span
337
+ attribute .String ("trace.parent_id" , spanIDHex ),
338
+ ),
339
+ }
340
+
341
+ ctxWithSpan , operatorSpan := telemetry .TRACER .Start (traceCtx , "MONGODB_OPERATOR_ROOT" , opts ... )
342
+ log .Debugf ("Started root operator span with ID: %s in trace %s" , operatorSpan .SpanContext ().SpanID ().String (), operatorSpan .SpanContext ().TraceID ().String ())
343
+ return ctxWithSpan , operatorSpan
344
+ }
345
+
346
+ func shutdownTracerProvider (signalCtx context.Context , tp * sdktrace.TracerProvider ) {
347
+ shutdownCtx , cancel := context .WithTimeout (signalCtx , 5 * time .Second )
348
+ defer cancel ()
349
+
350
+ if err := tp .Shutdown (shutdownCtx ); err != nil {
351
+ log .Errorf ("Error shutting down tracer provider: %v" , err )
352
+ } else {
353
+ log .Debug ("Tracer provider successfully shut down" )
354
+ }
355
+ }
356
+
380
357
func setupMongoDBCRD (ctx context.Context , mgr manager.Manager , imageUrls images.ImageUrls , initDatabaseNonStaticImageVersion , databaseNonStaticImageVersion string , forceEnterprise bool , memberClusterObjectsMap map [string ]runtime_cluster.Cluster ) error {
381
358
if err := operator .AddStandaloneController (ctx , mgr , imageUrls , initDatabaseNonStaticImageVersion , databaseNonStaticImageVersion , forceEnterprise ); err != nil {
382
359
return err
0 commit comments