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-11-15 23:01:20 +02:00
|
|
|
package stdout_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-06-13 09:55:01 +02:00
|
|
|
"fmt"
|
2019-11-15 23:01:20 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-15 23:01:20 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout"
|
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"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2019-11-15 23:01:20 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-07-25 05:32:52 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/metrictest"
|
2020-07-28 23:31:56 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/aggregatortest"
|
2020-03-11 01:00:37 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
|
2019-11-27 00:07:58 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
|
2020-03-12 05:21:34 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2020-04-24 18:44:21 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testFixture struct {
|
|
|
|
t *testing.T
|
|
|
|
ctx context.Context
|
|
|
|
exporter *stdout.Exporter
|
|
|
|
output *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:16:55 +02:00
|
|
|
var testResource = resource.NewWithAttributes(label.String("R", "V"))
|
2020-05-19 02:44:28 +02:00
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
func newFixture(t *testing.T, opts ...stdout.Option) testFixture {
|
2019-11-15 23:01:20 +02:00
|
|
|
buf := &bytes.Buffer{}
|
2020-07-22 21:34:44 +02:00
|
|
|
opts = append(opts, stdout.WithWriter(buf))
|
|
|
|
opts = append(opts, stdout.WithoutTimestamps())
|
|
|
|
exp, err := stdout.NewExporter(opts...)
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error building fixture: ", err)
|
|
|
|
}
|
|
|
|
return testFixture{
|
|
|
|
t: t,
|
|
|
|
ctx: context.Background(),
|
|
|
|
exporter: exp,
|
|
|
|
output: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fix testFixture) Output() string {
|
|
|
|
return strings.TrimSpace(fix.output.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fix testFixture) Export(checkpointSet export.CheckpointSet) {
|
2020-05-19 02:44:28 +02:00
|
|
|
err := fix.exporter.Export(fix.ctx, checkpointSet)
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
|
|
|
fix.t.Error("export failed: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutTimestamp(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
2020-07-22 21:34:44 +02:00
|
|
|
exporter, err := stdout.NewExporter(
|
|
|
|
stdout.WithWriter(&buf),
|
|
|
|
)
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
2020-01-03 19:48:45 +02:00
|
|
|
t.Fatal("Invalid config: ", err)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
before := time.Now()
|
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueObserverInstrumentKind, number.Int64Kind)
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
lvagg, ckpt := metrictest.Unslice2(lastvalue.New(2))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
aggregatortest.CheckedUpdate(t, lvagg, number.NewInt64Number(321), &desc)
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, lvagg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-19 02:44:28 +02:00
|
|
|
if err := exporter.Export(ctx, checkpointSet); err != nil {
|
2019-11-15 23:01:20 +02:00
|
|
|
t.Fatal("Unexpected export error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
after := time.Now()
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
var printed []interface{}
|
2019-11-15 23:01:20 +02:00
|
|
|
if err := json.Unmarshal(buf.Bytes(), &printed); err != nil {
|
|
|
|
t.Fatal("JSON parse error: ", err)
|
|
|
|
}
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Len(t, printed, 1)
|
|
|
|
lastValue, ok := printed[0].(map[string]interface{})
|
|
|
|
require.True(t, ok, "last value format")
|
|
|
|
require.Contains(t, lastValue, "Timestamp")
|
|
|
|
lastValueTS := lastValue["Timestamp"].(string)
|
2020-03-11 01:00:37 +02:00
|
|
|
lastValueTimestamp, err := time.Parse(time.RFC3339Nano, lastValueTS)
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
2020-03-11 01:00:37 +02:00
|
|
|
t.Fatal("JSON parse error: ", lastValueTS, ": ", err)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
assert.True(t, lastValueTimestamp.After(before))
|
|
|
|
assert.True(t, lastValueTimestamp.Before(after))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutCounterFormat(t *testing.T) {
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.CounterInstrumentKind, number.Int64Kind)
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
cagg, ckpt := metrictest.Unslice2(sum.New(2))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
aggregatortest.CheckedUpdate(fix.t, cagg, number.NewInt64Number(123), &desc)
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, cagg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt, label.String("A", "B"), label.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, `[{"Name":"test.name{R=V,A=B,C=D}","Sum":123}]`, fix.Output())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestStdoutLastValueFormat(t *testing.T) {
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueObserverInstrumentKind, number.Float64Kind)
|
2020-07-25 05:32:52 +02:00
|
|
|
lvagg, ckpt := metrictest.Unslice2(lastvalue.New(2))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
aggregatortest.CheckedUpdate(fix.t, lvagg, number.NewFloat64Number(123.456), &desc)
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, lvagg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt, label.String("A", "B"), label.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, `[{"Name":"test.name{R=V,A=B,C=D}","Last":123.456}]`, fix.Output())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 00:07:58 +02:00
|
|
|
func TestStdoutMinMaxSumCount(t *testing.T) {
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueRecorderInstrumentKind, number.Float64Kind)
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
magg, ckpt := metrictest.Unslice2(minmaxsumcount.New(2, &desc))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
aggregatortest.CheckedUpdate(fix.t, magg, number.NewFloat64Number(123.456), &desc)
|
|
|
|
aggregatortest.CheckedUpdate(fix.t, magg, number.NewFloat64Number(876.543), &desc)
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, magg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt, label.String("A", "B"), label.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, `[{"Name":"test.name{R=V,A=B,C=D}","Min":123.456,"Max":876.543,"Sum":999.999,"Count":2}]`, fix.Output())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
func TestStdoutValueRecorderFormat(t *testing.T) {
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t, stdout.WithPrettyPrint())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueRecorderInstrumentKind, number.Float64Kind)
|
2021-01-12 20:19:13 +02:00
|
|
|
aagg, ckpt := metrictest.Unslice2(minmaxsumcount.New(2, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2020-11-11 17:24:12 +02:00
|
|
|
aggregatortest.CheckedUpdate(fix.t, aagg, number.NewFloat64Number(float64(i)+0.5), &desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, aagg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt, label.String("A", "B"), label.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, `[
|
|
|
|
{
|
|
|
|
"Name": "test.name{R=V,A=B,C=D}",
|
|
|
|
"Min": 0.5,
|
|
|
|
"Max": 999.5,
|
|
|
|
"Sum": 500000,
|
2021-01-12 20:19:13 +02:00
|
|
|
"Count": 1000
|
2020-07-22 21:34:44 +02:00
|
|
|
}
|
|
|
|
]`, fix.Output())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 01:28:33 +02:00
|
|
|
func TestStdoutNoData(t *testing.T) {
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueRecorderInstrumentKind, number.Float64Kind)
|
2020-06-13 09:55:01 +02:00
|
|
|
|
|
|
|
runTwoAggs := func(agg, ckpt export.Aggregator) {
|
|
|
|
t.Run(fmt.Sprintf("%T", agg), func(t *testing.T) {
|
2019-11-25 19:51:49 +02:00
|
|
|
t.Parallel()
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, agg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, "", fix.Output())
|
2019-11-25 19:51:49 +02:00
|
|
|
})
|
|
|
|
}
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2021-01-12 20:19:13 +02:00
|
|
|
runTwoAggs(metrictest.Unslice2(lastvalue.New(2)))
|
2020-07-25 05:32:52 +02:00
|
|
|
runTwoAggs(metrictest.Unslice2(minmaxsumcount.New(2, &desc)))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestStdoutLastValueNotSet(t *testing.T) {
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(testResource)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueObserverInstrumentKind, number.Float64Kind)
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
lvagg, ckpt := metrictest.Unslice2(lastvalue.New(2))
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, lvagg.SynchronizedMove(ckpt, &desc))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
checkpointSet.Add(&desc, lvagg, label.String("A", "B"), label.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, "", fix.Output())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
2020-04-24 18:44:21 +02:00
|
|
|
|
|
|
|
func TestStdoutResource(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
expect string
|
|
|
|
res *resource.Resource
|
2020-08-18 05:25:03 +02:00
|
|
|
attrs []label.KeyValue
|
2020-04-24 18:44:21 +02:00
|
|
|
}
|
2020-08-18 05:25:03 +02:00
|
|
|
newCase := func(expect string, res *resource.Resource, attrs ...label.KeyValue) testCase {
|
2020-04-24 18:44:21 +02:00
|
|
|
return testCase{
|
|
|
|
expect: expect,
|
|
|
|
res: res,
|
|
|
|
attrs: attrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testCases := []testCase{
|
|
|
|
newCase("R1=V1,R2=V2,A=B,C=D",
|
2020-10-31 20:16:55 +02:00
|
|
|
resource.NewWithAttributes(label.String("R1", "V1"), label.String("R2", "V2")),
|
2020-08-18 05:25:03 +02:00
|
|
|
label.String("A", "B"),
|
|
|
|
label.String("C", "D")),
|
2020-04-24 18:44:21 +02:00
|
|
|
newCase("R1=V1,R2=V2",
|
2020-10-31 20:16:55 +02:00
|
|
|
resource.NewWithAttributes(label.String("R1", "V1"), label.String("R2", "V2")),
|
2020-04-24 18:44:21 +02:00
|
|
|
),
|
|
|
|
newCase("A=B,C=D",
|
|
|
|
nil,
|
2020-08-18 05:25:03 +02:00
|
|
|
label.String("A", "B"),
|
|
|
|
label.String("C", "D"),
|
2020-04-24 18:44:21 +02:00
|
|
|
),
|
|
|
|
// We explicitly do not de-duplicate between resources
|
|
|
|
// and metric labels in this exporter.
|
|
|
|
newCase("R1=V1,R2=V2,R1=V3,R2=V4",
|
2020-10-31 20:16:55 +02:00
|
|
|
resource.NewWithAttributes(label.String("R1", "V1"), label.String("R2", "V2")),
|
2020-08-18 05:25:03 +02:00
|
|
|
label.String("R1", "V3"),
|
|
|
|
label.String("R2", "V4")),
|
2020-04-24 18:44:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2020-07-22 21:34:44 +02:00
|
|
|
fix := newFixture(t)
|
2020-04-24 18:44:21 +02:00
|
|
|
|
2020-07-25 05:32:52 +02:00
|
|
|
checkpointSet := metrictest.NewCheckpointSet(tc.res)
|
2020-04-24 18:44:21 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
desc := metric.NewDescriptor("test.name", metric.ValueObserverInstrumentKind, number.Float64Kind)
|
2020-07-25 05:32:52 +02:00
|
|
|
lvagg, ckpt := metrictest.Unslice2(lastvalue.New(2))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
aggregatortest.CheckedUpdate(fix.t, lvagg, number.NewFloat64Number(123.456), &desc)
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, lvagg.SynchronizedMove(ckpt, &desc))
|
2020-04-24 18:44:21 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
checkpointSet.Add(&desc, ckpt, tc.attrs...)
|
2020-04-24 18:44:21 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, `[{"Name":"test.name{`+tc.expect+`}","Last":123.456}]`, fix.Output())
|
2020-04-24 18:44:21 +02:00
|
|
|
}
|
|
|
|
}
|