promxy

repository·master·Indexed 23 days ago

https://github.com/jacksontj/promxy

A Prometheus proxy that aggregates multiple Prometheus shards into a single API endpoint. It enables global PromQL aggregations, provides high availability by merging data from duplicate hosts in ServerGroups, and requires no changes to existing Prometheus installations. It is compatible with Prometheus versions 1.7 through 2.13+ and uses a fork based on Prometheus 2.24 for its internal engine.

Tokens
5.4K
Snippets
12
Records
30
Agent score
79%

What's inside promxy

  1. What is Promxy and why use it?

    master

    Promxy is a proxy designed to solve scaling and redundancy challenges in Prometheus deployments. It acts as a single entry point for your metrics, addressing several common pain points encountered when scaling Prometheus horizontally:

    • Eliminates Data Holes: Unlike standard load balancing between Prometheus replicas (which causes gaps in graphs during node reboots or failures), Promxy provides seamless data continuity.
    • Single Grafana Datasource: Instead of managing multiple Prometheus datasources or using complex 'mixed' sources in Grafana to query across shards/regions, you can point Grafana to a single Promxy endpoint.
    • Removes the Need for Aggregation Layers: Promxy replaces the need for complex Prometheus federation/aggregation shards. This reduces the load on your existing shards and eliminates the need to maintain separate alerting rules for an aggregation layer.
    • Access Logging: Provides access logs for your metrics queries, which is not natively available in standard Prometheus setups.
  2. High-level overview of Promxy

    master

    Promxy is a Prometheus proxy designed to make multiple shards of Prometheus appear as a single, unified API endpoint. It simplifies operations at scale by providing a single datasource for all PromQL queries, enabling global aggregations and High Availability (HA).

    Key benefits include:

    • Unified Access: Use a single datasource in tools like Grafana instead of managing multiple sources.
    • High Availability: Promxy can "merge" data from duplicate hosts in a ServerGroup, filling in data gaps caused by reboots or upgrades.
    • Global Aggregation: Enables PromQL queries that aggregate data across your entire Prometheus infrastructure.
    • Zero Infrastructure Changes: Requires no sidecars, custom builds, or changes to your existing Prometheus installation.
  3. What is a ServerGroup?

    master

    A ServerGroup is a set of Prometheus hosts configured identically. This is a common pattern for achieving High Availability in Prometheus.

    Promxy uses ServerGroups to:

    • Merge Data: It aggregates data from all hosts within the group.
    • Fill Gaps: If one host in the group has a gap in its time series (e.g., during a reboot or upgrade), Promxy will attempt to fill that gap using data from the other hosts in the same group.
  4. Using alerting and recording rules in Promxy

    master

    Because Promxy acts as an aggregating proxy, you can define alerting and recording rules that execute across your entire Prometheus infrastructure (e.g., calculating a global error rate).

    Important: Unlike standard Prometheus, Promxy does not have a local TSDB. To use recording rules or see metrics from alerting rules, you must define a remote_write endpoint in your Promxy configuration so that Promxy has a destination to send those metrics.

  5. Secure Promxy using Kubernetes Ingress or a Proxy

    master

    Promxy does not currently include built-in authentication mechanisms. To secure your Promxy deployment, you should implement authentication at the network edge using one of the following methods:

    1. Kubernetes Ingress: Use an Ingress controller (such as Nginx) to handle authentication before traffic reaches Promxy.
    2. Reverse Proxy: Deploy Nginx or another proxy as an authentication endpoint in front of Promxy to manage access (e.g., using Basic Auth).
  6. Install and run Promxy

    master

    You can use pre-built release binaries or build from source.

    Build from source

    To build Promxy from the repository, clone the repo and use go build with the required tags:

    git clone git@github.com:jacksontj/promxy.git
    cd promxy/cmd/promxy && go build -mod=vendor -tags netgo,builtinassets

    Run Promxy

    Once you have a configured config.yaml file, start the server using the --config flag:

    ./promxy --config=config.yaml
  7. Reload Promxy configuration

    master

    Promxy supports dynamic configuration reloading without restarting the process. You can trigger a reload using one of the following methods:

    1. SIGHUP Signal: Send a SIGHUP signal to the Promxy process.
    2. HTTP Lifecycle API: If --web.enable-lifecycle is enabled, you can trigger a reload via an HTTP request (typically POST /-/reload).
  8. Configure Label Filtering and Matcher Injection

    master

    Promxy provides two primary ways to scope or restrict queries sent to a specific server_group:

    Label Filtering (label_filter)

    Restricts queries to only those that match a specific set of labels known to exist in the downstream. This is not a security mechanism but an optimization.

    • dynamic_labels: A list of labels (like __name__ or job) that Promxy will periodically query from the downstream to build the filter.
    • sync_interval: How often to re-sync dynamic labels.
    • static_labels_include: A map of labels that MUST be present in the query.
    • static_labels_exclude: A map of labels that MUST NOT be present in the query.
    • on_sync_error: Behavior when the filter cannot sync (e.g., target is down). Options: abort (default), open (proceed without filtering), or closed (skip target until sync succeeds).

    Matcher Injection (inject_matchers)

    Unlike labels (which adds labels to the result), inject_matchers modifies the query itself. It adds specific PromQL matchers to every selector sent to the group. This is ideal for multi-tenant setups where you want to ensure a user only sees data for their specific cluster/tenant.

    Example: If you set inject_matchers: ['cluster="A"'], a query for up becomes up{cluster="A"}.

          label_filter:
            dynamic_labels:
              - __name__
              - job
            sync_interval: 5m
            on_sync_error: abort
    
          inject_matchers:
            - 'cluster="A"'
  9. Configure ServerGroup error handling with ignore_error

    master

    By default, if an entire ServerGroup is unavailable, Promxy returns an error to prevent returning inaccurate or incomplete data.

    If you prefer to receive whatever data is available even if some nodes are down, you can make a servergroup "optional" by using the ignore_error option in your configuration.

  10. Run Promxy using Docker Compose

    master

    You can deploy Promxy using the provided docker-compose.yml configuration. The setup includes Promxy, VictoriaMetrics (as a storage backend), and Alertmanager.

    By default, the Promxy service is configured with the following settings:

    • Image: quay.io/jacksontj/promxy
    • Port Mapping: 8082:8082
    • Default Command Flags:
      • --config=/cmd/promxy/config.yaml: Specifies the path to the Promxy configuration file.
      • --log-level=info: Sets the logging verbosity.
      • --web.enable-lifecycle: Enables the HTTP endpoint for managing the server lifecycle (e.g., reloading configuration via POST requests).
    • Volumes: Uses logvolume01 for /var/log and mounts a local directory to /cmd to provide configuration files.
    services:
      promxy:
        container_name: promxy
        image: quay.io/jacksontj/promxy
        ports:
        - "8082:8082"
        command:
          - --config=/cmd/promxy/config.yaml
          - --log-level=info
          - --web.enable-lifecycle
  11. Run Promxy via CLI

    master
    Promxy is a Prometheus proxy that can be started from the command line. You can specify configuration files, logging preferences, and query limits using various flags. By default, it listens on :8082 and uses config.yaml as the configuration file.
  12. Configure Alerting and Alert Templates

    master

    Promxy can manage alerting settings and customize the URLs sent to an Alertmanager.

    Alertmanager Configuration

    Under the alerting block, you define alertmanagers targets. You can also use http_headers to inject authentication tokens (like X-Gateway-Signature) if the Alertmanager is behind a gateway.

    Custom Alert Templates

    The alert_templates block allows you to customize the GeneratorURL sent with alerts. You can use Go text/template syntax with access to .ExternalURL, .Expr, .AlertName, .Labels, and .Annotations.

    Templates can be defined in three ways:

    1. default: An inline template or a reference to a named template.
    2. named: A collection of reusable templates.
    3. rules: A list of rules that select a template based on match_labels (evaluated top-to-bottom).
    alerting:
      alertmanagers:
      - scheme: http
        static_configs:
        - targets:
          - "127.0.0.1:12345"
    
    # Inside a server_group:
    alert_templates:
      default: '{{.ExternalURL}}/graph?g0.expr={{.Expr | urlquery}}&g0.tab=1'
      named:
        grafana: 'https://grafana.example.com/alerting/groups?queryString=alertname%3D%22{{.AlertName | urlquery}}%22'
      rules:
        - match_labels:
            severity: critical
          template: grafana