-
Notifications
You must be signed in to change notification settings - Fork 131
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
Open
ci-operator
wants to merge
1
commit into
tektoncd:main
Choose a base branch
from
ci-operator:distributed-tracing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| 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.uber.org/zap" | ||
| ) | ||
|
|
||
| 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 { | ||
| if os.Getenv(EnvOTLPEndpoint) == "" { | ||
| logger.Info("OTLP endpoint not configured, using noop tracer provider") | ||
| return noopProvider() | ||
| } | ||
| if os.Getenv(EnvTracesSampler) == "" { | ||
| logger.Info("OTEL_TRACES_SAMPLER not set, tracing disabled (set explicitly to opt in)") | ||
| return noopProvider() | ||
| } | ||
|
|
||
| exporter, err := newExporter(context.Background(), logger) | ||
| if err != nil { | ||
| logger.Errorw("failed to create OTLP exporter, using noop tracer provider", "error", err) | ||
| return noopProvider() | ||
| } | ||
|
|
||
| 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", protocolFromEnv()) | ||
|
|
||
| return &TracerProvider{shutdown: tp.Shutdown} | ||
| } | ||
|
|
||
| func noopProvider() *TracerProvider { | ||
| return &TracerProvider{shutdown: func(context.Context) error { return nil }} | ||
| } | ||
|
|
||
| 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) (sdktrace.SpanExporter, error) { | ||
| endpoint := os.Getenv(EnvOTLPEndpoint) | ||
| proto := protocolFromEnv() | ||
| 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", "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() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.