Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/v2/app_agent_receiver/payload_test.go
5363 views
1
package app_agent_receiver
2
3
import (
4
"encoding/json"
5
"os"
6
"path/filepath"
7
"testing"
8
"time"
9
10
"github.com/stretchr/testify/require"
11
)
12
13
func loadTestData(t *testing.T, file string) []byte {
14
t.Helper()
15
// Safe to disable, this is a test.
16
// nolint:gosec
17
content, err := os.ReadFile(filepath.Join("testdata", file))
18
require.NoError(t, err, "expected to be able to read file")
19
require.True(t, len(content) > 0)
20
return content
21
}
22
23
func TestUnmarshalPayloadJSON(t *testing.T) {
24
content := loadTestData(t, "payload.json")
25
var payload Payload
26
err := json.Unmarshal(content, &payload)
27
require.NoError(t, err)
28
29
now, err := time.Parse("2006-01-02T15:04:05Z0700", "2021-09-30T10:46:17.680Z")
30
require.NoError(t, err)
31
32
require.Equal(t, Meta{
33
SDK: SDK{
34
Name: "grafana-frontend-agent",
35
Version: "1.0.0",
36
},
37
App: App{
38
Name: "testapp",
39
Release: "0.8.2",
40
Version: "abcdefg",
41
Environment: "production",
42
},
43
User: User{
44
Username: "domasx2",
45
ID: "123",
46
Email: "[email protected]",
47
Attributes: map[string]string{"foo": "bar"},
48
},
49
Session: Session{
50
ID: "abcd",
51
Attributes: map[string]string{"time_elapsed": "100s"},
52
},
53
Page: Page{
54
URL: "https://example.com/page",
55
},
56
Browser: Browser{
57
Name: "chrome",
58
Version: "88.12.1",
59
OS: "linux",
60
Mobile: false,
61
},
62
View: View{
63
Name: "foobar",
64
},
65
}, payload.Meta)
66
67
require.Len(t, payload.Exceptions, 1)
68
require.Len(t, payload.Exceptions[0].Stacktrace.Frames, 26)
69
require.Equal(t, "Error", payload.Exceptions[0].Type)
70
require.Equal(t, "Cannot read property 'find' of undefined", payload.Exceptions[0].Value)
71
72
require.Equal(t, []Log{
73
{
74
Message: "opened pricing page",
75
LogLevel: LogLevelInfo,
76
Context: map[string]string{
77
"component": "AppRoot",
78
"page": "Pricing",
79
},
80
Timestamp: now,
81
Trace: TraceContext{
82
TraceID: "abcd",
83
SpanID: "def",
84
},
85
},
86
{
87
Message: "loading price list",
88
LogLevel: LogLevelTrace,
89
Context: map[string]string{
90
"component": "AppRoot",
91
"page": "Pricing",
92
},
93
Timestamp: now,
94
Trace: TraceContext{
95
TraceID: "abcd",
96
SpanID: "ghj",
97
},
98
},
99
}, payload.Logs)
100
101
require.Equal(t, []Event{
102
{
103
Name: "click_login_button",
104
Domain: "frontend",
105
Timestamp: now,
106
Attributes: map[string]string{
107
"foo": "bar",
108
"one": "two",
109
},
110
Trace: TraceContext{
111
TraceID: "abcd",
112
SpanID: "def",
113
},
114
},
115
{
116
Name: "click_reset_password_button",
117
Timestamp: now,
118
},
119
}, payload.Events)
120
}
121
122