forked from FarmBot/farmbot_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarmbot_telemetry.ex
188 lines (165 loc) · 4.77 KB
/
farmbot_telemetry.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
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
defmodule FarmbotTelemetry do
@moduledoc """
Interface for farmbot system introspection and metrics
"""
@typedoc "Type of telemetry data"
@type kind() :: :event | :metric
@typedoc "UUID of the telemetry data"
@type uuid() :: String.t()
@typedoc "Classifier for telemetry data"
@type subsystem() :: atom()
@typedoc "Name of subsystem measurement data"
@type measurement() :: atom()
@typedoc "Value of subsystem measurement data"
@type value() :: number()
@typedoc "Metadata for a telemetry event"
@type meta() :: Keyword.t()
@doc "Merges environment data with existing metadata"
@spec telemetry_meta(Macro.Env.t(), map()) :: meta()
def telemetry_meta(env, meta) do
Map.merge(meta, %{
module: env.module,
file: env.file,
line: env.line,
function: env.function
})
end
@doc "Execute a telemetry event"
def event(subsystem, measurement_or_event_name, value \\ nil, meta \\ [])
def event(subsystem, measurement, value, meta)
when is_atom(subsystem) and
is_atom(measurement) and
is_list(meta) do
FarmbotTelemetry.bare_telemetry(
UUID.uuid4(),
:event,
subsystem,
measurement,
value,
DateTime.utc_now(),
Keyword.merge(meta,
module: __ENV__.module,
file: __ENV__.file,
line: __ENV__.line,
function: __ENV__.function
)
)
end
def event(subsystem, measurement, value, meta) do
Mix.raise("""
Unknown args for telemetry event:
#{inspect(subsystem)}, #{inspect(measurement)}, #{inspect(value)}, #{inspect(meta)}
""")
end
@doc """
Function responsible for firing telemetry events
stores telemetry event in `dets` table to be cached until
farmbot has a chance to dispatch them. This is required because
dispatching can only be done when farmbot as an active internet
connection.
Dispatching is done by calling the `consume_telemetry/1` function.
"""
@spec bare_telemetry(
uuid,
kind(),
subsystem(),
measurement(),
value(),
DateTime.t(),
meta()
) ::
:ok
def bare_telemetry(
uuid,
kind,
subsystem,
measurement,
value,
captured_at,
meta
)
when is_binary(uuid) and is_atom(kind) and is_atom(subsystem) and
is_atom(measurement) and
is_list(meta) do
_ =
:telemetry.execute(
[:farmbot, kind, subsystem],
%{
measurement: measurement,
value: value,
captured_at: captured_at,
uuid: uuid
},
Map.new(meta)
)
_ =
:dets.insert(
:farmbot,
{uuid, captured_at, kind, subsystem, measurement, value, Map.new(meta)}
)
end
@doc "Attach a logger to a kind and subsystem"
def attach_logger(kind, subsystem, config \\ []) do
:telemetry.attach(
"farmbot-telemetry-logger-#{kind}-#{subsystem}-#{UUID.uuid4()}",
[:farmbot, kind, subsystem],
&FarmbotTelemetry.log_handler/4,
config
)
end
@doc "Attach a message sender to a kind and subsystem"
def attach_recv(kind, subsystem, pid) do
:telemetry.attach(
"farmbot-telemetry-recv-#{kind}-#{subsystem}-#{UUID.uuid4()}",
[:farmbot, kind, subsystem],
&Kernel.send(pid, {&1, &2, &3, &4}),
pid: self()
)
end
@doc false
def log_handler(event, measurements, meta, config) do
Logger.bare_log(
config[:level] || :info,
"#{inspect(event)} | #{inspect(measurements)}",
Map.to_list(meta)
)
end
@typedoc "Function passed to `consume_telemetry/1`"
@type consumer_fun() ::
({uuid(), DateTime.t(), kind(), subsystem(), measurement(), value(),
meta()} ->
:ok | any())
@doc """
Syncronously consume telemetry events.
Function will be evaluated once for every telemetry event,
blocking until complete. Function should complete within
5 seconds per each event. Function should return `:ok` if
the event was successfully consumed, anything else will
cause the event to be put back on the queue
"""
@spec consume_telemetry(consumer_fun()) :: :ok
def consume_telemetry(fun) do
all_events = :dets.match_object(:farmbot, :_)
tasks =
Enum.map(all_events, fn event ->
{elem(event, 0), Task.async(fn -> fun.(event) end)}
end)
_ =
Enum.map(tasks, fn {uuid, task} ->
result =
try do
Task.await(task)
catch
kind, err ->
msg = "Telemetry error: #{kind} #{inspect(err)}"
IO.warn(msg, __STACKTRACE__)
nil
end
case result do
:ok -> :dets.delete(:farmbot, uuid)
_ -> :ok
end
end)
:ok
end
end