Skip to content

Commit e515ea0

Browse files
committed
Checkpoint
1 parent 81dd1de commit e515ea0

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

.formatter.exs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Used by "mix format"
22
[
3+
line_length: 120,
34
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
45
]

lib/unplug.ex

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
defmodule Unplug do
22
@moduledoc """
3-
Documentation for `Unplug`.
4-
"""
3+
This plug acts as a filter for Plug.Telemetry and allows you to filter out
4+
events based on the incoming URL.
5+
6+
When servicing requests, Phoenix will log information about the incoming
7+
request and outgoing response. In some cases it may pollute the logs
8+
if certain endpoints are triggered and logged at regular intervals (for
9+
example logging for `/metrics` when using something like Prometheus to
10+
collect metrics or `/health` if you are health checking your application
11+
with Kubernetes).
512
6-
@doc """
7-
Hello world.
13+
To use this in your application, open up your `endpoint.ex` file and replace:
14+
`plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]`
815
9-
## Examples
16+
with
1017
11-
iex> Unplug.hello()
12-
:world
18+
`plug TelemetryFilter, filter_endpoints: ["/metrics", "/health"], event_prefix: [:phoenix, :endpoint]`
1319
20+
Feel free to replace the values for `:filter_endpoints` with whatever endpoints
21+
you would like to omit telemetry events for.
1422
"""
15-
def hello do
16-
:world
23+
24+
@behaviour Plug
25+
26+
plug(Unplug, run_plug: Plug.Telemetry, run_plug_opts: [], eval: {Unplug}, eval_opts: [])
27+
28+
@impl true
29+
def init(opts) do
30+
plug_telemetry_opts = Telemetry.init(opts)
31+
telemetry_filter_opts = Keyword.get(opts, :filter_endpoints, [])
32+
33+
{plug_telemetry_opts, telemetry_filter_opts}
34+
end
35+
36+
@impl true
37+
def call(conn, {plug_telemetry_opts, []}) do
38+
Telemetry.call(conn, plug_telemetry_opts)
39+
end
40+
41+
def call(conn, {plug_telemetry_opts, telemetry_filter_opts}) do
42+
if Enum.member?(telemetry_filter_opts, conn.request_path) do
43+
conn
44+
else
45+
Telemetry.call(conn, plug_telemetry_opts)
46+
end
1747
end
1848
end

mix.exs

+21-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ defmodule Unplug.MixProject do
55
[
66
app: :unplug,
77
version: "0.1.0",
8-
elixir: "~> 1.10",
8+
elixir: "~> 1.9",
9+
name: "Unplug",
10+
source_url: "https://github.com/akoutmos/unplug",
11+
homepage_url: "https://hex.pm/packages/unplug",
12+
description: "A plug that can be used to conditionally invoke other plugs",
913
start_permanent: Mix.env() == :prod,
14+
docs: [
15+
main: "readme",
16+
extras: ["README.md"]
17+
],
18+
package: package(),
1019
deps: deps()
1120
]
1221
end
@@ -18,11 +27,20 @@ defmodule Unplug.MixProject do
1827
]
1928
end
2029

30+
defp package() do
31+
[
32+
name: "unplug",
33+
files: ~w(lib mix.exs README.md LICENSE CHANGELOG.md),
34+
licenses: ["MIT"],
35+
links: %{"GitHub" => "https://github.com/akoutmos/unplug"}
36+
]
37+
end
38+
2139
# Run "mix help deps" to learn about dependencies.
2240
defp deps do
2341
[
24-
# {:dep_from_hexpm, "~> 0.3.0"},
25-
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
42+
{:plug, "~> 1.8"},
43+
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
2644
]
2745
end
2846
end

0 commit comments

Comments
 (0)