diff options
Diffstat (limited to 'vendor/go.opentelemetry.io/otel/internal/global/meter.go')
-rw-r--r-- | vendor/go.opentelemetry.io/otel/internal/global/meter.go | 274 |
1 files changed, 68 insertions, 206 deletions
diff --git a/vendor/go.opentelemetry.io/otel/internal/global/meter.go b/vendor/go.opentelemetry.io/otel/internal/global/meter.go index f2fc3929b..cfd1df9bf 100644 --- a/vendor/go.opentelemetry.io/otel/internal/global/meter.go +++ b/vendor/go.opentelemetry.io/otel/internal/global/meter.go @@ -5,8 +5,8 @@ package global // import "go.opentelemetry.io/otel/internal/global" import ( "container/list" - "reflect" "sync" + "sync/atomic" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/embedded" @@ -76,7 +76,7 @@ func (p *meterProvider) Meter(name string, opts ...metric.MeterOption) metric.Me return val } - t := &meter{name: name, opts: opts, instruments: make(map[instID]delegatedInstrument)} + t := &meter{name: name, opts: opts} p.meters[key] = t return t } @@ -92,29 +92,17 @@ type meter struct { opts []metric.MeterOption mtx sync.Mutex - instruments map[instID]delegatedInstrument + instruments []delegatedInstrument registry list.List - delegate metric.Meter + delegate atomic.Value // metric.Meter } type delegatedInstrument interface { setDelegate(metric.Meter) } -// instID are the identifying properties of a instrument. -type instID struct { - // name is the name of the stream. - name string - // description is the description of the stream. - description string - // kind defines the functional group of the instrument. - kind reflect.Type - // unit is the unit of the stream. - unit string -} - // setDelegate configures m to delegate all Meter functionality to Meters // created by provider. // @@ -122,12 +110,12 @@ type instID struct { // // It is guaranteed by the caller that this happens only once. func (m *meter) setDelegate(provider metric.MeterProvider) { + meter := provider.Meter(m.name, m.opts...) + m.delegate.Store(meter) + m.mtx.Lock() defer m.mtx.Unlock() - meter := provider.Meter(m.name, m.opts...) - m.delegate = meter - for _, inst := range m.instruments { inst.setDelegate(meter) } @@ -145,295 +133,169 @@ func (m *meter) setDelegate(provider metric.MeterProvider) { } func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64Counter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64Counter(name, options...) - } - i := &siCounter{name: name, opts: options} - cfg := metric.NewInt64CounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64UpDownCounter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64UpDownCounter(name, options...) - } - i := &siUpDownCounter{name: name, opts: options} - cfg := metric.NewInt64UpDownCounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64Histogram(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64Histogram(name, options...) - } - i := &siHistogram{name: name, opts: options} - cfg := metric.NewInt64HistogramConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64Gauge(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64Gauge(name, options...) - } - i := &siGauge{name: name, opts: options} - cfg := metric.NewInt64GaugeConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64ObservableCounter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64ObservableCounter(name, options...) - } - i := &aiCounter{name: name, opts: options} - cfg := metric.NewInt64ObservableCounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64ObservableUpDownCounter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64ObservableUpDownCounter(name, options...) - } - i := &aiUpDownCounter{name: name, opts: options} - cfg := metric.NewInt64ObservableUpDownCounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Int64ObservableGauge(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Int64ObservableGauge(name, options...) - } - i := &aiGauge{name: name, opts: options} - cfg := metric.NewInt64ObservableGaugeConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64Counter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64Counter(name, options...) - } - i := &sfCounter{name: name, opts: options} - cfg := metric.NewFloat64CounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64UpDownCounter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64UpDownCounter(name, options...) - } - i := &sfUpDownCounter{name: name, opts: options} - cfg := metric.NewFloat64UpDownCounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64Histogram(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64Histogram(name, options...) - } - i := &sfHistogram{name: name, opts: options} - cfg := metric.NewFloat64HistogramConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64Gauge(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64Gauge(name, options...) - } - i := &sfGauge{name: name, opts: options} - cfg := metric.NewFloat64GaugeConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64ObservableCounter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64ObservableCounter(name, options...) - } - i := &afCounter{name: name, opts: options} - cfg := metric.NewFloat64ObservableCounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64ObservableUpDownCounter(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64ObservableUpDownCounter(name, options...) - } - i := &afUpDownCounter{name: name, opts: options} - cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } func (m *meter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) { + if del, ok := m.delegate.Load().(metric.Meter); ok { + return del.Float64ObservableGauge(name, options...) + } m.mtx.Lock() defer m.mtx.Unlock() - - if m.delegate != nil { - return m.delegate.Float64ObservableGauge(name, options...) - } - i := &afGauge{name: name, opts: options} - cfg := metric.NewFloat64ObservableGaugeConfig(options...) - id := instID{ - name: name, - kind: reflect.TypeOf(i), - description: cfg.Description(), - unit: cfg.Unit(), - } - m.instruments[id] = i + m.instruments = append(m.instruments, i) return i, nil } // RegisterCallback captures the function that will be called during Collect. func (m *meter) RegisterCallback(f metric.Callback, insts ...metric.Observable) (metric.Registration, error) { - m.mtx.Lock() - defer m.mtx.Unlock() - - if m.delegate != nil { + if del, ok := m.delegate.Load().(metric.Meter); ok { insts = unwrapInstruments(insts) - return m.delegate.RegisterCallback(f, insts...) + return del.RegisterCallback(f, insts...) } + m.mtx.Lock() + defer m.mtx.Unlock() + reg := ®istration{instruments: insts, function: f} e := m.registry.PushBack(reg) reg.unreg = func() error { |