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.
|
|
|
|
|
2021-06-16 17:32:42 +02:00
|
|
|
package stdoutmetric_test
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2021-11-13 18:35:04 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2021-07-21 19:06:38 +02:00
|
|
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
2021-12-13 22:13:03 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
|
2021-07-21 19:06:38 +02:00
|
|
|
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
|
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
|
2021-07-21 19:06:38 +02:00
|
|
|
cont *controller.Controller
|
|
|
|
meter metric.Meter
|
2021-06-16 17:32:42 +02:00
|
|
|
exporter *stdoutmetric.Exporter
|
2019-11-15 23:01:20 +02:00
|
|
|
output *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:46:42 +02:00
|
|
|
var testResource = resource.NewSchemaless(attribute.String("R", "V"))
|
2020-05-19 02:44:28 +02:00
|
|
|
|
2021-06-16 17:32:42 +02:00
|
|
|
func newFixture(t *testing.T, opts ...stdoutmetric.Option) testFixture {
|
2021-07-21 19:06:38 +02:00
|
|
|
return newFixtureWithResource(t, testResource, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFixtureWithResource(t *testing.T, res *resource.Resource, opts ...stdoutmetric.Option) testFixture {
|
2019-11-15 23:01:20 +02:00
|
|
|
buf := &bytes.Buffer{}
|
2021-06-16 17:32:42 +02:00
|
|
|
opts = append(opts, stdoutmetric.WithWriter(buf))
|
|
|
|
opts = append(opts, stdoutmetric.WithoutTimestamps())
|
|
|
|
exp, err := stdoutmetric.New(opts...)
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error building fixture: ", err)
|
|
|
|
}
|
2021-07-21 19:06:38 +02:00
|
|
|
aggSel := processortest.AggregatorSelector()
|
2021-10-15 20:18:36 +02:00
|
|
|
proc := processor.NewFactory(aggSel, aggregation.StatelessTemporalitySelector())
|
2021-07-21 19:06:38 +02:00
|
|
|
cont := controller.New(proc,
|
|
|
|
controller.WithExporter(exp),
|
|
|
|
controller.WithResource(res),
|
|
|
|
)
|
|
|
|
ctx := context.Background()
|
|
|
|
require.NoError(t, cont.Start(ctx))
|
2021-09-27 17:51:47 +02:00
|
|
|
meter := cont.Meter("test")
|
2021-07-21 19:06:38 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
return testFixture{
|
|
|
|
t: t,
|
2021-07-21 19:06:38 +02:00
|
|
|
ctx: ctx,
|
2019-11-15 23:01:20 +02:00
|
|
|
exporter: exp,
|
2021-07-21 19:06:38 +02:00
|
|
|
cont: cont,
|
|
|
|
meter: meter,
|
2019-11-15 23:01:20 +02:00
|
|
|
output: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fix testFixture) Output() string {
|
|
|
|
return strings.TrimSpace(fix.output.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdoutTimestamp(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
2021-07-21 19:06:38 +02:00
|
|
|
aggSel := processortest.AggregatorSelector()
|
2021-10-15 20:18:36 +02:00
|
|
|
proc := processor.NewFactory(aggSel, aggregation.CumulativeTemporalitySelector())
|
2021-06-16 17:32:42 +02:00
|
|
|
exporter, err := stdoutmetric.New(
|
|
|
|
stdoutmetric.WithWriter(&buf),
|
2020-07-22 21:34:44 +02:00
|
|
|
)
|
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
|
|
|
}
|
2021-07-21 19:06:38 +02:00
|
|
|
cont := controller.New(proc,
|
|
|
|
controller.WithExporter(exporter),
|
|
|
|
controller.WithResource(testResource),
|
|
|
|
)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
require.NoError(t, cont.Start(ctx))
|
2021-09-27 17:51:47 +02:00
|
|
|
meter := cont.Meter("test")
|
2022-03-02 17:50:29 +02:00
|
|
|
counter, err := meter.SyncInt64().Counter("name.lastvalue")
|
|
|
|
require.NoError(t, err)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
before := time.Now()
|
2021-02-23 21:38:23 +02:00
|
|
|
// Ensure the timestamp is after before.
|
|
|
|
time.Sleep(time.Nanosecond)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
counter.Add(ctx, 1)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.NoError(t, cont.Stop(ctx))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-02-23 21:38:23 +02:00
|
|
|
// Ensure the timestamp is before after.
|
|
|
|
time.Sleep(time.Nanosecond)
|
2019-11-15 23:01:20 +02:00
|
|
|
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
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
counter, err := fix.meter.SyncInt64().Counter("name.sum")
|
|
|
|
require.NoError(t, err)
|
2021-07-21 19:06:38 +02:00
|
|
|
counter.Add(fix.ctx, 123, attribute.String("A", "B"), attribute.String("C", "D"))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.NoError(t, fix.cont.Stop(fix.ctx))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, `[{"Name":"name.sum{R=V,instrumentation.name=test,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
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
counter, err := fix.meter.SyncFloat64().Counter("name.lastvalue")
|
|
|
|
require.NoError(t, err)
|
2021-07-21 19:06:38 +02:00
|
|
|
counter.Add(fix.ctx, 123.456, attribute.String("A", "B"), attribute.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.NoError(t, fix.cont.Stop(fix.ctx))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, `[{"Name":"name.lastvalue{R=V,instrumentation.name=test,A=B,C=D}","Last":123.456}]`, fix.Output())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
func TestStdoutHistogramFormat(t *testing.T) {
|
2021-06-16 17:32:42 +02:00
|
|
|
fix := newFixture(t, stdoutmetric.WithPrettyPrint())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
inst, err := fix.meter.SyncFloat64().Histogram("name.histogram")
|
|
|
|
require.NoError(t, err)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2021-07-21 19:06:38 +02:00
|
|
|
inst.Record(fix.ctx, float64(i)+0.5, attribute.String("A", "B"), attribute.String("C", "D"))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
2021-07-21 19:06:38 +02:00
|
|
|
require.NoError(t, fix.cont.Stop(fix.ctx))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
// TODO: Stdout does not export `Count` for histogram, nor the buckets.
|
2020-07-22 21:34:44 +02:00
|
|
|
require.Equal(t, `[
|
|
|
|
{
|
2021-07-21 19:06:38 +02:00
|
|
|
"Name": "name.histogram{R=V,instrumentation.name=test,A=B,C=D}",
|
|
|
|
"Sum": 500000
|
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) {
|
2021-07-21 19:06:38 +02:00
|
|
|
runTwoAggs := func(aggName string) {
|
|
|
|
t.Run(aggName, 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)
|
2022-03-02 17:50:29 +02:00
|
|
|
_, err := fix.meter.SyncFloat64().Counter(fmt.Sprint("name.", aggName))
|
|
|
|
require.NoError(t, err)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.NoError(t, fix.cont.Stop(fix.ctx))
|
2019-11-25 19:51:49 +02:00
|
|
|
|
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-07-21 19:06:38 +02:00
|
|
|
runTwoAggs("lastvalue")
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
2020-04-24 18:44:21 +02:00
|
|
|
|
|
|
|
func TestStdoutResource(t *testing.T) {
|
|
|
|
type testCase struct {
|
2021-08-13 00:44:58 +02:00
|
|
|
name string
|
2020-04-24 18:44:21 +02:00
|
|
|
expect string
|
|
|
|
res *resource.Resource
|
2021-02-18 19:59:37 +02:00
|
|
|
attrs []attribute.KeyValue
|
2020-04-24 18:44:21 +02:00
|
|
|
}
|
2021-08-13 00:44:58 +02:00
|
|
|
newCase := func(name, expect string, res *resource.Resource, attrs ...attribute.KeyValue) testCase {
|
2020-04-24 18:44:21 +02:00
|
|
|
return testCase{
|
2021-08-13 00:44:58 +02:00
|
|
|
name: name,
|
2020-04-24 18:44:21 +02:00
|
|
|
expect: expect,
|
|
|
|
res: res,
|
|
|
|
attrs: attrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testCases := []testCase{
|
2021-08-13 00:44:58 +02:00
|
|
|
newCase("resource and attribute",
|
|
|
|
"R1=V1,R2=V2,instrumentation.name=test,A=B,C=D",
|
2021-06-08 18:46:42 +02:00
|
|
|
resource.NewSchemaless(attribute.String("R1", "V1"), attribute.String("R2", "V2")),
|
2021-02-18 19:59:37 +02:00
|
|
|
attribute.String("A", "B"),
|
|
|
|
attribute.String("C", "D")),
|
2021-08-13 00:44:58 +02:00
|
|
|
newCase("only resource",
|
|
|
|
"R1=V1,R2=V2,instrumentation.name=test",
|
2021-06-08 18:46:42 +02:00
|
|
|
resource.NewSchemaless(attribute.String("R1", "V1"), attribute.String("R2", "V2")),
|
2020-04-24 18:44:21 +02:00
|
|
|
),
|
2021-08-13 00:44:58 +02:00
|
|
|
newCase("empty resource",
|
|
|
|
"instrumentation.name=test,A=B,C=D",
|
|
|
|
resource.Empty(),
|
|
|
|
attribute.String("A", "B"),
|
|
|
|
attribute.String("C", "D"),
|
|
|
|
),
|
|
|
|
newCase("default resource",
|
|
|
|
fmt.Sprint(resource.Default().Encoded(attribute.DefaultEncoder()),
|
|
|
|
",instrumentation.name=test,A=B,C=D"),
|
|
|
|
resource.Default(),
|
2021-02-18 19:59:37 +02:00
|
|
|
attribute.String("A", "B"),
|
|
|
|
attribute.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.
|
2021-08-13 00:44:58 +02:00
|
|
|
newCase("resource deduplication",
|
|
|
|
"R1=V1,R2=V2,instrumentation.name=test,R1=V3,R2=V4",
|
2021-06-08 18:46:42 +02:00
|
|
|
resource.NewSchemaless(attribute.String("R1", "V1"), attribute.String("R2", "V2")),
|
2021-02-18 19:59:37 +02:00
|
|
|
attribute.String("R1", "V3"),
|
|
|
|
attribute.String("R2", "V4")),
|
2020-04-24 18:44:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-08-13 00:44:58 +02:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixtureWithResource(t, tc.res)
|
2020-04-24 18:44:21 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
counter, err := fix.meter.SyncFloat64().Counter("name.lastvalue")
|
|
|
|
require.NoError(t, err)
|
2021-08-13 00:44:58 +02:00
|
|
|
counter.Add(ctx, 123.456, tc.attrs...)
|
2020-04-24 18:44:21 +02:00
|
|
|
|
2021-08-13 00:44:58 +02:00
|
|
|
require.NoError(t, fix.cont.Stop(fix.ctx))
|
2020-04-24 18:44:21 +02:00
|
|
|
|
2021-08-13 00:44:58 +02:00
|
|
|
require.Equal(t, `[{"Name":"name.lastvalue{`+tc.expect+`}","Last":123.456}]`, fix.Output())
|
|
|
|
})
|
2020-04-24 18:44:21 +02:00
|
|
|
}
|
|
|
|
}
|