Page MenuHomePhorge

No OneTemporary

Size
10 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/telemetry.ex b/lib/tesla/middleware/telemetry.ex
index 8f3975c..f217c34 100644
--- a/lib/tesla/middleware/telemetry.ex
+++ b/lib/tesla/middleware/telemetry.ex
@@ -1,114 +1,111 @@
if Code.ensure_loaded?(:telemetry) do
defmodule Tesla.Middleware.Telemetry do
@moduledoc """
Emits events using the `:telemetry` library to expose instrumentation.
## Example usage
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.Telemetry
end
:telemetry.attach("my-tesla-telemetry", [:tesla, :request, :stop], fn event, measurements, meta, config ->
# Do something with the event
end)
```
- ## Options
-
- * `:event_prefix` - a list of atoms to prefix to the telemetry event name. This can be set if you need to distinguish events from different clients. Defaults to `[]`
-
## Telemetry Events
* `[:tesla, :request, :start]` - emitted at the beginning of the request.
* Measurement: `%{time: System.monotonic_time}`
- * Metadata: `%{env: Tesla.Env.t}`
+ * Metadata: `%{env: Tesla.Env.t()}`
* `[:tesla, :request, :stop]` - emitted at the end of the request.
* Measurement: `%{duration: native_time}`
- * Metadata: `%{env: Tesla.Env.t}`
+ * Metadata: `%{env: Tesla.Env.t()} | %{env: Tesla.Env.t, error: term}`
- * `[:tesla, :request, :error]` - emitted when there is an error.
+ * `[:tesla, :request, :fail]` - emitted when there is an error.
* Measurement: `%{value: 1}`
- * Metadata: `%{env: Tesla.Env.t, kind: Exception.kind | nil, reason: term, stacktrace: Exception.stacktrace}`
+ * Metadata: `%{env: Tesla.Env.t(), kind: Exception.kind | nil, reason: term, stacktrace: Exception.stacktrace}`
## Legacy Telemetry Events
* `[:tesla, :request]` - This event is emitted for backwards compatibility only and should be considered deprecated.
This event can be disabled by setting `config :tesla, Tesla.Middleware.Telemetry, disable_legacy_event: true` in your config. Be sure to run `mix deps.compile --force tesla` after changing this setting to ensure the change is picked up.
Please check the [telemetry](https://hexdocs.pm/telemetry/) for the further usage.
"""
@disable_legacy_event Application.get_env(:tesla, Tesla.Middleware.Telemetry,
disable_legacy_event: false
)[:disable_legacy_event]
@behaviour Tesla.Middleware
@impl Tesla.Middleware
- def call(env, next, opts) do
- prefix = Keyword.get(opts, :event_prefix, [])
+ def call(env, next, _opts) do
start_time = System.monotonic_time()
- emit_start(env, start_time, prefix)
+ emit_start(start_time, %{env: env})
try do
Tesla.run(env, next)
catch
kind, reason ->
stacktrace = System.stacktrace()
metadata = %{env: env, kind: kind, reason: reason, stacktrace: stacktrace}
- :telemetry.execute(
- prefix ++ [:tesla, :request, :error],
- %{value: 1},
- metadata
- )
-
- emit_stop(env, start_time, prefix, {:error, reason})
+ emit_fail(metadata)
:erlang.raise(kind, reason, stacktrace)
else
{:ok, env} = result ->
- emit_stop(env, start_time, prefix, result)
+ emit_stop(start_time, %{env: env})
+ emit_legacy_event(start_time, result)
+
result
- {:error, error} = result ->
- :telemetry.execute(
- prefix ++ [:tesla, :request, :error],
- %{value: 1},
- %{env: env, kind: nil, reason: error, stacktrace: []}
- )
+ {:error, reason} = result ->
+ emit_stop(start_time, %{env: env, error: reason})
+ emit_legacy_event(start_time, result)
- emit_stop(env, start_time, prefix, result)
result
end
end
- defp emit_start(env, start_time, prefix) do
- :telemetry.execute(prefix ++ [:tesla, :request, :start], %{time: start_time}, %{
- env: env
- })
+ defp emit_start(start_time, metadata) do
+ :telemetry.execute([:tesla, :request, :start], %{time: start_time}, metadata)
end
- defp emit_stop(env, start_time, prefix, result) do
+ defp emit_stop(start_time, metadata) do
duration = System.monotonic_time() - start_time
:telemetry.execute(
- prefix ++ [:tesla, :request, :stop],
+ [:tesla, :request, :stop],
%{duration: duration},
- %{env: env}
+ metadata
)
+ end
+ defp emit_legacy_event(start_time, result) do
if !@disable_legacy_event do
+ duration = System.monotonic_time() - start_time
+
# retained for backwards compatibility - remove in 2.0
:telemetry.execute([:tesla, :request], %{request_time: duration}, %{result: result})
end
end
+
+ defp emit_fail(metadata) do
+ :telemetry.execute(
+ [:tesla, :request, :fail],
+ %{value: 1},
+ metadata
+ )
+ end
end
end
diff --git a/test/tesla/middleware/telemetry_test.exs b/test/tesla/middleware/telemetry_test.exs
index 523bb55..add6e99 100644
--- a/test/tesla/middleware/telemetry_test.exs
+++ b/test/tesla/middleware/telemetry_test.exs
@@ -1,137 +1,76 @@
defmodule Tesla.Middleware.TelemetryTest do
use ExUnit.Case, async: true
defmodule Client do
use Tesla
plug Tesla.Middleware.Telemetry
adapter fn env ->
case env.url do
"/telemetry" -> {:ok, env}
"/telemetry_error" -> {:error, :econnrefused}
"/telemetry_exception" -> raise "some exception"
end
end
end
- defmodule ClientWithOptions do
- use Tesla
-
- plug Tesla.Middleware.Telemetry, event_prefix: [:my_client]
-
- adapter fn env ->
- case env.url do
- "/telemetry" -> {:ok, env}
- end
- end
- end
-
setup do
Application.ensure_all_started(:telemetry)
on_exit(fn ->
:telemetry.list_handlers([])
|> Enum.each(&:telemetry.detach(&1.id))
end)
:ok
end
- test "accepts options" do
- :telemetry.attach("with_opts", [:my_client, :tesla, :request, :stop], &echo_event/4, %{
- caller: self()
- })
-
- ClientWithOptions.get("/telemetry")
-
- assert_receive {:event, [:my_client, :tesla, :request, :stop], %{duration: time},
- %{env: %Tesla.Env{url: "/telemetry", method: :get}}}
- end
-
- test "with default options" do
- :telemetry.attach("with_default_opts_start", [:tesla, :request, :start], &echo_event/4, %{
- caller: self()
- })
+ test "events are all emitted properly" do
+ Enum.each(["/telemetry", "/telemetry_error"], fn path ->
+ :telemetry.attach("start event", [:tesla, :request, :start], &echo_event/4, %{
+ caller: self()
+ })
- :telemetry.attach("with_default_opts_stop", [:tesla, :request, :stop], &echo_event/4, %{
- caller: self()
- })
+ :telemetry.attach("stop event", [:tesla, :request, :stop], &echo_event/4, %{
+ caller: self()
+ })
- :telemetry.attach("with_default_opts_legacy", [:tesla, :request, :stop], &echo_event/4, %{
- caller: self()
- })
+ :telemetry.attach("legacy event", [:tesla, :request], &echo_event/4, %{
+ caller: self()
+ })
- Client.get("/telemetry")
+ Client.get(path)
- assert_receive {:event, [:tesla, :request, :start], %{time: time},
- %{env: %Tesla.Env{url: "/telemetry", method: :get}}}
+ assert_receive {:event, [:tesla, :request, :start], %{time: time},
+ %{env: %Tesla.Env{url: path, method: :get}}}
- assert_receive {:event, [:tesla, :request, :stop], %{duration: time},
- %{env: %Tesla.Env{url: "/telemetry", method: :get}}}
- end
+ assert_receive {:event, [:tesla, :request, :stop], %{duration: time},
+ %{env: %Tesla.Env{url: path, method: :get}}}
- test "legacy_event_emitted_by_default" do
- :telemetry.attach("with_default_opts_legacy", [:tesla, :request], &echo_event/4, %{
- caller: self()
- })
-
- Client.get("/telemetry")
-
- assert_receive {:event, [:tesla, :request], %{request_time: time}, %{result: result}}
- end
-
- test "with an error returned" do
- :telemetry.attach("with_error", [:tesla, :request, :error], &echo_event/4, %{caller: self()})
-
- :telemetry.attach("with_error_gets_stop", [:tesla, :request, :stop], &echo_event/4, %{
- caller: self()
- })
-
- Client.get("/telemetry_error")
-
- assert_receive {:event, [:tesla, :request, :error], %{value: 1},
- %{
- env: %Tesla.Env{url: "/telemetry_error", method: :get},
- kind: kind,
- reason: :econnrefused,
- stacktrace: []
- }}
-
- assert_receive {:event, [:tesla, :request, :stop], %{duration: time},
- %{
- env: %Tesla.Env{url: "/telemetry_error", method: :get}
- }}
+ assert_receive {:event, [:tesla, :request], %{request_time: time}, %{result: result}}
+ end)
end
test "with an exception raised" do
- :telemetry.attach("with_exception", [:tesla, :request, :error], &echo_event/4, %{
- caller: self()
- })
-
- :telemetry.attach("with_exception_gets_stop", [:tesla, :request, :stop], &echo_event/4, %{
+ :telemetry.attach("with_exception", [:tesla, :request, :fail], &echo_event/4, %{
caller: self()
})
assert_raise RuntimeError, fn ->
Client.get("/telemetry_exception")
end
- assert_receive {:event, [:tesla, :request, :error], %{value: 1},
+ assert_receive {:event, [:tesla, :request, :fail], %{value: 1},
%{
env: %Tesla.Env{url: "/telemetry_exception", method: :get},
kind: kind,
reason: reason,
stacktrace: stacktrace
}}
-
- assert_receive {:event, [:tesla, :request, :stop], %{duration: time},
- %{
- env: %Tesla.Env{url: "/telemetry_exception", method: :get}
- }}
end
def echo_event(event, measurements, metadata, config) do
send(config.caller, {:event, event, measurements, metadata})
end
end

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 9:42 PM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40080
Default Alt Text
(10 KB)

Event Timeline