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
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
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"
|
2019-10-30 21:59:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Handle struct {
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument *Sync
|
2019-10-30 21:59:34 +02:00
|
|
|
LabelSet *LabelSet
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelSet struct {
|
|
|
|
TheMeter *Meter
|
|
|
|
Labels map[core.Key]core.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
LabelSet *LabelSet
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
MeterProvider struct {
|
|
|
|
lock sync.Mutex
|
2020-03-11 20:57:57 +02:00
|
|
|
registered map[string]apimetric.Meter
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
Meter struct {
|
|
|
|
MeasurementBatches []Batch
|
2020-03-19 21:02:46 +02:00
|
|
|
AsyncInstruments []*Async
|
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.
|
2019-10-30 21:59:34 +02:00
|
|
|
Number core.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 {
|
|
|
|
meter *Meter
|
|
|
|
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-03-19 21:02:46 +02:00
|
|
|
callback func(func(core.Number, apimetric.LabelSet))
|
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{}
|
|
|
|
_ apimetric.LabelSet = &LabelSet{}
|
|
|
|
_ apimetric.MeterImpl = &Meter{}
|
|
|
|
_ 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-03-19 21:02:46 +02:00
|
|
|
func (s *Sync) Bind(labels apimetric.LabelSet) apimetric.BoundSyncImpl {
|
2019-12-24 09:03:04 +02:00
|
|
|
if ld, ok := labels.(apimetric.LabelSetDelegate); ok {
|
|
|
|
labels = ld.Delegate()
|
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
return &Handle{
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument: s,
|
2019-10-30 21:59:34 +02:00
|
|
|
LabelSet: labels.(*LabelSet),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (s *Sync) RecordOne(ctx context.Context, number core.Number, labels apimetric.LabelSet) {
|
2019-12-24 09:03:04 +02:00
|
|
|
if ld, ok := labels.(apimetric.LabelSetDelegate); ok {
|
|
|
|
labels = ld.Delegate()
|
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
s.meter.doRecordSingle(ctx, labels.(*LabelSet), s, number)
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) RecordOne(ctx context.Context, number core.Number) {
|
2020-03-19 21:02:46 +02:00
|
|
|
h.Instrument.meter.doRecordSingle(ctx, h.LabelSet, 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-03-19 21:02:46 +02:00
|
|
|
func (m *Meter) doRecordSingle(ctx context.Context, labelSet *LabelSet, instrument apimetric.InstrumentImpl, number core.Number) {
|
|
|
|
m.recordMockBatch(ctx, labelSet, Measurement{
|
2019-10-30 21:59:34 +02:00
|
|
|
Instrument: instrument,
|
|
|
|
Number: number,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
func NewProvider() *MeterProvider {
|
|
|
|
return &MeterProvider{
|
2020-03-11 20:57:57 +02:00
|
|
|
registered: map[string]apimetric.Meter{},
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MeterProvider) Meter(name string) apimetric.Meter {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
if lookup, ok := p.registered[name]; ok {
|
|
|
|
return lookup
|
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
_, m := NewMeter()
|
2019-12-24 09:03:04 +02:00
|
|
|
p.registered[name] = m
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func NewMeter() (*Meter, apimetric.Meter) {
|
|
|
|
mock := &Meter{}
|
|
|
|
return mock, apimetric.WrapMeterImpl(mock)
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Meter) Labels(labels ...core.KeyValue) apimetric.LabelSet {
|
|
|
|
ul := make(map[core.Key]core.Value)
|
|
|
|
for _, kv := range labels {
|
|
|
|
ul[kv.Key] = kv.Value
|
|
|
|
}
|
|
|
|
return &LabelSet{
|
|
|
|
TheMeter: m,
|
|
|
|
Labels: ul,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (m *Meter) NewSyncInstrument(descriptor metric.Descriptor) (apimetric.SyncImpl, error) {
|
|
|
|
return &Sync{
|
|
|
|
Instrument{
|
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
|
|
|
},
|
|
|
|
}, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (m *Meter) NewAsyncInstrument(descriptor metric.Descriptor, callback func(func(core.Number, apimetric.LabelSet))) (apimetric.AsyncImpl, error) {
|
|
|
|
a := &Async{
|
|
|
|
Instrument: Instrument{
|
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
2020-03-05 22:15:30 +02:00
|
|
|
},
|
|
|
|
callback: callback,
|
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
m.AsyncInstruments = append(m.AsyncInstruments, a)
|
|
|
|
return a, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
func (m *Meter) RecordBatch(ctx context.Context, labels apimetric.LabelSet, measurements ...apimetric.Measurement) {
|
|
|
|
ourLabelSet := labels.(*LabelSet)
|
|
|
|
mm := make([]Measurement, len(measurements))
|
|
|
|
for i := 0; i < len(measurements); i++ {
|
|
|
|
m := measurements[i]
|
|
|
|
mm[i] = Measurement{
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument: m.SyncImpl().(*Sync),
|
2019-10-30 21:59:34 +02:00
|
|
|
Number: m.Number(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.recordMockBatch(ctx, ourLabelSet, mm...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Meter) recordMockBatch(ctx context.Context, labelSet *LabelSet, measurements ...Measurement) {
|
|
|
|
m.MeasurementBatches = append(m.MeasurementBatches, Batch{
|
|
|
|
Ctx: ctx,
|
|
|
|
LabelSet: labelSet,
|
|
|
|
Measurements: measurements,
|
|
|
|
})
|
|
|
|
}
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (m *Meter) RunAsyncInstruments() {
|
|
|
|
for _, observer := range m.AsyncInstruments {
|
|
|
|
observer.callback(func(n core.Number, labels apimetric.LabelSet) {
|
|
|
|
|
|
|
|
if ld, ok := labels.(apimetric.LabelSetDelegate); ok {
|
|
|
|
labels = ld.Delegate()
|
|
|
|
}
|
|
|
|
|
|
|
|
m.doRecordSingle(context.Background(), labels.(*LabelSet), observer, n)
|
2020-03-05 22:15:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|