2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-06-14 22:09:41 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
package metric
|
|
|
|
|
2019-10-09 00:45:49 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2020-03-11 20:57:57 +02:00
|
|
|
"errors"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2019-10-09 00:45:49 +02:00
|
|
|
)
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
type syncInstrument struct {
|
|
|
|
instrument SyncImpl
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
type syncBoundInstrument struct {
|
|
|
|
boundInstrument BoundSyncImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
type asyncInstrument struct {
|
|
|
|
instrument AsyncImpl
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
var ErrSDKReturnedNilImpl = errors.New("SDK returned a nil implementation")
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
func (s syncInstrument) bind(labels []core.KeyValue) syncBoundInstrument {
|
2020-03-19 21:02:46 +02:00
|
|
|
return newSyncBoundInstrument(s.instrument.Bind(labels))
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (s syncInstrument) float64Measurement(value float64) Measurement {
|
|
|
|
return newMeasurement(s.instrument, core.NewFloat64Number(value))
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (s syncInstrument) int64Measurement(value int64) Measurement {
|
|
|
|
return newMeasurement(s.instrument, core.NewInt64Number(value))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
func (s syncInstrument) directRecord(ctx context.Context, number core.Number, labels []core.KeyValue) {
|
2020-03-19 21:02:46 +02:00
|
|
|
s.instrument.RecordOne(ctx, number, labels)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (s syncInstrument) SyncImpl() SyncImpl {
|
|
|
|
return s.instrument
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (h syncBoundInstrument) directRecord(ctx context.Context, number core.Number) {
|
2019-12-28 02:30:19 +02:00
|
|
|
h.boundInstrument.RecordOne(ctx, number)
|
2019-10-23 08:29:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (h syncBoundInstrument) Unbind() {
|
2019-12-28 02:30:19 +02:00
|
|
|
h.boundInstrument.Unbind()
|
2019-10-23 08:29:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (a asyncInstrument) AsyncImpl() AsyncImpl {
|
|
|
|
return a.instrument
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkNewSync receives an SyncImpl and potential
|
|
|
|
// error, and returns the same types, checking for and ensuring that
|
|
|
|
// the returned interface is not nil.
|
|
|
|
func checkNewSync(instrument SyncImpl, err error) (syncInstrument, error) {
|
2020-03-11 20:57:57 +02:00
|
|
|
if instrument == nil {
|
|
|
|
if err == nil {
|
|
|
|
err = ErrSDKReturnedNilImpl
|
|
|
|
}
|
|
|
|
// Note: an alternate behavior would be to synthesize a new name
|
|
|
|
// or group all duplicately-named instruments of a certain type
|
|
|
|
// together and use a tag for the original name, e.g.,
|
|
|
|
// name = 'invalid.counter.int64'
|
|
|
|
// label = 'original-name=duplicate-counter-name'
|
2020-03-19 21:02:46 +02:00
|
|
|
instrument = NoopSync{}
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
return syncInstrument{
|
2019-10-23 08:29:24 +02:00
|
|
|
instrument: instrument,
|
2020-03-11 20:57:57 +02:00
|
|
|
}, err
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func newSyncBoundInstrument(boundInstrument BoundSyncImpl) syncBoundInstrument {
|
|
|
|
return syncBoundInstrument{
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument: boundInstrument,
|
2019-10-23 08:29:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func newMeasurement(instrument SyncImpl, number core.Number) Measurement {
|
2019-10-23 08:29:24 +02:00
|
|
|
return Measurement{
|
|
|
|
instrument: instrument,
|
2019-10-29 22:27:22 +02:00
|
|
|
number: number,
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
// checkNewAsync receives an AsyncImpl and potential
|
|
|
|
// error, and returns the same types, checking for and ensuring that
|
|
|
|
// the returned interface is not nil.
|
|
|
|
func checkNewAsync(instrument AsyncImpl, err error) (asyncInstrument, error) {
|
|
|
|
if instrument == nil {
|
|
|
|
if err == nil {
|
|
|
|
err = ErrSDKReturnedNilImpl
|
|
|
|
}
|
|
|
|
instrument = NoopAsync{}
|
|
|
|
}
|
|
|
|
return asyncInstrument{
|
|
|
|
instrument: instrument,
|
|
|
|
}, err
|
|
|
|
}
|