1// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package metric // import "go.opentelemetry.io/otel/metric"
5
6import (
7 "context"
8
9 "go.opentelemetry.io/otel/metric/embedded"
10)
11
12// Int64Observable describes a set of instruments used asynchronously to record
13// int64 measurements once per collection cycle. Observations of these
14// instruments are only made within a callback.
15//
16// Warning: Methods may be added to this interface in minor releases.
17type Int64Observable interface {
18 Observable
19
20 int64Observable()
21}
22
23// Int64ObservableCounter is an instrument used to asynchronously record
24// increasing int64 measurements once per collection cycle. Observations are
25// only made within a callback for this instrument. The value observed is
26// assumed the to be the cumulative sum of the count.
27//
28// Warning: Methods may be added to this interface in minor releases. See
29// package documentation on API implementation for information on how to set
30// default behavior for unimplemented methods.
31type Int64ObservableCounter interface {
32 // Users of the interface can ignore this. This embedded type is only used
33 // by implementations of this interface. See the "API Implementations"
34 // section of the package documentation for more information.
35 embedded.Int64ObservableCounter
36
37 Int64Observable
38}
39
40// Int64ObservableCounterConfig contains options for asynchronous counter
41// instruments that record int64 values.
42type Int64ObservableCounterConfig struct {
43 description string
44 unit string
45 callbacks []Int64Callback
46}
47
48// NewInt64ObservableCounterConfig returns a new [Int64ObservableCounterConfig]
49// with all opts applied.
50func NewInt64ObservableCounterConfig(opts ...Int64ObservableCounterOption) Int64ObservableCounterConfig {
51 var config Int64ObservableCounterConfig
52 for _, o := range opts {
53 config = o.applyInt64ObservableCounter(config)
54 }
55 return config
56}
57
58// Description returns the configured description.
59func (c Int64ObservableCounterConfig) Description() string {
60 return c.description
61}
62
63// Unit returns the configured unit.
64func (c Int64ObservableCounterConfig) Unit() string {
65 return c.unit
66}
67
68// Callbacks returns the configured callbacks.
69func (c Int64ObservableCounterConfig) Callbacks() []Int64Callback {
70 return c.callbacks
71}
72
73// Int64ObservableCounterOption applies options to a
74// [Int64ObservableCounterConfig]. See [Int64ObservableOption] and
75// [InstrumentOption] for other options that can be used as an
76// Int64ObservableCounterOption.
77type Int64ObservableCounterOption interface {
78 applyInt64ObservableCounter(Int64ObservableCounterConfig) Int64ObservableCounterConfig
79}
80
81// Int64ObservableUpDownCounter is an instrument used to asynchronously record
82// int64 measurements once per collection cycle. Observations are only made
83// within a callback for this instrument. The value observed is assumed the to
84// be the cumulative sum of the count.
85//
86// Warning: Methods may be added to this interface in minor releases. See
87// package documentation on API implementation for information on how to set
88// default behavior for unimplemented methods.
89type Int64ObservableUpDownCounter interface {
90 // Users of the interface can ignore this. This embedded type is only used
91 // by implementations of this interface. See the "API Implementations"
92 // section of the package documentation for more information.
93 embedded.Int64ObservableUpDownCounter
94
95 Int64Observable
96}
97
98// Int64ObservableUpDownCounterConfig contains options for asynchronous counter
99// instruments that record int64 values.
100type Int64ObservableUpDownCounterConfig struct {
101 description string
102 unit string
103 callbacks []Int64Callback
104}
105
106// NewInt64ObservableUpDownCounterConfig returns a new
107// [Int64ObservableUpDownCounterConfig] with all opts applied.
108func NewInt64ObservableUpDownCounterConfig(opts ...Int64ObservableUpDownCounterOption) Int64ObservableUpDownCounterConfig {
109 var config Int64ObservableUpDownCounterConfig
110 for _, o := range opts {
111 config = o.applyInt64ObservableUpDownCounter(config)
112 }
113 return config
114}
115
116// Description returns the configured description.
117func (c Int64ObservableUpDownCounterConfig) Description() string {
118 return c.description
119}
120
121// Unit returns the configured unit.
122func (c Int64ObservableUpDownCounterConfig) Unit() string {
123 return c.unit
124}
125
126// Callbacks returns the configured callbacks.
127func (c Int64ObservableUpDownCounterConfig) Callbacks() []Int64Callback {
128 return c.callbacks
129}
130
131// Int64ObservableUpDownCounterOption applies options to a
132// [Int64ObservableUpDownCounterConfig]. See [Int64ObservableOption] and
133// [InstrumentOption] for other options that can be used as an
134// Int64ObservableUpDownCounterOption.
135type Int64ObservableUpDownCounterOption interface {
136 applyInt64ObservableUpDownCounter(Int64ObservableUpDownCounterConfig) Int64ObservableUpDownCounterConfig
137}
138
139// Int64ObservableGauge is an instrument used to asynchronously record
140// instantaneous int64 measurements once per collection cycle. Observations are
141// only made within a callback for this instrument.
142//
143// Warning: Methods may be added to this interface in minor releases. See
144// package documentation on API implementation for information on how to set
145// default behavior for unimplemented methods.
146type Int64ObservableGauge interface {
147 // Users of the interface can ignore this. This embedded type is only used
148 // by implementations of this interface. See the "API Implementations"
149 // section of the package documentation for more information.
150 embedded.Int64ObservableGauge
151
152 Int64Observable
153}
154
155// Int64ObservableGaugeConfig contains options for asynchronous counter
156// instruments that record int64 values.
157type Int64ObservableGaugeConfig struct {
158 description string
159 unit string
160 callbacks []Int64Callback
161}
162
163// NewInt64ObservableGaugeConfig returns a new [Int64ObservableGaugeConfig]
164// with all opts applied.
165func NewInt64ObservableGaugeConfig(opts ...Int64ObservableGaugeOption) Int64ObservableGaugeConfig {
166 var config Int64ObservableGaugeConfig
167 for _, o := range opts {
168 config = o.applyInt64ObservableGauge(config)
169 }
170 return config
171}
172
173// Description returns the configured description.
174func (c Int64ObservableGaugeConfig) Description() string {
175 return c.description
176}
177
178// Unit returns the configured unit.
179func (c Int64ObservableGaugeConfig) Unit() string {
180 return c.unit
181}
182
183// Callbacks returns the configured callbacks.
184func (c Int64ObservableGaugeConfig) Callbacks() []Int64Callback {
185 return c.callbacks
186}
187
188// Int64ObservableGaugeOption applies options to a
189// [Int64ObservableGaugeConfig]. See [Int64ObservableOption] and
190// [InstrumentOption] for other options that can be used as an
191// Int64ObservableGaugeOption.
192type Int64ObservableGaugeOption interface {
193 applyInt64ObservableGauge(Int64ObservableGaugeConfig) Int64ObservableGaugeConfig
194}
195
196// Int64Observer is a recorder of int64 measurements.
197//
198// Warning: Methods may be added to this interface in minor releases. See
199// package documentation on API implementation for information on how to set
200// default behavior for unimplemented methods.
201type Int64Observer interface {
202 // Users of the interface can ignore this. This embedded type is only used
203 // by implementations of this interface. See the "API Implementations"
204 // section of the package documentation for more information.
205 embedded.Int64Observer
206
207 // Observe records the int64 value.
208 //
209 // Use the WithAttributeSet (or, if performance is not a concern,
210 // the WithAttributes) option to include measurement attributes.
211 Observe(value int64, options ...ObserveOption)
212}
213
214// Int64Callback is a function registered with a Meter that makes observations
215// for an Int64Observable instrument it is registered with. Calls to the
216// Int64Observer record measurement values for the Int64Observable.
217//
218// The function needs to complete in a finite amount of time and the deadline
219// of the passed context is expected to be honored.
220//
221// The function needs to make unique observations across all registered
222// Int64Callbacks. Meaning, it should not report measurements with the same
223// attributes as another Int64Callbacks also registered for the same
224// instrument.
225//
226// The function needs to be concurrent safe.
227type Int64Callback func(context.Context, Int64Observer) error
228
229// Int64ObservableOption applies options to int64 Observer instruments.
230type Int64ObservableOption interface {
231 Int64ObservableCounterOption
232 Int64ObservableUpDownCounterOption
233 Int64ObservableGaugeOption
234}
235
236type int64CallbackOpt struct {
237 cback Int64Callback
238}
239
240func (o int64CallbackOpt) applyInt64ObservableCounter(cfg Int64ObservableCounterConfig) Int64ObservableCounterConfig {
241 cfg.callbacks = append(cfg.callbacks, o.cback)
242 return cfg
243}
244
245func (o int64CallbackOpt) applyInt64ObservableUpDownCounter(cfg Int64ObservableUpDownCounterConfig) Int64ObservableUpDownCounterConfig {
246 cfg.callbacks = append(cfg.callbacks, o.cback)
247 return cfg
248}
249
250func (o int64CallbackOpt) applyInt64ObservableGauge(cfg Int64ObservableGaugeConfig) Int64ObservableGaugeConfig {
251 cfg.callbacks = append(cfg.callbacks, o.cback)
252 return cfg
253}
254
255// WithInt64Callback adds callback to be called for an instrument.
256func WithInt64Callback(callback Int64Callback) Int64ObservableOption {
257 return int64CallbackOpt{callback}
258}