Skip to content

Commit

Permalink
major rewrite for version v1.0.0 with a bunch of breaking changes (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed May 22, 2019
1 parent 42235be commit 0e0639d
Show file tree
Hide file tree
Showing 35 changed files with 2,125 additions and 4,634 deletions.
46 changes: 11 additions & 35 deletions .drone.yml
Expand Up @@ -11,10 +11,10 @@ services:
image: redis:5
ports:
- 6379
- name: moar-redis
- name: pwd-redis
image: redis:5
commands:
- /usr/local/bin/redis-server --port 6380
commands:
- "/usr/local/bin/redis-server --port 6380 --requirepass redis-password"
ports:
- 6380
- name: redis-cluster
Expand All @@ -38,21 +38,21 @@ steps:
environment:
GO111MODULE: on
LOG_LEVEL: "info"
TEST_TILE38_URI: "tile38:9851"
TEST_SECOND_REDIS_URI: "redis://moar-redis:6380"
TEST_REDIS_CLUSTER_MASTER_URI: "redis-cluster:7000"
TEST_REDIS_CLUSTER_SLAVE_URI: "redis-cluster:7005"
TEST_TILE38_URI: "redis://tile38:9851"
TEST_REDIS_URI: "redis://redis:6379"
TEST_PWD_REDIS_URI: "redis://h:redis-password@pwd-redis:6380"
TEST_REDIS_CLUSTER_MASTER_URI: "redis://redis-cluster:7000"
TEST_REDIS_CLUSTER_SLAVE_URI: "redis://redis-cluster:7005"
COVERALLS_TOKEN:
from_secret: coveralls-token
commands:
- 'go build -mod=vendor'
- "sleep 10" # let the redis test instances all come up first
- 'go test -mod=vendor -v -covermode=atomic -cover -race -coverprofile=coverage.txt ./exporter/... --redis.addr=redis'
- 'go test -mod=vendor -v -covermode=atomic -cover -race -coverprofile=coverage.txt .'
- 'echo "checking gofmt"'
- 'echo " ! gofmt -d main.go 2>&1 | read " | bash'
- 'echo " ! gofmt -d exporter/*.go 2>&1 | read " | bash'
- 'echo " ! gofmt -d *.go 2>&1 | read " | bash'
- 'echo "checking gofmt - DONE"'
- 'go vet ./exporter/...'
- 'go vet .'
- 'go get github.com/mattn/goveralls'
- '/go/bin/goveralls -coverprofile=coverage.txt -service=drone.io'
when:
Expand Down Expand Up @@ -122,27 +122,3 @@ steps:
when:
event:
- tag


#
# temporary addtl. build step while working on #256
#
- name: release-docker-image-alpine-v1.0.0-rc.1
image: plugins/docker
settings:
tags: "v1.0.0-rc.1"
dockerfile: ./Dockerfile
repo: oliver006/redis_exporter
target: alpine
build_args:
- 'TAG=v1.0.0-rc.1'
- 'SHA1=${DRONE_COMMIT_SHA}'
username:
from_secret: docker_user
password:
from_secret: docker_pass
when:
branch:
- oh_1.0_here_we_come
repo:
- oliver006/redis_exporter
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
redis_exporter
coverage.out
coverage.txt
dist/
pkg/
src/
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Expand Up @@ -4,8 +4,7 @@
FROM golang:1.12-alpine as builder
WORKDIR /go/src/github.com/oliver006/redis_exporter/

ADD main.go /go/src/github.com/oliver006/redis_exporter/
ADD exporter /go/src/github.com/oliver006/redis_exporter/exporter
ADD *.go /go/src/github.com/oliver006/redis_exporter/
ADD vendor /go/src/github.com/oliver006/redis_exporter/vendor

ARG SHA1
Expand Down
198 changes: 114 additions & 84 deletions README.md
Expand Up @@ -2,13 +2,25 @@


[![Build Status](https://cloud.drone.io/api/badges/oliver006/redis_exporter/status.svg)](https://cloud.drone.io/oliver006/redis_exporter)
[![Coverage Status](https://coveralls.io/repos/github/oliver006/redis_exporter/badge.svg?branch=master)](https://coveralls.io/github/oliver006/redis_exporter?branch=master) [![codecov](https://codecov.io/gh/oliver006/redis_exporter/branch/master/graph/badge.svg)](https://codecov.io/gh/oliver006/redis_exporter)
[![Coverage Status](https://coveralls.io/repos/github/oliver006/redis_exporter/badge.svg?branch=master)](https://coveralls.io/github/oliver006/redis_exporter?branch=master) [![codecov](https://codecov.io/gh/oliver006/redis_exporter/branch/master/graph/badge.svg)](https://codecov.io/gh/oliver006/redis_exporter) [![docker_pulls](https://img.shields.io/docker/pulls/oliver006/redis_exporter.svg)](https://img.shields.io/docker/pulls/oliver006/redis_exporter.svg)


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


## Building, configuring, and running

### Upgrading from 0.x to 1.x ?

[PR #256](https://github.com/oliver006/redis_exporter/pull/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](https://github.com/oliver006/redis_exporter/pull/256) and this README for more information.



### Build and run locally:

```sh
Expand All @@ -19,23 +31,89 @@ Supports Redis 2.x, 3.x, 4.x, and 5.x
```


### Prometheus Configuration

### Basic Prometheus Configuration

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

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

and adjust the host name accordingly.



### Prometheus Configuration to Scrape Several 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.

```yaml

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
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 file supply multiple targets by using `file_sd_configs` like so:

- job_name: redis_exporter
static_configs:
- targets: ['localhost:9121']
```yaml

...
scrape_configs:
- job_name: 'redis_exporter'
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
```

and adjust the host name accordingly.
The `targets-redis-instances.json` should look something like this:

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

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



### Run via Docker:

Expand All @@ -44,7 +122,6 @@ The latest release is automatically published to the [Docker registry](https://h
You can run it like this:

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

Expand All @@ -55,96 +132,47 @@ 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
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:

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

### Run on Kubernetes

[Here](contrib/k8s-redis-and-exporter-deployment.yaml) is an example Kubernetes deployment configuration for how to deploy the redis_exporter as a sidecar with a Redis instance.


### Run on Openshift

In order to deploy the exporter on Openshift environment.

```sh
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 -
```

*NOTE*: Some of the parameters can be omitted if no authentication is used or the default redis config is applied.

```sh
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 -
```

If you are running Prometheus on Openshift on the same cluster, **target** in `prometheus.yml` should point to the correct service name of the exporter

```yaml
scrape_configs:
### Run on Kubernetes

...
[Here](contrib/k8s-redis-and-exporter-deployment.yaml) is an example Kubernetes deployment configuration for how to deploy the redis_exporter as a sidecar to a Redis instance.

- job_name: redis_exporter
static_configs:
- targets: ['<redis-exporter.myproject.svc>:9121']

...
```

### Run on Cloud Foundry
### Flags

```sh
cf push -f contrib/manifest.yml
```
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](https://redis.io/commands/scan). Use this option if you need glob pattern matching; `check-single-keys` is faster for non-pattern keys.
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_IS_TILE38 | 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.

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.

### Flags

Name | Description
--------------------|------------
debug | Verbose debug output
log-format | Log format, valid options are `txt` (default) and `json`.
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](https://redis.io/commands/scan). Use this option if you need glob pattern matching; `check-single-keys` is faster for non-pattern keys.
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 | Path to Redis Lua script for gathering extra metrics.
redis.addr | Address of one or more redis nodes, comma separated, defaults to `redis://localhost:6379`.
redis.password | Password to use when authenticating to Redis
redis.password-file | Path to a file containing the password to use when authenticating to Redis (note: this is mutually exclusive with `redis.password`)
redis.alias | Alias for redis node addr, comma separated.
redis.file | Path to file containing one or more redis nodes, separated by newline. This option is mutually exclusive with redis.addr. Each line can optionally be comma-separated with the fields `<addr>,<password>,<alias>`. See [here](./contrib/sample_redis_hosts_file.txt) for an example file.
namespace | Namespace for the metrics, defaults to `redis`.
web.listen-address | Address to listen on for web interface and telemetry, defaults to `0.0.0.0:9121`.
web.telemetry-path | Path under which to expose metrics, defaults to `metrics`.
use-cf-bindings | Enable usage of Cloud Foundry service bindings. Defaults to `false`
separator | Separator used to split redis.addr, redis.password and redis.alias into several elements. Defaults to `,`

Redis node addresses can be tcp addresses like `redis://localhost:6379`, `redis.example.com:6379` or unix socket addresses like `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).

These settings take precedence over any configurations provided by [environment variables](#environment-variables).

### Environment Variables

Name | Description
-------------------|------------
REDIS_ADDR | Address of Redis node(s)
REDIS_PASSWORD | Password to use when authenticating to Redis
REDIS_ALIAS | Alias name of Redis node(s)
REDIS_FILE | Path to file containing Redis node(s)

### What's exported?

Expand All @@ -155,6 +183,8 @@ You can also export values of keys if they're in numeric format by using the `-c

If you require custom metric collection, you can provide a [Redis Lua script](https://redis.io/commands/eval) using the `-script` flag. An example can be found [in the contrib folder](./contrib/sample_collect_script.lua).



### What does it look like?

Example [Grafana](http://grafana.org/) screenshots:\
Expand All @@ -163,8 +193,8 @@ Example [Grafana](http://grafana.org/) screenshots:\

Grafana dashboard is available on [grafana.net](https://grafana.net/dashboards/763) and/or [github.com](contrib/grafana_prometheus_redis_dashboard.json).

Grafana dashboard with host & alias selector is available on [github.com](contrib/grafana_prometheus_redis_dashboard_alias.json).


### What else?

Open an issue or PR if you have more suggestions or ideas about what to add.
Open an issue or PR if you have more suggestions, questions or ideas about what to add.
13 changes: 9 additions & 4 deletions build-github-binaries.sh
@@ -1,5 +1,6 @@
#!/usr/bin/env bash


set -u -e -o pipefail

if [[ -z "${DRONE_TAG}" ]] ; then
Expand All @@ -19,17 +20,21 @@ if [[ -f 'go.mod' ]] ; then
go mod tidy
fi

gox -verbose -os="darwin linux freebsd windows netbsd" -arch="386 amd64" -rebuild -ldflags "${GO_LDFLAGS}" -output '.build/{{.OS}}-{{.Arch}}/{{.Dir}}'
gox -verbose -os="darwin linux" -arch="386 amd64" -rebuild -ldflags "${GO_LDFLAGS}" -output ".build/redis_exporter-${DRONE_TAG}.{{.OS}}-{{.Arch}}/{{.Dir}}"

mkdir -p dist
for build in $(ls .build); do
echo "Creating archive for ${build}"
if [[ "${build}" =~ ^windows-.*$ ]] ; then

cp LICENSE README.md ".build/${build}/"

if [[ "${build}" =~ windows-.*$ ]] ; then

# Make sure to clear out zip files to prevent zip from appending to the archive.
rm "dist/redis_exporter-${DRONE_TAG}.${build}.zip" || true
cd ".build/${build}" && zip --quiet -9 "../../dist/redis_exporter-${DRONE_TAG}.${build}.zip" 'redis_exporter.exe' && cd ../../
cd ".build/" && zip -r --quiet -9 "../dist/${build}.zip" "${build}" && cd ../
else
tar -C ".build/${build}" -czf "dist/redis_exporter-${DRONE_TAG}.${build}.tar.gz" 'redis_exporter'
tar -C ".build/" -czf "dist/${build}.tar.gz" "${build}"
fi
done

Expand Down

0 comments on commit 0e0639d

Please sign in to comment.