| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/internal/global" |
| 12 |
"go.opentelemetry.io/otel/metric" |
| 13 |
"go.opentelemetry.io/otel/metric/embedded" |
| 14 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/internal/aggregate" |
| 16 |
) |
| 17 |
|
| 18 |
// ErrInstrumentName indicates the created instrument has an invalid name. |
| 19 |
// Valid names must consist of 255 or fewer characters including alphanumeric, _, ., -, / and start with a letter. |
| 20 |
var ErrInstrumentName = errors.New("invalid instrument name") |
| 21 |
|
| 22 |
// meter handles the creation and coordination of all metric instruments. A |
| 23 |
// meter represents a single instrumentation scope; all metric telemetry |
| 24 |
// produced by an instrumentation scope will use metric instruments from a |
| 25 |
// single meter. |
| 26 |
type meter struct { |
| 27 |
embedded.Meter |
| 28 |
|
| 29 |
scope instrumentation.Scope |
| 30 |
pipes pipelines |
| 31 |
|
| 32 |
int64Insts *cacheWithErr[instID, *int64Inst] |
| 33 |
float64Insts *cacheWithErr[instID, *float64Inst] |
| 34 |
int64ObservableInsts *cacheWithErr[instID, int64Observable] |
| 35 |
float64ObservableInsts *cacheWithErr[instID, float64Observable] |
| 36 |
|
| 37 |
int64Resolver resolver[int64] |
| 38 |
float64Resolver resolver[float64] |
| 39 |
} |
| 40 |
|
| 41 |
func newMeter(s instrumentation.Scope, p pipelines) *meter { |
| 42 |
// viewCache ensures instrument conflicts, including number conflicts, this |
| 43 |
// meter is asked to create are logged to the user. |
| 44 |
var viewCache cache[string, instID] |
| 45 |
|
| 46 |
var int64Insts cacheWithErr[instID, *int64Inst] |
| 47 |
var float64Insts cacheWithErr[instID, *float64Inst] |
| 48 |
var int64ObservableInsts cacheWithErr[instID, int64Observable] |
| 49 |
var float64ObservableInsts cacheWithErr[instID, float64Observable] |
| 50 |
|
| 51 |
return &meter{ |
| 52 |
scope: s, |
| 53 |
pipes: p, |
| 54 |
int64Insts: &int64Insts, |
| 55 |
float64Insts: &float64Insts, |
| 56 |
int64ObservableInsts: &int64ObservableInsts, |
| 57 |
float64ObservableInsts: &float64ObservableInsts, |
| 58 |
int64Resolver: newResolver[int64](p, &viewCache), |
| 59 |
float64Resolver: newResolver[float64](p, &viewCache), |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Compile-time check meter implements metric.Meter. |
| 64 |
var _ metric.Meter = (*meter)(nil) |
| 65 |
|
| 66 |
// Int64Counter returns a new instrument identified by name and configured with |
| 67 |
// options. The instrument is used to synchronously record increasing int64 |
| 68 |
// measurements during a computational operation. |
| 69 |
func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) { |
| 70 |
cfg := metric.NewInt64CounterConfig(options...) |
| 71 |
const kind = InstrumentKindCounter |
| 72 |
p := int64InstProvider{m} |
| 73 |
i, err := p.lookup(kind, name, cfg.Description(), cfg.Unit()) |
| 74 |
if err != nil { |
| 75 |
return i, err |
| 76 |
} |
| 77 |
|
| 78 |
return i, validateInstrumentName(name) |
| 79 |
} |
| 80 |
|
| 81 |
// Int64UpDownCounter returns a new instrument identified by name and |
| 82 |
// configured with options. The instrument is used to synchronously record |
| 83 |
// int64 measurements during a computational operation. |
| 84 |
func (m *meter) Int64UpDownCounter( |
| 85 |
name string, |
| 86 |
options ...metric.Int64UpDownCounterOption, |
| 87 |
) (metric.Int64UpDownCounter, error) { |
| 88 |
cfg := metric.NewInt64UpDownCounterConfig(options...) |
| 89 |
const kind = InstrumentKindUpDownCounter |
| 90 |
p := int64InstProvider{m} |
| 91 |
i, err := p.lookup(kind, name, cfg.Description(), cfg.Unit()) |
| 92 |
if err != nil { |
| 93 |
return i, err |
| 94 |
} |
| 95 |
|
| 96 |
return i, validateInstrumentName(name) |
| 97 |
} |
| 98 |
|
| 99 |
// Int64Histogram returns a new instrument identified by name and configured |
| 100 |
// with options. The instrument is used to synchronously record the |
| 101 |
// distribution of int64 measurements during a computational operation. |
| 102 |
func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) { |
| 103 |
cfg := metric.NewInt64HistogramConfig(options...) |
| 104 |
p := int64InstProvider{m} |
| 105 |
i, err := p.lookupHistogram(name, cfg) |
| 106 |
if err != nil { |
| 107 |
return i, err |
| 108 |
} |
| 109 |
|
| 110 |
return i, validateInstrumentName(name) |
| 111 |
} |
| 112 |
|
| 113 |
// Int64Gauge returns a new instrument identified by name and configured |
| 114 |
// with options. The instrument is used to synchronously record the |
| 115 |
// distribution of int64 measurements during a computational operation. |
| 116 |
func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) { |
| 117 |
cfg := metric.NewInt64GaugeConfig(options...) |
| 118 |
const kind = InstrumentKindGauge |
| 119 |
p := int64InstProvider{m} |
| 120 |
i, err := p.lookup(kind, name, cfg.Description(), cfg.Unit()) |
| 121 |
if err != nil { |
| 122 |
return i, err |
| 123 |
} |
| 124 |
|
| 125 |
return i, validateInstrumentName(name) |
| 126 |
} |
| 127 |
|
| 128 |
// int64ObservableInstrument returns a new observable identified by the Instrument. |
| 129 |
// It registers callbacks for each reader's pipeline. |
| 130 |
func (m *meter) int64ObservableInstrument(id Instrument, callbacks []metric.Int64Callback) (int64Observable, error) { |
| 131 |
key := instID{ |
| 132 |
Name: id.Name, |
| 133 |
Description: id.Description, |
| 134 |
Unit: id.Unit, |
| 135 |
Kind: id.Kind, |
| 136 |
} |
| 137 |
if m.int64ObservableInsts.HasKey(key) && len(callbacks) > 0 { |
| 138 |
warnRepeatedObservableCallbacks(id) |
| 139 |
} |
| 140 |
return m.int64ObservableInsts.Lookup(key, func() (int64Observable, error) { |
| 141 |
inst := newInt64Observable(m, id.Kind, id.Name, id.Description, id.Unit) |
| 142 |
for _, insert := range m.int64Resolver.inserters { |
| 143 |
// Connect the measure functions for instruments in this pipeline with the |
| 144 |
// callbacks for this pipeline. |
| 145 |
in, err := insert.Instrument(id, insert.readerDefaultAggregation(id.Kind)) |
| 146 |
if err != nil { |
| 147 |
return inst, err |
| 148 |
} |
| 149 |
// Drop aggregation |
| 150 |
if len(in) == 0 { |
| 151 |
inst.dropAggregation = true |
| 152 |
continue |
| 153 |
} |
| 154 |
inst.appendMeasures(in) |
| 155 |
|
| 156 |
// Add the measures to the pipeline. It is required to maintain |
| 157 |
// measures per pipeline to avoid calling the measure that |
| 158 |
// is not part of the pipeline. |
| 159 |
insert.pipeline.addInt64Measure(inst.observableID, in) |
| 160 |
for _, cback := range callbacks { |
| 161 |
inst := int64Observer{measures: in} |
| 162 |
fn := cback |
| 163 |
insert.addCallback(func(ctx context.Context) error { return fn(ctx, inst) }) |
| 164 |
} |
| 165 |
} |
| 166 |
return inst, validateInstrumentName(id.Name) |
| 167 |
}) |
| 168 |
} |
| 169 |
|
| 170 |
// Int64ObservableCounter returns a new instrument identified by name and |
| 171 |
// configured with options. The instrument is used to asynchronously record |
| 172 |
// increasing int64 measurements once per a measurement collection cycle. |
| 173 |
// Only the measurements recorded during the collection cycle are exported. |
| 174 |
// |
| 175 |
// If Int64ObservableCounter is invoked repeatedly with the same Name, |
| 176 |
// Description, and Unit, only the first set of callbacks provided are used. |
| 177 |
// Use meter.RegisterCallback and Registration.Unregister to manage callbacks |
| 178 |
// if instrumentation can be created multiple times with different callbacks. |
| 179 |
func (m *meter) Int64ObservableCounter( |
| 180 |
name string, |
| 181 |
options ...metric.Int64ObservableCounterOption, |
| 182 |
) (metric.Int64ObservableCounter, error) { |
| 183 |
cfg := metric.NewInt64ObservableCounterConfig(options...) |
| 184 |
id := Instrument{ |
| 185 |
Name: name, |
| 186 |
Description: cfg.Description(), |
| 187 |
Unit: cfg.Unit(), |
| 188 |
Kind: InstrumentKindObservableCounter, |
| 189 |
Scope: m.scope, |
| 190 |
} |
| 191 |
return m.int64ObservableInstrument(id, cfg.Callbacks()) |
| 192 |
} |
| 193 |
|
| 194 |
// Int64ObservableUpDownCounter returns a new instrument identified by name and |
| 195 |
// configured with options. The instrument is used to asynchronously record |
| 196 |
// int64 measurements once per a measurement collection cycle. Only the |
| 197 |
// measurements recorded during the collection cycle are exported. |
| 198 |
// |
| 199 |
// If Int64ObservableUpDownCounter is invoked repeatedly with the same Name, |
| 200 |
// Description, and Unit, only the first set of callbacks provided are used. |
| 201 |
// Use meter.RegisterCallback and Registration.Unregister to manage callbacks |
| 202 |
// if instrumentation can be created multiple times with different callbacks. |
| 203 |
func (m *meter) Int64ObservableUpDownCounter( |
| 204 |
name string, |
| 205 |
options ...metric.Int64ObservableUpDownCounterOption, |
| 206 |
) (metric.Int64ObservableUpDownCounter, error) { |
| 207 |
cfg := metric.NewInt64ObservableUpDownCounterConfig(options...) |
| 208 |
id := Instrument{ |
| 209 |
Name: name, |
| 210 |
Description: cfg.Description(), |
| 211 |
Unit: cfg.Unit(), |
| 212 |
Kind: InstrumentKindObservableUpDownCounter, |
| 213 |
Scope: m.scope, |
| 214 |
} |
| 215 |
return m.int64ObservableInstrument(id, cfg.Callbacks()) |
| 216 |
} |
| 217 |
|
| 218 |
// Int64ObservableGauge returns a new instrument identified by name and |
| 219 |
// configured with options. The instrument is used to asynchronously record |
| 220 |
// instantaneous int64 measurements once per a measurement collection cycle. |
| 221 |
// Only the measurements recorded during the collection cycle are exported. |
| 222 |
// |
| 223 |
// If Int64ObservableGauge is invoked repeatedly with the same Name, |
| 224 |
// Description, and Unit, only the first set of callbacks provided are used. |
| 225 |
// Use meter.RegisterCallback and Registration.Unregister to manage callbacks |
| 226 |
// if instrumentation can be created multiple times with different callbacks. |
| 227 |
func (m *meter) Int64ObservableGauge( |
| 228 |
name string, |
| 229 |
options ...metric.Int64ObservableGaugeOption, |
| 230 |
) (metric.Int64ObservableGauge, error) { |
| 231 |
cfg := metric.NewInt64ObservableGaugeConfig(options...) |
| 232 |
id := Instrument{ |
| 233 |
Name: name, |
| 234 |
Description: cfg.Description(), |
| 235 |
Unit: cfg.Unit(), |
| 236 |
Kind: InstrumentKindObservableGauge, |
| 237 |
Scope: m.scope, |
| 238 |
} |
| 239 |
return m.int64ObservableInstrument(id, cfg.Callbacks()) |
| 240 |
} |
| 241 |
|
| 242 |
// Float64Counter returns a new instrument identified by name and configured |
| 243 |
// with options. The instrument is used to synchronously record increasing |
| 244 |
// float64 measurements during a computational operation. |
| 245 |
func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) { |
| 246 |
cfg := metric.NewFloat64CounterConfig(options...) |
| 247 |
const kind = InstrumentKindCounter |
| 248 |
p := float64InstProvider{m} |
| 249 |
i, err := p.lookup(kind, name, cfg.Description(), cfg.Unit()) |
| 250 |
if err != nil { |
| 251 |
return i, err |
| 252 |
} |
| 253 |
|
| 254 |
return i, validateInstrumentName(name) |
| 255 |
} |
| 256 |
|
| 257 |
// Float64UpDownCounter returns a new instrument identified by name and |
| 258 |
// configured with options. The instrument is used to synchronously record |
| 259 |
// float64 measurements during a computational operation. |
| 260 |
func (m *meter) Float64UpDownCounter( |
| 261 |
name string, |
| 262 |
options ...metric.Float64UpDownCounterOption, |
| 263 |
) (metric.Float64UpDownCounter, error) { |
| 264 |
cfg := metric.NewFloat64UpDownCounterConfig(options...) |
| 265 |
const kind = InstrumentKindUpDownCounter |
| 266 |
p := float64InstProvider{m} |
| 267 |
i, err := p.lookup(kind, name, cfg.Description(), cfg.Unit()) |
| 268 |
if err != nil { |
| 269 |
return i, err |
| 270 |
} |
| 271 |
|
| 272 |
return i, validateInstrumentName(name) |
| 273 |
} |
| 274 |
|
| 275 |
// Float64Histogram returns a new instrument identified by name and configured |
| 276 |
// with options. The instrument is used to synchronously record the |
| 277 |
// distribution of float64 measurements during a computational operation. |
| 278 |
func (m *meter) Float64Histogram( |
| 279 |
name string, |
| 280 |
options ...metric.Float64HistogramOption, |
| 281 |
) (metric.Float64Histogram, error) { |
| 282 |
cfg := metric.NewFloat64HistogramConfig(options...) |
| 283 |
p := float64InstProvider{m} |
| 284 |
i, err := p.lookupHistogram(name, cfg) |
| 285 |
if err != nil { |
| 286 |
return i, err |
| 287 |
} |
| 288 |
|
| 289 |
return i, validateInstrumentName(name) |
| 290 |
} |
| 291 |
|
| 292 |
// Float64Gauge returns a new instrument identified by name and configured |
| 293 |
// with options. The instrument is used to synchronously record the |
| 294 |
// distribution of float64 measurements during a computational operation. |
| 295 |
func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) { |
| 296 |
cfg := metric.NewFloat64GaugeConfig(options...) |
| 297 |
const kind = InstrumentKindGauge |
| 298 |
p := float64InstProvider{m} |
| 299 |
i, err := p.lookup(kind, name, cfg.Description(), cfg.Unit()) |
| 300 |
if err != nil { |
| 301 |
return i, err |
| 302 |
} |
| 303 |
|
| 304 |
return i, validateInstrumentName(name) |
| 305 |
} |
| 306 |
|
| 307 |
// float64ObservableInstrument returns a new observable identified by the Instrument. |
| 308 |
// It registers callbacks for each reader's pipeline. |
| 309 |
func (m *meter) float64ObservableInstrument( |
| 310 |
id Instrument, |
| 311 |
callbacks []metric.Float64Callback, |
| 312 |
) (float64Observable, error) { |
| 313 |
key := instID{ |
| 314 |
Name: id.Name, |
| 315 |
Description: id.Description, |
| 316 |
Unit: id.Unit, |
| 317 |
Kind: id.Kind, |
| 318 |
} |
| 319 |
if m.int64ObservableInsts.HasKey(key) && len(callbacks) > 0 { |
| 320 |
warnRepeatedObservableCallbacks(id) |
| 321 |
} |
| 322 |
return m.float64ObservableInsts.Lookup(key, func() (float64Observable, error) { |
| 323 |
inst := newFloat64Observable(m, id.Kind, id.Name, id.Description, id.Unit) |
| 324 |
for _, insert := range m.float64Resolver.inserters { |
| 325 |
// Connect the measure functions for instruments in this pipeline with the |
| 326 |
// callbacks for this pipeline. |
| 327 |
in, err := insert.Instrument(id, insert.readerDefaultAggregation(id.Kind)) |
| 328 |
if err != nil { |
| 329 |
return inst, err |
| 330 |
} |
| 331 |
// Drop aggregation |
| 332 |
if len(in) == 0 { |
| 333 |
inst.dropAggregation = true |
| 334 |
continue |
| 335 |
} |
| 336 |
inst.appendMeasures(in) |
| 337 |
|
| 338 |
// Add the measures to the pipeline. It is required to maintain |
| 339 |
// measures per pipeline to avoid calling the measure that |
| 340 |
// is not part of the pipeline. |
| 341 |
insert.pipeline.addFloat64Measure(inst.observableID, in) |
| 342 |
for _, cback := range callbacks { |
| 343 |
inst := float64Observer{measures: in} |
| 344 |
fn := cback |
| 345 |
insert.addCallback(func(ctx context.Context) error { return fn(ctx, inst) }) |
| 346 |
} |
| 347 |
} |
| 348 |
return inst, validateInstrumentName(id.Name) |
| 349 |
}) |
| 350 |
} |
| 351 |
|
| 352 |
// Float64ObservableCounter returns a new instrument identified by name and |
| 353 |
// configured with options. The instrument is used to asynchronously record |
| 354 |
// increasing float64 measurements once per a measurement collection cycle. |
| 355 |
// Only the measurements recorded during the collection cycle are exported. |
| 356 |
// |
| 357 |
// If Float64ObservableCounter is invoked repeatedly with the same Name, |
| 358 |
// Description, and Unit, only the first set of callbacks provided are used. |
| 359 |
// Use meter.RegisterCallback and Registration.Unregister to manage callbacks |
| 360 |
// if instrumentation can be created multiple times with different callbacks. |
| 361 |
func (m *meter) Float64ObservableCounter( |
| 362 |
name string, |
| 363 |
options ...metric.Float64ObservableCounterOption, |
| 364 |
) (metric.Float64ObservableCounter, error) { |
| 365 |
cfg := metric.NewFloat64ObservableCounterConfig(options...) |
| 366 |
id := Instrument{ |
| 367 |
Name: name, |
| 368 |
Description: cfg.Description(), |
| 369 |
Unit: cfg.Unit(), |
| 370 |
Kind: InstrumentKindObservableCounter, |
| 371 |
Scope: m.scope, |
| 372 |
} |
| 373 |
return m.float64ObservableInstrument(id, cfg.Callbacks()) |
| 374 |
} |
| 375 |
|
| 376 |
// Float64ObservableUpDownCounter returns a new instrument identified by name |
| 377 |
// and configured with options. The instrument is used to asynchronously record |
| 378 |
// float64 measurements once per a measurement collection cycle. Only the |
| 379 |
// measurements recorded during the collection cycle are exported. |
| 380 |
// |
| 381 |
// If Float64ObservableUpDownCounter is invoked repeatedly with the same Name, |
| 382 |
// Description, and Unit, only the first set of callbacks provided are used. |
| 383 |
// Use meter.RegisterCallback and Registration.Unregister to manage callbacks |
| 384 |
// if instrumentation can be created multiple times with different callbacks. |
| 385 |
func (m *meter) Float64ObservableUpDownCounter( |
| 386 |
name string, |
| 387 |
options ...metric.Float64ObservableUpDownCounterOption, |
| 388 |
) (metric.Float64ObservableUpDownCounter, error) { |
| 389 |
cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...) |
| 390 |
id := Instrument{ |
| 391 |
Name: name, |
| 392 |
Description: cfg.Description(), |
| 393 |
Unit: cfg.Unit(), |
| 394 |
Kind: InstrumentKindObservableUpDownCounter, |
| 395 |
Scope: m.scope, |
| 396 |
} |
| 397 |
return m.float64ObservableInstrument(id, cfg.Callbacks()) |
| 398 |
} |
| 399 |
|
| 400 |
// Float64ObservableGauge returns a new instrument identified by name and |
| 401 |
// configured with options. The instrument is used to asynchronously record |
| 402 |
// instantaneous float64 measurements once per a measurement collection cycle. |
| 403 |
// Only the measurements recorded during the collection cycle are exported. |
| 404 |
// |
| 405 |
// If Float64ObservableGauge is invoked repeatedly with the same Name, |
| 406 |
// Description, and Unit, only the first set of callbacks provided are used. |
| 407 |
// Use meter.RegisterCallback and Registration.Unregister to manage callbacks |
| 408 |
// if instrumentation can be created multiple times with different callbacks. |
| 409 |
func (m *meter) Float64ObservableGauge( |
| 410 |
name string, |
| 411 |
options ...metric.Float64ObservableGaugeOption, |
| 412 |
) (metric.Float64ObservableGauge, error) { |
| 413 |
cfg := metric.NewFloat64ObservableGaugeConfig(options...) |
| 414 |
id := Instrument{ |
| 415 |
Name: name, |
| 416 |
Description: cfg.Description(), |
| 417 |
Unit: cfg.Unit(), |
| 418 |
Kind: InstrumentKindObservableGauge, |
| 419 |
Scope: m.scope, |
| 420 |
} |
| 421 |
return m.float64ObservableInstrument(id, cfg.Callbacks()) |
| 422 |
} |
| 423 |
|
| 424 |
func validateInstrumentName(name string) error { |
| 425 |
if name == "" { |
| 426 |
return fmt.Errorf("%w: %s: is empty", ErrInstrumentName, name) |
| 427 |
} |
| 428 |
if len(name) > 255 { |
| 429 |
return fmt.Errorf("%w: %s: longer than 255 characters", ErrInstrumentName, name) |
| 430 |
} |
| 431 |
if !isAlpha([]rune(name)[0]) { |
| 432 |
return fmt.Errorf("%w: %s: must start with a letter", ErrInstrumentName, name) |
| 433 |
} |
| 434 |
if len(name) == 1 { |
| 435 |
return nil |
| 436 |
} |
| 437 |
for _, c := range name[1:] { |
| 438 |
if !isAlphanumeric(c) && c != '_' && c != '.' && c != '-' && c != '/' { |
| 439 |
return fmt.Errorf("%w: %s: must only contain [A-Za-z0-9_.-/]", ErrInstrumentName, name) |
| 440 |
} |
| 441 |
} |
| 442 |
return nil |
| 443 |
} |
| 444 |
|
| 445 |
func isAlpha(c rune) bool { |
| 446 |
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') |
| 447 |
} |
| 448 |
|
| 449 |
func isAlphanumeric(c rune) bool { |
| 450 |
return isAlpha(c) || ('0' <= c && c <= '9') |
| 451 |
} |
| 452 |
|
| 453 |
func warnRepeatedObservableCallbacks(id Instrument) { |
| 454 |
inst := fmt.Sprintf( |
| 455 |
"Instrument{Name: %q, Description: %q, Kind: %q, Unit: %q}", |
| 456 |
id.Name, id.Description, "InstrumentKind"+id.Kind.String(), id.Unit, |
| 457 |
) |
| 458 |
global.Warn( |
| 459 |
"Repeated observable instrument creation with callbacks. Ignoring new callbacks. Use meter.RegisterCallback and Registration.Unregister to manage callbacks.", |
| 460 |
"instrument", |
| 461 |
inst, |
| 462 |
) |
| 463 |
} |
| 464 |
|
| 465 |
// RegisterCallback registers f to be called each collection cycle so it will |
| 466 |
// make observations for insts during those cycles. |
| 467 |
// |
| 468 |
// The only instruments f can make observations for are insts. All other |
| 469 |
// observations will be dropped and an error will be logged. |
| 470 |
// |
| 471 |
// Only instruments from this meter can be registered with f, an error is |
| 472 |
// returned if other instrument are provided. |
| 473 |
// |
| 474 |
// Only observations made in the callback will be exported. Unlike synchronous |
| 475 |
// instruments, asynchronous callbacks can "forget" attribute sets that are no |
| 476 |
// longer relevant by omitting the observation during the callback. |
| 477 |
// |
| 478 |
// The returned Registration can be used to unregister f. |
| 479 |
func (m *meter) RegisterCallback(f metric.Callback, insts ...metric.Observable) (metric.Registration, error) { |
| 480 |
if len(insts) == 0 { |
| 481 |
// Don't allocate a observer if not needed. |
| 482 |
return noopRegister{}, nil |
| 483 |
} |
| 484 |
|
| 485 |
var err error |
| 486 |
validInstruments := make([]metric.Observable, 0, len(insts)) |
| 487 |
for _, inst := range insts { |
| 488 |
switch o := inst.(type) { |
| 489 |
case int64Observable: |
| 490 |
if e := o.registerable(m); e != nil { |
| 491 |
if !errors.Is(e, errEmptyAgg) { |
| 492 |
err = errors.Join(err, e) |
| 493 |
} |
| 494 |
continue |
| 495 |
} |
| 496 |
|
| 497 |
validInstruments = append(validInstruments, inst) |
| 498 |
case float64Observable: |
| 499 |
if e := o.registerable(m); e != nil { |
| 500 |
if !errors.Is(e, errEmptyAgg) { |
| 501 |
err = errors.Join(err, e) |
| 502 |
} |
| 503 |
continue |
| 504 |
} |
| 505 |
|
| 506 |
validInstruments = append(validInstruments, inst) |
| 507 |
default: |
| 508 |
// Instrument external to the SDK. |
| 509 |
return nil, errors.New("invalid observable: from different implementation") |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if len(validInstruments) == 0 { |
| 514 |
// All insts use drop aggregation or are invalid. |
| 515 |
return noopRegister{}, err |
| 516 |
} |
| 517 |
|
| 518 |
unregs := make([]func(), len(m.pipes)) |
| 519 |
for ix, pipe := range m.pipes { |
| 520 |
reg := newObserver(pipe) |
| 521 |
for _, inst := range validInstruments { |
| 522 |
switch o := inst.(type) { |
| 523 |
case int64Observable: |
| 524 |
reg.registerInt64(o.observableID) |
| 525 |
case float64Observable: |
| 526 |
reg.registerFloat64(o.observableID) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// Some or all instruments were valid. |
| 531 |
cBack := func(ctx context.Context) error { return f(ctx, reg) } |
| 532 |
unregs[ix] = pipe.addMultiCallback(cBack) |
| 533 |
} |
| 534 |
|
| 535 |
return unregisterFuncs{f: unregs}, err |
| 536 |
} |
| 537 |
|
| 538 |
type observer struct { |
| 539 |
embedded.Observer |
| 540 |
|
| 541 |
pipe *pipeline |
| 542 |
float64 map[observableID[float64]]struct{} |
| 543 |
int64 map[observableID[int64]]struct{} |
| 544 |
} |
| 545 |
|
| 546 |
func newObserver(p *pipeline) observer { |
| 547 |
return observer{ |
| 548 |
pipe: p, |
| 549 |
float64: make(map[observableID[float64]]struct{}), |
| 550 |
int64: make(map[observableID[int64]]struct{}), |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
func (r observer) registerFloat64(id observableID[float64]) { |
| 555 |
r.float64[id] = struct{}{} |
| 556 |
} |
| 557 |
|
| 558 |
func (r observer) registerInt64(id observableID[int64]) { |
| 559 |
r.int64[id] = struct{}{} |
| 560 |
} |
| 561 |
|
| 562 |
var ( |
| 563 |
errUnknownObserver = errors.New("unknown observable instrument") |
| 564 |
errUnregObserver = errors.New("observable instrument not registered for callback") |
| 565 |
) |
| 566 |
|
| 567 |
func (r observer) ObserveFloat64(o metric.Float64Observable, v float64, opts ...metric.ObserveOption) { |
| 568 |
var oImpl float64Observable |
| 569 |
switch conv := o.(type) { |
| 570 |
case float64Observable: |
| 571 |
oImpl = conv |
| 572 |
default: |
| 573 |
global.Error(errUnknownObserver, "failed to record") |
| 574 |
return |
| 575 |
} |
| 576 |
|
| 577 |
if _, registered := r.float64[oImpl.observableID]; !registered { |
| 578 |
if !oImpl.dropAggregation { |
| 579 |
global.Error(errUnregObserver, "failed to record", |
| 580 |
"name", oImpl.name, |
| 581 |
"description", oImpl.description, |
| 582 |
"unit", oImpl.unit, |
| 583 |
"number", fmt.Sprintf("%T", float64(0)), |
| 584 |
) |
| 585 |
} |
| 586 |
return |
| 587 |
} |
| 588 |
c := metric.NewObserveConfig(opts) |
| 589 |
// Access to r.pipe.float64Measure is already guarded by a lock in pipeline.produce. |
| 590 |
// TODO (#5946): Refactor pipeline and observable measures. |
| 591 |
measures := r.pipe.float64Measures[oImpl.observableID] |
| 592 |
for _, m := range measures { |
| 593 |
m(context.Background(), v, c.Attributes()) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
func (r observer) ObserveInt64(o metric.Int64Observable, v int64, opts ...metric.ObserveOption) { |
| 598 |
var oImpl int64Observable |
| 599 |
switch conv := o.(type) { |
| 600 |
case int64Observable: |
| 601 |
oImpl = conv |
| 602 |
default: |
| 603 |
global.Error(errUnknownObserver, "failed to record") |
| 604 |
return |
| 605 |
} |
| 606 |
|
| 607 |
if _, registered := r.int64[oImpl.observableID]; !registered { |
| 608 |
if !oImpl.dropAggregation { |
| 609 |
global.Error(errUnregObserver, "failed to record", |
| 610 |
"name", oImpl.name, |
| 611 |
"description", oImpl.description, |
| 612 |
"unit", oImpl.unit, |
| 613 |
"number", fmt.Sprintf("%T", int64(0)), |
| 614 |
) |
| 615 |
} |
| 616 |
return |
| 617 |
} |
| 618 |
c := metric.NewObserveConfig(opts) |
| 619 |
// Access to r.pipe.int64Measures is already guarded b a lock in pipeline.produce. |
| 620 |
// TODO (#5946): Refactor pipeline and observable measures. |
| 621 |
measures := r.pipe.int64Measures[oImpl.observableID] |
| 622 |
for _, m := range measures { |
| 623 |
m(context.Background(), v, c.Attributes()) |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
type noopRegister struct{ embedded.Registration } |
| 628 |
|
| 629 |
func (noopRegister) Unregister() error { |
| 630 |
return nil |
| 631 |
} |
| 632 |
|
| 633 |
// int64InstProvider provides int64 OpenTelemetry instruments. |
| 634 |
type int64InstProvider struct{ *meter } |
| 635 |
|
| 636 |
func (p int64InstProvider) aggs(kind InstrumentKind, name, desc, u string) ([]aggregate.Measure[int64], error) { |
| 637 |
inst := Instrument{ |
| 638 |
Name: name, |
| 639 |
Description: desc, |
| 640 |
Unit: u, |
| 641 |
Kind: kind, |
| 642 |
Scope: p.scope, |
| 643 |
} |
| 644 |
return p.int64Resolver.Aggregators(inst) |
| 645 |
} |
| 646 |
|
| 647 |
func (p int64InstProvider) histogramAggs( |
| 648 |
name string, |
| 649 |
cfg metric.Int64HistogramConfig, |
| 650 |
) ([]aggregate.Measure[int64], error) { |
| 651 |
boundaries := cfg.ExplicitBucketBoundaries() |
| 652 |
aggError := AggregationExplicitBucketHistogram{Boundaries: boundaries}.err() |
| 653 |
if aggError != nil { |
| 654 |
// If boundaries are invalid, ignore them. |
| 655 |
boundaries = nil |
| 656 |
} |
| 657 |
inst := Instrument{ |
| 658 |
Name: name, |
| 659 |
Description: cfg.Description(), |
| 660 |
Unit: cfg.Unit(), |
| 661 |
Kind: InstrumentKindHistogram, |
| 662 |
Scope: p.scope, |
| 663 |
} |
| 664 |
measures, err := p.int64Resolver.HistogramAggregators(inst, boundaries) |
| 665 |
return measures, errors.Join(aggError, err) |
| 666 |
} |
| 667 |
|
| 668 |
// lookup returns the resolved instrumentImpl. |
| 669 |
func (p int64InstProvider) lookup(kind InstrumentKind, name, desc, u string) (*int64Inst, error) { |
| 670 |
return p.int64Insts.Lookup(instID{ |
| 671 |
Name: name, |
| 672 |
Description: desc, |
| 673 |
Unit: u, |
| 674 |
Kind: kind, |
| 675 |
}, func() (*int64Inst, error) { |
| 676 |
aggs, err := p.aggs(kind, name, desc, u) |
| 677 |
return &int64Inst{measures: aggs}, err |
| 678 |
}) |
| 679 |
} |
| 680 |
|
| 681 |
// lookupHistogram returns the resolved instrumentImpl. |
| 682 |
func (p int64InstProvider) lookupHistogram(name string, cfg metric.Int64HistogramConfig) (*int64Inst, error) { |
| 683 |
return p.int64Insts.Lookup(instID{ |
| 684 |
Name: name, |
| 685 |
Description: cfg.Description(), |
| 686 |
Unit: cfg.Unit(), |
| 687 |
Kind: InstrumentKindHistogram, |
| 688 |
}, func() (*int64Inst, error) { |
| 689 |
aggs, err := p.histogramAggs(name, cfg) |
| 690 |
return &int64Inst{measures: aggs}, err |
| 691 |
}) |
| 692 |
} |
| 693 |
|
| 694 |
// float64InstProvider provides float64 OpenTelemetry instruments. |
| 695 |
type float64InstProvider struct{ *meter } |
| 696 |
|
| 697 |
func (p float64InstProvider) aggs(kind InstrumentKind, name, desc, u string) ([]aggregate.Measure[float64], error) { |
| 698 |
inst := Instrument{ |
| 699 |
Name: name, |
| 700 |
Description: desc, |
| 701 |
Unit: u, |
| 702 |
Kind: kind, |
| 703 |
Scope: p.scope, |
| 704 |
} |
| 705 |
return p.float64Resolver.Aggregators(inst) |
| 706 |
} |
| 707 |
|
| 708 |
func (p float64InstProvider) histogramAggs( |
| 709 |
name string, |
| 710 |
cfg metric.Float64HistogramConfig, |
| 711 |
) ([]aggregate.Measure[float64], error) { |
| 712 |
boundaries := cfg.ExplicitBucketBoundaries() |
| 713 |
aggError := AggregationExplicitBucketHistogram{Boundaries: boundaries}.err() |
| 714 |
if aggError != nil { |
| 715 |
// If boundaries are invalid, ignore them. |
| 716 |
boundaries = nil |
| 717 |
} |
| 718 |
inst := Instrument{ |
| 719 |
Name: name, |
| 720 |
Description: cfg.Description(), |
| 721 |
Unit: cfg.Unit(), |
| 722 |
Kind: InstrumentKindHistogram, |
| 723 |
Scope: p.scope, |
| 724 |
} |
| 725 |
measures, err := p.float64Resolver.HistogramAggregators(inst, boundaries) |
| 726 |
return measures, errors.Join(aggError, err) |
| 727 |
} |
| 728 |
|
| 729 |
// lookup returns the resolved instrumentImpl. |
| 730 |
func (p float64InstProvider) lookup(kind InstrumentKind, name, desc, u string) (*float64Inst, error) { |
| 731 |
return p.float64Insts.Lookup(instID{ |
| 732 |
Name: name, |
| 733 |
Description: desc, |
| 734 |
Unit: u, |
| 735 |
Kind: kind, |
| 736 |
}, func() (*float64Inst, error) { |
| 737 |
aggs, err := p.aggs(kind, name, desc, u) |
| 738 |
return &float64Inst{measures: aggs}, err |
| 739 |
}) |
| 740 |
} |
| 741 |
|
| 742 |
// lookupHistogram returns the resolved instrumentImpl. |
| 743 |
func (p float64InstProvider) lookupHistogram(name string, cfg metric.Float64HistogramConfig) (*float64Inst, error) { |
| 744 |
return p.float64Insts.Lookup(instID{ |
| 745 |
Name: name, |
| 746 |
Description: cfg.Description(), |
| 747 |
Unit: cfg.Unit(), |
| 748 |
Kind: InstrumentKindHistogram, |
| 749 |
}, func() (*float64Inst, error) { |
| 750 |
aggs, err := p.histogramAggs(name, cfg) |
| 751 |
return &float64Inst{measures: aggs}, err |
| 752 |
}) |
| 753 |
} |
| 754 |
|
| 755 |
type int64Observer struct { |
| 756 |
embedded.Int64Observer |
| 757 |
measures[int64] |
| 758 |
} |
| 759 |
|
| 760 |
func (o int64Observer) Observe(val int64, opts ...metric.ObserveOption) { |
| 761 |
c := metric.NewObserveConfig(opts) |
| 762 |
o.observe(val, c.Attributes()) |
| 763 |
} |
| 764 |
|
| 765 |
type float64Observer struct { |
| 766 |
embedded.Float64Observer |
| 767 |
measures[float64] |
| 768 |
} |
| 769 |
|
| 770 |
func (o float64Observer) Observe(val float64, opts ...metric.ObserveOption) { |
| 771 |
c := metric.NewObserveConfig(opts) |
| 772 |
o.observe(val, c.Attributes()) |
| 773 |
} |