View Source Tesla.Middleware.Telemetry (tesla v1.7.0)
Emits events using the :telemetry
library to expose instrumentation.
examples
Examples
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,
nil
)
options
Options
:metadata
- additional metadata passed to telemetry events
telemetry-events
Telemetry Events
[:tesla, :request, :start]
- emitted at the beginning of the request.- Measurement:
%{system_time: System.system_time()}
- Metadata:
%{env: Tesla.Env.t()}
- Measurement:
[: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()}
- Measurement:
[:tesla, :request, :exception]
- emitted when an exception has been raised.- Measurement:
%{duration: native_time}
- Metadata:
%{env: Tesla.Env.t(), kind: Exception.kind(), reason: term(), stacktrace: Exception.stacktrace()}
- Measurement:
legacy-telemetry-events
Legacy Telemetry Events
[:tesla, :request]
- This event is emitted for backwards compatibility only and should be considered deprecated. This event can be disabled by settingconfig :tesla, Tesla.Middleware.Telemetry, disable_legacy_event: true
in your config. Be sure to runmix deps.compile --force tesla
after changing this setting to ensure the change is picked up.
Please check the telemetry for the further usage.
url-event-scoping-with-tesla-middleware-pathparams-and-tesla-middleware-keeprequest
URL event scoping with Tesla.Middleware.PathParams
and Tesla.Middleware.KeepRequest
Sometimes, it is useful to have access to a template url (i.e. "/users/:user_id"
) for grouping
Telemetry events. For such cases, a combination of the Tesla.Middleware.PathParams
,
Tesla.Middleware.Telemetry
and Tesla.Middleware.KeepRequest
may be used.
defmodule MyClient do
use Tesla
# The KeepRequest middleware sets the template url as a Tesla.Env.opts entry
# Said entry must be used because on happy-path scenarios,
# the Telemetry middleware will receive the Tesla.Env.url resolved by PathParams.
plug Tesla.Middleware.KeepRequest
plug Tesla.Middleware.Telemetry
plug Tesla.Middleware.PathParams
end
:telemetry.attach(
"my-tesla-telemetry",
[:tesla, :request, :stop],
fn event, measurements, meta, config ->
path_params_template_url = meta.env.opts[:req_url]
# The meta.env.url key will only present the resolved URL on happy-path scenarios.
# Error cases will still return the original template url.
path_params_resolved_url = meta.env.url
end,
nil
)