2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The 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.
|
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
package internal_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-11 20:57:57 +02:00
|
|
|
"errors"
|
2019-12-24 09:03:04 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
|
|
"go.opentelemetry.io/otel/api/global/internal"
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-03-05 22:15:30 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-12-24 09:03:04 +02:00
|
|
|
metrictest "go.opentelemetry.io/otel/internal/metric"
|
|
|
|
)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
var Must = metric.Must
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
// Note: Maybe this should be factored into ../../../internal/metric?
|
|
|
|
type measured struct {
|
2020-06-12 18:11:17 +02:00
|
|
|
Name string
|
|
|
|
InstrumentationName string
|
|
|
|
InstrumentationVersion string
|
2020-07-24 21:25:27 +02:00
|
|
|
Labels map[kv.Key]kv.Value
|
2020-06-12 18:11:17 +02:00
|
|
|
Number metric.Number
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func asStructs(batches []metrictest.Batch) []measured {
|
|
|
|
var r []measured
|
|
|
|
for _, batch := range batches {
|
|
|
|
for _, m := range batch.Measurements {
|
|
|
|
r = append(r, measured{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: m.Instrument.Descriptor().Name(),
|
|
|
|
InstrumentationName: m.Instrument.Descriptor().InstrumentationName(),
|
|
|
|
InstrumentationVersion: m.Instrument.Descriptor().InstrumentationVersion(),
|
|
|
|
Labels: asMap(batch.Labels...),
|
|
|
|
Number: m.Number,
|
2020-03-24 19:54:08 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:25:27 +02:00
|
|
|
func asMap(kvs ...kv.KeyValue) map[kv.Key]kv.Value {
|
|
|
|
m := map[kv.Key]kv.Value{}
|
2020-03-24 19:54:08 +02:00
|
|
|
for _, kv := range kvs {
|
|
|
|
m[kv.Key] = kv.Value
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
var asInt = metric.NewInt64Number
|
|
|
|
var asFloat = metric.NewFloat64Number
|
2020-03-24 19:54:08 +02:00
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
func TestDirect(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-06-12 18:11:17 +02:00
|
|
|
meter1 := global.Meter("test1", metric.WithInstrumentationVersion("semver:v1.0.0"))
|
2020-03-11 17:23:32 +02:00
|
|
|
meter2 := global.Meter("test2")
|
2020-05-14 01:06:03 +02:00
|
|
|
labels1 := []kv.KeyValue{kv.String("A", "B")}
|
|
|
|
labels2 := []kv.KeyValue{kv.String("C", "D")}
|
|
|
|
labels3 := []kv.KeyValue{kv.String("E", "F")}
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
counter := Must(meter1).NewInt64Counter("test.counter")
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1, labels1...)
|
|
|
|
counter.Add(ctx, 1, labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder := Must(meter1).NewFloat64ValueRecorder("test.valuerecorder")
|
|
|
|
valuerecorder.Record(ctx, 1, labels1...)
|
|
|
|
valuerecorder.Record(ctx, 2, labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
_ = Must(meter1).NewFloat64ValueObserver("test.valueobserver.float", func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-03-27 23:06:48 +02:00
|
|
|
result.Observe(1., labels1...)
|
|
|
|
result.Observe(2., labels2...)
|
2020-03-05 22:15:30 +02:00
|
|
|
})
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
_ = Must(meter1).NewInt64ValueObserver("test.valueobserver.int", func(_ context.Context, result metric.Int64ObserverResult) {
|
2020-03-27 23:06:48 +02:00
|
|
|
result.Observe(1, labels1...)
|
|
|
|
result.Observe(2, labels2...)
|
2020-03-05 22:15:30 +02:00
|
|
|
})
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
second := Must(meter2).NewFloat64ValueRecorder("test.second")
|
2020-03-27 23:06:48 +02:00
|
|
|
second.Record(ctx, 1, labels3...)
|
|
|
|
second.Record(ctx, 2, labels3...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
mock, provider := metrictest.NewProvider()
|
|
|
|
global.SetMeterProvider(provider)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1, labels1...)
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, 3, labels1...)
|
2020-03-27 23:06:48 +02:00
|
|
|
second.Record(ctx, 3, labels3...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
mock.RunAsyncInstruments()
|
2020-03-24 19:54:08 +02:00
|
|
|
|
|
|
|
measurements := asStructs(mock.MeasurementBatches)
|
|
|
|
|
|
|
|
require.EqualValues(t,
|
|
|
|
[]measured{
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.counter",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
|
|
|
Labels: asMap(labels1...),
|
|
|
|
Number: asInt(1),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valuerecorder",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
|
|
|
Labels: asMap(labels1...),
|
|
|
|
Number: asFloat(3),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.second",
|
|
|
|
InstrumentationName: "test2",
|
|
|
|
Labels: asMap(labels3...),
|
|
|
|
Number: asFloat(3),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valueobserver.float",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
|
|
|
Labels: asMap(labels1...),
|
|
|
|
Number: asFloat(1),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valueobserver.float",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
|
|
|
Labels: asMap(labels2...),
|
|
|
|
Number: asFloat(2),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valueobserver.int",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
|
|
|
Labels: asMap(labels1...),
|
|
|
|
Number: asInt(1),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valueobserver.int",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
|
|
|
Labels: asMap(labels2...),
|
|
|
|
Number: asInt(2),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
measurements,
|
|
|
|
)
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBound(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
2020-03-03 00:50:53 +02:00
|
|
|
// Note: this test uses opposite Float64/Int64 number kinds
|
2019-12-24 09:03:04 +02:00
|
|
|
// vs. the above, to cover all the instruments.
|
|
|
|
ctx := context.Background()
|
2020-03-11 17:23:32 +02:00
|
|
|
glob := global.Meter("test")
|
2020-05-14 01:06:03 +02:00
|
|
|
labels1 := []kv.KeyValue{kv.String("A", "B")}
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
counter := Must(glob).NewFloat64Counter("test.counter")
|
2020-03-27 23:06:48 +02:00
|
|
|
boundC := counter.Bind(labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
boundC.Add(ctx, 1)
|
|
|
|
boundC.Add(ctx, 1)
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder := Must(glob).NewInt64ValueRecorder("test.valuerecorder")
|
|
|
|
boundM := valuerecorder.Bind(labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
boundM.Record(ctx, 1)
|
|
|
|
boundM.Record(ctx, 2)
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
mock, provider := metrictest.NewProvider()
|
|
|
|
global.SetMeterProvider(provider)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
boundC.Add(ctx, 1)
|
|
|
|
boundM.Record(ctx, 3)
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
require.EqualValues(t,
|
|
|
|
[]measured{
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.counter",
|
|
|
|
InstrumentationName: "test",
|
|
|
|
Labels: asMap(labels1...),
|
|
|
|
Number: asFloat(1),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valuerecorder",
|
|
|
|
InstrumentationName: "test",
|
|
|
|
Labels: asMap(labels1...),
|
|
|
|
Number: asInt(3),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
asStructs(mock.MeasurementBatches))
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2019-12-28 02:30:19 +02:00
|
|
|
boundC.Unbind()
|
|
|
|
boundM.Unbind()
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:30:19 +02:00
|
|
|
func TestUnbind(t *testing.T) {
|
|
|
|
// Tests Unbind with SDK never installed.
|
2019-12-24 09:03:04 +02:00
|
|
|
internal.ResetForTest()
|
|
|
|
|
2020-03-11 17:23:32 +02:00
|
|
|
glob := global.Meter("test")
|
2020-05-14 01:06:03 +02:00
|
|
|
labels1 := []kv.KeyValue{kv.String("A", "B")}
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
counter := Must(glob).NewFloat64Counter("test.counter")
|
2020-03-27 23:06:48 +02:00
|
|
|
boundC := counter.Bind(labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder := Must(glob).NewInt64ValueRecorder("test.valuerecorder")
|
|
|
|
boundM := valuerecorder.Bind(labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2019-12-28 02:30:19 +02:00
|
|
|
boundC.Unbind()
|
|
|
|
boundM.Unbind()
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 23:05:19 +02:00
|
|
|
func TestUnbindThenRecordOne(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-03-24 19:54:08 +02:00
|
|
|
mock, provider := metrictest.NewProvider()
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2020-03-11 17:23:32 +02:00
|
|
|
meter := global.Meter("test")
|
2020-03-11 20:57:57 +02:00
|
|
|
counter := Must(meter).NewInt64Counter("test.counter")
|
2020-03-27 23:06:48 +02:00
|
|
|
boundC := counter.Bind()
|
2020-03-24 19:54:08 +02:00
|
|
|
global.SetMeterProvider(provider)
|
2020-02-20 23:05:19 +02:00
|
|
|
boundC.Unbind()
|
|
|
|
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
boundC.Add(ctx, 1)
|
|
|
|
})
|
|
|
|
require.Equal(t, 0, len(mock.MeasurementBatches))
|
|
|
|
}
|
2020-03-11 20:57:57 +02:00
|
|
|
|
|
|
|
type meterProviderWithConstructorError struct {
|
|
|
|
metric.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
type meterWithConstructorError struct {
|
2020-05-12 05:29:06 +02:00
|
|
|
metric.MeterImpl
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m *meterProviderWithConstructorError) Meter(iName string, opts ...metric.MeterOption) metric.Meter {
|
|
|
|
return metric.WrapMeterImpl(&meterWithConstructorError{m.Provider.Meter(iName, opts...).MeterImpl()}, iName, opts...)
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 05:29:06 +02:00
|
|
|
func (m *meterWithConstructorError) NewSyncInstrument(_ metric.Descriptor) (metric.SyncImpl, error) {
|
|
|
|
return metric.NoopSync{}, errors.New("constructor error")
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorInDeferredConstructor(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
meter := global.MeterProvider().Meter("builtin")
|
|
|
|
|
|
|
|
c1 := Must(meter).NewInt64Counter("test")
|
|
|
|
c2 := Must(meter).NewInt64Counter("test")
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
_, provider := metrictest.NewProvider()
|
|
|
|
sdk := &meterProviderWithConstructorError{provider}
|
2020-03-11 20:57:57 +02:00
|
|
|
|
|
|
|
require.Panics(t, func() {
|
|
|
|
global.SetMeterProvider(sdk)
|
|
|
|
})
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
c1.Add(ctx, 1)
|
|
|
|
c2.Add(ctx, 2)
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
func TestImplementationIndirection(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
// Test that Implementation() does the proper indirection, i.e.,
|
|
|
|
// returns the implementation interface not the global, after
|
|
|
|
// registered.
|
|
|
|
|
|
|
|
meter1 := global.Meter("test1")
|
|
|
|
|
|
|
|
// Sync: no SDK yet
|
|
|
|
counter := Must(meter1).NewInt64Counter("interface.counter")
|
|
|
|
|
|
|
|
ival := counter.Measurement(1).SyncImpl().Implementation()
|
|
|
|
require.NotNil(t, ival)
|
|
|
|
|
|
|
|
_, ok := ival.(*metrictest.Sync)
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
// Async: no SDK yet
|
2020-05-22 00:42:14 +02:00
|
|
|
valueobserver := Must(meter1).NewFloat64ValueObserver(
|
2020-05-18 20:03:43 +02:00
|
|
|
"interface.valueobserver",
|
2020-05-20 06:33:10 +02:00
|
|
|
func(_ context.Context, result metric.Float64ObserverResult) {},
|
2020-03-19 21:02:46 +02:00
|
|
|
)
|
|
|
|
|
2020-05-18 20:03:43 +02:00
|
|
|
ival = valueobserver.AsyncImpl().Implementation()
|
2020-03-19 21:02:46 +02:00
|
|
|
require.NotNil(t, ival)
|
|
|
|
|
|
|
|
_, ok = ival.(*metrictest.Async)
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
// Register the SDK
|
2020-03-24 19:54:08 +02:00
|
|
|
_, provider := metrictest.NewProvider()
|
|
|
|
global.SetMeterProvider(provider)
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
// Repeat the above tests
|
|
|
|
|
|
|
|
// Sync
|
|
|
|
ival = counter.Measurement(1).SyncImpl().Implementation()
|
|
|
|
require.NotNil(t, ival)
|
|
|
|
|
|
|
|
_, ok = ival.(*metrictest.Sync)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// Async
|
2020-05-18 20:03:43 +02:00
|
|
|
ival = valueobserver.AsyncImpl().Implementation()
|
2020-03-19 21:02:46 +02:00
|
|
|
require.NotNil(t, ival)
|
|
|
|
|
|
|
|
_, ok = ival.(*metrictest.Async)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
2020-04-30 01:13:55 +02:00
|
|
|
|
|
|
|
func TestRecordBatchMock(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
meter := global.MeterProvider().Meter("builtin")
|
|
|
|
|
|
|
|
counter := Must(meter).NewInt64Counter("test.counter")
|
|
|
|
|
|
|
|
meter.RecordBatch(context.Background(), nil, counter.Measurement(1))
|
|
|
|
|
|
|
|
mock, provider := metrictest.NewProvider()
|
|
|
|
global.SetMeterProvider(provider)
|
|
|
|
|
|
|
|
meter.RecordBatch(context.Background(), nil, counter.Measurement(1))
|
|
|
|
|
|
|
|
require.EqualValues(t,
|
|
|
|
[]measured{
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.counter",
|
|
|
|
InstrumentationName: "builtin",
|
|
|
|
Labels: asMap(),
|
|
|
|
Number: asInt(1),
|
2020-04-30 01:13:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
asStructs(mock.MeasurementBatches))
|
|
|
|
}
|