Skip to content

Commit fe2bf8a

Browse files
committed
instrumentation: add Enabled checks to OTLP log metrics
1 parent d45961b commit fe2bf8a

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

exporters/otlp/otlplog/otlploggrpc/internal/observ/instrumentation.go

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,22 @@ func NewInstrumentation(id int64, target string) (*Instrumentation, error) {
160160
// ExportLogs method returns.
161161
func (i *Instrumentation) ExportLogs(ctx context.Context, count int64) ExportOp {
162162
start := time.Now()
163-
addOpt := get[metric.AddOption](addOpPool)
164-
defer put(addOpPool, addOpt)
165-
166-
*addOpt = append(*addOpt, i.addOpt)
167-
168-
i.logInflightMetric.Add(ctx, count, *addOpt...)
169-
170-
return ExportOp{
163+
ext := ExportOp{
171164
nLogs: count,
172165
ctx: ctx,
173166
start: start,
174167
inst: i,
175168
}
169+
if(i.logInflightMetric.Enabled(ctx)){
170+
addOpt := get[metric.AddOption](addOpPool)
171+
defer put(addOpPool, addOpt)
172+
173+
*addOpt = append(*addOpt, i.addOpt)
174+
175+
i.logInflightMetric.Add(ctx, count, *addOpt...)
176+
}
177+
178+
return ext;
176179
}
177180

178181
// ExportOp tracks the operation being observed by [Instrumentation.ExportLogs].
@@ -193,33 +196,50 @@ type ExportOp struct {
193196
// of successfully exported logs will be determined by inspecting the
194197
// RejectedItems field of the PartialSuccess.
195198
func (e ExportOp) End(err error) {
196-
addOpt := get[metric.AddOption](addOpPool)
197-
defer put(addOpPool, addOpt)
198-
*addOpt = append(*addOpt, e.inst.addOpt)
199-
200-
e.inst.logInflightMetric.Add(e.ctx, -e.nLogs, *addOpt...)
201-
success := successful(e.nLogs, err)
202-
e.inst.logExportedMetric.Add(e.ctx, success, *addOpt...)
203-
204-
if err != nil {
205-
// Add the error.type attribute to the attribute set.
206-
attrs := get[attribute.KeyValue](attrsPool)
207-
defer put(attrsPool, attrs)
208-
*attrs = append(*attrs, e.inst.presetAttrs...)
209-
*attrs = append(*attrs, semconv.ErrorType(err))
210-
211-
o := metric.WithAttributeSet(attribute.NewSet(*attrs...))
212-
213-
// Reset addOpt with new attribute set
214-
*addOpt = append((*addOpt)[:0], o)
215-
216-
e.inst.logExportedMetric.Add(e.ctx, e.nLogs-success, *addOpt...)
199+
logInflightMetricEnabled := e.inst.logInflightMetric.Enabled(e.ctx)
200+
logExportedMetricEnabled := e.inst.logExportedMetric.Enabled(e.ctx)
201+
logExportedDurationMetricEnabled := e.inst.logExportedDurationMetric.Enabled(e.ctx)
202+
203+
var addOpt *[]metric.AddOption
204+
if logInflightMetricEnabled || logExportedMetricEnabled {
205+
success := successful(e.nLogs, err)
206+
addOpt = get[metric.AddOption](addOpPool)
207+
defer put(addOpPool, addOpt)
208+
*addOpt = append(*addOpt, e.inst.addOpt)
209+
210+
if logInflightMetricEnabled {
211+
e.inst.logInflightMetric.Add(e.ctx, -e.nLogs, *addOpt...)
212+
}
213+
214+
success := successful(e.nLogs, err)
215+
if logExportedMetricEnabled {
216+
e.inst.logExportedMetric.Add(e.ctx, success, *addOpt...)
217+
}
218+
219+
if err != nil && logExportedMetricEnabled {
220+
// Add the error.type attribute to the attribute set.
221+
attrs := get[attribute.KeyValue](attrsPool)
222+
defer put(attrsPool, attrs)
223+
*attrs = append(*attrs, e.inst.presetAttrs...)
224+
*attrs = append(*attrs, semconv.ErrorType(err))
225+
226+
o := metric.WithAttributeSet(attribute.NewSet(*attrs...))
227+
228+
// Reset addOpt with new attribute set
229+
// Note: addOpt is guaranteed non-nil here because it was
230+
// initialized in the parent if-block.
231+
*addOpt = append((*addOpt)[:0], o)
232+
233+
e.inst.logExportedMetric.Add(e.ctx, e.nLogs-success, *addOpt...)
234+
}
217235
}
218236

219-
recordOpt := get[metric.RecordOption](recordOptPool)
220-
defer put(recordOptPool, recordOpt)
221-
*recordOpt = append(*recordOpt, e.inst.recordOption(err))
222-
e.inst.logExportedDurationMetric.Record(e.ctx, time.Since(e.start).Seconds(), *recordOpt...)
237+
if logExportedDurationMetricEnabled {
238+
recordOpt := get[metric.RecordOption](recordOptPool)
239+
defer put(recordOptPool, recordOpt)
240+
*recordOpt = append(*recordOpt, e.inst.recordOption(err))
241+
e.inst.logExportedDurationMetric.Record(e.ctx, time.Since(e.start).Seconds(), *recordOpt...)
242+
}
223243
}
224244

225245
func (i *Instrumentation) recordOption(err error) metric.RecordOption {

0 commit comments

Comments
 (0)