@@ -186,4 +186,75 @@ defmodule Sentry.Telemetry.Category do
186186 def data_category ( :transaction ) , do: "transaction"
187187 def data_category ( :log ) , do: "log_item"
188188 def data_category ( :metric ) , do: "trace_metric"
189+
190+ @ doc """
191+ Returns the byte-based Sentry data category string for a given telemetry category.
192+
193+ Some data categories have a companion "byte" category used to report the total
194+ serialized size of dropped items in client reports and to honor byte-based rate
195+ limits. Only `:log` and `:metric` currently have such companion categories.
196+
197+ These strings are used in client reports and rate limiting alongside the
198+ count-based category returned by `data_category/1`.
199+
200+ ## Examples
201+
202+ iex> Sentry.Telemetry.Category.byte_data_category(:log)
203+ "log_byte"
204+
205+ iex> Sentry.Telemetry.Category.byte_data_category(:metric)
206+ "trace_metric_byte"
207+
208+ """
209+ @ spec byte_data_category ( :log | :metric ) :: String . t ( )
210+ def byte_data_category ( :log ) , do: "log_byte"
211+ def byte_data_category ( :metric ) , do: "trace_metric_byte"
212+
213+ @ doc """
214+ Returns every rate-limit data category that gates the given count-based data
215+ category, including any companion byte category.
216+
217+ Logs and metrics have a companion byte category (`log_byte` /
218+ `trace_metric_byte`) that Sentry can rate-limit independently, so an active
219+ limit on either the count or the byte category must suppress sending. All
220+ other categories gate on themselves only.
221+
222+ These strings are matched against the limits stored from the
223+ `X-Sentry-Rate-Limits` response header.
224+
225+ ## Examples
226+
227+ iex> Sentry.Telemetry.Category.rate_limit_categories("log_item")
228+ ["log_item", "log_byte"]
229+
230+ iex> Sentry.Telemetry.Category.rate_limit_categories("trace_metric")
231+ ["trace_metric", "trace_metric_byte"]
232+
233+ iex> Sentry.Telemetry.Category.rate_limit_categories("error")
234+ ["error"]
235+
236+ """
237+ @ spec rate_limit_categories ( String . t ( ) ) :: [ String . t ( ) , ... ]
238+ def rate_limit_categories ( "log_item" ) , do: [ "log_item" , "log_byte" ]
239+ def rate_limit_categories ( "trace_metric" ) , do: [ "trace_metric" , "trace_metric_byte" ]
240+ def rate_limit_categories ( category ) when is_binary ( category ) , do: [ category ]
241+
242+ @ doc """
243+ Returns all Sentry data category strings recognized by the SDK.
244+
245+ This includes the count-based categories returned by `data_category/1` as well
246+ as the byte-based categories returned by `byte_data_category/1`. Any category
247+ outside this set is unknown to the SDK.
248+
249+ ## Examples
250+
251+ iex> categories = Sentry.Telemetry.Category.data_categories()
252+ iex> "log_byte" in categories and "trace_metric_byte" in categories
253+ true
254+
255+ """
256+ @ spec data_categories ( ) :: [ String . t ( ) , ... ]
257+ def data_categories do
258+ Enum . map ( @ categories , & data_category / 1 ) ++ [ "log_byte" , "trace_metric_byte" ]
259+ end
189260end
0 commit comments