Skip to content

Commit

Permalink
feat(prometheus): Support kubernetes metrics
Browse files Browse the repository at this point in the history
This allows a user to configure the prometheus plugin to poll kuberentes metrics endpoint which expose prometheus metrics
  • Loading branch information
Jonathan Chauncey committed Mar 3, 2016
1 parent 72027b5 commit 58696bb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
78 changes: 54 additions & 24 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
package prometheus

import (
"crypto/tls"
"errors"
"fmt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
"io"
"io/ioutil"
"net"
"net/http"
"sync"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
)

type Prometheus struct {
Urls []string

// Use SSL but skip chain & host verification
InsecureSkipVerify bool
// Bearer Token authorization file path
BearerToken string `toml:"bearer_token"`
}

var sampleConfig = `
## An array of urls to scrape metrics from.
urls = ["http://localhost:9100/metrics"]
### Use SSL but skip chain & host verification
# insecure_skip_verify = false
### Use bearer token for authorization
# bearer_token = /path/to/bearer/token
`

func (r *Prometheus) SampleConfig() string {
func (p *Prometheus) SampleConfig() string {
return sampleConfig
}

func (r *Prometheus) Description() string {
func (p *Prometheus) Description() string {
return "Read metrics from one or many prometheus clients"
}

var ErrProtocolError = errors.New("prometheus protocol error")

// Reads stats from all configured servers accumulates stats.
// Returns one of the errors encountered while gather stats (if any).
func (g *Prometheus) Gather(acc telegraf.Accumulator) error {
func (p *Prometheus) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup

var outerr error

for _, serv := range g.Urls {
for _, serv := range p.Urls {
wg.Add(1)
go func(serv string) {
defer wg.Done()
outerr = g.gatherURL(serv, acc)
outerr = p.gatherURL(serv, acc)
}(serv)
}

Expand All @@ -52,17 +66,33 @@ func (g *Prometheus) Gather(acc telegraf.Accumulator) error {
return outerr
}

var tr = &http.Transport{
ResponseHeaderTimeout: time.Duration(3 * time.Second),
}
func (p *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
var req, err = http.NewRequest("GET", url, nil)
req.Header = make(http.Header)
var token []byte
var resp *http.Response

var rt http.RoundTripper = &http.Transport{
Dial: (&net.Dialer{
Timeout: 4 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
ResponseHeaderTimeout: time.Duration(3 * time.Second),
}

var client = &http.Client{
Transport: tr,
Timeout: time.Duration(4 * time.Second),
}
if p.BearerToken != "" {
token, err = ioutil.ReadFile(p.BearerToken)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+string(token))
}

func (g *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
resp, err := client.Get(url)
resp, err = rt.RoundTrip(req)
if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", url, err)
}
Expand All @@ -71,9 +101,7 @@ func (g *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
return fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
}
format := expfmt.ResponseFormat(resp.Header)

decoder := expfmt.NewDecoder(resp.Body, format)

options := &expfmt.DecodeOptions{
Timestamp: model.Now(),
}
Expand All @@ -88,8 +116,7 @@ func (g *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("error getting processing samples for %s: %s",
url, err)
return fmt.Errorf("error getting processing samples for %s: %s", url, err)
}
for _, sample := range samples {
tags := make(map[string]string)
Expand All @@ -99,8 +126,11 @@ func (g *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
}
tags[string(key)] = string(value)
}
acc.Add("prometheus_"+string(sample.Metric[model.MetricNameLabel]),
float64(sample.Value), tags)
if sample.Value.String() != "NaN" {
acc.Add("prometheus_"+string(sample.Metric[model.MetricNameLabel]), float64(sample.Value), tags)
} else {
acc.Add("prometheus_"+string(sample.Metric[model.MetricNameLabel]), float64(0), tags)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prometheus

import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -23,6 +24,7 @@ go_gc_duration_seconds_count 7
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 15
go_gc_metric NaN
`

func TestPrometheusGeneratesMetrics(t *testing.T) {
Expand All @@ -47,6 +49,7 @@ func TestPrometheusGeneratesMetrics(t *testing.T) {
}{
{"prometheus_go_gc_duration_seconds_count", 7, map[string]string{}},
{"prometheus_go_goroutines", 15, map[string]string{}},
{"prometheus_go_gc_metric", 0, map[string]string{}},
}

for _, e := range expected {
Expand Down

0 comments on commit 58696bb

Please sign in to comment.