Skip to content

Commit e4c1bec

Browse files
committed
cleanup main generation and logging
1 parent 5af0be9 commit e4c1bec

File tree

2 files changed

+43
-69
lines changed

2 files changed

+43
-69
lines changed

main.go

Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
sdktrace "go.opentelemetry.io/otel/sdk/trace"
78
"os"
89
"runtime/debug"
910
"slices"
@@ -26,7 +27,6 @@ import (
2627
"sigs.k8s.io/controller-runtime/pkg/manager"
2728
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
2829

29-
sdktrace "go.opentelemetry.io/otel/sdk/trace"
3030
corev1 "k8s.io/api/core/v1"
3131
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3232
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -135,74 +135,24 @@ func main() {
135135
// Get a config to talk to the apiserver
136136
cfg := ctrl.GetConfigOrDie()
137137

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)
139139
traceCtx, tp, err := telemetry.SetupTracing(signalCtx, traceIDHex, spanIDHex, endpoint)
140140
if err != nil {
141-
log.Errorf("Failed to setup tracing: %v", err)
141+
log.Warnf("Failed to setup tracing: %v", err)
142142
// Continue without tracing instead of failing
143143
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)
153148
} else {
154-
log.Info("Tracer provider successfully shut down")
149+
log.Warn("No tracer provider created, tracing will be disabled")
155150
}
156-
}(tp, signalCtx)
157-
log.Info("OpenTelemetry tracing successfully configured")
158-
} else {
159-
log.Warn("No tracer provider created, tracing will be disabled")
151+
}()
160152
}
161153

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()
206156

207157
managerOptions := ctrl.Options{
208158
Scheme: scheme,
@@ -377,6 +327,33 @@ func main() {
377327
}
378328
}
379329

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+
380357
func setupMongoDBCRD(ctx context.Context, mgr manager.Manager, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, memberClusterObjectsMap map[string]runtime_cluster.Cluster) error {
381358
if err := operator.AddStandaloneController(ctx, mgr, imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise); err != nil {
382359
return err

pkg/telemetry/trace.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ var TRACER = otel.Tracer("mongodb-kubernetes-operator")
1919
// SetupTracing initializes OpenTelemetry tracing from environment variables
2020
func SetupTracing(ctx context.Context, traceIDHex, parentIDHex, endpoint string) (context.Context, *sdktrace.TracerProvider, error) {
2121
if traceIDHex == "" || parentIDHex == "" || endpoint == "" {
22-
Logger.Info("tracing environment variables missing, not configuring tracing")
22+
Logger.Debug("tracing environment variables missing, not configuring tracing")
2323
return ctx, nil, nil
2424
}
2525

26-
Logger.Infof("Setting up tracing with traceIDHex=%s, parentIDHex=%s, endpoint=%s", traceIDHex, parentIDHex, endpoint)
26+
Logger.Debugf("Setting up tracing with traceIDHex=%s, parentIDHex=%s, endpoint=%s", traceIDHex, parentIDHex, endpoint)
2727

2828
traceID, err := trace.TraceIDFromHex(traceIDHex)
2929
if err != nil {
@@ -36,7 +36,7 @@ func SetupTracing(ctx context.Context, traceIDHex, parentIDHex, endpoint string)
3636
return ctx, nil, err
3737
}
3838

39-
Logger.Infof("Using trace ID: %s, parent span ID: %s", traceID.String(), parentSpanID.String())
39+
Logger.Debugf("Using trace ID: %s, parent span ID: %s", traceID.String(), parentSpanID.String())
4040
// Create a span context that marks this as a remote parent span context
4141
// This allows the operator to create spans that are children of the e2e test spans
4242
sc := trace.NewSpanContext(trace.SpanContextConfig{
@@ -46,15 +46,12 @@ func SetupTracing(ctx context.Context, traceIDHex, parentIDHex, endpoint string)
4646
Remote: true,
4747
})
4848

49-
// Create a context with the remote span context
5049
ctxWithSpan := trace.ContextWithRemoteSpanContext(ctx, sc)
5150

52-
// Log the details of the span context for debugging
5351
spanCtx := trace.SpanContextFromContext(ctxWithSpan)
54-
Logger.Infof("Created span context with TraceID: %s, SpanID: %s, Remote: %t",
52+
Logger.Debugf("Created span context with TraceID: %s, SpanID: %s, Remote: %t",
5553
spanCtx.TraceID().String(), spanCtx.SpanID().String(), spanCtx.IsRemote())
5654

57-
// Create a span processor with OTLP exporter
5855
exporter, err := otlptracegrpc.New(
5956
context.Background(),
6057
otlptracegrpc.WithEndpoint(endpoint),
@@ -69,11 +66,11 @@ func SetupTracing(ctx context.Context, traceIDHex, parentIDHex, endpoint string)
6966
sdktrace.WithBatchTimeout(5*time.Second),
7067
)
7168

72-
// Create a tracer provider with resource and better attributes for tracing
7369
res := resource.NewWithAttributes(
7470
semconv.SchemaURL,
7571
semconv.ServiceNameKey.String("mongodb-kubernetes-operator"),
7672
attribute.String("component", "operator"),
73+
// the trace we want the root to attach to
7774
attribute.String("trace.id", traceID.String()),
7875
)
7976

0 commit comments

Comments
 (0)