| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/metric" |
| 5 |
|
| 6 |
import ( |
| 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. |
| 17 |
type 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. |
| 32 |
type 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. |
| 43 |
type Float64ObservableCounterConfig struct { |
| 44 |
description string |
| 45 |
unit string |
| 46 |
callbacks []Float64Callback |
| 47 |
} |
| 48 |
|
| 49 |
// NewFloat64ObservableCounterConfig returns a new |
| 50 |
// [Float64ObservableCounterConfig] with all opts applied. |
| 51 |
func 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. |
| 60 |
func (c Float64ObservableCounterConfig) Description() string { |
| 61 |
return c.description |
| 62 |
} |
| 63 |
|
| 64 |
// Unit returns the configured unit. |
| 65 |
func (c Float64ObservableCounterConfig) Unit() string { |
| 66 |
return c.unit |
| 67 |
} |
| 68 |
|
| 69 |
// Callbacks returns the configured callbacks. |
| 70 |
func (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. |
| 78 |
type 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. |
| 90 |
type 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. |
| 101 |
type Float64ObservableUpDownCounterConfig struct { |
| 102 |
description string |
| 103 |
unit string |
| 104 |
callbacks []Float64Callback |
| 105 |
} |
| 106 |
|
| 107 |
// NewFloat64ObservableUpDownCounterConfig returns a new |
| 108 |
// [Float64ObservableUpDownCounterConfig] with all opts applied. |
| 109 |
func NewFloat64ObservableUpDownCounterConfig( |
| 110 |
opts ...Float64ObservableUpDownCounterOption, |
| 111 |
) Float64ObservableUpDownCounterConfig { |
| 112 |
var config Float64ObservableUpDownCounterConfig |
| 113 |
for _, o := range opts { |
| 114 |
config = o.applyFloat64ObservableUpDownCounter(config) |
| 115 |
} |
| 116 |
return config |
| 117 |
} |
| 118 |
|
| 119 |
// Description returns the configured description. |
| 120 |
func (c Float64ObservableUpDownCounterConfig) Description() string { |
| 121 |
return c.description |
| 122 |
} |
| 123 |
|
| 124 |
// Unit returns the configured unit. |
| 125 |
func (c Float64ObservableUpDownCounterConfig) Unit() string { |
| 126 |
return c.unit |
| 127 |
} |
| 128 |
|
| 129 |
// Callbacks returns the configured callbacks. |
| 130 |
func (c Float64ObservableUpDownCounterConfig) Callbacks() []Float64Callback { |
| 131 |
return c.callbacks |
| 132 |
} |
| 133 |
|
| 134 |
// Float64ObservableUpDownCounterOption applies options to a |
| 135 |
// [Float64ObservableUpDownCounterConfig]. See [Float64ObservableOption] and |
| 136 |
// [InstrumentOption] for other options that can be used as a |
| 137 |
// Float64ObservableUpDownCounterOption. |
| 138 |
type Float64ObservableUpDownCounterOption interface { |
| 139 |
applyFloat64ObservableUpDownCounter(Float64ObservableUpDownCounterConfig) Float64ObservableUpDownCounterConfig |
| 140 |
} |
| 141 |
|
| 142 |
// Float64ObservableGauge is an instrument used to asynchronously record |
| 143 |
// instantaneous float64 measurements once per collection cycle. Observations |
| 144 |
// are only made within a callback for this instrument. |
| 145 |
// |
| 146 |
// Warning: Methods may be added to this interface in minor releases. See |
| 147 |
// package documentation on API implementation for information on how to set |
| 148 |
// default behavior for unimplemented methods. |
| 149 |
type Float64ObservableGauge interface { |
| 150 |
// Users of the interface can ignore this. This embedded type is only used |
| 151 |
// by implementations of this interface. See the "API Implementations" |
| 152 |
// section of the package documentation for more information. |
| 153 |
embedded.Float64ObservableGauge |
| 154 |
|
| 155 |
Float64Observable |
| 156 |
} |
| 157 |
|
| 158 |
// Float64ObservableGaugeConfig contains options for asynchronous counter |
| 159 |
// instruments that record float64 values. |
| 160 |
type Float64ObservableGaugeConfig struct { |
| 161 |
description string |
| 162 |
unit string |
| 163 |
callbacks []Float64Callback |
| 164 |
} |
| 165 |
|
| 166 |
// NewFloat64ObservableGaugeConfig returns a new [Float64ObservableGaugeConfig] |
| 167 |
// with all opts applied. |
| 168 |
func NewFloat64ObservableGaugeConfig(opts ...Float64ObservableGaugeOption) Float64ObservableGaugeConfig { |
| 169 |
var config Float64ObservableGaugeConfig |
| 170 |
for _, o := range opts { |
| 171 |
config = o.applyFloat64ObservableGauge(config) |
| 172 |
} |
| 173 |
return config |
| 174 |
} |
| 175 |
|
| 176 |
// Description returns the configured description. |
| 177 |
func (c Float64ObservableGaugeConfig) Description() string { |
| 178 |
return c.description |
| 179 |
} |
| 180 |
|
| 181 |
// Unit returns the configured unit. |
| 182 |
func (c Float64ObservableGaugeConfig) Unit() string { |
| 183 |
return c.unit |
| 184 |
} |
| 185 |
|
| 186 |
// Callbacks returns the configured callbacks. |
| 187 |
func (c Float64ObservableGaugeConfig) Callbacks() []Float64Callback { |
| 188 |
return c.callbacks |
| 189 |
} |
| 190 |
|
| 191 |
// Float64ObservableGaugeOption applies options to a |
| 192 |
// [Float64ObservableGaugeConfig]. See [Float64ObservableOption] and |
| 193 |
// [InstrumentOption] for other options that can be used as a |
| 194 |
// Float64ObservableGaugeOption. |
| 195 |
type Float64ObservableGaugeOption interface { |
| 196 |
applyFloat64ObservableGauge(Float64ObservableGaugeConfig) Float64ObservableGaugeConfig |
| 197 |
} |
| 198 |
|
| 199 |
// Float64Observer is a recorder of float64 measurements. |
| 200 |
// |
| 201 |
// Warning: Methods may be added to this interface in minor releases. See |
| 202 |
// package documentation on API implementation for information on how to set |
| 203 |
// default behavior for unimplemented methods. |
| 204 |
type Float64Observer interface { |
| 205 |
// Users of the interface can ignore this. This embedded type is only used |
| 206 |
// by implementations of this interface. See the "API Implementations" |
| 207 |
// section of the package documentation for more information. |
| 208 |
embedded.Float64Observer |
| 209 |
|
| 210 |
// Observe records the float64 value. |
| 211 |
// |
| 212 |
// Use the WithAttributeSet (or, if performance is not a concern, |
| 213 |
// the WithAttributes) option to include measurement attributes. |
| 214 |
// |
| 215 |
// Implementations of this method need to be safe for a user to call |
| 216 |
// concurrently. |
| 217 |
Observe(value float64, options ...ObserveOption) |
| 218 |
} |
| 219 |
|
| 220 |
// Float64Callback is a function registered with a Meter that makes |
| 221 |
// observations for a Float64Observable instrument it is registered with. |
| 222 |
// Calls to the Float64Observer record measurement values for the |
| 223 |
// Float64Observable. |
| 224 |
// |
| 225 |
// The function needs to complete in a finite amount of time and the deadline |
| 226 |
// of the passed context is expected to be honored. |
| 227 |
// |
| 228 |
// The function needs to make unique observations across all registered |
| 229 |
// Float64Callbacks. Meaning, it should not report measurements with the same |
| 230 |
// attributes as another Float64Callbacks also registered for the same |
| 231 |
// instrument. |
| 232 |
// |
| 233 |
// The function needs to be reentrant and concurrent safe. |
| 234 |
// |
| 235 |
// Note that Go's mutexes are not reentrant, and locking a mutex takes |
| 236 |
// an indefinite amount of time. It is therefore advised to avoid |
| 237 |
// using mutexes inside callbacks. |
| 238 |
type Float64Callback func(context.Context, Float64Observer) error |
| 239 |
|
| 240 |
// Float64ObservableOption applies options to float64 Observer instruments. |
| 241 |
type Float64ObservableOption interface { |
| 242 |
Float64ObservableCounterOption |
| 243 |
Float64ObservableUpDownCounterOption |
| 244 |
Float64ObservableGaugeOption |
| 245 |
} |
| 246 |
|
| 247 |
type float64CallbackOpt struct { |
| 248 |
cback Float64Callback |
| 249 |
} |
| 250 |
|
| 251 |
func (o float64CallbackOpt) applyFloat64ObservableCounter( |
| 252 |
cfg Float64ObservableCounterConfig, |
| 253 |
) Float64ObservableCounterConfig { |
| 254 |
cfg.callbacks = append(cfg.callbacks, o.cback) |
| 255 |
return cfg |
| 256 |
} |
| 257 |
|
| 258 |
func (o float64CallbackOpt) applyFloat64ObservableUpDownCounter( |
| 259 |
cfg Float64ObservableUpDownCounterConfig, |
| 260 |
) Float64ObservableUpDownCounterConfig { |
| 261 |
cfg.callbacks = append(cfg.callbacks, o.cback) |
| 262 |
return cfg |
| 263 |
} |
| 264 |
|
| 265 |
func (o float64CallbackOpt) applyFloat64ObservableGauge(cfg Float64ObservableGaugeConfig) Float64ObservableGaugeConfig { |
| 266 |
cfg.callbacks = append(cfg.callbacks, o.cback) |
| 267 |
return cfg |
| 268 |
} |
| 269 |
|
| 270 |
// WithFloat64Callback adds callback to be called for an instrument. |
| 271 |
func WithFloat64Callback(callback Float64Callback) Float64ObservableOption { |
| 272 |
return float64CallbackOpt{callback} |
| 273 |
} |