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