Elixir:仅为 prometheus 指标启动 http 服务器

Elixir: start http server only for prometheus metrics

我有以下代码:

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

但它什么也没做。当我添加:

plug MyApp.MetricsExporter
plug MyApp.PlugPipelineInstrumenter

我收到编译错误:undefined function plug/1。我使用长生不老药 1.10.3。我在这里做错了什么?

我给你做了一个叫做prometheus_sidecar that creates a ranch server dedicated to prometheus (so you could run it in any elixir application) and wires up prometheus_plugs的十六进制包。

它利用了 erlang 的应用程序启动生命周期,因此除了将其作为库添加到您的应用程序之外,不需要任何配置。

如果觉得有用请告诉我。