/
event_test.go
89 lines (81 loc) · 2.74 KB
/
event_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package logg_test
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/rafaelespinoza/logg"
)
func TestEvent(t *testing.T) {
const traceIDKey = "x_trace_id"
var (
parsedRoot map[string]interface{}
numTraceKeyValues int
traceIDVal string
)
ctx := logg.CtxWithID(context.Background())
sink := newDataSink()
logger := logg.New(map[string]interface{}{"sierra": "nevada"}, sink).WithID(ctx)
logger.WithData(map[string]interface{}{"bravo": true}).Infof("test")
if err := json.Unmarshal(sink.Raw(), &parsedRoot); err != nil {
t.Fatal(err)
}
if id, ok := parsedRoot[traceIDKey]; !ok {
t.Errorf("expected output to have key %q", traceIDKey)
} else if id.(string) == "" {
t.Errorf("expected non-empty value at %q", traceIDKey)
} else {
traceIDVal = id.(string)
}
if traceIDVal == "" {
t.Log("traceIDVal is empty")
}
numTraceKeyValues = strings.Count(string(sink.Raw()), traceIDKey)
if numTraceKeyValues != 1 {
t.Errorf("wrong count of %q values; got %d, expected %d", traceIDKey, numTraceKeyValues, 1)
}
if t.Failed() {
t.Logf("%s", sink.Raw())
}
// Same logger context on a new event yields the same ID
logger.WithData(map[string]interface{}{"bravo": true}).Infof("test")
if err := json.Unmarshal(sink.Raw(), &parsedRoot); err != nil {
t.Fatal(err)
}
if id, ok := parsedRoot[traceIDKey]; !ok {
t.Errorf("expected output to have key %q", traceIDKey)
} else if id.(string) != traceIDVal {
t.Errorf("wrong id; got %q, expected %q", id.(string), traceIDVal)
}
numTraceKeyValues = strings.Count(string(sink.Raw()), traceIDKey)
if numTraceKeyValues != 1 {
t.Errorf("wrong count of %q values; got %d, expected %d", traceIDKey, numTraceKeyValues, 1)
}
if t.Failed() {
t.Logf("%s", sink.Raw())
}
// When the logger context has an ID, is passed to the event but the event
// also calls WithID, it should yield the same ID as the logger ID.
logger.WithData(map[string]interface{}{"bravo": true}).WithID(ctx).Infof("test")
if err := json.Unmarshal(sink.Raw(), &parsedRoot); err != nil {
t.Fatal(err)
}
if id, ok := parsedRoot[traceIDKey]; !ok {
t.Errorf("expected output to have key %q", traceIDKey)
} else if id.(string) != traceIDVal {
t.Errorf("wrong id; got %q, expected %q", id.(string), traceIDVal)
}
// Documents known behavior stemming from the logging library, which doesn't
// do any field de-duplication. Calling WithID multiple times just adds
// another id. See the github.com/rs/zerolog README for more info.
numTraceKeyValues = strings.Count(string(sink.Raw()), traceIDKey)
if numTraceKeyValues != 1 {
t.Logf(
"info about number of %q values; got %d, but %d would be ideal; %s",
traceIDKey, numTraceKeyValues, 1, `¯\_(ツ)_/¯`,
)
}
if t.Failed() {
t.Logf("%s", sink.Raw())
}
}