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