-
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathtelemetry_processor.ex
More file actions
369 lines (297 loc) · 11 KB
/
Copy pathtelemetry_processor.ex
File metadata and controls
369 lines (297 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
defmodule Sentry.TelemetryProcessor do
@moduledoc """
Supervisor managing telemetry buffers and scheduler for the Sentry SDK.
The TelemetryProcessor is the coordinator for telemetry data flowing
through the SDK. It manages ring buffers coordinated by a weighted
round-robin scheduler.
## Architecture
The processor starts as a supervisor with the following children:
* Error Buffer - for error events (critical priority)
* Check-in Buffer - for cron check-ins (high priority)
* Transaction Buffer - for performance transactions (medium priority)
* Log Buffer - for log entries (low priority)
* Scheduler - weighted round-robin scheduler with integrated transport queue
## Usage
# Add error events to the buffer
TelemetryProcessor.add(processor, %Sentry.Event{...})
# Add check-ins to the buffer
TelemetryProcessor.add(processor, %Sentry.CheckIn{...})
# Add transactions to the buffer
TelemetryProcessor.add(processor, %Sentry.Transaction{...})
# Add log events to the buffer
TelemetryProcessor.add(processor, %Sentry.LogEvent{...})
# Flush all pending items
TelemetryProcessor.flush(processor)
"""
@moduledoc since: "12.0.0"
use Supervisor
alias Sentry.Telemetry.{Buffer, Category, Scheduler}
alias Sentry.{CheckIn, Event, LogEvent, Transaction}
@default_name __MODULE__
@type option ::
{:name, atom()}
| {:buffer_capacities, %{Category.t() => pos_integer()}}
| {:buffer_configs, %{Category.t() => map()}}
| {:scheduler_weights, %{Category.priority() => pos_integer()}}
| {:on_envelope, (Sentry.Envelope.t() -> any())}
| {:transport_capacity, pos_integer()}
## Public API
@doc """
Returns the default processor name.
"""
@spec default_name() :: atom()
def default_name, do: @default_name
@doc """
Starts the TelemetryProcessor supervisor.
## Options
* `:name` - Name to register the supervisor under (defaults to `#{inspect(@default_name)}`)
* `:buffer_capacities` - Map of category to capacity override (optional)
* `:buffer_configs` - Map of category to config map with `:capacity`, `:batch_size`, `:timeout` (optional)
* `:scheduler_weights` - Map of priority to weight override (optional)
* `:on_envelope` - Callback function invoked when envelopes are ready to send (optional)
* `:transport_capacity` - Maximum number of items the transport queue can hold (default: 1000). For log envelopes, each log event counts as one item.
## Examples
TelemetryProcessor.start_link()
TelemetryProcessor.start_link(
buffer_capacities: %{log: 2000},
scheduler_weights: %{low: 3}
)
"""
@spec start_link([option()]) :: Supervisor.on_start()
def start_link(opts \\ []) do
{name, opts} = Keyword.pop(opts, :name, @default_name)
Supervisor.start_link(__MODULE__, [{:processor_name, name} | opts], name: name)
end
@doc """
Adds an event to the appropriate buffer.
Uses the processor from process dictionary or the default (`#{inspect(@default_name)}`).
See `add/2` for the version accepting a custom processor.
Returns `:ok`.
"""
@spec add(Event.t() | CheckIn.t() | Transaction.t() | LogEvent.t()) :: :ok
def add(%Event{} = item) do
add(processor_name(), item)
end
def add(%CheckIn{} = item) do
add(processor_name(), item)
end
def add(%Transaction{} = item) do
add(processor_name(), item)
end
def add(%LogEvent{} = item) do
add(processor_name(), item)
end
@doc """
Adds an event to the appropriate buffer.
After adding, the scheduler is signaled to wake and process items.
Returns `:ok`.
"""
@spec add(Supervisor.supervisor(), Event.t() | CheckIn.t() | Transaction.t() | LogEvent.t()) ::
:ok
def add(processor, %Event{} = item) when is_atom(processor) do
Buffer.add(buffer_name(processor, :error), item)
Scheduler.signal(scheduler_name(processor))
:ok
end
def add(processor, %Event{} = item) do
buffer = get_buffer(processor, :error)
Buffer.add(buffer, item)
scheduler = get_scheduler(processor)
Scheduler.signal(scheduler)
:ok
end
def add(processor, %CheckIn{} = item) when is_atom(processor) do
Buffer.add(buffer_name(processor, :check_in), item)
Scheduler.signal(scheduler_name(processor))
:ok
end
def add(processor, %CheckIn{} = item) do
buffer = get_buffer(processor, :check_in)
Buffer.add(buffer, item)
scheduler = get_scheduler(processor)
Scheduler.signal(scheduler)
:ok
end
def add(processor, %Transaction{} = item) when is_atom(processor) do
Buffer.add(buffer_name(processor, :transaction), item)
Scheduler.signal(scheduler_name(processor))
:ok
end
def add(processor, %Transaction{} = item) do
buffer = get_buffer(processor, :transaction)
Buffer.add(buffer, item)
scheduler = get_scheduler(processor)
Scheduler.signal(scheduler)
:ok
end
def add(processor, %LogEvent{} = item) when is_atom(processor) do
Buffer.add(buffer_name(processor, :log), item)
Scheduler.signal(scheduler_name(processor))
:ok
end
def add(processor, %LogEvent{} = item) do
buffer = get_buffer(processor, :log)
Buffer.add(buffer, item)
scheduler = get_scheduler(processor)
Scheduler.signal(scheduler)
:ok
end
@doc """
Flushes all buffers by draining their contents and sending all items.
Uses the processor from process dictionary or the default (`#{inspect(@default_name)}`).
This is a blocking call that returns when all items have been processed.
"""
@spec flush() :: :ok
def flush do
flush(processor_name())
end
@doc """
Flushes all buffers by draining their contents and sending all items.
This is a blocking call that returns when all items have been processed.
The optional timeout specifies how long to wait (default: 5000ms).
"""
@spec flush(Supervisor.supervisor(), timeout()) :: :ok
def flush(processor, timeout \\ 5000)
def flush(processor, timeout) when is_atom(processor) do
Scheduler.flush(scheduler_name(processor), timeout)
end
def flush(processor, timeout) do
scheduler = get_scheduler(processor)
Scheduler.flush(scheduler, timeout)
end
@doc """
Returns the buffer pid for a given category.
"""
@spec get_buffer(Supervisor.supervisor(), Category.t()) :: pid()
def get_buffer(processor, category) when category in [:error, :check_in, :transaction, :log] do
children = Supervisor.which_children(processor)
buffer_id = buffer_id(category)
case List.keyfind(children, buffer_id, 0) do
{^buffer_id, pid, :worker, _} when is_pid(pid) -> pid
_ -> raise "Buffer not found for category: #{category}"
end
end
@doc """
Returns the scheduler pid.
"""
@spec get_scheduler(Supervisor.supervisor()) :: pid()
def get_scheduler(processor) do
children = Supervisor.which_children(processor)
case List.keyfind(children, :scheduler, 0) do
{:scheduler, pid, :worker, _} when is_pid(pid) -> pid
_ -> raise "Scheduler not found"
end
end
@doc """
Returns the current size of a buffer for a given category.
Uses the processor from process dictionary or the default.
Returns 0 if the processor is not running.
"""
@spec buffer_size(Category.t()) :: non_neg_integer()
def buffer_size(category) when category in [:error, :check_in, :transaction, :log] do
buffer_size(processor_name(), category)
end
@doc """
Returns the current size of a buffer for a given category.
Returns 0 if the processor is not running.
"""
@spec buffer_size(Supervisor.supervisor(), Category.t()) :: non_neg_integer()
def buffer_size(processor, category) when category in [:error, :check_in, :transaction, :log] do
case safe_get_buffer(processor, category) do
{:ok, buffer} -> Buffer.size(buffer)
:error -> 0
end
end
defp safe_get_buffer(processor, category) when is_atom(processor) do
try do
{:ok,
buffer_name(processor, category) |> GenServer.whereis() || get_buffer(processor, category)}
catch
:exit, _ -> :error
end
end
defp safe_get_buffer(processor, category) do
try do
{:ok, get_buffer(processor, category)}
catch
:exit, _ -> :error
end
end
@impl true
def init(opts) do
processor_name = Keyword.fetch!(opts, :processor_name)
buffer_capacities = Keyword.get(opts, :buffer_capacities, %{})
buffer_configs = Keyword.get(opts, :buffer_configs, %{})
scheduler_weights = Keyword.get(opts, :scheduler_weights)
on_envelope = Keyword.get(opts, :on_envelope)
transport_capacity = Keyword.get(opts, :transport_capacity, 1000)
buffer_names =
for category <- Category.all(), into: %{} do
{category, buffer_name(processor_name, category)}
end
buffer_specs =
for category <- Category.all() do
config = build_buffer_config(category, buffer_capacities, buffer_configs)
%{
id: buffer_id(category),
start:
{Buffer, :start_link,
[
[
category: category,
name: Map.fetch!(buffer_names, category),
capacity: config.capacity,
batch_size: config.batch_size,
timeout: config.timeout
]
]}
}
end
scheduler_opts =
[
buffers: %{
error: Map.fetch!(buffer_names, :error),
check_in: Map.fetch!(buffer_names, :check_in),
transaction: Map.fetch!(buffer_names, :transaction),
log: Map.fetch!(buffer_names, :log)
},
name: scheduler_name(processor_name),
capacity: transport_capacity
]
|> maybe_add_opt(:weights, scheduler_weights)
|> maybe_add_opt(:on_envelope, on_envelope)
scheduler_spec = %{
id: :scheduler,
start: {Scheduler, :start_link, [scheduler_opts]}
}
children = buffer_specs ++ [scheduler_spec]
Supervisor.init(children, strategy: :one_for_one)
end
defp buffer_id(:error), do: :error_buffer
defp buffer_id(:check_in), do: :check_in_buffer
defp buffer_id(:transaction), do: :transaction_buffer
defp buffer_id(:log), do: :log_buffer
@doc false
@spec buffer_name(atom(), Category.t()) :: atom()
def buffer_name(processor, category), do: :"#{processor}.buffer.#{category}"
@doc false
@spec scheduler_name(atom()) :: atom()
def scheduler_name(processor), do: :"#{processor}.scheduler"
defp build_buffer_config(category, capacities, configs) do
defaults = Category.default_config(category)
config =
case Map.get(capacities, category) do
nil -> defaults
capacity -> Map.put(defaults, :capacity, capacity)
end
case Map.get(configs, category) do
nil -> config
category_config -> Map.merge(config, category_config)
end
end
defp processor_name do
Process.get(:sentry_telemetry_processor, @default_name)
end
defp maybe_add_opt(opts, _key, nil), do: opts
defp maybe_add_opt(opts, key, value), do: Keyword.put(opts, key, value)
end