@@ -8,14 +8,26 @@ defmodule Sentry.Envelope do
88 ClientReport ,
99 Config ,
1010 Event ,
11+ LogEvent ,
1112 Transaction ,
1213 UUID
1314 }
1415
16+ @ typep log_batch ( ) :: % {
17+ __struct__: :log_batch ,
18+ log_events: [ LogEvent . t ( ) ]
19+ }
20+
1521 @ type t ( ) :: % __MODULE__ {
1622 event_id: UUID . t ( ) ,
1723 items: [
18- Attachment . t ( ) | CheckIn . t ( ) | ClientReport . t ( ) | Event . t ( ) | Transaction . t ( ) ,
24+ Attachment . t ( )
25+ | CheckIn . t ( )
26+ | ClientReport . t ( )
27+ | Event . t ( )
28+ | log_batch ( )
29+ | LogEvent . t ( )
30+ | Transaction . t ( ) ,
1931 ...
2032 ]
2133 }
@@ -68,6 +80,28 @@ defmodule Sentry.Envelope do
6880 }
6981 end
7082
83+ @ doc """
84+ Creates a new envelope containing log events.
85+
86+ According to the Sentry Logs Protocol, log events are sent in batches
87+ within a single envelope item with content_type "application/vnd.sentry.items.log+json".
88+ All log events are wrapped in a single item with { items: [...] }.
89+ """
90+ @ doc since: "11.0.0"
91+ @ spec from_log_events ( [ LogEvent . t ( ) ] ) :: t ( )
92+ def from_log_events ( log_events ) when is_list ( log_events ) do
93+ # Create a single log batch item that wraps all log events
94+ log_batch = % {
95+ __struct__: :log_batch ,
96+ log_events: log_events
97+ }
98+
99+ % __MODULE__ {
100+ event_id: UUID . uuid4_hex ( ) ,
101+ items: [ log_batch ]
102+ }
103+ end
104+
71105 @ doc """
72106 Returns the "data category" of the envelope's contents (to be used in client reports and more).
73107 """
@@ -77,6 +111,8 @@ defmodule Sentry.Envelope do
77111 | CheckIn . t ( )
78112 | ClientReport . t ( )
79113 | Event . t ( )
114+ | log_batch ( )
115+ | LogEvent . t ( )
80116 | Transaction . t ( )
81117 ) ::
82118 String . t ( )
@@ -85,6 +121,7 @@ defmodule Sentry.Envelope do
85121 def get_data_category ( % CheckIn { } ) , do: "monitor"
86122 def get_data_category ( % ClientReport { } ) , do: "internal"
87123 def get_data_category ( % Event { } ) , do: "error"
124+ def get_data_category ( % { __struct__: :log_batch } ) , do: "log_item"
88125
89126 @ doc """
90127 Encodes the envelope into its binary representation.
@@ -166,4 +203,24 @@ defmodule Sentry.Envelope do
166203 throw ( error )
167204 end
168205 end
206+
207+ defp item_to_binary ( json_library , % { __struct__: :log_batch , log_events: log_events } ) do
208+ items = Enum . map ( log_events , & LogEvent . to_map / 1 )
209+ payload = % { items: items }
210+
211+ case Sentry.JSON . encode ( payload , json_library ) do
212+ { :ok , encoded_payload } ->
213+ header = % {
214+ "type" => "log" ,
215+ "item_count" => length ( items ) ,
216+ "content_type" => "application/vnd.sentry.items.log+json"
217+ }
218+
219+ { :ok , encoded_header } = Sentry.JSON . encode ( header , json_library )
220+ [ encoded_header , ?\n , encoded_payload , ?\n ]
221+
222+ { :error , _reason } = error ->
223+ throw ( error )
224+ end
225+ end
169226end
0 commit comments