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.
|
|
|
|
|
2021-06-10 21:05:25 +02:00
|
|
|
package metrictest // import "go.opentelemetry.io/otel/metric/metrictest"
|
2019-10-30 21:59:34 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-24 09:03:04 +02:00
|
|
|
"sync"
|
2020-10-17 18:48:21 +02:00
|
|
|
"testing"
|
2019-10-30 21:59:34 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-10-11 20:46:29 +02:00
|
|
|
internalmetric "go.opentelemetry.io/otel/internal/metric"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2021-09-27 17:51:47 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
2019-10-30 21:59:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Handle struct {
|
2020-03-19 21:02:46 +02:00
|
|
|
Instrument *Sync
|
2021-02-18 19:59:37 +02:00
|
|
|
Labels []attribute.KeyValue
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// Library is the same as "sdk/instrumentation".Library but there is
|
|
|
|
// a package cycle to use it.
|
|
|
|
Library struct {
|
|
|
|
InstrumentationName string
|
|
|
|
InstrumentationVersion string
|
|
|
|
SchemaURL string
|
|
|
|
}
|
|
|
|
|
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
|
2021-02-18 19:59:37 +02:00
|
|
|
Labels []attribute.KeyValue
|
2021-09-27 17:51:47 +02:00
|
|
|
Library Library
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 22:03:56 +02:00
|
|
|
// MeterImpl is an OpenTelemetry Meter implementation used for testing.
|
2020-03-24 19:54:08 +02:00
|
|
|
MeterImpl struct {
|
2021-09-27 17:51:47 +02:00
|
|
|
library Library
|
|
|
|
provider *MeterProvider
|
|
|
|
asyncInstruments *internalmetric.AsyncInstrumentState
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeterProvider is a collection of named MeterImpls used for testing.
|
|
|
|
MeterProvider struct {
|
2020-05-14 01:27:52 +02:00
|
|
|
lock sync.Mutex
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
MeasurementBatches []Batch
|
2021-09-27 17:51:47 +02:00
|
|
|
impls []*MeterImpl
|
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-11-11 17:24:12 +02:00
|
|
|
Number number.Number
|
2021-10-14 18:06:22 +02:00
|
|
|
Instrument sdkapi.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
|
2021-10-14 18:06:22 +02:00
|
|
|
descriptor sdkapi.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
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
runner sdkapi.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 (
|
2021-10-14 18:06:22 +02:00
|
|
|
_ sdkapi.SyncImpl = &Sync{}
|
|
|
|
_ sdkapi.BoundSyncImpl = &Handle{}
|
|
|
|
_ sdkapi.MeterImpl = &MeterImpl{}
|
|
|
|
_ sdkapi.AsyncImpl = &Async{}
|
2019-10-30 21:59:34 +02:00
|
|
|
)
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// NewDescriptor is a test helper for constructing test metric
|
|
|
|
// descriptors using standard options.
|
2021-10-14 18:06:22 +02:00
|
|
|
func NewDescriptor(name string, ikind sdkapi.InstrumentKind, nkind number.Kind, opts ...metric.InstrumentOption) sdkapi.Descriptor {
|
2021-09-27 17:51:47 +02:00
|
|
|
cfg := metric.NewInstrumentConfig(opts...)
|
2021-10-14 18:06:22 +02:00
|
|
|
return sdkapi.NewDescriptor(name, ikind, nkind, cfg.Description(), cfg.Unit())
|
2021-09-27 17:51:47 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (i Instrument) Descriptor() sdkapi.Descriptor {
|
2020-03-19 21:02:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (s *Sync) Bind(labels []attribute.KeyValue) sdkapi.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
func (s *Sync) RecordOne(ctx context.Context, number number.Number, labels []attribute.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-11-11 17:24:12 +02:00
|
|
|
func (h *Handle) RecordOne(ctx context.Context, number number.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
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (m *MeterImpl) doRecordSingle(ctx context.Context, labels []attribute.KeyValue, instrument sdkapi.InstrumentImpl, number number.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
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// NewMeterProvider returns a MeterProvider suitable for testing.
|
|
|
|
// When the test is complete, consult MeterProvider.MeasurementBatches.
|
|
|
|
func NewMeterProvider() *MeterProvider {
|
|
|
|
return &MeterProvider{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meter implements metric.MeterProvider.
|
|
|
|
func (p *MeterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
cfg := metric.NewMeterConfig(opts...)
|
2020-05-14 01:27:52 +02:00
|
|
|
impl := &MeterImpl{
|
2021-09-27 17:51:47 +02:00
|
|
|
library: Library{
|
|
|
|
InstrumentationName: name,
|
|
|
|
InstrumentationVersion: cfg.InstrumentationVersion(),
|
|
|
|
SchemaURL: cfg.SchemaURL(),
|
|
|
|
},
|
|
|
|
provider: p,
|
2020-10-11 20:46:29 +02:00
|
|
|
asyncInstruments: internalmetric.NewAsyncInstrumentState(),
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
p.impls = append(p.impls, impl)
|
|
|
|
return metric.WrapMeterImpl(impl)
|
2019-10-30 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// NewSyncInstrument implements sdkapi.MeterImpl.
|
|
|
|
func (m *MeterImpl) NewSyncInstrument(descriptor sdkapi.Descriptor) (sdkapi.SyncImpl, error) {
|
2020-03-19 21:02:46 +02:00
|
|
|
return &Sync{
|
|
|
|
Instrument{
|
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
|
|
|
},
|
|
|
|
}, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// NewAsyncInstrument implements sdkapi.MeterImpl.
|
|
|
|
func (m *MeterImpl) NewAsyncInstrument(descriptor sdkapi.Descriptor, runner sdkapi.AsyncRunner) (sdkapi.AsyncImpl, error) {
|
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
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
m.provider.registerAsyncInstrument(a, m, runner)
|
2020-03-19 21:02:46 +02:00
|
|
|
return a, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// RecordBatch implements sdkapi.MeterImpl.
|
|
|
|
func (m *MeterImpl) RecordBatch(ctx context.Context, labels []attribute.KeyValue, measurements ...sdkapi.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
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// CollectAsync is called from asyncInstruments.Run() with the lock held.
|
2021-10-14 18:06:22 +02:00
|
|
|
func (m *MeterImpl) CollectAsync(labels []attribute.KeyValue, obs ...sdkapi.Observation) {
|
2020-05-14 01:27:52 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// collect is called from CollectAsync() or RecordBatch() with the lock held.
|
2021-02-18 19:59:37 +02:00
|
|
|
func (m *MeterImpl) collect(ctx context.Context, labels []attribute.KeyValue, measurements []Measurement) {
|
2021-09-27 17:51:47 +02:00
|
|
|
m.provider.addMeasurement(Batch{
|
2019-10-30 21:59:34 +02:00
|
|
|
Ctx: ctx,
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: labels,
|
2019-10-30 21:59:34 +02:00
|
|
|
Measurements: measurements,
|
2021-09-27 17:51:47 +02:00
|
|
|
Library: m.library,
|
2019-10-30 21:59:34 +02:00
|
|
|
})
|
|
|
|
}
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// registerAsyncInstrument locks the provider and registers the new Async instrument.
|
2021-10-14 18:06:22 +02:00
|
|
|
func (p *MeterProvider) registerAsyncInstrument(a *Async, m *MeterImpl, runner sdkapi.AsyncRunner) {
|
2021-09-27 17:51:47 +02:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
m.asyncInstruments.Register(a, runner)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addMeasurement locks the provider and adds the new measurement batch.
|
|
|
|
func (p *MeterProvider) addMeasurement(b Batch) {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
p.MeasurementBatches = append(p.MeasurementBatches, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyImpls locks the provider and copies the current list of *MeterImpls.
|
|
|
|
func (p *MeterProvider) copyImpls() []*MeterImpl {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
cpy := make([]*MeterImpl, len(p.impls))
|
|
|
|
copy(cpy, p.impls)
|
|
|
|
return cpy
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunAsyncInstruments is used in tests to trigger collection from
|
|
|
|
// asynchronous instruments.
|
|
|
|
func (p *MeterProvider) RunAsyncInstruments() {
|
|
|
|
for _, impl := range p.copyImpls() {
|
|
|
|
impl.asyncInstruments.Run(context.Background(), impl)
|
|
|
|
}
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
2020-10-17 18:48:21 +02:00
|
|
|
|
|
|
|
// Measured is the helper struct which provides flat representation of recorded measurements
|
|
|
|
// to simplify testing
|
|
|
|
type Measured struct {
|
2021-09-27 17:51:47 +02:00
|
|
|
Name string
|
|
|
|
Labels map[attribute.Key]attribute.Value
|
|
|
|
Number number.Number
|
|
|
|
Library Library
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LabelsToMap converts label set to keyValue map, to be easily used in tests
|
2021-02-18 19:59:37 +02:00
|
|
|
func LabelsToMap(kvs ...attribute.KeyValue) map[attribute.Key]attribute.Value {
|
|
|
|
m := map[attribute.Key]attribute.Value{}
|
2020-10-17 18:48:21 +02:00
|
|
|
for _, label := range kvs {
|
|
|
|
m[label.Key] = label.Value
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsStructs converts recorded batches to array of flat, readable Measured helper structures
|
|
|
|
func AsStructs(batches []Batch) []Measured {
|
|
|
|
var r []Measured
|
|
|
|
for _, batch := range batches {
|
|
|
|
for _, m := range batch.Measurements {
|
|
|
|
r = append(r, Measured{
|
2021-09-27 17:51:47 +02:00
|
|
|
Name: m.Instrument.Descriptor().Name(),
|
|
|
|
Labels: LabelsToMap(batch.Labels...),
|
|
|
|
Number: m.Number,
|
|
|
|
Library: batch.Library,
|
2020-10-17 18:48:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveNumberByKind takes defined metric descriptor creates a concrete typed metric number
|
2020-11-11 17:24:12 +02:00
|
|
|
func ResolveNumberByKind(t *testing.T, kind number.Kind, value float64) number.Number {
|
2020-10-17 18:48:21 +02:00
|
|
|
t.Helper()
|
|
|
|
switch kind {
|
2020-11-11 17:24:12 +02:00
|
|
|
case number.Int64Kind:
|
|
|
|
return number.NewInt64Number(int64(value))
|
|
|
|
case number.Float64Kind:
|
|
|
|
return number.NewFloat64Number(value)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
panic("invalid number kind")
|
|
|
|
}
|