Skip to content

Tesla plugin #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions lib/prom_ex/plugins/tesla.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
if Code.ensure_loaded?(Tesla) do
defmodule PromEx.Plugins.Tesla do
@moduledoc """
This plugin captures HTTP request metrics emitted by Tesla.

This plugin exposes the following metric group:
- `:tesla_http_event_metrics`

This plugin supports the following options:
- `otp_app`: This is a REQUIRED option and is the name of you application in snake case (e.g. :my_cool_app).

- `metric_prefix`: This option is OPTIONAL and is used to override the default metric prefix of
`[otp_app, :prom_ex, :application]`. If this changes you will also want to set `application_metric_prefix`
in your `dashboard_assigns` to the snakecase version of your prefix, the default
`application_metric_prefix` is `{otp_app}_prom_ex_application`.



To use plugin in your application, add the following to your PromEx module:

```
defmodule WebApp.PromEx do
use PromEx, otp_app: :web_app

@impl true
def plugins do
[
...
PromEx.Plugins.Tesla
]
end

@impl true
def dashboards do
[
...
{:prom_ex, "tesla.json"}
]
end
end
```


"""

use PromEx.Plugin

require Logger

# @start_event [:tesla, :request, :start]
@stop_event [:tesla, :request, :stop]
@exception_event [:tesla, :request, :exception]

@impl true
def event_metrics(opts) do
otp_app = Keyword.fetch!(opts, :otp_app)
metric_prefix = Keyword.get(opts, :metric_prefix, PromEx.metric_prefix(otp_app, :tesla))

[
http_events(metric_prefix, opts)
]
end

defp http_events(metric_prefix, _opts) do
http_metrics_tags = [:status, :method, :url]

Event.build(
:tesla_http_event_metrics,
[
distribution(
metric_prefix ++ [:http, :request, :duration, :milliseconds],
event_name: @stop_event,
measurement: :duration,
description: "The time it takes for an HTTP request to complete.",
reporter_options: [
buckets: [10, 100, 500, 1_000, 5_000, 10_000, 30_000]
],
tag_values: &get_tags(&1),
tags: http_metrics_tags,
unit: {:native, :millisecond}
),
distribution(
metric_prefix ++ [:http, :request, :exception, :duration, :milliseconds],
event_name: @exception_event,
measurement: :duration,
description: "The time it takes for an HTTP request to result in an exception.",
reporter_options: [
buckets: [10, 100, 500, 1_000, 5_000, 10_000, 30_000]
],
tag_values: &get_tags(&1),
tags: http_metrics_tags,
unit: {:native, :millisecond}
),
distribution(
metric_prefix ++ [:http, :response, :size, :bytes],
event_name: @stop_event,
description: "The size of the HTTP response payload.",
reporter_options: [
buckets: [64, 512, 4_096, 65_536, 262_144, 1_048_576, 4_194_304, 16_777_216]
],
measurement: &resp_body_size/2,
tag_values: &get_tags(&1),
tags: http_metrics_tags,
unit: :byte
),
counter(
metric_prefix ++ [:http, :requests, :total],
event_name: @stop_event,
description: "The number of requests that have been made.",
tag_values: &get_tags(&1),
tags: http_metrics_tags
)
]
)
end

defp get_tags(%{env: %Tesla.Env{status: status, method: method, url: url}}) do
%{
status: status,
method: method,
url: url
}
end

defp resp_body_size(_, metadata) do
case metadata.env.body do
nil -> 0
_ -> metadata.env.body |> :erlang.term_to_binary() |> :erlang.byte_size()
end
end
end
else
defmodule PromEx.Plugins.Tesla do
@moduledoc false
use PromEx.Plugin

@impl true
def event_metrics(_opts) do
PromEx.Plugin.no_dep_raise(__MODULE__, "Tesla")
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule PromEx.MixProject do
{:oban, ">= 2.4.0", optional: true},
{:absinthe, ">= 1.6.0", optional: true},
{:broadway, ">= 1.0.2", optional: true},
{:tesla, ">= 1.4.0", optional: true},

