Redis Metrics Exporter

Redis Metrics 的 Prometheus Exporter。 支持 Redis 2.x,3.x,4.x和5.x。(Prometheus Exporter for Redis Metrics. Supports Redis 2.x, 3.x and 4.x.)

Github stars Tracking Chart

Redis Metrics Exporter

针对 Redis 指标的 Prometheus 导出器。 支持Redis 2.x,3.x,4.x和5.x。

构建,配置和运行

在本地构建和运行:

$ go get github.com/oliver006/redis_exporter
$ cd $GOPATH/src/github.com/oliver006/redis_exporter
$ go build
$ ./redis_exporter <flags>

Prometheus 配置

在 prometheus.yml 配置文件的 scrape_configs 中增加1个块

scrape_configs:
...
- job_name: redis_exporter
  static_configs:
  - targets: ['localhost:9121']
...

并相应地调整主机名称。

通过Docker运行:

最新版本会自动发布到 Docker registry

你可以像这样运行它:

$ docker pull oliver006/redis_exporter
$ docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter

最新的 docker 镜像仅包含导出器(exporter)的二进制文件。如果,例如出于调试目的,您需要在具有 shell 等的映像中运行导出器,然后您可以运行 alpine 映像:

$ docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter:alpine

如果您尝试访问在主机节点上运行的redis实例,则需要添加 --network host,以便 redis_exporter 容器可以访问它:

$ docker run -d --name redis_exporter --network host oliver006/redis_exporter

在 Kubernetes 上运行

这里是一个示例 Kubernetes 部署配置,用于将 redis_exporter 部署为具有 Redis 实例的 sidecar。

在 Openshift 上运行

为了在 Openshift 环境中部署导出器。

oc project <myproject>
oc process -f https://raw.githubusercontent.com/oliver006/redis_exporter/master/contrib/openshift-template.yaml \
    -p REDIS_ADDR=<redis-service>:<redis-port> \
    -p REDIS_PASSWORD=<redis-pass> \
    -p REDIS_ALIAS=<redis-alias> \
    -p REDIS_FILE=<redis-file> \
    | oc create -f -

注意:如果未使用身份验证或应用默认的redis配置,则可以省略某些参数。

oc process -f https://raw.githubusercontent.com/oliver006/redis_exporter/master/contrib/openshift-template.yaml \
    -p REDIS_ADDR=<redis-service>:<redis-port> \
    | oc create -f -

如果您在同一群集上的 Openshift 上运行 Prometheus,则 prometheus.yml 中的target 应指向导出器的正确服务名称。

在Cloud Foundry上运行

要在Cloud Foundry上运行,请使用:

cf push -f contrib/manifest.yml

标志

名称 描述
debug 详细的调试输出
log-format 日志格式,有效选项是 txt (default) 和 json.
check-keys 输入值和长度/大小的逗号分隔键列表,例如: db3=user_count 将从db 3 导出键user_count。如果省略,db 默认为0。可以使用 SCAN 找到使用此标志指定的键模式。 如果需要glob模式匹配,请使用此选项; 非代码键的 check-single-keys 更快。
check-single-keys 用于导出值和长度/大小的逗号分隔键列表,例如: db3 = user_count 将从db 3导出密钥user_count。 如果省略,db默认为0。 使用此标志指定的键将直接查找,而不进行任何glob模式匹配。 如果您不需要glob模式匹配,请使用此选项; 它比check-keys更快。
script Redis Lua 脚本用于收集额外指标的路径。
redis.addr 一个或多个redis节点的地址,逗号分隔,默认为 redis://localhost:6379
redis.password 验证 Redis 时使用的密码
redis.password-file 包含在向Redis进行身份验证时使用的密码的文件的路径(注意:这与redis.password 互斥)
redis.alias redis node addr的别名,逗号分隔。
redis.file 包含一个或多个redis节点的文件路径,由换行符分隔。 此选项与redis.addr互斥。 每行可以选择用字段<addr>,<password>,<alias>进行逗号分隔。 有关示例文件,请参见此处
namespace 指标的命名空间,默认为 redis.
web.listen-address 监听网络界面和遥测的地址,默认为0.0.0.0:9121.
web.telemetry-path 公开指标的路径,默认为 metrics.
use-cf-bindings 启用 Cloud Foundry 服务绑定的使用。 默认为false
separator Separator 用于将 redis.addr,redis.password 和 redis.alias 拆分为多个元素。 默认为,

