Search code examples
httpserverelixirprometheusmetrics

Elixir: start http server only for prometheus metrics


I have following code:

defmodule MyApp.Http do
  use Application

  require Logger

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    MyApp.PlugPipelineInstrumenter.setup()
    MyApp.MetricsExporter.setup()

    opts = [strategy: :one_for_one, name: MyApp.Http.Supervisor]
    Supervisor.start_link([], opts)
  end
end

defmodule MyApp.MetricsExporter do
  use Prometheus.PlugExporter
end

defmodule MyApp.PlugPipelineInstrumenter do
  use Prometheus.PlugPipelineInstrumenter
end

But it does nothing. When I add:

plug MyApp.MetricsExporter
plug MyApp.PlugPipelineInstrumenter

I get compilation error: undefined function plug/1. I use elixir 1.10.3. What do I do wrong here?


Solution

  • I made a hex package called prometheus_sidecar that creates a ranch server dedicated to prometheus (so you could run it in any elixir application) and wires up prometheus_plugs for you.

    It leverages erlang's application start lifecycle so there is no required configuration, besides adding it as a library to your application.

    Let me know if you find it useful.