@@ -183,6 +183,9 @@ type Timer struct {
183183//
184184// If err is non-nil, an appropriate error type attribute will be included.
185185func (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.
207209func (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