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

stdouttrace observability: skip metric work when instruments are disabled (#7853)

Add `Enabled(ctx)` checks to stdout trace exporter observability.

Benchmarks improved from ~120–370 ns/op to ~67 ns/op.

No user-facing changes.

Existing tests still cover behavior and i think this change doesn’t need
a changelog entry.
Issue: #7800

Co-authored-by: David Ashpole <dashpole@google.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Nesterov Yehor
2026-02-03 22:52:02 +01:00
committed by GitHub
parent e0ad47f3fe
commit 90c75c4b66
@@ -150,10 +150,12 @@ func NewInstrumentation(id int64) (*Instrumentation, error) {
func (i *Instrumentation) ExportSpans(ctx context.Context, nSpans int) ExportOp {
start := time.Now()
addOpt := get[metric.AddOption](addOptPool)
defer put(addOptPool, addOpt)
*addOpt = append(*addOpt, i.setOpt)
i.inflightSpans.Add(ctx, int64(nSpans), *addOpt...)
if i.inflightSpans.Enabled(ctx) {
addOpt := get[metric.AddOption](addOptPool)
defer put(addOptPool, addOpt)
*addOpt = append(*addOpt, i.setOpt)
i.inflightSpans.Add(ctx, int64(nSpans), *addOpt...)
}
return ExportOp{
ctx: ctx,
@@ -177,20 +179,32 @@ type ExportOp struct {
// The err parameter indicates whether the operation failed. If err is not nil,
// the number of failed spans (nSpans - success) is also recorded.
func (e ExportOp) End(success int64, err error) {
inflightSpansEnable := e.inst.inflightSpans.Enabled(e.ctx)
exportedSpansEnable := e.inst.exportedSpans.Enabled(e.ctx)
opDurationEnable := e.inst.opDuration.Enabled(e.ctx)
if !inflightSpansEnable && !exportedSpansEnable && !opDurationEnable {
return
}
addOpt := get[metric.AddOption](addOptPool)
defer put(addOptPool, addOpt)
*addOpt = append(*addOpt, e.inst.setOpt)
e.inst.inflightSpans.Add(e.ctx, -e.nSpans, *addOpt...)
if inflightSpansEnable {
e.inst.inflightSpans.Add(e.ctx, -e.nSpans, *addOpt...)
}
// Record the success and duration of the operation.
//
// Do not exclude 0 values, as they are valid and indicate no spans
// were exported which is meaningful for certain aggregations.
e.inst.exportedSpans.Add(e.ctx, success, *addOpt...)
if exportedSpansEnable {
e.inst.exportedSpans.Add(e.ctx, success, *addOpt...)
}
mOpt := e.inst.setOpt
if err != nil {
if err != nil && exportedSpansEnable {
attrs := get[attribute.KeyValue](measureAttrsPool)
defer put(measureAttrsPool, attrs)
*attrs = append(*attrs, e.inst.attrs...)
@@ -207,8 +221,10 @@ func (e ExportOp) End(success int64, err error) {
e.inst.exportedSpans.Add(e.ctx, e.nSpans-success, *addOpt...)
}
recordOpt := get[metric.RecordOption](recordOptPool)
defer put(recordOptPool, recordOpt)
*recordOpt = append(*recordOpt, mOpt)
e.inst.opDuration.Record(e.ctx, time.Since(e.start).Seconds(), *recordOpt...)
if opDurationEnable {
recordOpt := get[metric.RecordOption](recordOptPool)
defer put(recordOptPool, recordOpt)
*recordOpt = append(*recordOpt, mOpt)
e.inst.opDuration.Record(e.ctx, time.Since(e.start).Seconds(), *recordOpt...)
}
}