Page MenuHomePhorge

No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/telemetry.ex b/lib/tesla/middleware/telemetry.ex
index 2a6f61e..9f452fa 100644
--- a/lib/tesla/middleware/telemetry.ex
+++ b/lib/tesla/middleware/telemetry.ex
@@ -1,118 +1,118 @@
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)
```
## Telemetry Events
* `[:tesla, :request, :start]` - emitted at the beginning of the request.
* Measurement: `%{system_time: System.system_time()}`
* Metadata: `%{env: Tesla.Env.t()}`
* `[:tesla, :request, :stop]` - emitted at the end of the request.
* Measurement: `%{duration: native_time}`
* Metadata: `%{env: Tesla.Env.t()} | %{env: Tesla.Env.t(), error: term()}`
- * `[:tesla, :request, :failure]` - emitted when an exception has been raised.
+ * `[:tesla, :request, :exception]` - emitted when an exception has been raised.
* Measurement: `%{duration: native_time}`
- * Metadata: `%{kind: Exception.kind(), reason: term(), stacktrace: Exception.stacktrace(), env: Tesla.Env.t()}`
+ * Metadata: `%{kind: Exception.kind(), 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
start_time = System.monotonic_time()
emit_start(%{env: env})
try do
Tesla.run(env, next)
catch
kind, reason ->
stacktrace = System.stacktrace()
duration = System.monotonic_time() - start_time
- emit_failure(duration, %{kind: kind, reason: reason, stacktrace: stacktrace, env: env})
+ emit_exception(duration, %{kind: kind, reason: reason, stacktrace: stacktrace})
:erlang.raise(kind, reason, stacktrace)
else
{:ok, env} = result ->
duration = System.monotonic_time() - start_time
emit_stop(duration, %{env: env})
emit_legacy_event(duration, result)
result
{:error, reason} = result ->
duration = System.monotonic_time() - start_time
emit_stop(duration, %{env: env, error: reason})
emit_legacy_event(duration, result)
result
end
end
defp emit_start(metadata) do
:telemetry.execute(
[:tesla, :request, :start],
%{system_time: System.system_time()},
metadata
)
end
defp emit_stop(duration, metadata) do
:telemetry.execute(
[:tesla, :request, :stop],
%{duration: duration},
metadata
)
end
defp emit_legacy_event(duration, result) do
if !@disable_legacy_event do
:telemetry.execute(
[:tesla, :request],
%{request_time: duration},
%{result: result}
)
end
end
- defp emit_failure(duration, metadata) do
+ defp emit_exception(duration, metadata) do
:telemetry.execute(
- [:tesla, :request, :failure],
+ [:tesla, :request, :exception],
%{duration: duration},
metadata
)
end
end
end
diff --git a/test/tesla/middleware/telemetry_test.exs b/test/tesla/middleware/telemetry_test.exs
index 55185c4..6986333 100644
--- a/test/tesla/middleware/telemetry_test.exs
+++ b/test/tesla/middleware/telemetry_test.exs
@@ -1,76 +1,75 @@
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
setup do
Application.ensure_all_started(:telemetry)
on_exit(fn ->
:telemetry.list_handlers([])
|> Enum.each(&:telemetry.detach(&1.id))
end)
:ok
end
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("stop event", [:tesla, :request, :stop], &echo_event/4, %{
caller: self()
})
:telemetry.attach("legacy event", [:tesla, :request], &echo_event/4, %{
caller: self()
})
Client.get(path)
assert_receive {:event, [:tesla, :request, :start], %{system_time: time},
%{env: %Tesla.Env{url: path, method: :get}}}
assert_receive {:event, [:tesla, :request, :stop], %{duration: time},
%{env: %Tesla.Env{url: path, 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, :failure], &echo_event/4, %{
+ :telemetry.attach("with_exception", [:tesla, :request, :exception], &echo_event/4, %{
caller: self()
})
assert_raise RuntimeError, fn ->
Client.get("/telemetry_exception")
end
- assert_receive {:event, [:tesla, :request, :failure], %{duration: time},
+ assert_receive {:event, [:tesla, :request, :exception], %{duration: time},
%{
- env: %Tesla.Env{url: "/telemetry_exception", method: :get},
kind: kind,
reason: reason,
stacktrace: stacktrace
}}
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, 10:46 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39529
Default Alt Text
(6 KB)

Event Timeline