Skip to content

Commit c3f955a

Browse files
itssaharshdmathieu
andauthored
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>
1 parent 647a41e commit c3f955a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

exporters/prometheus/internal/observ/instrumentation.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ type Timer struct {
183183
//
184184
// If err is non-nil, an appropriate error type attribute will be included.
185185
func (t Timer) Stop(err error) {
186+
if !t.hist.Enabled(t.ctx) {
187+
return
188+
}
186189
recordOpt := get[metric.RecordOption](recordOptPool)
187190
defer put(recordOptPool, recordOpt)
188191
*recordOpt = append(*recordOpt, t.inst.setOpt)
@@ -196,7 +199,6 @@ func (t Timer) Stop(err error) {
196199
set := attribute.NewSet(*attrs...)
197200
*recordOpt = append((*recordOpt)[:0], metric.WithAttributeSet(set))
198201
}
199-
200202
t.hist.Record(t.ctx, time.Since(t.start).Seconds(), *recordOpt...)
201203
}
202204

@@ -205,11 +207,12 @@ func (t Timer) Stop(err error) {
205207
// It returns an [ExportOp] that tracks the export operation. The
206208
// [ExportOp.End] method must be called when the export completes.
207209
func (i *Instrumentation) ExportMetrics(ctx context.Context, n int64) ExportOp {
208-
addOpt := get[metric.AddOption](addOptPool)
209-
defer put(addOptPool, addOpt)
210-
*addOpt = append(*addOpt, i.setOpt)
211-
212-
i.inflightMetric.Add(ctx, n, *addOpt...)
210+
if i.inflightMetric.Enabled(ctx) {
211+
addOpt := get[metric.AddOption](addOptPool)
212+
defer put(addOptPool, addOpt)
213+
*addOpt = append(*addOpt, i.setOpt)
214+
i.inflightMetric.Add(ctx, n, *addOpt...)
215+
}
213216

214217
return ExportOp{ctx: ctx, nMetrics: n, inst: i}
215218
}
@@ -232,10 +235,14 @@ func (e ExportOp) End(success int64, err error) {
232235
defer put(addOptPool, addOpt)
233236
*addOpt = append(*addOpt, e.inst.setOpt)
234237

235-
e.inst.inflightMetric.Add(e.ctx, -e.nMetrics, *addOpt...)
236-
e.inst.exportedMetric.Add(e.ctx, success, *addOpt...)
238+
if e.inst.inflightMetric.Enabled(e.ctx) {
239+
e.inst.inflightMetric.Add(e.ctx, -e.nMetrics, *addOpt...)
240+
}
241+
if e.inst.exportedMetric.Enabled(e.ctx) {
242+
e.inst.exportedMetric.Add(e.ctx, success, *addOpt...)
243+
}
237244

238-
if err != nil {
245+
if err != nil && e.inst.exportedMetric.Enabled(e.ctx) {
239246
attrs := get[attribute.KeyValue](measureAttrsPool)
240247
defer put(measureAttrsPool, attrs)
241248
*attrs = append(*attrs, e.inst.attrs...)

0 commit comments

Comments
 (0)