Skip to content

Commit 2b9dc34

Browse files
committed
Fix pre-serialized event drops spans and user fields
1 parent ad228c5 commit 2b9dc34

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

interfaces.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,6 @@ func (e *Event) preSerializedMarshalJSON() ([]byte, error) {
641641
Contexts json.RawMessage `json:"contexts,omitempty"`
642642
Breadcrumbs json.RawMessage `json:"breadcrumbs,omitempty"`
643643
Exception json.RawMessage `json:"exception,omitempty"`
644-
Spans json.RawMessage `json:"spans,omitempty"`
645644
User json.RawMessage `json:"user,omitempty"`
646645
}
647646
return json.Marshal(safeTransaction{
@@ -904,7 +903,7 @@ func (e *Event) MakeSerializationSafe() {
904903
}
905904
}
906905

907-
if len(e.User.Data) > 0 {
906+
if !e.User.IsEmpty() {
908907
if b, err := json.Marshal(e.User); err == nil {
909908
e.serializedUser = b
910909
}

interfaces_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,73 @@ func TestMakeSerializationSafe(t *testing.T) {
10281028
}
10291029
})
10301030

1031+
t.Run("pre-serializes User when Data is empty", func(t *testing.T) {
1032+
event := &Event{
1033+
Extra: map[string]interface{}{"key": "value"},
1034+
User: User{ID: "1", Email: "user@example.com"},
1035+
}
1036+
event.MakeSerializationSafe()
1037+
1038+
if event.serializedUser == nil {
1039+
t.Fatal("serializedUser should be set")
1040+
}
1041+
1042+
jsonData, err := json.Marshal(event)
1043+
if err != nil {
1044+
t.Fatalf("marshal failed: %v", err)
1045+
}
1046+
1047+
var got map[string]interface{}
1048+
if err := json.Unmarshal(jsonData, &got); err != nil {
1049+
t.Fatalf("unmarshal failed: %v", err)
1050+
}
1051+
1052+
user, ok := got["user"].(map[string]interface{})
1053+
if !ok {
1054+
t.Fatalf("expected user field in JSON, got %s", jsonData)
1055+
}
1056+
if user["id"] != "1" {
1057+
t.Errorf("expected user.id=1, got %v", user["id"])
1058+
}
1059+
if user["email"] != "user@example.com" {
1060+
t.Errorf("expected user.email=user@example.com, got %v", user["email"])
1061+
}
1062+
})
1063+
1064+
t.Run("pre-serializes transaction spans", func(t *testing.T) {
1065+
event := &Event{
1066+
Type: transactionType,
1067+
Contexts: map[string]Context{
1068+
"trace": TraceContext{
1069+
TraceID: TraceIDFromHex("90d57511038845dcb4164a70fc3a7fdb"),
1070+
SpanID: SpanIDFromHex("f7f3fd754a9040eb"),
1071+
}.Map(),
1072+
},
1073+
Spans: []*Span{{
1074+
TraceID: TraceIDFromHex("90d57511038845dcb4164a70fc3a7fdb"),
1075+
SpanID: SpanIDFromHex("4aaf45ea7db94520"),
1076+
StartTime: time.Unix(1, 0).UTC(),
1077+
EndTime: time.Unix(2, 0).UTC(),
1078+
}},
1079+
}
1080+
event.MakeSerializationSafe()
1081+
1082+
jsonData, err := json.Marshal(event)
1083+
if err != nil {
1084+
t.Fatalf("marshal failed: %v", err)
1085+
}
1086+
1087+
var got map[string]interface{}
1088+
if err := json.Unmarshal(jsonData, &got); err != nil {
1089+
t.Fatalf("unmarshal failed: %v", err)
1090+
}
1091+
1092+
spans, ok := got["spans"].([]interface{})
1093+
if !ok || len(spans) != 1 {
1094+
t.Fatalf("expected one span in JSON, got %s", jsonData)
1095+
}
1096+
})
1097+
10311098
t.Run("marshaled output matches original event", func(t *testing.T) {
10321099
event := &Event{
10331100
EventID: "12345678901234567890123456789012",

0 commit comments

Comments
 (0)