From 20bb650b2d1de5290c14b75e837ad0aaf3340dc3 Mon Sep 17 00:00:00 2001 From: Rebecca Pruim Date: Fri, 24 Jan 2020 05:51:58 -0800 Subject: [PATCH] Improve detail in stdout exporter (#436) * Switch stdout exporter to use ungrouped batcher * Add unspecified keys to name without equals signs * Fix tests for stdout exporter * Add test for unspecified keys * Move test to stdout_test.go --- api/global/internal/meter_test.go | 2 +- exporter/metric/stdout/stdout.go | 27 ++++++++++++++++++++++----- exporter/metric/stdout/stdout_test.go | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) 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) } diff --git a/exporter/metric/stdout/stdout.go b/exporter/metric/stdout/stdout.go index b14175d194d..166d90fc7f6 100644 --- a/exporter/metric/stdout/stdout.go +++ b/exporter/metric/stdout/stdout.go @@ -23,12 +23,12 @@ import ( "strings" "time" + "go.opentelemetry.io/otel/api/core" "go.opentelemetry.io/otel/api/global" 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 +130,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() @@ -233,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('}') } 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()) +}