|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package notifier |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/sha256" |
| 22 | + "crypto/tls" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "net/url" |
| 26 | + "slices" |
| 27 | + "strings" |
| 28 | + |
| 29 | + apiv1beta3 "github.com/fluxcd/notification-controller/api/v1beta3" |
| 30 | + eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" |
| 31 | + "go.opentelemetry.io/otel" |
| 32 | + "go.opentelemetry.io/otel/attribute" |
| 33 | + "go.opentelemetry.io/otel/codes" |
| 34 | + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
| 35 | + "go.opentelemetry.io/otel/sdk/resource" |
| 36 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 37 | + semconv "go.opentelemetry.io/otel/semconv/v1.34.0" |
| 38 | + "go.opentelemetry.io/otel/trace" |
| 39 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 40 | +) |
| 41 | + |
| 42 | +type OTLPTracer struct { |
| 43 | + URL string |
| 44 | + ProxyURL string |
| 45 | + Headers map[string]string |
| 46 | + TLSConfig *tls.Config |
| 47 | +} |
| 48 | + |
| 49 | +func NewOTLPTracer(url string, proxyURL string, headers map[string]string, tlsConfig *tls.Config) *OTLPTracer { |
| 50 | + return &OTLPTracer{ |
| 51 | + URL: url, |
| 52 | + ProxyURL: proxyURL, |
| 53 | + Headers: headers, |
| 54 | + TLSConfig: tlsConfig, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Post implements the notifier.Interface |
| 59 | +func (t *OTLPTracer) Post(ctx context.Context, event eventv1.Event) error { |
| 60 | + logger := log.FromContext(ctx).WithValues( |
| 61 | + "event", event.Reason, |
| 62 | + "object", fmt.Sprintf("%s/%s/%s", event.InvolvedObject.Kind, event.InvolvedObject.Namespace, event.InvolvedObject.Name), |
| 63 | + "severity", event.Severity, |
| 64 | + ) |
| 65 | + |
| 66 | + // Set up OTLP exporter options |
| 67 | + logger.V(1).Info("Configuring OTLP HTTP options", "url", t.URL) |
| 68 | + // Parse URL to extract host and port |
| 69 | + parsedURL, err := url.Parse(t.URL) |
| 70 | + if err != nil { |
| 71 | + logger.Error(err, "Failed to parse URL", "url", t.URL) |
| 72 | + return fmt.Errorf("failed to parse URL: %w", err) |
| 73 | + } |
| 74 | + httpOptions := []otlptracehttp.Option{ |
| 75 | + otlptracehttp.WithEndpoint(parsedURL.Host), |
| 76 | + } |
| 77 | + |
| 78 | + // Add headers if available |
| 79 | + if len(t.Headers) > 0 { |
| 80 | + logger.V(1).Info("Adding headers to OTLP exporter", "headerCount", len(t.Headers)) |
| 81 | + httpOptions = append(httpOptions, otlptracehttp.WithHeaders(t.Headers)) |
| 82 | + } |
| 83 | + |
| 84 | + // Add TLS config if available |
| 85 | + if t.TLSConfig != nil { |
| 86 | + logger.V(1).Info("Configuring TLS for OTLP exporter") |
| 87 | + httpOptions = append(httpOptions, otlptracehttp.WithTLSClientConfig(t.TLSConfig)) |
| 88 | + } else if parsedURL.Scheme == "http" { |
| 89 | + logger.V(1).Info("Using insecure connection for OTLP exporter") |
| 90 | + httpOptions = append(httpOptions, otlptracehttp.WithInsecure()) |
| 91 | + } |
| 92 | + |
| 93 | + // Add proxy if available |
| 94 | + if t.ProxyURL != "" { |
| 95 | + logger.V(1).Info("Setting up Proxy URL for OTLP exporter", "proxyURL", t.ProxyURL) |
| 96 | + proxyURL, err := url.Parse(t.ProxyURL) |
| 97 | + if err != nil { |
| 98 | + logger.Error(err, "Failed to parse proxy URL", "proxyURL", t.ProxyURL) |
| 99 | + } else { |
| 100 | + httpOptions = append(httpOptions, otlptracehttp.WithProxy(func(*http.Request) (*url.URL, error) { |
| 101 | + return proxyURL, nil |
| 102 | + })) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Create exporter |
| 107 | + logger.V(1).Info("Creating OTLP exporter") |
| 108 | + exporter, err := otlptracehttp.New(ctx, httpOptions...) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed to create OTLP exporter: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + // Extract revision from event metadata |
| 114 | + revision := "" |
| 115 | + for k, v := range event.Metadata { |
| 116 | + if strings.Contains(k, "revision") { |
| 117 | + revision = v |
| 118 | + logger.V(1).Info("Found revision in metadata", "revision", revision) |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Get value from context (this would need to be passed in from event_handlers.go) |
| 124 | + alertUID, ok := ctx.Value("alertUID").(string) |
| 125 | + if !ok { |
| 126 | + alertUID = "unknown" |
| 127 | + logger.V(1).Info("alertUID not found in context, using default", "alertUID", alertUID) |
| 128 | + } else { |
| 129 | + logger.V(1).Info("Using alertUID from context", "alertUID", alertUID) |
| 130 | + } |
| 131 | + alertName, ok := ctx.Value("alertName").(string) |
| 132 | + if !ok { |
| 133 | + alertName = "unknown" |
| 134 | + logger.V(1).Info("alertName not found in context, using default", "alertName", alertName) |
| 135 | + } else { |
| 136 | + logger.V(1).Info("Using alertName from context", "alertName", alertName) |
| 137 | + } |
| 138 | + alertNamespace, ok := ctx.Value("alertNamespace").(string) |
| 139 | + if !ok { |
| 140 | + alertNamespace = "unknown" |
| 141 | + logger.V(1).Info("alertNamespace not found in context, using default", "alertNamespace", alertNamespace) |
| 142 | + } else { |
| 143 | + logger.V(1).Info("Using alertNamespace from context", "alertNamespace", alertNamespace) |
| 144 | + } |
| 145 | + |
| 146 | + // Create trace provider with resource attributes |
| 147 | + logger.V(1).Info("Creating trace provider") |
| 148 | + serviceName := fmt.Sprintf("%s: %s/%s", apiv1beta3.AlertKind, alertNamespace, alertName) |
| 149 | + resource := resource.NewWithAttributes( |
| 150 | + semconv.SchemaURL, |
| 151 | + semconv.ServiceInstanceID(alertUID), |
| 152 | + semconv.ServiceName(serviceName), |
| 153 | + semconv.ServiceNamespace(alertNamespace), |
| 154 | + ) |
| 155 | + tp := sdktrace.NewTracerProvider( |
| 156 | + sdktrace.WithBatcher(exporter), |
| 157 | + sdktrace.WithResource(resource), |
| 158 | + ) |
| 159 | + |
| 160 | + // Setup global tracer provider |
| 161 | + otel.SetTracerProvider(tp) |
| 162 | + |
| 163 | + // Tracer instatiation for span creation |
| 164 | + tracer := otel.Tracer("flux:notification-controller") |
| 165 | + |
| 166 | + // Generate the following IDs: |
| 167 | + // - SpanID: <AlertUID>:<AlertNamespace/AlertName> |
| 168 | + // - TraceID: <AlertUID>:<revisionID> |
| 169 | + logger.V(1).Info("Generating trace IDs", "alertUID", alertUID, "revision", revision) |
| 170 | + spanIDStr := generateID(alertUID, fmt.Sprintf("%s/%s", alertNamespace, alertName)) |
| 171 | + traceIDStr := generateID(alertUID, revision) |
| 172 | + |
| 173 | + var traceID trace.TraceID |
| 174 | + var spanID trace.SpanID |
| 175 | + copy(traceID[:], traceIDStr[:16]) |
| 176 | + copy(spanID[:], spanIDStr[:8]) |
| 177 | + |
| 178 | + // Create trace context with the generated ID |
| 179 | + var spanCtx context.Context |
| 180 | + |
| 181 | + // Replace trace context to use Alert UID + revision |
| 182 | + logger.V(1).Info("Trace context", "kind", event.InvolvedObject.Kind) |
| 183 | + // Create new context for root span |
| 184 | + currentSpanContext := trace.SpanContextFromContext(ctx) |
| 185 | + |
| 186 | + // For source objects: create root span with custom traceID |
| 187 | + if isSource(event.InvolvedObject.Kind) { |
| 188 | + logger.V(1).Info("Create a new trace", "traceID", traceID.String()) |
| 189 | + spanCtx = trace.ContextWithSpanContext(ctx, |
| 190 | + trace.NewSpanContext(trace.SpanContextConfig{ |
| 191 | + TraceID: traceID, |
| 192 | + SpanID: spanID, |
| 193 | + TraceFlags: trace.FlagsSampled, |
| 194 | + }), |
| 195 | + ) |
| 196 | + } else { |
| 197 | + // For non-source objects: use existing trace context (becomes child) |
| 198 | + if currentSpanContext.IsValid() { |
| 199 | + logger.V(1).Info("Creating child span", "parentTraceID", traceID.String()) |
| 200 | + spanCtx = ctx // Use existing context as parent |
| 201 | + } else { |
| 202 | + // Fallback: create context with same traceID but no parent |
| 203 | + logger.V(1).Info("Creating orphan span with shared traceID", "traceID", traceID.String()) |
| 204 | + spanCtx = trace.ContextWithSpanContext(ctx, |
| 205 | + trace.NewSpanContext(trace.SpanContextConfig{ |
| 206 | + TraceID: traceID, |
| 207 | + TraceFlags: trace.FlagsSampled, |
| 208 | + }), |
| 209 | + ) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Create single span with proper attributes |
| 214 | + spanName := fmt.Sprintf("%s: %s/%s", event.InvolvedObject.Kind, event.InvolvedObject.Namespace, event.InvolvedObject.Name) |
| 215 | + _, span := tracer.Start(spanCtx, spanName, |
| 216 | + trace.WithAttributes( |
| 217 | + attribute.String("flux.object.uid", string(event.InvolvedObject.UID)), |
| 218 | + attribute.String("flux.object.kind", event.InvolvedObject.Kind), |
| 219 | + attribute.String("flux.object.name", event.InvolvedObject.Name), |
| 220 | + attribute.String("flux.object.namespace", event.InvolvedObject.Namespace), |
| 221 | + attribute.String("flux.event.severity", event.Severity), |
| 222 | + attribute.String("flux.event.reason", event.Reason), |
| 223 | + attribute.String("flux.event.message", event.Message), |
| 224 | + ), |
| 225 | + trace.WithTimestamp(event.Timestamp.Time), |
| 226 | + ) |
| 227 | + |
| 228 | + // Add metadata attributes |
| 229 | + for k, v := range event.Metadata { |
| 230 | + span.SetAttributes(attribute.String(fmt.Sprintf("flux.event.metadata.%s", k), v)) |
| 231 | + } |
| 232 | + |
| 233 | + // Set status based on event severity |
| 234 | + if event.Severity == eventv1.EventSeverityError { |
| 235 | + span.SetStatus(codes.Error, event.Message) |
| 236 | + } else { |
| 237 | + span.SetStatus(codes.Ok, event.Message) |
| 238 | + } |
| 239 | + |
| 240 | + logger.Info("Successfully sent trace to OTLP endpoint", |
| 241 | + "url", t.URL, |
| 242 | + "object", fmt.Sprintf("%s/%s/%s", event.InvolvedObject.Kind, event.InvolvedObject.Namespace, event.InvolvedObject.Name), |
| 243 | + "reason", event.Reason) |
| 244 | + |
| 245 | + defer func() { |
| 246 | + span.End() |
| 247 | + tp.ForceFlush(ctx) |
| 248 | + tp.Shutdown(ctx) |
| 249 | + exporter.Shutdown(ctx) |
| 250 | + }() |
| 251 | + |
| 252 | + return nil |
| 253 | +} |
| 254 | + |
| 255 | +// Add this function to generate trace and span ID |
| 256 | +func generateID(alertUID, sourceRevision string) []byte { |
| 257 | + input := fmt.Sprintf("%s:%s", alertUID, sourceRevision) |
| 258 | + hash := sha256.Sum256([]byte(input)) |
| 259 | + return hash[:] |
| 260 | +} |
| 261 | + |
| 262 | +func isSource(kind string) bool { |
| 263 | + sourceKinds := []string{"GitRepository", "HelmRepository", "OCIRepository", "Bucket"} |
| 264 | + return slices.Contains(sourceKinds, kind) |
| 265 | +} |
0 commit comments