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