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

Checked if instrument enabled before measuring in prometheus (#7866)

This pr updates proemetheus instrumentation file to check if instrument
enabled before measuring in prometheus.
tracked in #7800

---------

Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
Saharsh Tibrewala
2026-02-09 14:31:29 +05:30
committed by GitHub
parent 647a41e5be
commit c3f955a9cd
@@ -183,6 +183,9 @@ type Timer struct {
//
// If err is non-nil, an appropriate error type attribute will be included.
func (t Timer) Stop(err error) {
if !t.hist.Enabled(t.ctx) {
return
}
recordOpt := get[metric.RecordOption](recordOptPool)
defer put(recordOptPool, recordOpt)
*recordOpt = append(*recordOpt, t.inst.setOpt)
@@ -196,7 +199,6 @@ func (t Timer) Stop(err error) {
set := attribute.NewSet(*attrs...)
*recordOpt = append((*recordOpt)[:0], metric.WithAttributeSet(set))
}
t.hist.Record(t.ctx, time.Since(t.start).Seconds(), *recordOpt...)
}
@@ -205,11 +207,12 @@ func (t Timer) Stop(err error) {
// It returns an [ExportOp] that tracks the export operation. The
// [ExportOp.End] method must be called when the export completes.
func (i *Instrumentation) ExportMetrics(ctx context.Context, n int64) ExportOp {
addOpt := get[metric.AddOption](addOptPool)
defer put(addOptPool, addOpt)
*addOpt = append(*addOpt, i.setOpt)
i.inflightMetric.Add(ctx, n, *addOpt...)
if i.inflightMetric.Enabled(ctx) {
addOpt := get[metric.AddOption](addOptPool)
defer put(addOptPool, addOpt)
*addOpt = append(*addOpt, i.setOpt)
i.inflightMetric.Add(ctx, n, *addOpt...)
}
return ExportOp{ctx: ctx, nMetrics: n, inst: i}
}
@@ -232,10 +235,14 @@ func (e ExportOp) End(success int64, err error) {
defer put(addOptPool, addOpt)
*addOpt = append(*addOpt, e.inst.setOpt)
e.inst.inflightMetric.Add(e.ctx, -e.nMetrics, *addOpt...)
e.inst.exportedMetric.Add(e.ctx, success, *addOpt...)
if e.inst.inflightMetric.Enabled(e.ctx) {
e.inst.inflightMetric.Add(e.ctx, -e.nMetrics, *addOpt...)
}
if e.inst.exportedMetric.Enabled(e.ctx) {
e.inst.exportedMetric.Add(e.ctx, success, *addOpt...)
}
if err != nil {
if err != nil && e.inst.exportedMetric.Enabled(e.ctx) {
attrs := get[attribute.KeyValue](measureAttrsPool)
defer put(measureAttrsPool, attrs)
*attrs = append(*attrs, e.inst.attrs...)