Skip to content

Commit

Permalink
Improve detail in stdout exporter (#436)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rebeccajae authored and rghetia committed Jan 24, 2020
1 parent 2c460f0 commit 20bb650
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/global/internal/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 22 additions & 5 deletions exporter/metric/stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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('}')
}

Expand Down
19 changes: 19 additions & 0 deletions exporter/metric/stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 20bb650

Please sign in to comment.