PromEx Documentation

repository·master·Indexed 20 days ago

https://github.com/akoutmos/prom_ex

A framework for managing the lifecycle of Prometheus metrics and Grafana dashboards for Elixir libraries. PromEx provides plugins for BEAM, Phoenix, Ecto, Oban, Phoenix LiveView, Absinthe, Plug.Cowboy, Plug.Router, and Broadway. It includes tools for exposing metrics via PromEx.Plug or a standalone HTTP server, generating configurations via mix tasks, and integrating with Grafana Cloud using the Grafana Agent.

Tokens
12.2K
Snippets
42
Records
60
Agent score
66%

What's inside PromEx

  1. PromEx core utilities and capabilities

    master

    PromEx provides several tools to manage the observability lifecycle:

    • PromEx.Plug: Exposes metrics in Phoenix or Plug applications.
    • Standalone HTTP Server: For projects where Phoenix is not a dependency.
    • Mix Tasks: For generating configuration and uploading complimentary Grafana dashboards.
    • Behaviours: PromEx.Plugin (for defining plugin logic) and a behaviour for the metrics capture module.
    • Grafana Integration: Tailored dashboards for plugins, Grafana API support for uploading dashboards/annotations, and EEx templates for dynamic dashboard tweaking.
  2. What is Telemetry and how does it work?

    master

    Telemetry is a library used by Elixir and Erlang applications to surface internal events. It allows libraries to emit events (such as the start of a task, completion of work, or an exception) which can then be consumed by attached callbacks.

    Key Characteristics

    • Pub/Sub Model: It functions like a synchronous pub/sub system. When an event is triggered, Telemetry dynamically dispatches to all registered callbacks for that specific event.
    • Synchronous Execution: Callbacks are executed serially and synchronously. Because of this, callbacks must be kept extremely lightweight. If you need to perform blocking work or heavy processing in response to an event, you should offload that work to a separate Task or GenServer to avoid blocking the event emitter.
    • Low Friction: It requires no global configuration. Events are identified by a list of atoms (e.g., [:my_app, :module, :event]), and callbacks are attached dynamically.
  3. Implement the PromEx.Plugin behaviour

    master

    To create a custom PromEx plugin, your module must use the PromEx.Plugin module (which implements the PromEx behaviour). A plugin is used to load metrics for a dependent library or for internal application metrics.

    Your module must implement one or more of the following optional callbacks:

    • event_metrics(opts): For metrics triggered by specific events (e.g., HTTP requests).
    • polling_metrics(opts): For metrics that are sampled at a regular interval.
    • manual_metrics(opts): For metrics that are sampled once at startup and only updated when manually triggered.

    Each callback should return a list of metric type structs (e.g., PromEx.MetricTypes.Polling) or a single struct. These structs contain a :metrics field which holds a list of Telemetry.Metrics definitions created via the Event.build/2, Polling.build/4, or Manual.build/3 functions.

    defmodule MyApp.PromEx.Plugins.MyPlugin do
      use PromEx.Plugin
    
      @impl true
      def event_metrics(opts) do
        # ...
      end
    end
  4. Start PromEx early in the supervision tree

    master
    To ensure all initialization events from libraries like Ecto, Phoenix, and Oban are captured, PromEx must be started before these libraries. If PromEx is started after these supervision trees, you will miss their initial metrics and events.