Debian Codegraph @

sdk/log/internal/observ/simple_log_processor.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package observ // import "go.opentelemetry.io/otel/sdk/log/internal/observ"
5
6 import (
7 "context"
8 "fmt"
9 "sync"
10 "sync/atomic"
11
12 "go.opentelemetry.io/otel"
13 "go.opentelemetry.io/otel/attribute"
14 "go.opentelemetry.io/otel/metric"
15 "go.opentelemetry.io/otel/sdk"
16 "go.opentelemetry.io/otel/sdk/log/internal/x"
17 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
18 "go.opentelemetry.io/otel/semconv/v1.40.0/otelconv"
19 )
20
21 const (
22 // ScopeName is the name of the instrumentation scope.
23 ScopeName = "go.opentelemetry.io/otel/sdk/log/internal/observ"
24 )
25
26 var measureAttrsPool = sync.Pool{
27 New: func() any {
28 // "component.name" + "component.type" + "error.type"
29 const n = 1 + 1 + 1
30 s := make([]attribute.KeyValue, 0, n)
31 // Return a pointer to a slice instead of a slice itself
32 // to avoid allocations on every call.
33 return &s
34 },
35 }
36
37 // simpleProcessorN is a global 0-based count of the number of simple processor created.
38 var simpleProcessorN atomic.Int64
39
40 // NextSimpleProcessorID returns the next unique ID for a simpleProcessor.
41 func NextSimpleProcessorID() int64 {
42 const inc = 1
43 return simpleProcessorN.Add(inc) - inc
44 }
45
46 // SetSimpleProcessorID sets the exporter ID counter to v and returns the previous
47 // value.
48 //
49 // This function is useful for testing purposes, allowing you to reset the
50 // counter. It should not be used in production code.
51 func SetSimpleProcessorID(v int64) int64 {
52 return simpleProcessorN.Swap(v)
53 }
54
55 // GetSLPComponentName returns the component name attribute for a
56 // SimpleLogProcessor with the given ID.
57 func GetSLPComponentName(id int64) attribute.KeyValue {
58 t := otelconv.ComponentTypeSimpleLogProcessor
59 name := fmt.Sprintf("%s/%d", t, id)
60 return semconv.OTelComponentName(name)
61 }
62
63 // SLP is the instrumentation for an OTel SDK SimpleLogProcessor.
64 type SLP struct {
65 processed metric.Int64Counter
66 attrs []attribute.KeyValue
67 addOpts []metric.AddOption
68 }
69
70 // NewSLP returns instrumentation for an OTel SDK SimpleLogProcessor with the
71 // provided ID.
72 //
73 // If the experimental observability is disabled, nil is returned.
74 func NewSLP(id int64) (*SLP, error) {
75 if !x.Observability.Enabled() {
76 return nil, nil
77 }
78
79 meter := otel.GetMeterProvider()
80 mt := meter.Meter(
81 ScopeName,
82 metric.WithInstrumentationVersion(sdk.Version()),
83 metric.WithSchemaURL(semconv.SchemaURL),
84 )
85
86 p, err := otelconv.NewSDKProcessorLogProcessed(mt)
87 if err != nil {
88 err = fmt.Errorf("failed to create a processed log metric: %w", err)
89 return nil, err
90 }
91
92 name := GetSLPComponentName(id)
93 componentType := p.AttrComponentType(otelconv.ComponentTypeSimpleLogProcessor)
94 attrs := []attribute.KeyValue{name, componentType}
95 addOpts := []metric.AddOption{metric.WithAttributeSet(attribute.NewSet(attrs...))}
96
97 return &SLP{
98 processed: p.Inst(),
99 attrs: attrs,
100 addOpts: addOpts,
101 }, nil
102 }
103
104 // LogProcessed records that a log has been processed by the SimpleLogProcessor.
105 // If err is non-nil, it records the processing error as an attribute.
106 func (slp *SLP) LogProcessed(ctx context.Context, err error) {
107 if slp.processed.Enabled(ctx) {
108 slp.processed.Add(ctx, 1, slp.addOption(err)...)
109 }
110 }
111
112 func (slp *SLP) addOption(err error) []metric.AddOption {
113 if err == nil {
114 return slp.addOpts
115 }
116 attrs := measureAttrsPool.Get().(*[]attribute.KeyValue)
117 defer func() {
118 *attrs = (*attrs)[:0] // reset the slice
119 measureAttrsPool.Put(attrs)
120 }()
121
122 *attrs = append(*attrs, slp.attrs...)
123 *attrs = append(*attrs, semconv.ErrorType(err))
124
125 // Do not inefficiently make a copy of attrs by using
126 // WithAttributes instead of WithAttributeSet.
127 return []metric.AddOption{metric.WithAttributeSet(attribute.NewSet(*attrs...))}
128 }