-
Notifications
You must be signed in to change notification settings - Fork 136
fix(tracing): direct OTel SDK setup for chain-coherent sampling #2756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
theakshaypant marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package tracing | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "strconv" | ||
|
|
||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
| "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
| "go.opentelemetry.io/otel/propagation" | ||
| "go.opentelemetry.io/otel/sdk/resource" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| semconv "go.opentelemetry.io/otel/semconv/v1.40.0" | ||
| "go.opentelemetry.io/otel/trace/noop" | ||
| "go.uber.org/zap" | ||
| knativetracing "knative.dev/pkg/observability/tracing" | ||
| ) | ||
|
|
||
| const ( | ||
| EnvOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT" | ||
| EnvOTLPProtocol = "OTEL_EXPORTER_OTLP_PROTOCOL" | ||
| EnvOTLPTracesProtocol = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL" | ||
| EnvTracesSampler = "OTEL_TRACES_SAMPLER" | ||
| EnvTracesSamplerArg = "OTEL_TRACES_SAMPLER_ARG" | ||
|
|
||
| protocolGRPC = "grpc" | ||
| protocolHTTP = "http/protobuf" | ||
| ) | ||
|
|
||
| type TracerProvider struct { | ||
| shutdown func(context.Context) error | ||
| } | ||
|
|
||
| func New(logger *zap.SugaredLogger) *TracerProvider { | ||
| otelConfigured := os.Getenv(EnvOTLPEndpoint) != "" && os.Getenv(EnvTracesSampler) != "" | ||
| if otelConfigured && !globalIsNoop() { | ||
| logger.Warn("OpenTelemetry and Knative tracing both configured; spans go through OpenTelemetry, Knative's tracer is unused. Set `tracing-protocol: none` in `pipelines-as-code-config-observability` to disable Knative, or unset `OTEL_EXPORTER_OTLP_ENDPOINT` to disable OpenTelemetry.") | ||
| } | ||
|
|
||
| if os.Getenv(EnvOTLPEndpoint) == "" { | ||
| logger.Info("OpenTelemetry not configured (OTLP endpoint missing)") | ||
| return passthroughProvider() | ||
| } | ||
| if os.Getenv(EnvTracesSampler) == "" { | ||
| logger.Info("OpenTelemetry not configured (sampler missing)") | ||
| return passthroughProvider() | ||
| } | ||
|
|
||
| proto := protocolFromEnv() | ||
| exporter, err := newExporter(context.Background(), logger, proto) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as per some AI review, does timeout handling needed here??
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No - |
||
| if err != nil { | ||
| logger.Errorw("failed to create OTLP exporter", "error", err) | ||
| return passthroughProvider() | ||
| } | ||
|
|
||
| res, err := resource.Merge( | ||
| resource.Default(), | ||
| resource.NewWithAttributes( | ||
| semconv.SchemaURL, | ||
| semconv.ServiceName(TracerName), | ||
| ), | ||
| ) | ||
| if err != nil { | ||
| logger.Errorw("failed to create resource", "error", err) | ||
| res = resource.Default() | ||
| } | ||
|
|
||
| tp := sdktrace.NewTracerProvider( | ||
| sdktrace.WithBatcher(exporter), | ||
| sdktrace.WithResource(res), | ||
| sdktrace.WithSampler(samplerFromEnv(logger)), | ||
| ) | ||
|
|
||
| otel.SetTracerProvider(tp) | ||
| otel.SetTextMapPropagator(propagation.TraceContext{}) | ||
|
|
||
| logger.Infow("tracing initialized", "endpoint", os.Getenv(EnvOTLPEndpoint), "protocol", proto) | ||
|
|
||
| return &TracerProvider{shutdown: tp.Shutdown} | ||
| } | ||
|
|
||
| func passthroughProvider() *TracerProvider { | ||
| return &TracerProvider{shutdown: func(context.Context) error { return nil }} | ||
| } | ||
|
|
||
| func globalIsNoop() bool { | ||
| tp := otel.GetTracerProvider() | ||
| if _, ok := tp.(noop.TracerProvider); ok { | ||
| return true | ||
| } | ||
| // Knative wraps noop in its own TracerProvider when tracing-protocol is none/absent. | ||
| if knativeProvider, ok := tp.(*knativetracing.TracerProvider); ok { | ||
| _, isNoop := knativeProvider.TracerProvider.(noop.TracerProvider) | ||
| return isNoop | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func protocolFromEnv() string { | ||
| if v := os.Getenv(EnvOTLPTracesProtocol); v != "" { | ||
| return v | ||
| } | ||
| if v := os.Getenv(EnvOTLPProtocol); v != "" { | ||
| return v | ||
| } | ||
| return protocolGRPC | ||
| } | ||
|
|
||
| func newExporter(ctx context.Context, logger *zap.SugaredLogger, proto string) (sdktrace.SpanExporter, error) { | ||
| endpoint := os.Getenv(EnvOTLPEndpoint) | ||
| switch proto { | ||
| case protocolHTTP: | ||
| return otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(endpoint)) | ||
| case protocolGRPC: | ||
| return otlptracegrpc.New(ctx, otlptracegrpc.WithEndpointURL(endpoint)) | ||
| default: | ||
| logger.Errorw("unsupported OTLP protocol; falling back to grpc", "protocol", proto) | ||
| return otlptracegrpc.New(ctx, otlptracegrpc.WithEndpointURL(endpoint)) | ||
| } | ||
| } | ||
|
|
||
| func (tp *TracerProvider) Shutdown(ctx context.Context) error { | ||
| if tp.shutdown != nil { | ||
| return tp.shutdown(ctx) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func samplerFromEnv(logger *zap.SugaredLogger) sdktrace.Sampler { | ||
| name := os.Getenv(EnvTracesSampler) | ||
| argStr := os.Getenv(EnvTracesSamplerArg) | ||
| arg, err := strconv.ParseFloat(argStr, 64) | ||
| if err != nil && argStr != "" { | ||
| logger.Errorw("ignoring malformed sampler argument; defaulting to 0% sampling", "env", EnvTracesSamplerArg, "value", argStr) | ||
| } | ||
| if argStr == "" && (name == "traceidratio" || name == "parentbased_traceidratio") { | ||
| logger.Infow("ratio sampler selected without "+EnvTracesSamplerArg+"; defaulting to 0% sampling", "env", EnvTracesSampler, "value", name) | ||
| } | ||
| switch name { | ||
| case "always_on": | ||
| return sdktrace.AlwaysSample() | ||
| case "always_off": | ||
| return sdktrace.NeverSample() | ||
| case "traceidratio": | ||
| return sdktrace.TraceIDRatioBased(arg) | ||
| case "parentbased_always_on": | ||
| return sdktrace.ParentBased(sdktrace.AlwaysSample()) | ||
| case "parentbased_always_off": | ||
| return sdktrace.ParentBased(sdktrace.NeverSample()) | ||
| case "parentbased_traceidratio": | ||
| return sdktrace.ParentBased(sdktrace.TraceIDRatioBased(arg)) | ||
| } | ||
| logger.Warnw("unrecognized OTEL_TRACES_SAMPLER value; falling back to never sample", "value", name) | ||
| return sdktrace.NeverSample() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the configmap keys are removed, I see no change in controller. Does that mean the configmap based tracing continue to work even with these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Knative tracing wrapper that consumed these
_examplekeys is replaced; the new tracer inpkg/tracing/provider.goreads the OTel-standard env vars directly. The_exampleblock was just documentation for keys nothing reads anymore.This is not the same ConfigMap as
pipelines-as-code(the main one), which holds thetracing-label-action|application|componentoperator label-name mappings.On the "no change in controller" observation - we verified end-to-end that with neither
OTEL_EXPORTER_OTLP_ENDPOINTnorOTEL_TRACES_SAMPLERset, PaC falls back to a noop tracer and emits no spans. If you're seeing tracing behavior unchanged from the pre-PR state, could you share how to reproduce so we can dig in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By changes in controller, I intended to ask if the knative base (eventing/adapter) that pac uses, reads these observability configmap keys for any arbitrary configuration/operations. Ref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - the Knative eventing-adapter still consumes
config-observabilityfor non-tracing observability (metrics/profiling/etc. viaevadapter.NewObservabilityConfiguratorFromConfigMap()at the line you linked, logging is read fromconfig-loggingseparately). What this PR changes is specifically the tracing portion: PaC's old Knative tracing wrapper (added in bd9f468) readtracing-protocol/tracing-endpoint/tracing-sampling-ratefrom this ConfigMap; the newpkg/tracing/provider.goreads the OTel-standard env vars directly and that wrapper is gone, so those three keys went with the_exampleblock. The Knative-base reads for non-tracing observability are unchanged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ci-operator as it looks breaking change and @theakshaypant is considerate about, can't we introduce env based configuration as fallback to configmap so that we're keeping both, considering other users may want to use configmap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we read those keys, the
tracing-sampling-ratevalue would have to map to eithertraceidratio(which preserves the flat-sampling behavior this PR is fixing) orparentbased_traceidratio(which silently changes existing users' behavior). It's a breaking change either way - a ConfigMap fallback wouldn't actually preserve the old behavior. We're going env-vars-only and calling out the break in the tracing doc.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a user still sets these keys in their configmap?
IIUC knative's
sharedmain.SetupObservabilityOrDieis still called by the watcher which starts tracing. Ref sharedmain, Ref watcherHow would these "conflicting" tracing configs be running? Would this cause a leak or is it a functional bug? Or have I completely missed the mark π€¨
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this would be an issue. Both tracers initialize when an operator has set the Knative tracing keys. OpenTelemetry's setup runs after Knative's and overrides the global tracer provider, so spans flow through OpenTelemetry; Knative's tracer is configured but receives no spans.
There's now a startup warning when both are configured, the
noopProviderhelper was renamed topassthroughProvider(it never installed a noop globally), and the info-log messages no longer claim otherwise. The docs cover all this in a newWhen both are configuredsubsection.Whether a leak or a functional bug, a tracer receiving no spans is wasted-resource either way. There's now a startup warning, the log messages are clearer, and the docs have a dedicated section for this case.