Skip to content

Commit 95dc4cb

Browse files
committed
simplify log serialization
1 parent 18df0e7 commit 95dc4cb

3 files changed

Lines changed: 24 additions & 46 deletions

File tree

interfaces.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (b *Breadcrumb) MarshalJSON() ([]byte, error) {
8080

8181
if b.Timestamp.IsZero() {
8282
return json.Marshal(struct {
83-
// Embed all of the fields of Breadcrumb.
83+
// Embed all the fields of Breadcrumb.
8484
*breadcrumb
8585
// Timestamp shadows the original Timestamp field and is meant to
8686
// remain nil, triggering the omitempty behavior.
@@ -734,42 +734,7 @@ type Log struct {
734734

735735
// ToEnvelopeItem converts the Log to a Sentry envelope item for batching.
736736
func (l *Log) ToEnvelopeItem() (*protocol.EnvelopeItem, error) {
737-
type logJSON struct {
738-
Timestamp *float64 `json:"timestamp"`
739-
TraceID string `json:"trace_id"`
740-
SpanID string `json:"span_id,omitempty"`
741-
Level string `json:"level"`
742-
Severity int `json:"severity_number,omitempty"`
743-
Body string `json:"body"`
744-
Attributes map[string]protocol.LogAttribute `json:"attributes,omitempty"`
745-
}
746-
747-
// Convert time.Time to seconds float if set
748-
var ts *float64
749-
if !l.Timestamp.IsZero() {
750-
sec := float64(l.Timestamp.UnixNano()) / 1e9
751-
ts = &sec
752-
}
753-
754-
attrs := make(map[string]protocol.LogAttribute, len(l.Attributes))
755-
for k, v := range l.Attributes {
756-
attrs[k] = protocol.LogAttribute{Value: v.Value, Type: string(v.Type)}
757-
}
758-
759-
var spanIDStr string
760-
if l.SpanID != zeroSpanID {
761-
spanIDStr = l.SpanID.String()
762-
}
763-
764-
logData, err := json.Marshal(logJSON{
765-
Timestamp: ts,
766-
TraceID: l.TraceID.String(),
767-
SpanID: spanIDStr,
768-
Level: string(l.Level),
769-
Severity: l.Severity,
770-
Body: l.Body,
771-
Attributes: attrs,
772-
})
737+
logData, err := json.Marshal(l)
773738
if err != nil {
774739
return nil, err
775740
}
@@ -817,18 +782,31 @@ type Attribute struct {
817782
Type AttrType `json:"type"`
818783
}
819784

820-
// MarshalJSON is a custom implementation of a marshaller that skips SpanID when zero.
785+
// MarshalJSON is a custom implementation that skips SpanID and timestamp when empty.
821786
func (l *Log) MarshalJSON() ([]byte, error) {
822787
type log Log
788+
823789
var spanID string
824790
if l.SpanID != zeroSpanID {
825791
spanID = l.SpanID.String()
826792
}
793+
794+
var ts json.RawMessage
795+
if !l.Timestamp.IsZero() {
796+
b, err := l.Timestamp.MarshalJSON()
797+
if err != nil {
798+
return nil, err
799+
}
800+
ts = b
801+
}
802+
827803
return json.Marshal(struct {
828804
*log
829-
SpanID string `json:"span_id,omitempty"`
805+
SpanID string `json:"span_id,omitempty"`
806+
Timestamp json.RawMessage `json:"timestamp,omitempty"`
830807
}{
831-
log: (*log)(l),
832-
SpanID: spanID,
808+
log: (*log)(l),
809+
SpanID: spanID,
810+
Timestamp: ts,
833811
})
834812
}

interfaces_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ func TestLog_ToEnvelopeItem_And_Getters(t *testing.T) {
985985
}
986986

987987
var payload struct {
988-
Timestamp *float64 `json:"timestamp,omitempty"`
988+
Timestamp string `json:"timestamp,omitempty"`
989989
TraceID string `json:"trace_id,omitempty"`
990990
Level string `json:"level"`
991991
Severity int `json:"severity_number,omitempty"`
@@ -996,11 +996,11 @@ func TestLog_ToEnvelopeItem_And_Getters(t *testing.T) {
996996
t.Fatalf("failed to unmarshal payload: %v", err)
997997
}
998998

999-
if payload.Timestamp == nil {
999+
if payload.Timestamp == "" {
10001000
t.Fatal("expected timestamp to be set")
10011001
}
1002-
if *payload.Timestamp < 1.7e9 || *payload.Timestamp > 1.700000001e9 {
1003-
t.Fatalf("unexpected timestamp: %v", *payload.Timestamp)
1002+
if _, err := time.Parse(time.RFC3339, payload.Timestamp); err != nil {
1003+
t.Fatalf("invalid timestamp format: %v", err)
10041004
}
10051005
if payload.TraceID != trace.String() {
10061006
t.Fatalf("unexpected trace id: %q", payload.TraceID)

transport_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func TestEnvelopeFromLogEvent(t *testing.T) {
292292
got := b.String()
293293
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://public@example.com/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
294294
{"type":"log","item_count":1,"content_type":"application/vnd.sentry.items.log+json"}
295-
{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sdk":{"name":"sentry.go","version":"0.0.1"},"user":{},"items":[{"timestamp":"0001-01-01T00:00:00Z","trace_id":"d49d9bf66f13450b81f65bc51cf49c03","level":"info","severity_number":9,"body":"test log message","attributes":{"key.bool":{"value":true,"type":"boolean"},"key.float":{"value":42.2,"type":"double"},"key.int":{"value":42,"type":"integer"},"key.string":{"value":"str","type":"string"},"sentry.environment":{"value":"testing","type":"string"},"sentry.release":{"value":"v1.2.3","type":"string"},"sentry.sdk.name":{"value":"sentry.go","type":"string"},"sentry.sdk.version":{"value":"0.0.1","type":"string"},"sentry.server.address":{"value":"test-server","type":"string"}}}]}
295+
{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sdk":{"name":"sentry.go","version":"0.0.1"},"user":{},"items":[{"trace_id":"d49d9bf66f13450b81f65bc51cf49c03","level":"info","severity_number":9,"body":"test log message","attributes":{"key.bool":{"value":true,"type":"boolean"},"key.float":{"value":42.2,"type":"double"},"key.int":{"value":42,"type":"integer"},"key.string":{"value":"str","type":"string"},"sentry.environment":{"value":"testing","type":"string"},"sentry.release":{"value":"v1.2.3","type":"string"},"sentry.sdk.name":{"value":"sentry.go","type":"string"},"sentry.sdk.version":{"value":"0.0.1","type":"string"},"sentry.server.address":{"value":"test-server","type":"string"}}}]}
296296
`
297297
if diff := cmp.Diff(want, got); diff != "" {
298298
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)

0 commit comments

Comments
 (0)