@@ -8,14 +8,21 @@ defmodule Sentry.Envelope do
88 ClientReport ,
99 Config ,
1010 Event ,
11+ LogBatch ,
12+ LogEvent ,
1113 Transaction ,
1214 UUID
1315 }
1416
1517 @ type t ( ) :: % __MODULE__ {
1618 event_id: UUID . t ( ) ,
1719 items: [
18- Attachment . t ( ) | CheckIn . t ( ) | ClientReport . t ( ) | Event . t ( ) | Transaction . t ( ) ,
20+ Attachment . t ( )
21+ | CheckIn . t ( )
22+ | ClientReport . t ( )
23+ | Event . t ( )
24+ | LogBatch . t ( )
25+ | Transaction . t ( ) ,
1926 ...
2027 ]
2128 }
@@ -68,6 +75,25 @@ defmodule Sentry.Envelope do
6875 }
6976 end
7077
78+ @ doc """
79+ Creates a new envelope containing log events.
80+
81+ According to the Sentry Logs Protocol, log events are sent in batches
82+ within a single envelope item with content type `application/vnd.sentry.items.log+json`.
83+ All log events are wrapped in a single item with `{ items: [...] }`.
84+ """
85+ @ doc since: "12.0.0"
86+ @ spec from_log_events ( [ LogEvent . t ( ) ] ) :: t ( )
87+ def from_log_events ( log_events ) when is_list ( log_events ) do
88+ # Create a single log batch item that wraps all log events
89+ log_batch = % LogBatch { log_events: log_events }
90+
91+ % __MODULE__ {
92+ event_id: UUID . uuid4_hex ( ) ,
93+ items: [ log_batch ]
94+ }
95+ end
96+
7197 @ doc """
7298 Returns the "data category" of the envelope's contents (to be used in client reports and more).
7399 """
@@ -77,6 +103,7 @@ defmodule Sentry.Envelope do
77103 | CheckIn . t ( )
78104 | ClientReport . t ( )
79105 | Event . t ( )
106+ | LogBatch . t ( )
80107 | Transaction . t ( )
81108 ) ::
82109 String . t ( )
@@ -85,6 +112,7 @@ defmodule Sentry.Envelope do
85112 def get_data_category ( % CheckIn { } ) , do: "monitor"
86113 def get_data_category ( % ClientReport { } ) , do: "internal"
87114 def get_data_category ( % Event { } ) , do: "error"
115+ def get_data_category ( % LogBatch { } ) , do: "log_item"
88116
89117 @ doc """
90118 Encodes the envelope into its binary representation.
@@ -166,4 +194,24 @@ defmodule Sentry.Envelope do
166194 throw ( error )
167195 end
168196 end
197+
198+ defp item_to_binary ( json_library , % LogBatch { log_events: log_events } ) do
199+ items = Enum . map ( log_events , & LogEvent . to_map / 1 )
200+ payload = % { items: items }
201+
202+ case Sentry.JSON . encode ( payload , json_library ) do
203+ { :ok , encoded_payload } ->
204+ header = % {
205+ "type" => "log" ,
206+ "item_count" => length ( items ) ,
207+ "content_type" => "application/vnd.sentry.items.log+json"
208+ }
209+
210+ { :ok , encoded_header } = Sentry.JSON . encode ( header , json_library )
211+ [ encoded_header , ?\n , encoded_payload , ?\n ]
212+
213+ { :error , _reason } = error ->
214+ throw ( error )
215+ end
216+ end
169217end
0 commit comments