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 (
|
2020-04-30 01:13:55 +02:00
|
|
|
"bytes"
|
2019-12-24 09:03:04 +02:00
|
|
|
"context"
|
2020-03-11 20:57:57 +02:00
|
|
|
"errors"
|
2019-12-24 09:03:04 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
|
|
"go.opentelemetry.io/otel/api/global/internal"
|
|
|
|
"go.opentelemetry.io/otel/api/key"
|
2020-03-05 22:15:30 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/stdout"
|
2019-12-24 09:03:04 +02:00
|
|
|
metrictest "go.opentelemetry.io/otel/internal/metric"
|
|
|
|
)
|
|
|
|
|
2020-03-24 19:54:08 +02:00
|
|
|
// Note: Maybe this should be factored into ../../../internal/metric?
|
|
|
|
type measured struct {
|
|
|
|
Name string
|
|
|
|
LibraryName string
|
|
|
|
Labels map[core.Key]core.Value
|
|
|
|
Number core.Number
|
|
|
|
}
|
|
|
|
|
|
|
|
func asStructs(batches []metrictest.Batch) []measured {
|
|
|
|
var r []measured
|
|
|
|
for _, batch := range batches {
|
|
|
|
for _, m := range batch.Measurements {
|
|
|
|
r = append(r, measured{
|
|
|
|
Name: m.Instrument.Descriptor().Name(),
|
|
|
|
LibraryName: m.Instrument.Descriptor().LibraryName(),
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(batch.Labels...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: m.Number,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func asMap(kvs ...core.KeyValue) map[core.Key]core.Value {
|
|
|
|
m := map[core.Key]core.Value{}
|
|
|
|
for _, kv := range kvs {
|
|
|
|
m[kv.Key] = kv.Value
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
var asInt = core.NewInt64Number
|
|
|
|
var asFloat = core.NewFloat64Number
|
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
func TestDirect(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-03-11 17:23:32 +02:00
|
|
|
meter1 := global.Meter("test1")
|
|
|
|
meter2 := global.Meter("test2")
|
2020-03-27 23:06:48 +02:00
|
|
|
labels1 := []core.KeyValue{key.String("A", "B")}
|
|
|
|
labels2 := []core.KeyValue{key.String("C", "D")}
|
|
|
|
labels3 := []core.KeyValue{key.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-03-11 20:57:57 +02:00
|
|
|
measure := Must(meter1).NewFloat64Measure("test.measure")
|
2020-03-27 23:06:48 +02:00
|
|
|
measure.Record(ctx, 1, labels1...)
|
|
|
|
measure.Record(ctx, 2, labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
_ = Must(meter1).RegisterFloat64Observer("test.observer.float", func(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-03-11 20:57:57 +02:00
|
|
|
_ = Must(meter1).RegisterInt64Observer("test.observer.int", func(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-03-11 20:57:57 +02:00
|
|
|
second := Must(meter2).NewFloat64Measure("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...)
|
|
|
|
measure.Record(ctx, 3, labels1...)
|
|
|
|
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{
|
|
|
|
{
|
|
|
|
Name: "test.counter",
|
|
|
|
LibraryName: "test1",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels1...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.measure",
|
|
|
|
LibraryName: "test1",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels1...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asFloat(3),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.second",
|
|
|
|
LibraryName: "test2",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels3...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asFloat(3),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.observer.float",
|
|
|
|
LibraryName: "test1",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels1...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asFloat(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.observer.float",
|
|
|
|
LibraryName: "test1",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels2...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asFloat(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.observer.int",
|
|
|
|
LibraryName: "test1",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels1...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.observer.int",
|
|
|
|
LibraryName: "test1",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels2...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asInt(2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-03-27 23:06:48 +02:00
|
|
|
labels1 := []core.KeyValue{key.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-03-11 20:57:57 +02:00
|
|
|
measure := Must(glob).NewInt64Measure("test.measure")
|
2020-03-27 23:06:48 +02:00
|
|
|
boundM := measure.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{
|
|
|
|
{
|
|
|
|
Name: "test.counter",
|
|
|
|
LibraryName: "test",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels1...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asFloat(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test.measure",
|
|
|
|
LibraryName: "test",
|
2020-03-27 23:06:48 +02:00
|
|
|
Labels: asMap(labels1...),
|
2020-03-24 19:54:08 +02:00
|
|
|
Number: asInt(3),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-03-27 23:06:48 +02:00
|
|
|
labels1 := []core.KeyValue{key.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-03-11 20:57:57 +02:00
|
|
|
measure := Must(glob).NewInt64Measure("test.measure")
|
2020-03-27 23:06:48 +02:00
|
|
|
boundM := measure.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
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultSDK(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-03-11 17:23:32 +02:00
|
|
|
meter1 := global.Meter("builtin")
|
2020-03-27 23:06:48 +02:00
|
|
|
labels1 := []core.KeyValue{key.String("A", "B")}
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
counter := Must(meter1).NewInt64Counter("test.builtin")
|
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
|
|
|
|
|
|
|
in, out := io.Pipe()
|
2020-01-03 19:48:45 +02:00
|
|
|
pusher, err := stdout.InstallNewPipeline(stdout.Config{
|
2020-01-02 18:40:37 +02:00
|
|
|
Writer: out,
|
2019-12-24 09:03:04 +02:00
|
|
|
DoNotPrintTime: true,
|
|
|
|
})
|
2020-01-02 20:41:21 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-24 09:03:04 +02:00
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1, labels1...)
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
ch := make(chan string)
|
|
|
|
go func() {
|
|
|
|
data, _ := ioutil.ReadAll(in)
|
|
|
|
ch <- string(data)
|
|
|
|
}()
|
|
|
|
|
2020-01-02 20:41:21 +02:00
|
|
|
pusher.Stop()
|
2019-12-24 09:03:04 +02:00
|
|
|
out.Close()
|
|
|
|
|
2020-01-24 15:51:58 +02:00
|
|
|
require.Equal(t, `{"updates":[{"name":"test.builtin{A=B}","sum":1}]}
|
2019-12-24 09:03:04 +02:00
|
|
|
`, <-ch)
|
|
|
|
}
|
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 {
|
|
|
|
metric.Meter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *meterProviderWithConstructorError) Meter(name string) metric.Meter {
|
|
|
|
return &meterWithConstructorError{m.Provider.Meter(name)}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func (m *meterWithConstructorError) NewInt64Counter(name string, opts ...metric.Option) (metric.Int64Counter, error) {
|
2020-03-11 20:57:57 +02:00
|
|
|
return metric.Int64Counter{}, errors.New("constructor error")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
observer := Must(meter1).RegisterFloat64Observer(
|
|
|
|
"interface.observer",
|
|
|
|
func(result metric.Float64ObserverResult) {},
|
|
|
|
)
|
|
|
|
|
|
|
|
ival = observer.AsyncImpl().Implementation()
|
|
|
|
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
|
|
|
|
ival = observer.AsyncImpl().Implementation()
|
|
|
|
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{
|
|
|
|
{
|
|
|
|
Name: "test.counter",
|
|
|
|
LibraryName: "builtin",
|
|
|
|
Labels: asMap(),
|
|
|
|
Number: asInt(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
asStructs(mock.MeasurementBatches))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordBatchRealSDK(t *testing.T) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
|
|
|
|
meter := global.MeterProvider().Meter("builtin")
|
|
|
|
|
|
|
|
counter := Must(meter).NewInt64Counter("test.counter")
|
|
|
|
|
|
|
|
meter.RecordBatch(context.Background(), nil, counter.Measurement(1))
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
pusher, err := stdout.InstallNewPipeline(stdout.Config{
|
|
|
|
Writer: &buf,
|
|
|
|
DoNotPrintTime: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
global.SetMeterProvider(pusher)
|
|
|
|
|
|
|
|
meter.RecordBatch(context.Background(), nil, counter.Measurement(1))
|
|
|
|
pusher.Stop()
|
|
|
|
|
|
|
|
require.Equal(t, `{"updates":[{"name":"test.counter","sum":1}]}
|
|
|
|
`, buf.String())
|
|
|
|
}
|