2019-11-15 23:01:20 +02:00
|
|
|
package stdout_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/api/key"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/stdout"
|
|
|
|
"go.opentelemetry.io/otel/exporters/metric/test"
|
2019-11-15 23:01:20 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
|
|
|
sdk "go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/array"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/counter"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/ddsketch"
|
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"
|
2019-11-15 23:01:20 +02:00
|
|
|
aggtest "go.opentelemetry.io/otel/sdk/metric/aggregator/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testFixture struct {
|
|
|
|
t *testing.T
|
|
|
|
ctx context.Context
|
|
|
|
exporter *stdout.Exporter
|
|
|
|
output *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:48:45 +02:00
|
|
|
func newFixture(t *testing.T, config stdout.Config) testFixture {
|
2019-11-15 23:01:20 +02:00
|
|
|
buf := &bytes.Buffer{}
|
2020-01-03 19:48:45 +02:00
|
|
|
config.Writer = buf
|
|
|
|
config.DoNotPrintTime = true
|
|
|
|
exp, err := stdout.NewRawExporter(config)
|
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) {
|
|
|
|
err := fix.exporter.Export(fix.ctx, checkpointSet)
|
|
|
|
if err != nil {
|
|
|
|
fix.t.Error("export failed: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutInvalidQuantile(t *testing.T) {
|
2020-01-03 19:48:45 +02:00
|
|
|
_, err := stdout.NewRawExporter(stdout.Config{
|
2019-11-15 23:01:20 +02:00
|
|
|
Quantiles: []float64{1.1, 0.9},
|
|
|
|
})
|
|
|
|
require.Error(t, err, "Invalid quantile error expected")
|
|
|
|
require.Equal(t, aggregator.ErrInvalidQuantile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutTimestamp(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
2020-01-03 19:48:45 +02:00
|
|
|
exporter, err := stdout.NewRawExporter(stdout.Config{
|
2020-01-02 18:40:37 +02:00
|
|
|
Writer: &buf,
|
2019-11-15 23:01:20 +02:00
|
|
|
DoNotPrintTime: false,
|
|
|
|
})
|
|
|
|
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()
|
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-03-11 01:00:37 +02:00
|
|
|
desc := export.NewDescriptor("test.name", export.ObserverKind, nil, "", "", core.Int64NumberKind, false)
|
|
|
|
lvagg := lastvalue.New()
|
|
|
|
aggtest.CheckedUpdate(t, lvagg, core.NewInt64Number(321), desc)
|
|
|
|
lvagg.Checkpoint(ctx, desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
checkpointSet.Add(desc, lvagg)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
if err := exporter.Export(ctx, checkpointSet); err != nil {
|
|
|
|
t.Fatal("Unexpected export error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
after := time.Now()
|
|
|
|
|
|
|
|
var printed map[string]interface{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(buf.Bytes(), &printed); err != nil {
|
|
|
|
t.Fatal("JSON parse error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTS := printed["time"].(string)
|
|
|
|
updateTimestamp, err := time.Parse(time.RFC3339Nano, updateTS)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("JSON parse error: ", updateTS, ": ", err)
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
lastValueTS := printed["updates"].([]interface{})[0].(map[string]interface{})["time"].(string)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, updateTimestamp.After(before))
|
|
|
|
require.True(t, updateTimestamp.Before(after))
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
require.True(t, lastValueTimestamp.After(before))
|
|
|
|
require.True(t, lastValueTimestamp.Before(after))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
require.True(t, lastValueTimestamp.Before(updateTimestamp))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutCounterFormat(t *testing.T) {
|
2020-01-03 19:48:45 +02:00
|
|
|
fix := newFixture(t, stdout.Config{})
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
desc := export.NewDescriptor("test.name", export.CounterKind, nil, "", "", core.Int64NumberKind, false)
|
|
|
|
cagg := counter.New()
|
|
|
|
aggtest.CheckedUpdate(fix.t, cagg, core.NewInt64Number(123), desc)
|
|
|
|
cagg.Checkpoint(fix.ctx, desc)
|
|
|
|
|
|
|
|
checkpointSet.Add(desc, cagg, key.String("A", "B"), key.String("C", "D"))
|
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
|
|
|
require.Equal(t, `{"updates":[{"name":"test.name{A=B,C=D}","sum":123}]}`, fix.Output())
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestStdoutLastValueFormat(t *testing.T) {
|
2020-01-03 19:48:45 +02:00
|
|
|
fix := newFixture(t, stdout.Config{})
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
desc := export.NewDescriptor("test.name", export.ObserverKind, nil, "", "", core.Float64NumberKind, false)
|
|
|
|
lvagg := lastvalue.New()
|
|
|
|
aggtest.CheckedUpdate(fix.t, lvagg, core.NewFloat64Number(123.456), desc)
|
|
|
|
lvagg.Checkpoint(fix.ctx, desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
checkpointSet.Add(desc, lvagg, key.String("A", "B"), key.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
|
|
|
require.Equal(t, `{"updates":[{"name":"test.name{A=B,C=D}","last":123.456}]}`, fix.Output())
|
|
|
|
}
|
|
|
|
|
2019-11-27 00:07:58 +02:00
|
|
|
func TestStdoutMinMaxSumCount(t *testing.T) {
|
2020-01-03 19:48:45 +02:00
|
|
|
fix := newFixture(t, stdout.Config{})
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
desc := export.NewDescriptor("test.name", export.MeasureKind, nil, "", "", core.Float64NumberKind, false)
|
2019-11-27 00:07:58 +02:00
|
|
|
magg := minmaxsumcount.New(desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
aggtest.CheckedUpdate(fix.t, magg, core.NewFloat64Number(123.456), desc)
|
|
|
|
aggtest.CheckedUpdate(fix.t, magg, core.NewFloat64Number(876.543), desc)
|
|
|
|
magg.Checkpoint(fix.ctx, desc)
|
|
|
|
|
|
|
|
checkpointSet.Add(desc, magg, key.String("A", "B"), key.String("C", "D"))
|
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
2019-11-27 00:07:58 +02:00
|
|
|
require.Equal(t, `{"updates":[{"name":"test.name{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
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutMeasureFormat(t *testing.T) {
|
2020-01-03 19:48:45 +02:00
|
|
|
fix := newFixture(t, stdout.Config{
|
2019-11-15 23:01:20 +02:00
|
|
|
PrettyPrint: true,
|
|
|
|
})
|
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
desc := export.NewDescriptor("test.name", export.MeasureKind, nil, "", "", core.Float64NumberKind, false)
|
|
|
|
magg := array.New()
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
aggtest.CheckedUpdate(fix.t, magg, core.NewFloat64Number(float64(i)+0.5), desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
magg.Checkpoint(fix.ctx, desc)
|
|
|
|
|
|
|
|
checkpointSet.Add(desc, magg, key.String("A", "B"), key.String("C", "D"))
|
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
|
|
|
require.Equal(t, `{
|
|
|
|
"updates": [
|
|
|
|
{
|
|
|
|
"name": "test.name{A=B,C=D}",
|
2019-11-27 00:07:58 +02:00
|
|
|
"min": 0.5,
|
2019-11-15 23:01:20 +02:00
|
|
|
"max": 999.5,
|
|
|
|
"sum": 500000,
|
|
|
|
"count": 1000,
|
|
|
|
"quantiles": [
|
|
|
|
{
|
|
|
|
"q": 0.5,
|
|
|
|
"v": 500.5
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"q": 0.9,
|
|
|
|
"v": 900.5
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"q": 0.99,
|
|
|
|
"v": 990.5
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`, fix.Output())
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
func TestStdoutEmptyDataSet(t *testing.T) {
|
|
|
|
desc := export.NewDescriptor("test.name", export.MeasureKind, nil, "", "", core.Float64NumberKind, false)
|
|
|
|
for name, tc := range map[string]export.Aggregator{
|
2019-11-27 00:07:58 +02:00
|
|
|
"ddsketch": ddsketch.New(ddsketch.NewDefaultConfig(), desc),
|
|
|
|
"minmaxsumcount": minmaxsumcount.New(desc),
|
2019-11-25 19:51:49 +02:00
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-01-03 19:48:45 +02:00
|
|
|
fix := newFixture(t, stdout.Config{})
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
magg := tc
|
|
|
|
magg.Checkpoint(fix.ctx, desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
checkpointSet.Add(desc, magg)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
|
|
|
require.Equal(t, `{"updates":null}`, fix.Output())
|
|
|
|
})
|
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestStdoutLastValueNotSet(t *testing.T) {
|
2020-01-03 19:48:45 +02:00
|
|
|
fix := newFixture(t, stdout.Config{})
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-11-22 06:46:05 +02:00
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
desc := export.NewDescriptor("test.name", export.ObserverKind, nil, "", "", core.Float64NumberKind, false)
|
|
|
|
lvagg := lastvalue.New()
|
|
|
|
lvagg.Checkpoint(fix.ctx, desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
checkpointSet.Add(desc, lvagg, key.String("A", "B"), key.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
|
|
|
require.Equal(t, `{"updates":null}`, fix.Output())
|
|
|
|
}
|
2020-01-24 15:51:58 +02:00
|
|
|
|
|
|
|
func TestStdoutCounterWithUnspecifiedKeys(t *testing.T) {
|
|
|
|
fix := newFixture(t, stdout.Config{})
|
|
|
|
|
|
|
|
checkpointSet := test.NewCheckpointSet(sdk.NewDefaultLabelEncoder())
|
|
|
|
|
|
|
|
keys := []core.Key{key.New("C"), key.New("D")}
|
|
|
|
|
|
|
|
desc := export.NewDescriptor("test.name", export.CounterKind, keys, "", "", core.Int64NumberKind, false)
|
|
|
|
cagg := counter.New()
|
|
|
|
aggtest.CheckedUpdate(fix.t, cagg, core.NewInt64Number(10), desc)
|
|
|
|
cagg.Checkpoint(fix.ctx, desc)
|
|
|
|
|
|
|
|
checkpointSet.Add(desc, cagg, key.String("A", "B"))
|
|
|
|
|
|
|
|
fix.Export(checkpointSet)
|
|
|
|
|
|
|
|
require.Equal(t, `{"updates":[{"name":"test.name{A=B,C,D}","sum":10}]}`, fix.Output())
|
|
|
|
}
|