Redis 节点地址可以是 tcp 地址,如 redis://localhost:6379,redis.example.com:6379 或 unix 套接字地址,如unix:///tmp/redis.sock。 使用rediss://schema 支持 SSL,例如:rediss://azure-ssl-enabled-host.redis.cache.windows.net:6380(注意连接到非标准时需要端口 6379端口,例如 Azure Redis 实例)。

这些设置优先于环境变量提供的任何配置。

环境变量

名称 描述
REDIS_ADDR Redis 节点的地址
REDIS_PASSWORD 在向 Redis 进行身份验证时使用的密码
REDIS_ALIAS Redis 节点的别名
REDIS_FILE包含 Redis 节点的文件路径

Overview

Name With Owneroliver006/redis_exporter
Primary LanguageGo
Program languageGo (Language Count: 6)
PlatformLinux, Mac, Windows
License:MIT License
Release Count152
Last Release Namev1.59.0 (Posted on )
First Release Name0.2 (Posted on )
Created At2015-04-28 03:11:21
Pushed At2024-04-21 03:56:07
Last Commit At2024-04-21 05:32:26
Stargazers Count3k
Watchers Count49
Fork Count853
Commits Count645
Has Issues Enabled
Issues Count470
Issue Open Count8
Pull Requests Count331
Pull Requests Open Count3
Pull Requests Close Count96
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Redis Metrics Exporter

Build Status
Coverage Status codecov docker_pulls

Prometheus exporter for Redis metrics.
Supports Redis 2.x, 3.x, 4.x, and 5.x

Upgrading from 0.x to 1.x?

PR #256 introduced breaking changes which were released as version v1.0.0.

If you only scrape one Redis instance and use command line flags --redis.address
and --redis.password then you're most probably not affected.
Otherwise, please see PR #256 and this README for more information.

Build and run locally

    $ git clone https://github.com/oliver006/redis_exporter.git
    $ cd redis_exporter
    $ go build .
    $ ./redis_exporter --version

Configuring

Basic Prometheus Configuration

Add a block to the scrape_configs of your prometheus.yml config file:

scrape_configs:
  - job_name: redis_exporter
    static_configs:
    - targets: ['<<REDIS-EXPORTER-HOSTNAME>>:9121']

and adjust the host name accordingly.

Prometheus Configuration to Scrape Multiple Redis Hosts

Run the exporter with the command line flag --redis.addr= so it won't try to access
the local instance every time the /metrics endpoint is scraped.


scrape_configs:
  ## config for the multiple Redis targets that the exporter will scrape
  - job_name: 'redis_exporter_targets'
    static_configs:
      - targets:
        - redis://first-redis-host:6379
        - redis://second-redis-host:6379
        - redis://second-redis-host:6380
        - redis://second-redis-host:6381
    metrics_path: /scrape
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: <<REDIS-EXPORTER-HOSTNAME>>:9121
  
  ## config for scraping the exporter itself
  - job_name: 'redis_exporter'
    static_configs:
      - targets:
        - <<REDIS-EXPORTER-HOSTNAME>>:9121

The Redis instances are listed under targets, the Redis exporter hostname is configured via the last relabel_config rule.
If authentication is needed for the Redis instances then you can set the password via the --redis.password command line option of
the exporter (this means you can currently only use one password across the instances you try to scrape this way. Use several
exporters if this is a problem).
You can also use a json file to supply multiple targets by using file_sd_configs like so:


scrape_configs:
  - job_name: 'redis_exporter_targets'
    file_sd_configs:
      - files:
        - targets-redis-instances.json
    metrics_path: /scrape
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: <<REDIS-EXPORTER-HOSTNAME>>:9121

  ## config for scraping the exporter itself
  - job_name: 'redis_exporter'
    static_configs:
      - targets:
        - <<REDIS-EXPORTER-HOSTNAME>>:9121

The targets-redis-instances.json should look something like this:

[
  {
    "targets": [ "redis://redis-host-01:6379", "redis://redis-host-02:6379"],
    "labels": { }
  }
]

Prometheus uses file watches and all changes to the json file are applied immediately.

Running

Flags

