VictoriaMetrics/vendor/go.opentelemetry.io/otel/internal/global/instruments.go

372 lines
8.4 KiB
Go
Raw Normal View History

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-07-07 09:04:32 +02:00
package global // import "go.opentelemetry.io/otel/internal/global"
import (
"context"
"sync/atomic"
"go.opentelemetry.io/otel/metric"
2023-05-10 08:13:50 +02:00
"go.opentelemetry.io/otel/metric/embedded"
)
2023-01-31 20:03:20 +01:00
// unwrapper unwraps to return the underlying instrument implementation.
type unwrapper interface {
2023-05-10 08:13:50 +02:00
Unwrap() metric.Observable
2023-01-31 20:03:20 +01:00
}
type afCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Float64ObservableCounter
metric.Float64Observable
2023-01-31 20:03:20 +01:00
name string
2023-05-10 08:13:50 +02:00
opts []metric.Float64ObservableCounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Float64ObservableCounter
}
2023-11-13 18:50:16 +01:00
var (
_ unwrapper = (*afCounter)(nil)
_ metric.Float64ObservableCounter = (*afCounter)(nil)
)
2023-01-31 20:03:20 +01:00
func (i *afCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Float64ObservableCounter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *afCounter) Unwrap() metric.Observable {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
return ctr.(metric.Float64ObservableCounter)
}
return nil
}
type afUpDownCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Float64ObservableUpDownCounter
metric.Float64Observable
2023-01-31 20:03:20 +01:00
name string
2023-05-10 08:13:50 +02:00
opts []metric.Float64ObservableUpDownCounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Float64ObservableUpDownCounter
}
2023-11-13 18:50:16 +01:00
var (
_ unwrapper = (*afUpDownCounter)(nil)
_ metric.Float64ObservableUpDownCounter = (*afUpDownCounter)(nil)
)
2023-01-31 20:03:20 +01:00
func (i *afUpDownCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Float64ObservableUpDownCounter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *afUpDownCounter) Unwrap() metric.Observable {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
return ctr.(metric.Float64ObservableUpDownCounter)
}
return nil
}
type afGauge struct {
2023-05-10 08:13:50 +02:00
embedded.Float64ObservableGauge
metric.Float64Observable
2023-01-31 20:03:20 +01:00
name string
2023-05-10 08:13:50 +02:00
opts []metric.Float64ObservableGaugeOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Float64ObservableGauge
}
2023-11-13 18:50:16 +01:00
var (
_ unwrapper = (*afGauge)(nil)
_ metric.Float64ObservableGauge = (*afGauge)(nil)
)
2023-01-31 20:03:20 +01:00
func (i *afGauge) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Float64ObservableGauge(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *afGauge) Unwrap() metric.Observable {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
return ctr.(metric.Float64ObservableGauge)
}
return nil
}
type aiCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Int64ObservableCounter
metric.Int64Observable
2023-01-31 20:03:20 +01:00
name string
2023-05-10 08:13:50 +02:00
opts []metric.Int64ObservableCounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Int64ObservableCounter
}
2023-11-13 18:50:16 +01:00
var (
_ unwrapper = (*aiCounter)(nil)
_ metric.Int64ObservableCounter = (*aiCounter)(nil)
)
2023-01-31 20:03:20 +01:00
func (i *aiCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Int64ObservableCounter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *aiCounter) Unwrap() metric.Observable {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
return ctr.(metric.Int64ObservableCounter)
}
return nil
}
type aiUpDownCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Int64ObservableUpDownCounter
metric.Int64Observable
2023-01-31 20:03:20 +01:00
name string
2023-05-10 08:13:50 +02:00
opts []metric.Int64ObservableUpDownCounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Int64ObservableUpDownCounter
}
2023-11-13 18:50:16 +01:00
var (
_ unwrapper = (*aiUpDownCounter)(nil)
_ metric.Int64ObservableUpDownCounter = (*aiUpDownCounter)(nil)
)
2023-01-31 20:03:20 +01:00
func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Int64ObservableUpDownCounter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *aiUpDownCounter) Unwrap() metric.Observable {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
return ctr.(metric.Int64ObservableUpDownCounter)
}
return nil
}
type aiGauge struct {
2023-05-10 08:13:50 +02:00
embedded.Int64ObservableGauge
metric.Int64Observable
2023-01-31 20:03:20 +01:00
name string
2023-05-10 08:13:50 +02:00
opts []metric.Int64ObservableGaugeOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Int64ObservableGauge
}
2023-11-13 18:50:16 +01:00
var (
_ unwrapper = (*aiGauge)(nil)
_ metric.Int64ObservableGauge = (*aiGauge)(nil)
)
2023-01-31 20:03:20 +01:00
func (i *aiGauge) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Int64ObservableGauge(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *aiGauge) Unwrap() metric.Observable {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
return ctr.(metric.Int64ObservableGauge)
}
return nil
}
// Sync Instruments.
type sfCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Float64Counter
2023-05-10 08:13:50 +02:00
name string
opts []metric.Float64CounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Float64Counter
}
2023-05-10 08:13:50 +02:00
var _ metric.Float64Counter = (*sfCounter)(nil)
2023-01-31 20:03:20 +01:00
func (i *sfCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Float64Counter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *sfCounter) Add(ctx context.Context, incr float64, opts ...metric.AddOption) {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
ctr.(metric.Float64Counter).Add(ctx, incr, opts...)
}
}
type sfUpDownCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Float64UpDownCounter
2023-05-10 08:13:50 +02:00
name string
opts []metric.Float64UpDownCounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Float64UpDownCounter
}
2023-05-10 08:13:50 +02:00
var _ metric.Float64UpDownCounter = (*sfUpDownCounter)(nil)
2023-01-31 20:03:20 +01:00
func (i *sfUpDownCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Float64UpDownCounter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *sfUpDownCounter) Add(ctx context.Context, incr float64, opts ...metric.AddOption) {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
ctr.(metric.Float64UpDownCounter).Add(ctx, incr, opts...)
}
}
type sfHistogram struct {
2023-05-10 08:13:50 +02:00
embedded.Float64Histogram
2023-05-10 08:13:50 +02:00
name string
opts []metric.Float64HistogramOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Float64Histogram
}
2023-05-10 08:13:50 +02:00
var _ metric.Float64Histogram = (*sfHistogram)(nil)
2023-01-31 20:03:20 +01:00
func (i *sfHistogram) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Float64Histogram(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *sfHistogram) Record(ctx context.Context, x float64, opts ...metric.RecordOption) {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
ctr.(metric.Float64Histogram).Record(ctx, x, opts...)
}
}
type siCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Int64Counter
2023-05-10 08:13:50 +02:00
name string
opts []metric.Int64CounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Int64Counter
}
2023-05-10 08:13:50 +02:00
var _ metric.Int64Counter = (*siCounter)(nil)
2023-01-31 20:03:20 +01:00
func (i *siCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Int64Counter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *siCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption) {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
ctr.(metric.Int64Counter).Add(ctx, x, opts...)
}
}
type siUpDownCounter struct {
2023-05-10 08:13:50 +02:00
embedded.Int64UpDownCounter
2023-05-10 08:13:50 +02:00
name string
opts []metric.Int64UpDownCounterOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Int64UpDownCounter
}
2023-05-10 08:13:50 +02:00
var _ metric.Int64UpDownCounter = (*siUpDownCounter)(nil)
2023-01-31 20:03:20 +01:00
func (i *siUpDownCounter) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Int64UpDownCounter(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *siUpDownCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption) {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
ctr.(metric.Int64UpDownCounter).Add(ctx, x, opts...)
}
}
type siHistogram struct {
2023-05-10 08:13:50 +02:00
embedded.Int64Histogram
2023-05-10 08:13:50 +02:00
name string
opts []metric.Int64HistogramOption
2023-11-13 18:50:16 +01:00
delegate atomic.Value // metric.Int64Histogram
}
2023-05-10 08:13:50 +02:00
var _ metric.Int64Histogram = (*siHistogram)(nil)
2023-01-31 20:03:20 +01:00
func (i *siHistogram) setDelegate(m metric.Meter) {
2023-01-31 20:03:20 +01:00
ctr, err := m.Int64Histogram(i.name, i.opts...)
if err != nil {
2023-07-07 09:04:32 +02:00
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}
2023-05-10 08:13:50 +02:00
func (i *siHistogram) Record(ctx context.Context, x int64, opts ...metric.RecordOption) {
if ctr := i.delegate.Load(); ctr != nil {
2023-05-10 08:13:50 +02:00
ctr.(metric.Int64Histogram).Record(ctx, x, opts...)
}
}