Add Prometheus plugin.
This allows pulling Prometheus metrics from any client library or exporter over HTTP.
This commit is contained in:
parent
e34c52402f
commit
5390a8ea71
|
@ -47,6 +47,7 @@ Telegraf currently has support for collecting metrics from:
|
||||||
* System (memory, CPU, network, etc.)
|
* System (memory, CPU, network, etc.)
|
||||||
* Docker
|
* Docker
|
||||||
* MySQL
|
* MySQL
|
||||||
|
* Prometheus (client libraries and exporters)
|
||||||
* PostgreSQL
|
* PostgreSQL
|
||||||
* Redis
|
* Redis
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
_ "github.com/influxdb/telegraf/plugins/memcached"
|
_ "github.com/influxdb/telegraf/plugins/memcached"
|
||||||
_ "github.com/influxdb/telegraf/plugins/mysql"
|
_ "github.com/influxdb/telegraf/plugins/mysql"
|
||||||
_ "github.com/influxdb/telegraf/plugins/postgresql"
|
_ "github.com/influxdb/telegraf/plugins/postgresql"
|
||||||
|
_ "github.com/influxdb/telegraf/plugins/prometheus"
|
||||||
_ "github.com/influxdb/telegraf/plugins/redis"
|
_ "github.com/influxdb/telegraf/plugins/redis"
|
||||||
_ "github.com/influxdb/telegraf/plugins/system"
|
_ "github.com/influxdb/telegraf/plugins/system"
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
package prometheus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdb/telegraf/plugins"
|
||||||
|
"github.com/prometheus/client_golang/extraction"
|
||||||
|
"github.com/prometheus/client_golang/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Prometheus struct {
|
||||||
|
Urls []string
|
||||||
|
}
|
||||||
|
|
||||||
|
var sampleConfig = `
|
||||||
|
# An array of urls to scrape metrics from.
|
||||||
|
urls = ["http://localhost:9100/metrics"]`
|
||||||
|
|
||||||
|
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).
|
||||||
|
func (g *Prometheus) Gather(acc plugins.Accumulator) error {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Prometheus) gatherURL(url string, acc plugins.Accumulator) error {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
processor, err := extraction.ProcessorForRequestHeader(resp.Header)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting extractor for %s: %s", url, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ingestor := &Ingester{
|
||||||
|
acc: acc,
|
||||||
|
}
|
||||||
|
|
||||||
|
options := &extraction.ProcessOptions{
|
||||||
|
Timestamp: model.TimestampFromTime(time.Now()),
|
||||||
|
}
|
||||||
|
|
||||||
|
err = processor.ProcessSingle(resp.Body, ingestor, options)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting processing samples for %s: %s", url, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Ingester struct {
|
||||||
|
acc plugins.Accumulator
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ingest implements an extraction.Ingester.
|
||||||
|
func (i *Ingester) Ingest(samples model.Samples) error {
|
||||||
|
for _, sample := range samples {
|
||||||
|
tags := map[string]string{}
|
||||||
|
for key, value := range sample.Metric {
|
||||||
|
if key == model.MetricNameLabel {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tags[string(key)] = string(value)
|
||||||
|
}
|
||||||
|
i.acc.Add(string(sample.Metric[model.MetricNameLabel]), float64(sample.Value), tags)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
plugins.Add("prometheus", func() plugins.Plugin {
|
||||||
|
return &Prometheus{}
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package prometheus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/influxdb/telegraf/testutil"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
const sampleTextFormat = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
||||||
|
# TYPE go_gc_duration_seconds summary
|
||||||
|
go_gc_duration_seconds{quantile="0"} 0.00010425500000000001
|
||||||
|
go_gc_duration_seconds{quantile="0.25"} 0.000139108
|
||||||
|
go_gc_duration_seconds{quantile="0.5"} 0.00015749400000000002
|
||||||
|
go_gc_duration_seconds{quantile="0.75"} 0.000331463
|
||||||
|
go_gc_duration_seconds{quantile="1"} 0.000667154
|
||||||
|
go_gc_duration_seconds_sum 0.0018183950000000002
|
||||||
|
go_gc_duration_seconds_count 7
|
||||||
|
# HELP go_goroutines Number of goroutines that currently exist.
|
||||||
|
# TYPE go_goroutines gauge
|
||||||
|
go_goroutines 15
|
||||||
|
`
|
||||||
|
|
||||||
|
func TestPrometheusGeneratesMetrics(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, sampleTextFormat)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
p := &Prometheus{
|
||||||
|
Urls: []string{ts.URL},
|
||||||
|
}
|
||||||
|
|
||||||
|
var acc testutil.Accumulator
|
||||||
|
|
||||||
|
err := p.Gather(&acc)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expected := []struct {
|
||||||
|
name string
|
||||||
|
value float64
|
||||||
|
tags map[string]string
|
||||||
|
}{
|
||||||
|
{"go_gc_duration_seconds_count", 7, map[string]string{}},
|
||||||
|
{"go_goroutines", 15, map[string]string{}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range expected {
|
||||||
|
assert.NoError(t, acc.ValidateValue(e.name, e.value))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue