Provide metrics on root URL
This commit is contained in:
45
lib/kafkaex_lag_exporter_web/endpoint.ex
Normal file
45
lib/kafkaex_lag_exporter_web/endpoint.ex
Normal file
@@ -0,0 +1,45 @@
|
||||
defmodule KafkaexLagExporterWeb.Endpoint do
|
||||
use Phoenix.Endpoint, otp_app: :kafkaex_lag_exporter
|
||||
|
||||
plug PromEx.Plug, path: "/", prom_ex_module: KafkaexLagExporter.PromEx
|
||||
|
||||
# The session will be stored in the cookie and signed,
|
||||
# this means its contents can be read but not tampered with.
|
||||
# Set :encryption_salt if you would also like to encrypt it.
|
||||
@session_options [
|
||||
store: :cookie,
|
||||
key: "_kafkaex_lag_exporter_key",
|
||||
signing_salt: "f/R6/xEO"
|
||||
]
|
||||
|
||||
# socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
|
||||
|
||||
# Serve at "/" the static files from "priv/static" directory.
|
||||
#
|
||||
# You should set gzip to true if you are running phx.digest
|
||||
# when deploying your static files in production.
|
||||
plug Plug.Static,
|
||||
at: "/",
|
||||
from: :kafkaex_lag_exporter,
|
||||
gzip: false,
|
||||
only: ~w(assets fonts images favicon.ico robots.txt)
|
||||
|
||||
# Code reloading can be explicitly enabled under the
|
||||
# :code_reloader configuration of your endpoint.
|
||||
if code_reloading? do
|
||||
plug Phoenix.CodeReloader
|
||||
end
|
||||
|
||||
plug Plug.RequestId
|
||||
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
|
||||
|
||||
plug Plug.Parsers,
|
||||
parsers: [:urlencoded, :multipart, :json],
|
||||
pass: ["*/*"],
|
||||
json_decoder: Phoenix.json_library()
|
||||
|
||||
plug Plug.MethodOverride
|
||||
plug Plug.Head
|
||||
plug Plug.Session, @session_options
|
||||
plug KafkaexLagExporterWeb.Router
|
||||
end
|
||||
11
lib/kafkaex_lag_exporter_web/router.ex
Normal file
11
lib/kafkaex_lag_exporter_web/router.ex
Normal file
@@ -0,0 +1,11 @@
|
||||
defmodule KafkaexLagExporterWeb.Router do
|
||||
use KafkaexLagExporterWeb, :router
|
||||
|
||||
pipeline :api do
|
||||
plug :accepts, ["json"]
|
||||
end
|
||||
|
||||
scope "/api", KafkaexLagExporterWeb do
|
||||
pipe_through :api
|
||||
end
|
||||
end
|
||||
50
lib/kafkaex_lag_exporter_web/telemetry.ex
Normal file
50
lib/kafkaex_lag_exporter_web/telemetry.ex
Normal file
@@ -0,0 +1,50 @@
|
||||
defmodule KafkaexLagExporterWeb.Telemetry do
|
||||
@moduledoc false
|
||||
|
||||
use Supervisor
|
||||
import Telemetry.Metrics
|
||||
|
||||
def start_link(arg) do
|
||||
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_arg) do
|
||||
children = [
|
||||
# Telemetry poller will execute the given period measurements
|
||||
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
|
||||
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
|
||||
# Add reporters as children of your supervision tree.
|
||||
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
end
|
||||
|
||||
def metrics do
|
||||
[
|
||||
# Phoenix Metrics
|
||||
summary("phoenix.endpoint.stop.duration",
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
summary("phoenix.router_dispatch.stop.duration",
|
||||
tags: [:route],
|
||||
unit: {:native, :millisecond}
|
||||
),
|
||||
|
||||
# VM Metrics
|
||||
summary("vm.memory.total", unit: {:byte, :kilobyte}),
|
||||
summary("vm.total_run_queue_lengths.total"),
|
||||
summary("vm.total_run_queue_lengths.cpu"),
|
||||
summary("vm.total_run_queue_lengths.io")
|
||||
]
|
||||
end
|
||||
|
||||
defp periodic_measurements do
|
||||
[
|
||||
# A module, function and arguments to be invoked periodically.
|
||||
# This function must call :telemetry.execute/3 and a metric must be added above.
|
||||
# {KafkaexLagExporterWeb, :count_users, []}
|
||||
]
|
||||
end
|
||||
end
|
||||
16
lib/kafkaex_lag_exporter_web/views/error_helpers.ex
Normal file
16
lib/kafkaex_lag_exporter_web/views/error_helpers.ex
Normal file
@@ -0,0 +1,16 @@
|
||||
defmodule KafkaexLagExporterWeb.ErrorHelpers do
|
||||
@moduledoc """
|
||||
Conveniences for translating and building error messages.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Translates an error message.
|
||||
"""
|
||||
def translate_error({msg, opts}) do
|
||||
# Because the error messages we show in our forms and APIs
|
||||
# are defined inside Ecto, we need to translate them dynamically.
|
||||
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
16
lib/kafkaex_lag_exporter_web/views/error_view.ex
Normal file
16
lib/kafkaex_lag_exporter_web/views/error_view.ex
Normal file
@@ -0,0 +1,16 @@
|
||||
defmodule KafkaexLagExporterWeb.ErrorView do
|
||||
use KafkaexLagExporterWeb, :view
|
||||
|
||||
# If you want to customize a particular status code
|
||||
# for a certain format, you may uncomment below.
|
||||
# def render("500.json", _assigns) do
|
||||
# %{errors: %{detail: "Internal Server Error"}}
|
||||
# end
|
||||
|
||||
# By default, Phoenix returns the status message from
|
||||
# the template name. For example, "404.json" becomes
|
||||
# "Not Found".
|
||||
def template_not_found(template, _assigns) do
|
||||
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user