1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

sdk/trace/internal/observ: guard SpanStarted and spanLive with Enabled (#8067)

Guards SpanStarted and spanLive with Enabled(ctx) to avoid building
metric
options/attributes when instruments are disabled.

```
BenchmarkTracer (noop MeterProvider):

  SpanStarted: ~1.77 ns/op -> ~0.48 ns/op (~3.7x)
  SpanLive:    ~2.05 ns/op -> ~0.52 ns/op (~3.9x)
  SpanEnded:   ~2.05 ns/op -> ~0.52 ns/op (~3.9x)
```

No behavior change when enabled; existing tests cover enabled path.
~~No CHANGELOG entry, following #7848.~~

Issue: #7800

---------

Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
yoshimura
2026-03-19 00:11:37 +09:00
committed by GitHub
parent 025b01be59
commit 528ebabbf0
2 changed files with 9 additions and 0 deletions
+1
View File
@@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Introduce the `EMPTY` Type in `go.opentelemetry.io/otel/attribute` to reflect that an empty value is now a valid value, with `INVALID` remaining as a deprecated alias of `EMPTY`. (#8038)
- Refactor slice handling in `go.opentelemetry.io/otel/attribute` to optimize short slice values with fixed-size fast paths. (#8039)
- Improve performance of span metric recording in `go.opentelemetry.io/otel/sdk/trace` by returning early if self-observability is not enabled. (#8067)
### Deprecated
+8
View File
@@ -55,6 +55,10 @@ func NewTracer() (Tracer, error) {
func (t Tracer) Enabled() bool { return t.enabled }
func (t Tracer) SpanStarted(ctx context.Context, psc trace.SpanContext, span trace.Span) {
if !t.started.Enabled(ctx) {
return
}
key := spanStartedKey{
parent: parentStateNoParent,
sampling: samplingStateDrop,
@@ -89,6 +93,10 @@ func (t Tracer) SpanEnded(ctx context.Context, span trace.Span) {
}
func (t Tracer) spanLive(ctx context.Context, value int64, span trace.Span) {
if !t.live.Enabled(ctx) {
return
}
key := spanLiveKey{sampled: span.SpanContext().IsSampled()}
opts := spanLiveOpts[key]
t.live.Add(ctx, value, opts...)