1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-18 03:22:12 +02:00
opentelemetry-go/api/metric/common.go
Joshua MacDonald 4047c0877a
Introduce metric constructor errors, MeterMust wrapper (#529)
* Update api for Must constructors, with SDK helpers

* Update for Must constructors, leaving TODOs about global errors

* Add tests

* Move Must methods into metric.Must

* Apply the feedback

* Remove interfaces

* Remove more interfaces

* Again...

* Remove a sentence about a dead inteface
2020-03-11 11:57:57 -07:00

91 lines
2.5 KiB
Go

// Copyright 2019, 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.
package metric
import (
"context"
"errors"
"go.opentelemetry.io/otel/api/core"
)
type commonMetric struct {
instrument InstrumentImpl
}
type commonBoundInstrument struct {
boundInstrument BoundInstrumentImpl
}
var ErrSDKReturnedNilImpl = errors.New("SDK returned a nil implementation")
func (m commonMetric) bind(labels LabelSet) commonBoundInstrument {
return newCommonBoundInstrument(m.instrument.Bind(labels))
}
func (m commonMetric) float64Measurement(value float64) Measurement {
return newMeasurement(m.instrument, core.NewFloat64Number(value))
}
func (m commonMetric) int64Measurement(value int64) Measurement {
return newMeasurement(m.instrument, core.NewInt64Number(value))
}
func (m commonMetric) directRecord(ctx context.Context, number core.Number, labels LabelSet) {
m.instrument.RecordOne(ctx, number, labels)
}
func (m commonMetric) Impl() InstrumentImpl {
return m.instrument
}
func (h commonBoundInstrument) directRecord(ctx context.Context, number core.Number) {
h.boundInstrument.RecordOne(ctx, number)
}
func (h commonBoundInstrument) Unbind() {
h.boundInstrument.Unbind()
}
func newCommonMetric(instrument InstrumentImpl, err error) (commonMetric, error) {
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'
instrument = noopInstrument{}
}
return commonMetric{
instrument: instrument,
}, err
}
func newCommonBoundInstrument(boundInstrument BoundInstrumentImpl) commonBoundInstrument {
return commonBoundInstrument{
boundInstrument: boundInstrument,
}
}
func newMeasurement(instrument InstrumentImpl, number core.Number) Measurement {
return Measurement{
instrument: instrument,
number: number,
}
}