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 e439e41..525b67c 100644
--- a/lib/tesla/middleware/telemetry.ex
+++ b/lib/tesla/middleware/telemetry.ex
@@ -1,113 +1,114 @@
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: `%{time: System.monotonic_time}`
+ * Measurement: `%{start_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}`
+ * Metadata: `%{env: Tesla.Env.t()} | %{env: Tesla.Env.t(), error: term()}`
- * `[: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}`
+ * `[:tesla, :request, :failure]` - emitted when an exception has been raised.
+ * Measurement: `%{duration: native_time}`
+ * Metadata: `%{kind: Exception.kind(), reason: term(), stacktrace: Exception.stacktrace(), env: Tesla.Env.t()}`
## 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(start_time, %{env: env})
+ emit_start(%{env: env})
try do
Tesla.run(env, next)
catch
kind, reason ->
stacktrace = System.stacktrace()
+ duration = System.monotonic_time() - start_time
- emit_fail(%{env: env, kind: kind, reason: reason, stacktrace: stacktrace})
+ emit_failure(duration, %{kind: kind, reason: reason, stacktrace: stacktrace, env: env})
: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(time, metadata) do
- :telemetry.execute([:tesla, :request, :start], %{time: time}, metadata)
+ defp emit_start(metadata) do
+ :telemetry.execute([:tesla, :request, :start], %{start_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_fail(metadata) do
+ defp emit_failure(duration, metadata) do
:telemetry.execute(
- [:tesla, :request, :fail],
- %{value: 1},
+ [:tesla, :request, :failure],
+ %{duration: duration},
metadata
)
end
end
end
diff --git a/test/tesla/middleware/telemetry_test.exs b/test/tesla/middleware/telemetry_test.exs
index add6e99..40ed198 100644
--- a/test/tesla/middleware/telemetry_test.exs
+++ b/test/tesla/middleware/telemetry_test.exs
@@ -1,76 +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
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], %{time: time},
+ assert_receive {:event, [:tesla, :request, :start], %{start_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, :fail], &echo_event/4, %{
+ :telemetry.attach("with_exception", [:tesla, :request, :failure], &echo_event/4, %{
caller: self()
})
assert_raise RuntimeError, fn ->
Client.get("/telemetry_exception")
end
- assert_receive {:event, [:tesla, :request, :fail], %{value: 1},
+ assert_receive {:event, [:tesla, :request, :failure], %{duration: _},
%{
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, 11:03 PM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39755
Default Alt Text
(6 KB)

Event Timeline