Page MenuHomePhorge

No OneTemporary

Size
8 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/telemetry.ex b/lib/tesla/middleware/telemetry.ex
index bc12982..e633a51 100644
--- a/lib/tesla/middleware/telemetry.ex
+++ b/lib/tesla/middleware/telemetry.ex
@@ -1,100 +1,105 @@
if Code.ensure_loaded?(:telemetry) do
defmodule Tesla.Middleware.Telemetry do
@behaviour Tesla.Middleware
@moduledoc """
Emits events using the `:telemetry` library to expose instrumentation.
### Example usage
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.Telemetry, prefix: [:my_client]
end
:telemetry.attach("my-tesla-telemetry", [:my_client, :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. Defaults to `[]`
## Telemetry Events
* `[:tesla, :request, :start]` - emitted at the beginning of the request.
* Measurement: `%{time: System.monotonic_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}`
* `[:tesla, :request, :error]` - emitted at the end of the request when there is an error.
* Measurement: `%{duration: native_time}`
* Metadata: `%{env: Tesla.Env.t, reason: term}`
* `[:tesla, :request, :exception]` - emitted at the end of the request when an exception is raised.
* Measurement: `%{duration: native_time}`
* Metadata: `%{env: Tesla.Env.t, exception: Exception.t, stacktrace: Exception.stacktrace}`
Please check the [telemetry](https://hexdocs.pm/telemetry/) for the further usage.
"""
@doc false
- def call(req_env, next, opts) do
+ def call(env, next, opts) do
prefix = Keyword.get(opts, :event_prefix, [])
start_time = System.monotonic_time()
- try do
- emit_start(req_env, start_time, prefix)
+ emit_start(env, start_time, prefix)
- Tesla.run(req_env, next)
- |> emit_result(start_time, prefix, req_env)
- rescue
- e ->
- stacktrace = System.stacktrace()
- metadata = %{env: req_env, exception: e, stacktrace: stacktrace}
+ result =
+ try do
+ Tesla.run(env, next)
+ rescue
+ e ->
+ stacktrace = System.stacktrace()
+ metadata = %{env: env, exception: e, stacktrace: stacktrace}
- :telemetry.execute(
- prefix ++ [:tesla, :request, :exception],
- %{duration: System.monotonic_time() - start_time},
- metadata
- )
+ :telemetry.execute(
+ prefix ++ [:tesla, :request, :exception],
+ %{duration: System.monotonic_time() - start_time},
+ metadata
+ )
- reraise e, stacktrace
- end
+ reraise e, stacktrace
+ end
+
+ emit_stop(result, start_time, prefix, env)
+
+ result
end
- defp emit_start(req_env, start_time, prefix) do
+ defp emit_start(env, start_time, prefix) do
:telemetry.execute(prefix ++ [:tesla, :request, :start], %{time: start_time}, %{
- env: req_env
+ env: env
})
end
- defp emit_result(result, start_time, prefix, req_env) do
- try do
- result
- after
- case result do
- {:ok, env} ->
- :telemetry.execute(
- prefix ++ [:tesla, :request, :stop],
- %{duration: System.monotonic_time() - start_time},
- %{env: env}
- )
+ defp emit_stop(result, start_time, prefix, req_env) do
+ duration = System.monotonic_time() - start_time
- {:error, error} ->
- :telemetry.execute(
- prefix ++ [:tesla, :request, :error],
- %{duration: System.monotonic_time() - start_time},
- %{env: req_env, reason: error}
- )
- end
+ case result do
+ {:ok, env} ->
+ :telemetry.execute(
+ prefix ++ [:tesla, :request, :stop],
+ %{duration: duration},
+ %{env: env}
+ )
+
+ {:error, error} ->
+ :telemetry.execute(
+ prefix ++ [:tesla, :request, :error],
+ %{duration: duration},
+ %{env: req_env, reason: error}
+ )
end
+
+ # retained for backwards compatibility - remove in 2.0
+ :telemetry.execute([:tesla, :request], %{request_time: duration}, %{result: result})
end
end
end
diff --git a/mix.exs b/mix.exs
index 4d5da12..a4d3399 100644
--- a/mix.exs
+++ b/mix.exs
@@ -1,134 +1,134 @@
defmodule Tesla.Mixfile do
use Mix.Project
@version "1.3.0"
def project do
[
app: :tesla,
version: @version,
description: description(),
package: package(),
source_ref: "v#{@version}",
source_url: "https://github.com/teamon/tesla",
elixir: "~> 1.5",
elixirc_paths: elixirc_paths(Mix.env()),
deps: deps(),
lockfile: lockfile(System.get_env("LOCKFILE")),
test_coverage: [tool: ExCoveralls],
dialyzer: [
plt_add_apps: [:inets],
plt_add_deps: :project
],
docs: docs()
]
end
# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[applications: applications(Mix.env())]
end
def applications(:test), do: applications(:dev) ++ [:httparrot, :hackney, :ibrowse, :gun]
def applications(_), do: [:logger, :ssl, :inets]
defp description do
"HTTP client library, with support for middleware and multiple adapters."
end
defp package do
[
maintainers: ["Tymon Tobolski"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/teamon/tesla"}
]
end
# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
defp lockfile(nil), do: "mix.lock"
defp lockfile(lockfile), do: "test/lockfiles/#{lockfile}.lock"
defp deps do
[
{:mime, "~> 1.0"},
# http clients
{:ibrowse, "~> 4.4.0", optional: true},
{:hackney, "~> 1.6", optional: true},
{:gun, "~> 1.3", optional: true},
{:castore, "~> 0.1", optional: true},
{:mint, "~> 0.4", optional: true},
# json parsers
{:jason, ">= 1.0.0", optional: true},
{:poison, ">= 1.0.0", optional: true},
{:exjsx, ">= 3.0.0", optional: true},
# other
{:fuse, "~> 2.4", optional: true},
- {:telemetry, "~> 0.3", optional: true},
+ {:telemetry, "~> 0.4", optional: true},
# testing & docs
{:excoveralls, "~> 0.8", only: :test},
{:httparrot, "~> 1.2", only: :test},
{:ex_doc, "~> 0.21.1", only: :dev},
{:mix_test_watch, "~> 0.5", only: :dev},
{:dialyxir, "~> 1.0.0-rc.3", only: [:dev, :test]},
{:inch_ex, "~> 0.5.6", only: :docs}
]
end
defp docs do
[
main: "readme",
extras: ["README.md"],
groups_for_modules: [
Behaviours: [
Tesla.Adapter,
Tesla.Middleware
],
Adapters: [
Tesla.Adapter.Gun,
Tesla.Adapter.Hackney,
Tesla.Adapter.Httpc,
Tesla.Adapter.Ibrowse,
Tesla.Adapter.Mint
],
Middlewares: [
Tesla.Middleware.BaseUrl,
Tesla.Middleware.BasicAuth,
Tesla.Middleware.Compression,
Tesla.Middleware.CompressRequest,
Tesla.Middleware.DecodeJson,
Tesla.Middleware.DecodeRels,
Tesla.Middleware.DecompressResponse,
Tesla.Middleware.DigestAuth,
Tesla.Middleware.EncodeJson,
Tesla.Middleware.FollowRedirects,
Tesla.Middleware.FormUrlencoded,
Tesla.Middleware.Fuse,
Tesla.Middleware.Headers,
Tesla.Middleware.JSON,
Tesla.Middleware.KeepRequest,
Tesla.Middleware.Logger,
Tesla.Middleware.MethodOverride,
Tesla.Middleware.Opts,
Tesla.Middleware.PathParams,
Tesla.Middleware.Query,
Tesla.Middleware.Retry,
Tesla.Middleware.Telemetry,
Tesla.Middleware.Timeout
]
],
nest_modules_by_prefix: [
Tesla.Adapter,
Tesla.Middleware
]
]
end
end

File Metadata

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

Event Timeline