diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/observ/instrumentation.go b/exporters/otlp/otlptrace/otlptracehttp/internal/observ/instrumentation.go index 50e53c6a709..5def1ab81ce 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/observ/instrumentation.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/observ/instrumentation.go @@ -261,10 +261,12 @@ func parseIP(ip string) string { 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.addOpt) - 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.addOpt) + i.inflightSpans.Add(ctx, int64(nSpans), *addOpt...) + } return ExportOp{ ctx: ctx, @@ -299,14 +301,18 @@ func (e ExportOp) End(err error, status int) { defer put(addOptPool, addOpt) *addOpt = append(*addOpt, e.inst.addOpt) - e.inst.inflightSpans.Add(e.ctx, -e.nSpans, *addOpt...) + if e.inst.inflightSpans.Enabled(e.ctx) { + e.inst.inflightSpans.Add(e.ctx, -e.nSpans, *addOpt...) + } success := successful(e.nSpans, err) // Record successfully exported spans, even if the value is 0 which are // meaningful to distribution aggregations. - e.inst.exportedSpans.Add(e.ctx, success, *addOpt...) + if e.inst.exportedSpans.Enabled(e.ctx) { + e.inst.exportedSpans.Add(e.ctx, success, *addOpt...) + } - if err != nil { + if err != nil && e.inst.exportedSpans.Enabled(e.ctx) { attrs := get[attribute.KeyValue](measureAttrsPool) defer put(measureAttrsPool, attrs) *attrs = append(*attrs, e.inst.attrs...) @@ -321,12 +327,14 @@ func (e ExportOp) End(err error, status int) { e.inst.exportedSpans.Add(e.ctx, e.nSpans-success, *addOpt...) } - recOpt := get[metric.RecordOption](recordOptPool) - defer put(recordOptPool, recOpt) - *recOpt = append(*recOpt, e.inst.recordOption(err, status)) + if e.inst.opDuration.Enabled(e.ctx) { + recOpt := get[metric.RecordOption](recordOptPool) + defer put(recordOptPool, recOpt) + *recOpt = append(*recOpt, e.inst.recordOption(err, status)) - d := time.Since(e.start).Seconds() - e.inst.opDuration.Record(e.ctx, d, *recOpt...) + d := time.Since(e.start).Seconds() + e.inst.opDuration.Record(e.ctx, d, *recOpt...) + } } // recordOption returns a RecordOption with attributes representing the