Repository navigation
Conversation
The spans added in prometheus#19410 call otel.Tracer("") inline, which locks and looks up the global provider on every span, and build span attributes (AST serialization, URL redaction, timestamp formatting) even when no tracer is installed. Use a cached package-level tracer and only set attributes when span.IsRecording() is true, in promql, scrape and rules. Signed-off-by: Jan Fajerski <[email protected]> Assisted-by: opencode (claude-opus-4-8)
Use a cached package-level tracer instead of calling otel.Tracer("")
inline, and guard span attribute construction with span.IsRecording(),
in storage/remote, util/stats and web/api/v1.
Signed-off-by: Jan Fajerski <[email protected]>
Assisted-by: opencode (claude-opus-4-8)
Name each cached tracer after its package instead of using an empty instrumentation scope name, so exported spans are attributed to the package that created them. Signed-off-by: Jan Fajerski <[email protected]> Assisted-by: opencode (claude-opus-4-8)
|
/prombench v3.14.0 |
|
⏱️ Welcome to Prometheus Benchmarking Tool. ⏱️ Compared versions: After the successful deployment (check status here), the benchmarking results can be viewed at: Available Commands:
|
|
cc @prymitive |
| NewRangeQuery(ctx context.Context, q storage.Queryable, opts QueryOpts, qs string, start, end time.Time, interval time.Duration) (Query, error) | ||
| } | ||
|
|
||
| var tracer = otel.Tracer("github.com/prometheus/prometheus/promql") |
There was a problem hiding this comment.
NOTE: I was surprised too (I thought global tracer is generally a recommended pattern), but apparently this won't work well without hot reloading for tracing providers in ApplyConfig (AI hinted but I verified). As a consequence promql engine tracing provider won't be updated after the first update.
The one-shot behaviour is NOT in the public godoc: otel.SetTracerProvider only says "registers tp as the global trace provider". It is stated only in internal comments: "It is guaranteed by the caller that this happens only once" — https://github.com/open-telemetry/opentelemetry-go/blob/v1.46.0/internal/global/trace.go#L51-L74
and the delegateTraceOnce in https://github.com/open-telemetry/opentelemetry-go/blob/v1.46.0/internal/global/state.go#L85-L91
Repro test:
const scope = "github.com/prometheus/prometheus/otelswap"
// cachedTracer mimics a package-level `var tracer = otel.Tracer(...)`: it is
// resolved at init time, before any TracerProvider is installed.
var cachedTracer = otel.Tracer(scope)
// TestGlobalDelegationIsOneShot shows that otel.SetTracerProvider only re-points
// already-created tracers on its first call.
//
// NOTE: OTel's global state is process-wide and cannot be reset, so this has to
// be the only test in the package that installs a TracerProvider.
func TestGlobalDelegationIsOneShot(t *testing.T) {
ctx := context.Background()
exp1 := tracetest.NewInMemoryExporter()
tp1 := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exp1))
// First install: the cached tracer is retroactively re-pointed at tp1.
otel.SetTracerProvider(tp1)
startEnd(ctx, cachedTracer, "cached/first")
startEnd(ctx, otel.Tracer(scope), "inline/first")
require.Equal(t, []string{"cached/first", "inline/first"}, spanNames(exp1))
// Second install, exactly what tracing.Manager.ApplyConfig does on reload.
exp2 := tracetest.NewInMemoryExporter()
tp2 := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exp2))
otel.SetTracerProvider(tp2)
exp1.Reset()
startEnd(ctx, cachedTracer, "cached/second")
startEnd(ctx, otel.Tracer(scope), "inline/second")
// An inline lookup follows the new provider...
require.Equal(t, []string{"inline/second"}, spanNames(exp2))
// ...while the cached tracer still writes to the *previous* provider.
require.Equal(t, []string{"cached/second"}, spanNames(exp1))
// Now shut the old provider down, which ApplyConfig does before installing a
// new one. Spans from the cached tracer now go nowhere at all.
require.NoError(t, tp1.Shutdown(ctx))
exp1.Reset()
exp2.Reset()
_, span := cachedTracer.Start(ctx, "cached/third")
// The span is still sampled and recorded, so the cost of building
// attributes is still paid...
require.True(t, span.IsRecording())
span.End()
// ...but it is exported nowhere.
require.Empty(t, spanNames(exp1))
require.Empty(t, spanNames(exp2))
}There was a problem hiding this comment.
@dashpole confirmed, and more important -- global provider is not always recommended.
The global is for convenience, similar to a global prometheus registry. It supports delegating only once to handle races between init functions or package import ordering (so otel.Tracer() and otel.SetTracerProvider() can race, but still work). It isn't meant for repeated applyconfig.
There was a problem hiding this comment.
This adds a subtle bug I think. We can also run microbenchmarks but it's unlikely this is a reason for the overhead. We're likely hitting #18046
Commented on the 3.15 PR prombench
| // n.String() serializes the AST node, so only build it when the | ||
| // span is actually recorded. | ||
| selectSpan.SetAttributes( | ||
| attribute.String("selector", n.String()), |
There was a problem hiding this comment.
Those optimizations are pretty nice, just the global cache is tricky
|
/prombench cancel |
|
Benchmark cancel is in progress. |
The spans added in #19410 call
otel.Tracer("")inline, which locks andlooks up the global provider on every span, and build span attributes
(AST serialization, URL redaction, timestamp formatting) even when no
tracer is installed. Use a cached package-level tracer and only set
attributes when span.IsRecording() is true, in promql, scrape and rules.
Checking if this addresses the prombench regression observed in #19669.
Which issue(s) does the PR fix:
Release notes for end users (ALL commits must be considered).
Reviewers should verify clarity and quality.