@@ -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.
736736func (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 .
821786func (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}
0 commit comments