-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanuch.ex
109 lines (98 loc) · 2.58 KB
/
lanuch.ex
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
defmodule Membrane.OpenTelemetry.Plugs.Launch do
@moduledoc """
Attaches OpenTelemetry spans describing events during launch of the component.
Each span starts when the `handle_init` callback is invoked and lasts until the `handle_start_of_stream` invocation ends.
"""
alias __MODULE__.{HandlerFunctions, ETSWrapper}
@all_callbacks [
Membrane.Pipeline,
Membrane.Bin,
Membrane.Element.Base,
Membrane.Element.WithInputPads,
Membrane.Element.WithOutputPads
]
|> Enum.flat_map(fn module -> module.behaviour_info(:callbacks) end)
|> Keyword.keys()
|> Enum.uniq()
|> List.delete(:__struct__)
@spec plug() :: :ok
def plug() do
ETSWrapper.setup_ets_tables()
attach_telemetry_handlers()
:ok
end
@spec unplug() :: :ok
def unplug() do
detach_telemetry_handlers()
ETSWrapper.delete_ets_tables()
:ok
end
defp attach_telemetry_handlers() do
:ok =
attach(
:start_span,
:handle_init,
:start,
&HandlerFunctions.start_span/4
)
@all_callbacks
|> Enum.each(fn callback ->
:ok =
attach(
{callback, :start},
callback,
:start,
&HandlerFunctions.callback_start/4
)
:ok =
attach(
{callback, :stop},
callback,
:stop,
&HandlerFunctions.callback_stop/4
)
end)
:ok =
attach(
:maybe_end_span_on_start_of_stream,
:handle_start_of_stream,
:stop,
&HandlerFunctions.ensure_span_ended/4
)
:ok =
attach(
:maybe_end_span_on_playing,
:handle_playing,
:stop,
&HandlerFunctions.maybe_end_span/4
)
end
defp attach(id, callback, start_or_stop, handler) do
[:pipeline, :bin, :element]
|> Enum.each(fn component_type ->
:telemetry.attach(
{__MODULE__, component_type, id},
[:membrane, component_type, callback, start_or_stop],
handler,
nil
)
end)
end
defp detach_telemetry_handlers() do
@all_callbacks
|> Enum.flat_map(fn callback ->
[
{__MODULE__, callback, :start},
{__MODULE__, callback, :stop}
]
end)
|> Enum.concat([
{__MODULE__, :start_span},
{__MODULE__, :maybe_end_span_on_start_of_stream},
{__MODULE__, :maybe_end_span_on_playing}
])
|> Enum.each(fn handler_id ->
:ok = :telemetry.detach(handler_id)
end)
end
end