Name, Environment Variable Name, Description
-----------------------, --------------------------------------, -----------------
redis.addr, REDIS_ADDR, Address of the Redis instance, defaults to redis://localhost:6379.
redis.password, REDIS_PASSWORD, Password of the Redis instance, defaults to "" (no password).
check-keys, REDIS_EXPORTER_CHECK_KEYS, Comma separated list of key patterns to export value and length/size, eg: db3=user_count will export key user_count from db 3. db defaults to 0 if omitted. The key patterns specified with this flag will be found using SCAN. Use this option if you need glob pattern matching; check-single-keys is faster for non-pattern keys. Warning: using --check-keys to match a very large number of keys can slow down the exporter to the point where it doesn't finish scraping the redis instance.
check-single-keys, REDIS_EXPORTER_CHECK_SINGLE_KEYS, Comma separated list of keys to export value and length/size, eg: db3=user_count will export key user_count from db 3. db defaults to 0 if omitted. The keys specified with this flag will be looked up directly without any glob pattern matching. Use this option if you don't need glob pattern matching; it is faster than check-keys.
script, REDIS_EXPORTER_SCRIPT, Path to Redis Lua script for gathering extra metrics.
debug, REDIS_EXPORTER_DEBUG, Verbose debug output
log-format, REDIS_EXPORTER_LOG_FORMAT, Log format, valid options are txt (default) and json.
namespace, REDIS_EXPORTER_NAMESPACE, Namespace for the metrics, defaults to redis.
connection-timeout, REDIS_EXPORTER_CONNECTION_TIMEOUT, Timeout for connection to Redis instance, defaults to "15s" (in Golang duration format)
web.listen-address, REDIS_EXPORTER_WEB_LISTEN_ADDRESS, Address to listen on for web interface and telemetry, defaults to 0.0.0.0:9121.
web.telemetry-path, REDIS_EXPORTER_WEB_TELEMETRY_PATH, Path under which to expose metrics, defaults to /metrics.
redis-only-metrics, REDIS_EXPORTER_REDIS_ONLY_METRICS, Whether to also export go runtime metrics, defaults to false.
include-system-metrics, REDIS_EXPORTER_INCL_SYSTEM_METRICS, Whether to include system metrics like total_system_memory_bytes, defaults to false.
is-tile38, REDIS_EXPORTER_IS_TILE38, Whether to scrape Tile38 specific metrics, defaults to false.
export-client-list, REDIS_EXPORTER_EXPORT_CLIENT_LIST, Whether to scrape Client List specific metrics, defaults to false.
skip-tls-verification, REDIS_EXPORTER_SKIP_TLS_VERIFICATION, Whether to to skip TLS verification
tls-client-key-file, REDIS_EXPORTER_TLS_CLIENT_KEY_FILE, Name of the client key file (including full path) if the server requires TLS client authentication
tls-client-cert-file, REDIS_EXPORTER_TLS_CLIENT_CERT_FILE, Name the client cert file (including full path) if the server requires TLS client authentication
set-client-name, REDIS_EXPORTER_SET_CLIENT_NAME, Whether to set client name to redis_exporter, defaults to true.

Redis instance addresses can be tcp addresses: redis://localhost:6379, redis.example.com:6379 or e.g. unix sockets: unix:///tmp/redis.sock.
SSL is supported by using the rediss:// schema, for example: rediss://azure-ssl-enabled-host.redis.cache.windows.net:6380 (note that the port is required when connecting to a non-standard 6379 port, e.g. with Azure Redis instances).
Password-protected instances can be accessed by using the URI format including a password: redis://h:<<PASSWORD>>@<<HOSTNAME>>:<<PORT>>

Command line settings take precedence over any configurations provided by the environment variables.

Run via Docker

The latest release is automatically published to the Docker registry.

You can run it like this:

    $ docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter

The latest docker image contains only the exporter binary.
If, e.g. for debugging purposes, you need the exporter running
in an image that has a shell, etc then you can run the alpine image:

    $ docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter:alpine

If you try to access a Redis instance running on the host node, you'll need to add --network host so the
redis_exporter container can access it:

    $ docker run -d --name redis_exporter --network host oliver006/redis_exporter

Run on Kubernetes

Here is an example Kubernetes deployment configuration for how to deploy the redis_exporter as a sidecar to a Redis instance.

What's exported?

Most items from the INFO command are exported,
see https://redis.io/commands/info for details.
In addition, for every database there are metrics for total keys, expiring keys and the average TTL for keys in the database.
You can also export values of keys if they're in numeric format by using the -check-keys flag. The exporter will also export the size (or, depending on the data type, the length) of the key. This can be used to export the number of elements in (sorted) sets, hashes, lists, streams, etc.

If you require custom metric collection, you can provide a Redis Lua script using the -script flag. An example can be found in the contrib folder.

What does it look like?

Example Grafana screenshots:

Grafana dashboard is available on grafana.com and/or github.com.

What else?

Open an issue or PR if you have more suggestions, questions or ideas about what to add.

To the top