-
Notifications
You must be signed in to change notification settings - Fork 124
/
opentelemetry_phoenix.ex
225 lines (187 loc) · 6.47 KB
/
opentelemetry_phoenix.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
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
defmodule OpentelemetryPhoenix do
@options_schema NimbleOptions.new!(
endpoint_prefix: [
type: {:list, :atom},
default: [:phoenix, :endpoint],
doc: "The endpoint prefix in your endpoint."
],
adapter: [
type: {:in, [:cowboy2, :bandit]},
default: :cowboy2,
required: true,
doc: "The phoenix server adapter being used.",
type_doc: ":atom"
],
liveview: [
type: :boolean,
default: true,
doc: "Whether LiveView traces will be instrumented."
]
)
@moduledoc """
OpentelemetryPhoenix uses [telemetry](https://hexdocs.pm/telemetry/) handlers to create `OpenTelemetry` spans.
Current events which are supported include endpoint start/stop, router start/stop,
and router exceptions.
### Supported options
#{NimbleOptions.docs(@options_schema)}
#### Adapters
* `cowboy2` - when using PlugCowboy as your adapter you must add `:opentelemetry_cowboy` to your project
and pass `adapter: :cowboy2` option when calling setup.
* `bandit` - when using `Bandit.PhoenixAdapter` as your adapter you must add `:opentelemetry_bandit` to your project
and pass `adapter: :bandit` option when calling setup
## Usage
In your application start:
def start(_type, _args) do
:opentelemetry_cowboy.setup()
OpentelemetryPhoenix.setup(adapter: :cowboy2)
children = [
{Phoenix.PubSub, name: MyApp.PubSub},
MyAppWeb.Endpoint
]
opts = [strategy: :one_for_one, name: MyStore.Supervisor]
Supervisor.start_link(children, opts)
end
"""
alias OpenTelemetry.SemConv.Incubating.URLAttributes
alias OpenTelemetry.Tracer
require OpenTelemetry.Tracer
@tracer_id __MODULE__
@typedoc "Setup options"
@type opts :: [endpoint_prefix() | adapter()]
@typedoc "The endpoint prefix in your endpoint. Defaults to `[:phoenix, :endpoint]`"
@type endpoint_prefix :: {:endpoint_prefix, [atom()]}
@typedoc "The phoenix server adapter being used. Required"
@type adapter :: {:adapter, :cowboy2 | :bandit}
@doc """
Initializes and configures the telemetry handlers.
"""
@spec setup(opts()) :: :ok
def setup(opts \\ []) do
opts = NimbleOptions.validate!(opts, @options_schema)
attach_endpoint_start_handler(opts)
attach_router_start_handler(opts)
if opts[:liveview] do
attach_liveview_handlers()
end
:ok
end
@doc false
def attach_endpoint_start_handler(opts) do
:telemetry.attach(
{__MODULE__, :endpoint_start},
opts[:endpoint_prefix] ++ [:start],
&__MODULE__.handle_endpoint_start/4,
%{adapter: opts[:adapter]}
)
end
@doc false
def attach_router_start_handler(_opts) do
:telemetry.attach(
{__MODULE__, :router_dispatch_start},
[:phoenix, :router_dispatch, :start],
&__MODULE__.handle_router_dispatch_start/4,
%{}
)
end
def attach_liveview_handlers do
:telemetry.attach_many(
{__MODULE__, :live_view},
[
[:phoenix, :live_view, :mount, :start],
[:phoenix, :live_view, :mount, :stop],
[:phoenix, :live_view, :mount, :exception],
[:phoenix, :live_view, :handle_params, :start],
[:phoenix, :live_view, :handle_params, :stop],
[:phoenix, :live_view, :handle_params, :exception],
[:phoenix, :live_view, :handle_event, :start],
[:phoenix, :live_view, :handle_event, :stop],
[:phoenix, :live_view, :handle_event, :exception],
[:phoenix, :live_component, :handle_event, :start],
[:phoenix, :live_component, :handle_event, :stop],
[:phoenix, :live_component, :handle_event, :exception]
],
&__MODULE__.handle_liveview_event/4,
%{}
)
:ok
end
# TODO: do we still need exception handling? Only when cowboy?
@doc false
def handle_endpoint_start(_event, _measurements, _meta, %{adapter: :bandit}), do: :ok
def handle_endpoint_start(_event, _measurements, _meta, %{adapter: :cowboy2}) do
cowboy2_start()
end
defp cowboy2_start do
OpentelemetryProcessPropagator.fetch_parent_ctx()
|> OpenTelemetry.Ctx.attach()
end
@doc false
def handle_router_dispatch_start(_event, _measurements, meta, _config) do
attributes = %{
:"phoenix.plug" => meta.plug,
:"phoenix.action" => meta.plug_opts,
URLAttributes.url_template() => meta.route
}
Tracer.update_name("#{meta.conn.method} #{meta.route}")
Tracer.set_attributes(attributes)
end
def handle_liveview_event(
[:phoenix, _live, :mount, :start],
_measurements,
%{socket: %{view: live_view}} = meta,
_handler_configuration
) do
OpentelemetryTelemetry.start_telemetry_span(
@tracer_id,
"#{inspect(live_view)}.mount",
meta,
%{kind: :server}
)
end
def handle_liveview_event(
[:phoenix, _live, :handle_params, :start],
_measurements,
%{socket: %{view: live_view}} = meta,
_handler_configuration
) do
OpentelemetryTelemetry.start_telemetry_span(
@tracer_id,
"#{inspect(live_view)}.handle_params",
meta,
%{kind: :server}
)
end
def handle_liveview_event(
[:phoenix, _live, :handle_event, :start],
_measurements,
%{socket: %{view: live_view}, event: event} = meta,
_handler_configuration
) do
OpentelemetryTelemetry.start_telemetry_span(
@tracer_id,
"#{inspect(live_view)}.handle_event##{event}",
meta,
%{kind: :server}
)
end
def handle_liveview_event(
[:phoenix, _live, _event, :stop],
_measurements,
meta,
_handler_configuration
) do
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta)
end
def handle_liveview_event(
[:phoenix, _live, _action, :exception],
_,
%{kind: kind, reason: reason, stacktrace: stacktrace} = meta,
_
) do
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta)
exception = Exception.normalize(kind, reason, stacktrace)
OpenTelemetry.Span.record_exception(ctx, exception, stacktrace, [])
OpenTelemetry.Span.set_status(ctx, OpenTelemetry.status(:error, ""))
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta)
end
end