2020-03-24 19:54:08 +02:00
|
|
|
// 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.
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
package registry // import "go.opentelemetry.io/otel/sdk/metric/registry"
|
2020-03-24 19:54:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/instrument"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
|
2020-03-24 19:54:08 +02:00
|
|
|
)
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// UniqueInstrumentMeterImpl implements the metric.MeterImpl interface, adding
|
|
|
|
// uniqueness checking for instrument descriptors.
|
|
|
|
type UniqueInstrumentMeterImpl struct {
|
2020-03-24 19:54:08 +02:00
|
|
|
lock sync.Mutex
|
2021-10-14 18:06:22 +02:00
|
|
|
impl sdkapi.MeterImpl
|
|
|
|
state map[string]sdkapi.InstrumentImpl
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
var _ sdkapi.MeterImpl = (*UniqueInstrumentMeterImpl)(nil)
|
2020-05-18 19:48:58 +02:00
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
// ErrMetricKindMismatch is the standard error for mismatched metric
|
|
|
|
// instrument definitions.
|
|
|
|
var ErrMetricKindMismatch = fmt.Errorf(
|
2021-02-04 21:32:43 +02:00
|
|
|
"a metric was already registered by this name with another kind or number type")
|
2020-03-24 19:54:08 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// NewUniqueInstrumentMeterImpl returns a wrapped metric.MeterImpl
|
|
|
|
// with the addition of instrument name uniqueness checking.
|
2021-10-14 18:06:22 +02:00
|
|
|
func NewUniqueInstrumentMeterImpl(impl sdkapi.MeterImpl) *UniqueInstrumentMeterImpl {
|
2021-09-27 17:51:47 +02:00
|
|
|
return &UniqueInstrumentMeterImpl{
|
2020-03-24 19:54:08 +02:00
|
|
|
impl: impl,
|
2021-10-14 18:06:22 +02:00
|
|
|
state: map[string]sdkapi.InstrumentImpl{},
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// MeterImpl gives the caller access to the underlying MeterImpl
|
|
|
|
// used by this UniqueInstrumentMeterImpl.
|
2021-10-14 18:06:22 +02:00
|
|
|
func (u *UniqueInstrumentMeterImpl) MeterImpl() sdkapi.MeterImpl {
|
2021-09-27 17:51:47 +02:00
|
|
|
return u.impl
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetricKindMismatchError formats an error that describes a
|
|
|
|
// mismatched metric instrument definition.
|
2021-10-14 18:06:22 +02:00
|
|
|
func NewMetricKindMismatchError(desc sdkapi.Descriptor) error {
|
2021-09-27 17:51:47 +02:00
|
|
|
return fmt.Errorf("metric %s registered as %s %s: %w",
|
2020-03-24 19:54:08 +02:00
|
|
|
desc.Name(),
|
|
|
|
desc.NumberKind(),
|
2020-10-13 16:55:31 +02:00
|
|
|
desc.InstrumentKind(),
|
2020-03-24 19:54:08 +02:00
|
|
|
ErrMetricKindMismatch)
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// Compatible determines whether two sdkapi.Descriptors are considered
|
2020-03-24 19:54:08 +02:00
|
|
|
// the same for the purpose of uniqueness checking.
|
2021-10-14 18:06:22 +02:00
|
|
|
func Compatible(candidate, existing sdkapi.Descriptor) bool {
|
2020-10-13 16:55:31 +02:00
|
|
|
return candidate.InstrumentKind() == existing.InstrumentKind() &&
|
2020-03-24 19:54:08 +02:00
|
|
|
candidate.NumberKind() == existing.NumberKind()
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkUniqueness returns an ErrMetricKindMismatch error if there is
|
|
|
|
// a conflict between a descriptor that was already registered and the
|
|
|
|
// `descriptor` argument. If there is an existing compatible
|
|
|
|
// registration, this returns the already-registered instrument. If
|
|
|
|
// there is no conflict and no prior registration, returns (nil, nil).
|
2021-10-14 18:06:22 +02:00
|
|
|
func (u *UniqueInstrumentMeterImpl) checkUniqueness(descriptor sdkapi.Descriptor) (sdkapi.InstrumentImpl, error) {
|
2021-09-27 17:51:47 +02:00
|
|
|
impl, ok := u.state[descriptor.Name()]
|
2020-03-24 19:54:08 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !Compatible(descriptor, impl.Descriptor()) {
|
|
|
|
return nil, NewMetricKindMismatchError(impl.Descriptor())
|
|
|
|
}
|
|
|
|
|
|
|
|
return impl, nil
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// NewSyncInstrument implements sdkapi.MeterImpl.
|
|
|
|
func (u *UniqueInstrumentMeterImpl) NewSyncInstrument(descriptor sdkapi.Descriptor) (sdkapi.SyncImpl, error) {
|
2020-03-24 19:54:08 +02:00
|
|
|
u.lock.Lock()
|
|
|
|
defer u.lock.Unlock()
|
|
|
|
|
|
|
|
impl, err := u.checkUniqueness(descriptor)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if impl != nil {
|
2021-10-14 18:06:22 +02:00
|
|
|
return impl.(sdkapi.SyncImpl), nil
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
syncInst, err := u.impl.NewSyncInstrument(descriptor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
u.state[descriptor.Name()] = syncInst
|
2020-03-24 19:54:08 +02:00
|
|
|
return syncInst, nil
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// NewAsyncInstrument implements sdkapi.MeterImpl.
|
2022-03-02 17:50:29 +02:00
|
|
|
func (u *UniqueInstrumentMeterImpl) NewAsyncInstrument(descriptor sdkapi.Descriptor) (sdkapi.AsyncImpl, error) {
|
2020-03-24 19:54:08 +02:00
|
|
|
u.lock.Lock()
|
|
|
|
defer u.lock.Unlock()
|
|
|
|
|
|
|
|
impl, err := u.checkUniqueness(descriptor)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if impl != nil {
|
2021-10-14 18:06:22 +02:00
|
|
|
return impl.(sdkapi.AsyncImpl), nil
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
asyncInst, err := u.impl.NewAsyncInstrument(descriptor)
|
2020-03-24 19:54:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
u.state[descriptor.Name()] = asyncInst
|
2020-03-24 19:54:08 +02:00
|
|
|
return asyncInst, nil
|
|
|
|
}
|
2022-03-02 17:50:29 +02:00
|
|
|
|
2022-05-19 22:15:07 +02:00
|
|
|
// RegisterCallback registers callback with insts.
|
2022-03-02 17:50:29 +02:00
|
|
|
func (u *UniqueInstrumentMeterImpl) RegisterCallback(insts []instrument.Asynchronous, callback func(context.Context)) error {
|
|
|
|
u.lock.Lock()
|
|
|
|
defer u.lock.Unlock()
|
|
|
|
|
|
|
|
return u.impl.RegisterCallback(insts, callback)
|
|
|
|
}
|