Skip to content

Commit

Permalink
Merge pull request #2 from DavidWittman/redis-auth
Browse files Browse the repository at this point in the history
Add support for authentication
  • Loading branch information
oliver006 committed Sep 10, 2015
2 parents ba78a82 + 15910c8 commit e9f8b26
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ and adjust the host name accordingly.
Name | Description
-------------------|------------
redis.addr | Address of one or more redis nodes, comma separated, defaults to `localhost:6379`.
redis.password | Password to use when authenticating to Redis
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`.

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

### Environment Variables

Name | Description
-------------------|------------
REDIS_PASSWORD | Password to use when authenticating to Redis

### What's exported?

Expand Down
24 changes: 19 additions & 5 deletions redis_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
// "fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
Expand All @@ -16,13 +17,19 @@ import (

var (
redisAddr = flag.String("redis.addr", "localhost:6379", "Address of one or more redis nodes, comma separated")
redisPassword = flag.String("redis.password", os.Getenv("REDIS_PASSWORD"), "Password for Redis server")
namespace = flag.String("namespace", "redis", "Namespace for metrics")
listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)

type RedisHost struct {
addrs []string
password string
}

type Exporter struct {
addrs []string
redis RedisHost
namespace string
duration prometheus.Gauge
scrapeErrors prometheus.Gauge
Expand Down Expand Up @@ -58,9 +65,9 @@ func (e *Exporter) initGauges() {
}, []string{"addr", "db"})
}

func NewRedisExporter(addrs []string, namespace string) *Exporter {
func NewRedisExporter(redis RedisHost, namespace string) *Exporter {
e := Exporter{
addrs: addrs,
redis: redis,
namespace: namespace,

duration: prometheus.NewGauge(prometheus.GaugeOpts{
Expand Down Expand Up @@ -219,14 +226,21 @@ func (e *Exporter) scrape(scrapes chan<- scrapeResult) {

//var err error
errorCount := 0
for _, addr := range e.addrs {
for _, addr := range e.redis.addrs {
// log.Printf("opening connection to redis node %s", addr)
c, err := redis.Dial("tcp", addr)
if err != nil {
log.Printf("redis err: %s", err)
errorCount++
continue
}
if e.redis.password != "" {
if _, err := c.Do("AUTH", e.redis.password); err != nil {
log.Printf("redis err: %s", err)
errorCount++
continue
}
}
info, err := redis.String(c.Do("INFO"))
c.Close()
if err != nil {
Expand Down Expand Up @@ -276,7 +290,7 @@ func main() {
log.Fatal("Invalid parameter --redis.addr")
}

e := NewRedisExporter(addrs, *namespace)
e := NewRedisExporter(RedisHost{addrs, *redisPassword}, *namespace)
prometheus.MustRegister(e)

http.Handle(*metricPath, prometheus.Handler())
Expand Down
4 changes: 2 additions & 2 deletions redis_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func deleteKeysFromDB(t *testing.T, db string) error {

func TestCountingKeys(t *testing.T) {

e := NewRedisExporter(addrs, "test")
e := NewRedisExporter(RedisHost{addrs: addrs}, "test")

scrapes := make(chan scrapeResult)
go e.scrape(scrapes)
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestCountingKeys(t *testing.T) {

func TestExporter(t *testing.T) {

e := NewRedisExporter(addrs, "test")
e := NewRedisExporter(RedisHost{addrs: addrs}, "test")

scrapes := make(chan scrapeResult)
go e.scrape(scrapes)
Expand Down

0 comments on commit e9f8b26

Please sign in to comment.