From d399518ddfd9e31d09d15ad6117bc732058b07d8 Mon Sep 17 00:00:00 2001 From: Rebecca Pruim Date: Tue, 21 Jan 2020 10:42:58 -0800 Subject: [PATCH 1/5] Switch stdout exporter to use ungrouped batcher --- exporter/metric/stdout/stdout.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/exporter/metric/stdout/stdout.go b/exporter/metric/stdout/stdout.go index b14175d194d..481d5179dee 100644 --- a/exporter/metric/stdout/stdout.go +++ b/exporter/metric/stdout/stdout.go @@ -27,8 +27,7 @@ import ( export "go.opentelemetry.io/otel/sdk/export/metric" "go.opentelemetry.io/otel/sdk/export/metric/aggregator" - metricsdk "go.opentelemetry.io/otel/sdk/metric" - "go.opentelemetry.io/otel/sdk/metric/batcher/defaultkeys" + "go.opentelemetry.io/otel/sdk/metric/batcher/ungrouped" "go.opentelemetry.io/otel/sdk/metric/controller/push" "go.opentelemetry.io/otel/sdk/metric/selector/simple" ) @@ -130,7 +129,7 @@ func NewExportPipeline(config Config) (*push.Controller, error) { if err != nil { return nil, err } - batcher := defaultkeys.New(selector, metricsdk.NewDefaultLabelEncoder(), true) + batcher := ungrouped.New(selector, true) pusher := push.New(batcher, exporter, time.Second) pusher.Start() From 79a44881ec6a57f88ff733317a90d1f60d89a43a Mon Sep 17 00:00:00 2001 From: Rebecca Pruim Date: Tue, 21 Jan 2020 23:37:03 -0800 Subject: [PATCH 2/5] Add unspecified keys to name without equals signs --- exporter/metric/stdout/stdout.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/exporter/metric/stdout/stdout.go b/exporter/metric/stdout/stdout.go index 481d5179dee..166d90fc7f6 100644 --- a/exporter/metric/stdout/stdout.go +++ b/exporter/metric/stdout/stdout.go @@ -23,6 +23,7 @@ import ( "strings" "time" + "go.opentelemetry.io/otel/api/core" "go.opentelemetry.io/otel/api/global" export "go.opentelemetry.io/otel/sdk/export/metric" @@ -232,13 +233,30 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet) } } + specifiedKeyMap := make(map[core.Key]core.Value) + for _, kv := range record.Labels().Ordered() { + specifiedKeyMap[kv.Key] = kv.Value + } + + var materializedKeys []string + + if labels := record.Labels(); labels.Len() > 0 { + materializedKeys = append(materializedKeys, labels.Encoded()) + } + + for _, k := range desc.Keys() { + if _, ok := specifiedKeyMap[k]; !ok { + materializedKeys = append(materializedKeys, string(k)) + } + } + var sb strings.Builder sb.WriteString(desc.Name()) - if labels := record.Labels(); labels.Len() > 0 { + if len(materializedKeys) > 0 { sb.WriteRune('{') - sb.WriteString(labels.Encoded()) + sb.WriteString(strings.Join(materializedKeys, ",")) sb.WriteRune('}') } From 0ae79c2571ff50b49a599d32f31acbfd44898a4b Mon Sep 17 00:00:00 2001 From: Rebecca Pruim Date: Tue, 21 Jan 2020 23:51:56 -0800 Subject: [PATCH 3/5] Fix tests for stdout exporter --- api/global/internal/meter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/global/internal/meter_test.go b/api/global/internal/meter_test.go index c0b72f44d7f..9c67d4ff474 100644 --- a/api/global/internal/meter_test.go +++ b/api/global/internal/meter_test.go @@ -222,6 +222,6 @@ func TestDefaultSDK(t *testing.T) { pusher.Stop() out.Close() - require.Equal(t, `{"updates":[{"name":"test.builtin","sum":1}]} + require.Equal(t, `{"updates":[{"name":"test.builtin{A=B}","sum":1}]} `, <-ch) } From e86744f74831b6894dd7098714411da24c93994e Mon Sep 17 00:00:00 2001 From: Rebecca Pruim Date: Thu, 23 Jan 2020 14:16:28 -0800 Subject: [PATCH 4/5] Add test for unspecified keys --- api/global/internal/meter_test.go | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/api/global/internal/meter_test.go b/api/global/internal/meter_test.go index 9c67d4ff474..473830a22db 100644 --- a/api/global/internal/meter_test.go +++ b/api/global/internal/meter_test.go @@ -12,6 +12,7 @@ import ( "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/global/internal" "go.opentelemetry.io/otel/api/key" + "go.opentelemetry.io/otel/api/metric" "go.opentelemetry.io/otel/exporter/metric/stdout" metrictest "go.opentelemetry.io/otel/internal/metric" ) @@ -225,3 +226,39 @@ func TestDefaultSDK(t *testing.T) { require.Equal(t, `{"updates":[{"name":"test.builtin{A=B}","sum":1}]} `, <-ch) } + +func TestDefaultUnspecifiedKey(t *testing.T) { + internal.ResetForTest() + + ctx := context.Background() + meter1 := global.MeterProvider().Meter("builtin") + + counter := meter1.NewInt64Counter("test.builtin", + metric.WithKeys(key.New("ex.com/bar")), + ) + counter.Add(ctx, 1, nil) + counter.Add(ctx, 1, nil) + + in, out := io.Pipe() + pusher, err := stdout.InstallNewPipeline(stdout.Config{ + Writer: out, + DoNotPrintTime: true, + }) + if err != nil { + panic(err) + } + + counter.Add(ctx, 1, nil) + + ch := make(chan string) + go func() { + data, _ := ioutil.ReadAll(in) + ch <- string(data) + }() + + pusher.Stop() + out.Close() + + require.Equal(t, `{"updates":[{"name":"test.builtin{ex.com/bar}","sum":1}]} +`, <-ch) +} From e9b18379fb9bb41cf6900c8432117c7670e143d2 Mon Sep 17 00:00:00 2001 From: Rebecca Pruim Date: Thu, 23 Jan 2020 18:07:04 -0800 Subject: [PATCH 5/5] Move test to stdout_test.go --- api/global/internal/meter_test.go | 37 --------------------------- exporter/metric/stdout/stdout_test.go | 19 ++++++++++++++ 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/api/global/internal/meter_test.go b/api/global/internal/meter_test.go index 473830a22db..9c67d4ff474 100644 --- a/api/global/internal/meter_test.go +++ b/api/global/internal/meter_test.go @@ -12,7 +12,6 @@ import ( "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/global/internal" "go.opentelemetry.io/otel/api/key" - "go.opentelemetry.io/otel/api/metric" "go.opentelemetry.io/otel/exporter/metric/stdout" metrictest "go.opentelemetry.io/otel/internal/metric" ) @@ -226,39 +225,3 @@ func TestDefaultSDK(t *testing.T) { require.Equal(t, `{"updates":[{"name":"test.builtin{A=B}","sum":1}]} `, <-ch) } - -func TestDefaultUnspecifiedKey(t *testing.T) { - internal.ResetForTest() - - ctx := context.Background() - meter1 := global.MeterProvider().Meter("builtin") - - counter := meter1.NewInt64Counter("test.builtin", - metric.WithKeys(key.New("ex.com/bar")), - ) - counter.Add(ctx, 1, nil) - counter.Add(ctx, 1, nil) - - in, out := io.Pipe() - pusher, err := stdout.InstallNewPipeline(stdout.Config{ - Writer: out, - DoNotPrintTime: true, - }) - if err != nil { - panic(err) - } - - counter.Add(ctx, 1, nil) - - ch := make(chan string) - go func() { - data, _ := ioutil.ReadAll(in) - ch <- string(data) - }() - - pusher.Stop() - out.Close() - - require.Equal(t, `{"updates":[{"name":"test.builtin{ex.com/bar}","sum":1}]} -`, <-ch) -} diff --git a/exporter/metric/stdout/stdout_test.go b/exporter/metric/stdout/stdout_test.go index 6a5db9602fd..40bfd7d5c57 100644 --- a/exporter/metric/stdout/stdout_test.go +++ b/exporter/metric/stdout/stdout_test.go @@ -262,3 +262,22 @@ func TestStdoutGaugeNotSet(t *testing.T) { require.Equal(t, `{"updates":null}`, fix.Output()) } + +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()) +}