2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-30 21:59:34 +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.
|
|
|
|
|
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-24 09:03:04 +02:00
|
|
|
"sync"
|
2019-10-30 21:59:34 +02:00
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-11-01 20:40:29 +02:00
|
|
|
apimetric "go.opentelemetry.io/otel/api/metric"
|
2020-03-24 19:54:08 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric/registry"
|
2019-10-30 21:59:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Handle struct {
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument *Sync
|
2020-05-14 01:06:03 +02:00
|
|
|
Labels []kv.KeyValue
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Batch struct {
|
2020-01-06 20:08:40 +02:00
|
|
|
// Measurement needs to be aligned for 64-bit atomic operations.
|
|
|
|
Measurements []Measurement
|
2019-10-30 21:59:34 +02:00
|
|
|
Ctx context.Context
|
2020-05-14 01:06:03 +02:00
|
|
|
Labels []kv.KeyValue
|
2020-03-24 19:54:08 +02:00
|
|
|
LibraryName string
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
MeterImpl struct {
|
2020-05-14 01:27:52 +02:00
|
|
|
lock sync.Mutex
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
MeasurementBatches []Batch
|
2020-05-14 01:27:52 +02:00
|
|
|
|
|
|
|
asyncInstruments *AsyncInstrumentState
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Measurement struct {
|
2020-01-06 20:08:40 +02:00
|
|
|
// Number needs to be aligned for 64-bit atomic operations.
|
2020-05-11 08:44:42 +02:00
|
|
|
Number apimetric.Number
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument apimetric.InstrumentImpl
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument struct {
|
2020-03-24 19:54:08 +02:00
|
|
|
meter *MeterImpl
|
2020-03-19 21:02:46 +02:00
|
|
|
descriptor apimetric.Descriptor
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
Async struct {
|
|
|
|
Instrument
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2020-05-14 01:27:52 +02:00
|
|
|
runner apimetric.AsyncRunner
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
Sync struct {
|
|
|
|
Instrument
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-19 21:02:46 +02:00
|
|
|
_ apimetric.SyncImpl = &Sync{}
|
|
|
|
_ apimetric.BoundSyncImpl = &Handle{}
|
2020-03-24 19:54:08 +02:00
|
|
|
_ apimetric.MeterImpl = &MeterImpl{}
|
2020-03-19 21:02:46 +02:00
|
|
|
_ apimetric.AsyncImpl = &Async{}
|
2019-10-30 21:59:34 +02:00
|
|
|
)
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (i Instrument) Descriptor() apimetric.Descriptor {
|
|
|
|
return i.descriptor
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (a *Async) Implementation() interface{} {
|
|
|
|
return a
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (s *Sync) Implementation() interface{} {
|
|
|
|
return s
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Sync) Bind(labels []kv.KeyValue) apimetric.BoundSyncImpl {
|
2019-10-30 21:59:34 +02:00
|
|
|
return &Handle{
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument: s,
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: labels,
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Sync) RecordOne(ctx context.Context, number apimetric.Number, labels []kv.KeyValue) {
|
2020-03-27 23:06:48 +02:00
|
|
|
s.meter.doRecordSingle(ctx, labels, s, number)
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
func (h *Handle) RecordOne(ctx context.Context, number apimetric.Number) {
|
2020-03-27 23:06:48 +02:00
|
|
|
h.Instrument.meter.doRecordSingle(ctx, h.Labels, h.Instrument, number)
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:30:19 +02:00
|
|
|
func (h *Handle) Unbind() {
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (m *MeterImpl) doRecordSingle(ctx context.Context, labels []kv.KeyValue, instrument apimetric.InstrumentImpl, number apimetric.Number) {
|
2020-05-14 01:27:52 +02:00
|
|
|
m.collect(ctx, labels, []Measurement{{
|
2019-10-30 21:59:34 +02:00
|
|
|
Instrument: instrument,
|
|
|
|
Number: number,
|
2020-05-14 01:27:52 +02:00
|
|
|
}})
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
func NewProvider() (*MeterImpl, apimetric.Provider) {
|
2020-05-14 01:27:52 +02:00
|
|
|
impl := &MeterImpl{
|
2020-06-02 21:27:55 +02:00
|
|
|
asyncInstruments: NewAsyncInstrumentState(),
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
2020-05-18 19:48:58 +02:00
|
|
|
return impl, registry.NewProvider(impl)
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
func NewMeter() (*MeterImpl, apimetric.Meter) {
|
|
|
|
impl, p := NewProvider()
|
|
|
|
return impl, p.Meter("mock")
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
func (m *MeterImpl) NewSyncInstrument(descriptor metric.Descriptor) (apimetric.SyncImpl, error) {
|
2020-05-14 01:27:52 +02:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
return &Sync{
|
|
|
|
Instrument{
|
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
|
|
|
},
|
|
|
|
}, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:27:52 +02:00
|
|
|
func (m *MeterImpl) NewAsyncInstrument(descriptor metric.Descriptor, runner metric.AsyncRunner) (apimetric.AsyncImpl, error) {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
a := &Async{
|
|
|
|
Instrument: Instrument{
|
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
2020-03-05 22:15:30 +02:00
|
|
|
},
|
2020-05-14 01:27:52 +02:00
|
|
|
runner: runner,
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
2020-05-14 01:27:52 +02:00
|
|
|
m.asyncInstruments.Register(a, runner)
|
2020-03-19 21:02:46 +02:00
|
|
|
return a, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (m *MeterImpl) RecordBatch(ctx context.Context, labels []kv.KeyValue, measurements ...apimetric.Measurement) {
|
2019-10-30 21:59:34 +02:00
|
|
|
mm := make([]Measurement, len(measurements))
|
|
|
|
for i := 0; i < len(measurements); i++ {
|
|
|
|
m := measurements[i]
|
|
|
|
mm[i] = Measurement{
|
2020-04-30 01:13:55 +02:00
|
|
|
Instrument: m.SyncImpl().Implementation().(*Sync),
|
2019-10-30 21:59:34 +02:00
|
|
|
Number: m.Number(),
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 01:27:52 +02:00
|
|
|
m.collect(ctx, labels, mm)
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:27:52 +02:00
|
|
|
func (m *MeterImpl) CollectAsync(labels []kv.KeyValue, obs ...metric.Observation) {
|
|
|
|
mm := make([]Measurement, len(obs))
|
|
|
|
for i := 0; i < len(obs); i++ {
|
|
|
|
o := obs[i]
|
|
|
|
mm[i] = Measurement{
|
|
|
|
Instrument: o.AsyncImpl(),
|
|
|
|
Number: o.Number(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.collect(context.Background(), labels, mm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MeterImpl) collect(ctx context.Context, labels []kv.KeyValue, measurements []Measurement) {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
m.MeasurementBatches = append(m.MeasurementBatches, Batch{
|
|
|
|
Ctx: ctx,
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: labels,
|
2019-10-30 21:59:34 +02:00
|
|
|
Measurements: measurements,
|
|
|
|
})
|
|
|
|
}
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
func (m *MeterImpl) RunAsyncInstruments() {
|
2020-05-20 06:33:10 +02:00
|
|
|
m.asyncInstruments.Run(context.Background(), m)
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|