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.
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
package global_test
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel/internal/global"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal "go.opentelemetry.io/otel/metric/global"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2020-10-17 18:48:21 +02:00
|
|
|
"go.opentelemetry.io/otel/oteltest"
|
2019-12-24 09:03:04 +02:00
|
|
|
)
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
var Must = metric.Must
|
2020-07-28 19:47:08 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
var asInt = number.NewInt64Number
|
|
|
|
var asFloat = number.NewFloat64Number
|
2020-03-24 19:54:08 +02:00
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
func TestDirect(t *testing.T) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-02-12 18:23:51 +02:00
|
|
|
meter1 := metricglobal.Meter("test1", metric.WithInstrumentationVersion("semver:v1.0.0"))
|
|
|
|
meter2 := metricglobal.Meter("test2")
|
2020-08-18 05:25:03 +02:00
|
|
|
labels1 := []label.KeyValue{label.String("A", "B")}
|
|
|
|
labels2 := []label.KeyValue{label.String("C", "D")}
|
|
|
|
labels3 := []label.KeyValue{label.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-11-12 17:28:32 +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-11-12 17:28:32 +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-10-17 18:48:21 +02:00
|
|
|
mock, provider := oteltest.NewMeterProvider()
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal.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
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
measurements := oteltest.AsStructs(mock.MeasurementBatches)
|
2020-03-24 19:54:08 +02:00
|
|
|
|
|
|
|
require.EqualValues(t,
|
2020-10-17 18:48:21 +02:00
|
|
|
[]oteltest.Measured{
|
2020-03-24 19:54:08 +02:00
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.counter",
|
|
|
|
InstrumentationName: "test1",
|
|
|
|
InstrumentationVersion: "semver:v1.0.0",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels1...),
|
2020-06-12 18:11:17 +02:00
|
|
|
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",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels1...),
|
2020-06-12 18:11:17 +02:00
|
|
|
Number: asFloat(3),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.second",
|
|
|
|
InstrumentationName: "test2",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels3...),
|
2020-06-12 18:11:17 +02:00
|
|
|
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",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels1...),
|
2020-06-12 18:11:17 +02:00
|
|
|
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",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels2...),
|
2020-06-12 18:11:17 +02:00
|
|
|
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",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels1...),
|
2020-06-12 18:11:17 +02:00
|
|
|
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",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels2...),
|
2020-06-12 18:11:17 +02:00
|
|
|
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) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2019-12-24 09:03:04 +02:00
|
|
|
|
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()
|
2021-02-12 18:23:51 +02:00
|
|
|
glob := metricglobal.Meter("test")
|
2020-08-18 05:25:03 +02:00
|
|
|
labels1 := []label.KeyValue{label.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-10-17 18:48:21 +02:00
|
|
|
mock, provider := oteltest.NewMeterProvider()
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal.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,
|
2020-10-17 18:48:21 +02:00
|
|
|
[]oteltest.Measured{
|
2020-03-24 19:54:08 +02:00
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.counter",
|
|
|
|
InstrumentationName: "test",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels1...),
|
2020-06-12 18:11:17 +02:00
|
|
|
Number: asFloat(1),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.valuerecorder",
|
|
|
|
InstrumentationName: "test",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(labels1...),
|
2020-06-12 18:11:17 +02:00
|
|
|
Number: asInt(3),
|
2020-03-24 19:54:08 +02:00
|
|
|
},
|
|
|
|
},
|
2020-10-17 18:48:21 +02:00
|
|
|
oteltest.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.
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2021-02-12 18:23:51 +02:00
|
|
|
glob := metricglobal.Meter("test")
|
2020-08-18 05:25:03 +02:00
|
|
|
labels1 := []label.KeyValue{label.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) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2020-02-20 23:05:19 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-10-17 18:48:21 +02:00
|
|
|
mock, provider := oteltest.NewMeterProvider()
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2021-02-12 18:23:51 +02:00
|
|
|
meter := metricglobal.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()
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal.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 {
|
2020-11-12 17:28:32 +02:00
|
|
|
metric.MeterProvider
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type meterWithConstructorError struct {
|
2020-11-12 17:28:32 +02:00
|
|
|
metric.MeterImpl
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
func (m *meterProviderWithConstructorError) Meter(iName string, opts ...metric.MeterOption) metric.Meter {
|
|
|
|
return metric.WrapMeterImpl(&meterWithConstructorError{m.MeterProvider.Meter(iName, opts...).MeterImpl()}, iName, opts...)
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 17:28:32 +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) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2020-03-11 20:57:57 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-02-12 18:23:51 +02:00
|
|
|
meter := metricglobal.GetMeterProvider().Meter("builtin")
|
2020-03-11 20:57:57 +02:00
|
|
|
|
|
|
|
c1 := Must(meter).NewInt64Counter("test")
|
|
|
|
c2 := Must(meter).NewInt64Counter("test")
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
_, provider := oteltest.NewMeterProvider()
|
2020-03-24 19:54:08 +02:00
|
|
|
sdk := &meterProviderWithConstructorError{provider}
|
2020-03-11 20:57:57 +02:00
|
|
|
|
|
|
|
require.Panics(t, func() {
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal.SetMeterProvider(sdk)
|
2020-03-11 20:57:57 +02:00
|
|
|
})
|
|
|
|
|
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) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
// Test that Implementation() does the proper indirection, i.e.,
|
|
|
|
// returns the implementation interface not the global, after
|
|
|
|
// registered.
|
|
|
|
|
2021-02-12 18:23:51 +02:00
|
|
|
meter1 := metricglobal.Meter("test1")
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
// Sync: no SDK yet
|
|
|
|
counter := Must(meter1).NewInt64Counter("interface.counter")
|
|
|
|
|
|
|
|
ival := counter.Measurement(1).SyncImpl().Implementation()
|
|
|
|
require.NotNil(t, ival)
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
_, ok := ival.(*oteltest.Sync)
|
2020-03-19 21:02:46 +02:00
|
|
|
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-11-12 17:28:32 +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)
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
_, ok = ival.(*oteltest.Async)
|
2020-03-19 21:02:46 +02:00
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
// Register the SDK
|
2020-10-17 18:48:21 +02:00
|
|
|
_, provider := oteltest.NewMeterProvider()
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal.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)
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
_, ok = ival.(*oteltest.Sync)
|
2020-03-19 21:02:46 +02:00
|
|
|
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)
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
_, ok = ival.(*oteltest.Async)
|
2020-03-19 21:02:46 +02:00
|
|
|
require.True(t, ok)
|
|
|
|
}
|
2020-04-30 01:13:55 +02:00
|
|
|
|
|
|
|
func TestRecordBatchMock(t *testing.T) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2020-04-30 01:13:55 +02:00
|
|
|
|
2021-02-12 18:23:51 +02:00
|
|
|
meter := metricglobal.GetMeterProvider().Meter("builtin")
|
2020-04-30 01:13:55 +02:00
|
|
|
|
|
|
|
counter := Must(meter).NewInt64Counter("test.counter")
|
|
|
|
|
|
|
|
meter.RecordBatch(context.Background(), nil, counter.Measurement(1))
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
mock, provider := oteltest.NewMeterProvider()
|
2021-02-12 18:23:51 +02:00
|
|
|
metricglobal.SetMeterProvider(provider)
|
2020-04-30 01:13:55 +02:00
|
|
|
|
|
|
|
meter.RecordBatch(context.Background(), nil, counter.Measurement(1))
|
|
|
|
|
|
|
|
require.EqualValues(t,
|
2020-10-17 18:48:21 +02:00
|
|
|
[]oteltest.Measured{
|
2020-04-30 01:13:55 +02:00
|
|
|
{
|
2020-06-12 18:11:17 +02:00
|
|
|
Name: "test.counter",
|
|
|
|
InstrumentationName: "builtin",
|
2020-10-17 18:48:21 +02:00
|
|
|
Labels: oteltest.LabelsToMap(),
|
2020-06-12 18:11:17 +02:00
|
|
|
Number: asInt(1),
|
2020-04-30 01:13:55 +02:00
|
|
|
},
|
|
|
|
},
|
2020-10-17 18:48:21 +02:00
|
|
|
oteltest.AsStructs(mock.MeasurementBatches))
|
2020-04-30 01:13:55 +02:00
|
|
|
}
|