Skip to content

Commit

Permalink
use correct field for "labels"
Browse files Browse the repository at this point in the history
This was implemented in StackDriver in
GoogleCloudPlatform/fluent-plugin-google-cloud#292.

fixes #4
  • Loading branch information
JeanMertz committed Oct 28, 2019
1 parent fc8725b commit 134a944
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 6 additions & 6 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestWithLabels(t *testing.T) {

want := []zap.Field{
zap.String("hello", "world"),
zap.Object("labels", labels),
zap.Object("logging.googleapis.com/labels", labels),
}

assert.Equal(t, want, (&core{}).withLabels(fields))
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestWrite(t *testing.T) {
err := core.Write(zapcore.Entry{}, fields)
require.NoError(t, err)

assert.NotNil(t, logs.All()[0].ContextMap()["labels"])
assert.NotNil(t, logs.All()[0].ContextMap()[labelsKey])
}

func TestWriteConcurrent(t *testing.T) {
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestWriteConcurrent(t *testing.T) {
}
wg.Wait()

assert.NotNil(t, logs.All()[0].ContextMap()["labels"])
assert.NotNil(t, logs.All()[0].ContextMap()[labelsKey])
}

func TestWithAndWrite(t *testing.T) {
Expand All @@ -227,7 +227,7 @@ func TestWithAndWrite(t *testing.T) {
err := core.Write(zapcore.Entry{}, []zapcore.Field{Label("two", "worlds")})
require.NoError(t, err)

labels := logs.All()[0].ContextMap()["labels"].(map[string]interface{})
labels := logs.All()[0].ContextMap()[labelsKey].(map[string]interface{})

assert.Equal(t, "world", labels["one"])
assert.Equal(t, "worlds", labels["two"])
Expand All @@ -245,7 +245,7 @@ func TestWithAndWrite_MultipleEntries(t *testing.T) {
err := core.Write(zapcore.Entry{}, []zapcore.Field{Label("two", "worlds")})
require.NoError(t, err)

labels := logs.All()[0].ContextMap()["labels"].(map[string]interface{})
labels := logs.All()[0].ContextMap()[labelsKey].(map[string]interface{})
require.Len(t, labels, 2)

assert.Equal(t, "world", labels["one"])
Expand All @@ -254,7 +254,7 @@ func TestWithAndWrite_MultipleEntries(t *testing.T) {
err = core.Write(zapcore.Entry{}, []zapcore.Field{Label("three", "worlds")})
require.NoError(t, err)

labels = logs.All()[1].ContextMap()["labels"].(map[string]interface{})
labels = logs.All()[1].ContextMap()[labelsKey].(map[string]interface{})
require.Len(t, labels, 2)

assert.Equal(t, "world", labels["one"])
Expand Down
4 changes: 3 additions & 1 deletion label.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"go.uber.org/zap/zapcore"
)

const labelsKey = "logging.googleapis.com/labels"

// Label adds an optional label to the payload.
//
// Labels are a set of user-defined (key, value) data that provides additional
Expand Down Expand Up @@ -40,7 +42,7 @@ func isLabelField(field zap.Field) bool {
}

func labelsField(l *labels) zap.Field {
return zap.Object("labels", l)
return zap.Object(labelsKey, l)
}

type labels struct {
Expand Down
2 changes: 1 addition & 1 deletion label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func TestLabels(t *testing.T) {
labels := newLabels()
labels.store = map[string]string{"hello": "world", "hi": "universe"}

assert.Equal(t, zap.Object("labels", labels), field)
assert.Equal(t, zap.Object(labelsKey, labels), field)
}

0 comments on commit 134a944

Please sign in to comment.