# PromEx development related dependencies
{:bypass, "~> 2.1", only: :test},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
"telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "1.0.2", "c98b1c580de637bfeac00db41b9fb91fb4c3548ee3d512a8ed7299172312eaf3", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "48351a0d56f80e38c997b44232b1043e0a081670d16766eee920e6254175b730"},
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
"tesla": {:hex, :tesla, "1.4.4", "bb89aa0c9745190930366f6a2ac612cdf2d0e4d7fff449861baa7875afd797b2", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.3", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "d5503a49f9dec1b287567ea8712d085947e247cb11b06bc54adb05bfde466457"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}
45 changes: 45 additions & 0 deletions test/prom_ex/plugins/tesla_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule PromEx.Plugins.TeslaTest do
use ExUnit.Case, async: false

alias PromEx.Plugins.Tesla
alias PromEx.Test.Support.{Events, Metrics}

defmodule WebApp.PromEx do
use PromEx, otp_app: :web_app

@impl true
def plugins do
[Tesla]
end
end

test "telemetry works for start/stop" do
start_supervised!(WebApp.PromEx)
Events.execute_all(:tesla)

metrics =
WebApp.PromEx
|> PromEx.get_metrics()
|> Metrics.sort()

assert metrics == Metrics.read_expected(:tesla)
end

describe "event_metrics/1" do
test "should return the correct number of metrics" do
assert length(Tesla.event_metrics(otp_app: :prom_ex)) == 1
end
end

describe "polling_metrics/1" do
test "should return the correct number of metrics" do
assert Tesla.polling_metrics([]) == []
end
end

describe "manual_metrics/1" do
test "should return the correct number of metrics" do
assert Tesla.manual_metrics(otp_app: :prom_ex) == []
end
end
end
58 changes: 58 additions & 0 deletions test/support/events/tesla.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[
%{
event: [:tesla, :request, :start],
measurements: %{system_time: 1_654_972_577_479_492_414},
metadata: %{
env: %Tesla.Env{
__client__: %Tesla.Client{adapter: nil, fun: nil, post: [], pre: []},
__module__: TestApp,
body: nil,
headers: [],
method: :get,
opts: [],
query: [],
status: nil,
url: "/users"
}
}
},
%{
event: [:tesla, :request, :stop],
measurements: %{duration: 100_115_245},
metadata: %{
env: %Tesla.Env{
__client__: %Tesla.Client{adapter: nil, fun: nil, post: [], pre: []},
__module__: TestApp,
body: [
%{
"age" => 46,
"first_name" => "Frankie",
"id" => "7cbd7a74-c09a-4d4d-a5b5-a9c25dab8ed3",
"last_name" => "Price",
"points" => 0
},
%{
"age" => 26,
"first_name" => "Matt",
"id" => "0c4a8465-2731-4ecf-9c93-80bfe6d9eac7",
"last_name" => "Hermann",
"points" => 0
}
],
headers: [
{"cache-control", "max-age=0, private, must-revalidate"},
{"date", "Sat, 11 Jun 2022 18:36:16 GMT"},
{"server", "Cowboy"},
{"content-length", "109512"},
{"content-type", "application/json; charset=utf-8"},
{"x-request-id", "FvekynXlvJB0pCMAAAGC"}
],
method: :get,
opts: [],
query: [],
status: 200,
url: "http://127.0.0.1:4000/users"
}
}
}
]
31 changes: 31 additions & 0 deletions test/support/metrics/tesla.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# HELP web_app_prom_ex_prom_ex_status_info Information regarding the PromEx library. Primarily used as a source of truth for Prometheus default labels.
# HELP web_app_prom_ex_tesla_http_request_duration_milliseconds The time it takes for an HTTP request to complete.
# HELP web_app_prom_ex_tesla_http_requests_total The number of requests that have been made.
# HELP web_app_prom_ex_tesla_http_response_size_bytes The size of the HTTP response payload.
# TYPE web_app_prom_ex_prom_ex_status_info gauge
# TYPE web_app_prom_ex_tesla_http_request_duration_milliseconds histogram
# TYPE web_app_prom_ex_tesla_http_requests_total counter
# TYPE web_app_prom_ex_tesla_http_response_size_bytes histogram
web_app_prom_ex_prom_ex_status_info 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="+Inf"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="10"} 0
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="100"} 0
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="1000"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="10000"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="30000"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="500"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="5000"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_count{method="get",status="200",url="http://127.0.0.1:4000/users"} 1
web_app_prom_ex_tesla_http_request_duration_milliseconds_sum{method="get",status="200",url="http://127.0.0.1:4000/users"} 100.115245
web_app_prom_ex_tesla_http_requests_total{method="get",status="200",url="http://127.0.0.1:4000/users"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="+Inf"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="1048576"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="16777216"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="262144"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="4096"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="4194304"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="512"} 1
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="64"} 0
web_app_prom_ex_tesla_http_response_size_bytes_bucket{method="get",status="200",url="http://127.0.0.1:4000/users",le="65536"} 1
web_app_prom_ex_tesla_http_response_size_bytes_count{method="get",status="200",url="http://127.0.0.1:4000/users"} 1
web_app_prom_ex_tesla_http_response_size_bytes_sum{method="get",status="200",url="http://127.0.0.1:4000/users"} 260