2015-06-20 12:38:01 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-01-07 20:39:43 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins/inputs"
|
2015-10-22 16:17:57 +00:00
|
|
|
"github.com/prometheus/common/expfmt"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"io"
|
2015-06-20 12:38:01 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Prometheus struct {
|
|
|
|
Urls []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# An array of urls to scrape metrics from.
|
|
|
|
urls = ["http://localhost:9100/metrics"]
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-06-20 12:38:01 +00:00
|
|
|
|
|
|
|
func (r *Prometheus) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *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).
|
2016-01-07 20:39:43 +00:00
|
|
|
func (g *Prometheus) Gather(acc inputs.Accumulator) error {
|
2015-06-20 12:38:01 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
var outerr error
|
|
|
|
|
|
|
|
for _, serv := range g.Urls {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(serv string) {
|
|
|
|
defer wg.Done()
|
|
|
|
outerr = g.gatherURL(serv, acc)
|
|
|
|
}(serv)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return outerr
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func (g *Prometheus) gatherURL(url string, acc inputs.Accumulator) error {
|
2015-06-20 12:38:01 +00:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error making HTTP request to %s: %s", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
|
|
|
|
}
|
2015-10-22 16:17:57 +00:00
|
|
|
format := expfmt.ResponseFormat(resp.Header)
|
2015-06-20 12:38:01 +00:00
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
decoder := expfmt.NewDecoder(resp.Body, format)
|
2015-06-20 12:38:01 +00:00
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
options := &expfmt.DecodeOptions{
|
|
|
|
Timestamp: model.Now(),
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
2015-10-22 16:17:57 +00:00
|
|
|
sampleDecoder := &expfmt.SampleDecoder{
|
|
|
|
Dec: decoder,
|
|
|
|
Opts: options,
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
for {
|
|
|
|
var samples model.Vector
|
|
|
|
err := sampleDecoder.Decode(&samples)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2016-01-07 00:56:30 +00:00
|
|
|
return fmt.Errorf("error getting processing samples for %s: %s",
|
|
|
|
url, err)
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
for _, sample := range samples {
|
2015-12-15 16:03:22 +00:00
|
|
|
tags := make(map[string]string)
|
2015-10-22 16:17:57 +00:00
|
|
|
for key, value := range sample.Metric {
|
|
|
|
if key == model.MetricNameLabel {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tags[string(key)] = string(value)
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
2015-12-15 16:03:22 +00:00
|
|
|
acc.Add("prometheus_"+string(sample.Metric[model.MetricNameLabel]),
|
2015-11-23 21:16:20 +00:00
|
|
|
float64(sample.Value), tags)
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-22 16:17:57 +00:00
|
|
|
|
2015-06-20 12:38:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-07 20:39:43 +00:00
|
|
|
inputs.Add("prometheus", func() inputs.Input {
|
2015-06-20 12:38:01 +00:00
|
|
|
return &Prometheus{}
|
|
|
|
})
|
|
|
|
}
|