1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/internal/metric/mock.go
Joshua MacDonald dd781560d4
Rename handle to bound instrument (@sircelsius) (#400)
* Rename metric Handle to Bound Instrument

* Rename metric Handle to Bound Instrument

* Rename metric Handle to Bound Instrument

* chore(meter): renamed from *BoundInstrument to Bound*, renamed AcquireBoundInstrument to Bind

* chore(meter): renamed Release to Unbind

* Self feedback in doc.go

* Rename confusing method name

Co-authored-by: Marc Bramaud <sircelsius@users.noreply.github.com>
2019-12-27 16:30:19 -08:00

230 lines
6.1 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"
"sync"
"go.opentelemetry.io/otel/api/core"
apimetric "go.opentelemetry.io/otel/api/metric"
)
type (
Handle struct {
Instrument *Instrument
LabelSet *LabelSet
}
Instrument struct {
Name string
Kind Kind
NumberKind core.NumberKind
Opts apimetric.Options
}
LabelSet struct {
TheMeter *Meter
Labels map[core.Key]core.Value
}
Batch struct {
Ctx context.Context
LabelSet *LabelSet
Measurements []Measurement
}
MeterProvider struct {
lock sync.Mutex
registered map[string]*Meter
}
Meter struct {
MeasurementBatches []Batch
}
Kind int8
Measurement struct {
Instrument *Instrument
Number core.Number
}
)
var (
_ apimetric.InstrumentImpl = &Instrument{}
_ apimetric.BoundInstrumentImpl = &Handle{}
_ apimetric.LabelSet = &LabelSet{}
_ apimetric.Meter = &Meter{}
)
const (
KindCounter Kind = iota
KindGauge
KindMeasure
)
func (i *Instrument) Bind(labels apimetric.LabelSet) apimetric.BoundInstrumentImpl {
if ld, ok := labels.(apimetric.LabelSetDelegate); ok {
labels = ld.Delegate()
}
return &Handle{
Instrument: i,
LabelSet: labels.(*LabelSet),
}
}
func (i *Instrument) RecordOne(ctx context.Context, number core.Number, labels apimetric.LabelSet) {
if ld, ok := labels.(apimetric.LabelSetDelegate); ok {
labels = ld.Delegate()
}
doRecordBatch(ctx, labels.(*LabelSet), i, number)
}
func (h *Handle) RecordOne(ctx context.Context, number core.Number) {
doRecordBatch(ctx, h.LabelSet, h.Instrument, number)
}
func (h *Handle) Unbind() {
}
func doRecordBatch(ctx context.Context, labelSet *LabelSet, instrument *Instrument, number core.Number) {
labelSet.TheMeter.recordMockBatch(ctx, labelSet, Measurement{
Instrument: instrument,
Number: number,
})
}
func (s *LabelSet) Meter() apimetric.Meter {
return s.TheMeter
}
func NewProvider() *MeterProvider {
return &MeterProvider{
registered: map[string]*Meter{},
}
}
func (p *MeterProvider) Meter(name string) apimetric.Meter {
p.lock.Lock()
defer p.lock.Unlock()
if lookup, ok := p.registered[name]; ok {
return lookup
}
m := NewMeter()
p.registered[name] = m
return m
}
func NewMeter() *Meter {
return &Meter{}
}
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,
}
}
func (m *Meter) NewInt64Counter(name string, cos ...apimetric.CounterOptionApplier) apimetric.Int64Counter {
instrument := m.newCounterInstrument(name, core.Int64NumberKind, cos...)
return apimetric.WrapInt64CounterInstrument(instrument)
}
func (m *Meter) NewFloat64Counter(name string, cos ...apimetric.CounterOptionApplier) apimetric.Float64Counter {
instrument := m.newCounterInstrument(name, core.Float64NumberKind, cos...)
return apimetric.WrapFloat64CounterInstrument(instrument)
}
func (m *Meter) newCounterInstrument(name string, numberKind core.NumberKind, cos ...apimetric.CounterOptionApplier) *Instrument {
opts := apimetric.Options{}
apimetric.ApplyCounterOptions(&opts, cos...)
return &Instrument{
Name: name,
Kind: KindCounter,
NumberKind: numberKind,
Opts: opts,
}
}
func (m *Meter) NewInt64Gauge(name string, gos ...apimetric.GaugeOptionApplier) apimetric.Int64Gauge {
instrument := m.newGaugeInstrument(name, core.Int64NumberKind, gos...)
return apimetric.WrapInt64GaugeInstrument(instrument)
}
func (m *Meter) NewFloat64Gauge(name string, gos ...apimetric.GaugeOptionApplier) apimetric.Float64Gauge {
instrument := m.newGaugeInstrument(name, core.Float64NumberKind, gos...)
return apimetric.WrapFloat64GaugeInstrument(instrument)
}
func (m *Meter) newGaugeInstrument(name string, numberKind core.NumberKind, gos ...apimetric.GaugeOptionApplier) *Instrument {
opts := apimetric.Options{}
apimetric.ApplyGaugeOptions(&opts, gos...)
return &Instrument{
Name: name,
Kind: KindGauge,
NumberKind: numberKind,
Opts: opts,
}
}
func (m *Meter) NewInt64Measure(name string, mos ...apimetric.MeasureOptionApplier) apimetric.Int64Measure {
instrument := m.newMeasureInstrument(name, core.Int64NumberKind, mos...)
return apimetric.WrapInt64MeasureInstrument(instrument)
}
func (m *Meter) NewFloat64Measure(name string, mos ...apimetric.MeasureOptionApplier) apimetric.Float64Measure {
instrument := m.newMeasureInstrument(name, core.Float64NumberKind, mos...)
return apimetric.WrapFloat64MeasureInstrument(instrument)
}
func (m *Meter) newMeasureInstrument(name string, numberKind core.NumberKind, mos ...apimetric.MeasureOptionApplier) *Instrument {
opts := apimetric.Options{}
apimetric.ApplyMeasureOptions(&opts, mos...)
return &Instrument{
Name: name,
Kind: KindMeasure,
NumberKind: numberKind,
Opts: opts,
}
}
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{
Instrument: m.InstrumentImpl().(*Instrument),
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,
})
}