-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_test.go
101 lines (94 loc) · 2.31 KB
/
formatter_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
90
91
92
93
94
95
96
97
98
99
100
101
package formatter
import (
"encoding/json"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestLogrusDataModelFormatter(t *testing.T) {
testCases := []struct {
name string
inputFields logrus.Fields
expectedOutput map[string]interface{}
}{
{
name: "Standard DD Fields",
inputFields: logrus.Fields{
"dd.trace_id": "1234",
"dd.span_id": "12345678",
"dd.service": "test-service",
"dd.version": "1.0.0",
},
expectedOutput: map[string]interface{}{
"dd": map[string]interface{}{
"trace_id": "1234",
"span_id": "12345678",
"service": "test-service",
"version": "1.0.0",
},
"timestamp": "2024-11-28T14:00:00.000Z",
"message": "Test message",
"level": "info",
},
},
{
name: "Custom Fields",
inputFields: logrus.Fields{
"user_id": "123",
},
expectedOutput: map[string]interface{}{
"custom": map[string]interface{}{
"user_id": "123",
},
"timestamp": "2024-11-28T14:00:00.000Z",
"message": "Test message",
"level": "info",
},
},
{
name: "No DD Fields",
inputFields: logrus.Fields{
"dd.trace_id": "1234",
"dd.span_id": "12345678",
"dd.service": "test-service",
"dd.version": "1.0.0",
"user_id": "123",
"request_type": "api",
},
expectedOutput: map[string]interface{}{
"dd": map[string]interface{}{
"trace_id": "1234",
"span_id": "12345678",
"service": "test-service",
"version": "1.0.0",
},
"custom": map[string]interface{}{
"user_id": "123",
"request_type": "api",
},
"timestamp": "2024-11-28T14:00:00.000Z",
"message": "Test message",
"level": "info",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
formatter := &LogrusDataModelFormatter{}
entry := &logrus.Entry{
Logger: logrus.StandardLogger(),
Data: tc.inputFields,
Time: time.Date(2024, 11, 28, 14, 0, 0, 0, time.UTC),
Level: logrus.InfoLevel,
Message: "Test message",
}
formattedBytes, err := formatter.Format(entry)
assert.NoError(t, err)
var output map[string]interface{}
err = json.Unmarshal(formattedBytes, &output)
assert.NoError(t, err)
assert.Equal(t, tc.expectedOutput, output)
})
}
}