From 9a6754137a2d196acf6b119c6464efed743b0782 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sat, 20 Jun 2015 13:32:32 +0100 Subject: [PATCH 1/3] Improve test infrastructure --- plugins/mysql/mysql_test.go | 2 +- plugins/postgresql/postgresql_test.go | 2 +- plugins/system/system_test.go | 4 +++- testutil/accumulator.go | 25 +++++++++++-------------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/plugins/mysql/mysql_test.go b/plugins/mysql/mysql_test.go index 33643861a..76cb28b0d 100644 --- a/plugins/mysql/mysql_test.go +++ b/plugins/mysql/mysql_test.go @@ -39,7 +39,7 @@ func TestMysqlGeneratesMetrics(t *testing.T) { var count int for _, p := range acc.Points { - if strings.HasPrefix(p.Name, prefix.prefix) { + if strings.HasPrefix(p.Measurement, prefix.prefix) { count++ } } diff --git a/plugins/postgresql/postgresql_test.go b/plugins/postgresql/postgresql_test.go index b11200e9f..91776d619 100644 --- a/plugins/postgresql/postgresql_test.go +++ b/plugins/postgresql/postgresql_test.go @@ -91,7 +91,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { var found bool for _, pnt := range acc.Points { - if pnt.Name == "xact_commit" { + if pnt.Measurement == "xact_commit" { if pnt.Tags["db"] == "postgres" { found = true break diff --git a/plugins/system/system_test.go b/plugins/system/system_test.go index b8e0e169c..1c40b7f97 100644 --- a/plugins/system/system_test.go +++ b/plugins/system/system_test.go @@ -272,7 +272,9 @@ func TestSystemStats_GenerateStats(t *testing.T) { require.NoError(t, err) dockertags := map[string]string{ - "id": "blah", + "name": "blah", + "id": "", + "command": "", } assert.True(t, acc.CheckTaggedValue("user", 3.1, dockertags)) diff --git a/testutil/accumulator.go b/testutil/accumulator.go index 645366fd5..6a7ad99f0 100644 --- a/testutil/accumulator.go +++ b/testutil/accumulator.go @@ -2,6 +2,7 @@ package testutil import ( "fmt" + "reflect" "time" ) @@ -18,6 +19,9 @@ type Accumulator struct { } func (a *Accumulator) Add(measurement string, value interface{}, tags map[string]string) { + if tags == nil { + tags = map[string]string{} + } a.Points = append( a.Points, &Point{ @@ -70,30 +74,23 @@ func (a *Accumulator) CheckTaggedValue(measurement string, val interface{}, tags } func (a *Accumulator) ValidateTaggedValue(measurement string, val interface{}, tags map[string]string) error { + if tags == nil { + tags = map[string]string{} + } for _, p := range a.Points { - var found bool - - if p.Tags == nil && tags == nil { - found = true - } else { - for k, v := range p.Tags { - if tags[k] == v { - found = true - break - } - } + if !reflect.DeepEqual(tags, p.Tags) { + continue } - if found && p.Measurement == measurement { + if p.Measurement == measurement { if p.Value != val { return fmt.Errorf("%v (%T) != %v (%T)", p.Value, p.Value, val, val) } - return nil } } - return fmt.Errorf("unknown value %s with tags %v", measurement, tags) + return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags) } func (a *Accumulator) ValidateValue(measurement string, val interface{}) error { From 05924b9d09f0e0b5af684c50156c7e58e63c6882 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sat, 20 Jun 2015 13:38:01 +0100 Subject: [PATCH 2/3] Add Prometheus plugin. This allows pulling Prometheus metrics from any client library or exporter over HTTP. --- README.md | 1 + plugins/all/all.go | 1 + plugins/prometheus/prometheus.go | 105 ++++++++++++++++++++++++++ plugins/prometheus/prometheus_test.go | 56 ++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 plugins/prometheus/prometheus.go create mode 100644 plugins/prometheus/prometheus_test.go diff --git a/README.md b/README.md index d72701aaf..8b457abb4 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Telegraf currently has support for collecting metrics from: * System (memory, CPU, network, etc.) * Docker * MySQL +* Prometheus (client libraries and exporters) * PostgreSQL * Redis diff --git a/plugins/all/all.go b/plugins/all/all.go index 8acedb33f..a1493940e 100644 --- a/plugins/all/all.go +++ b/plugins/all/all.go @@ -3,6 +3,7 @@ package all import ( _ "github.com/influxdb/telegraf/plugins/mysql" _ "github.com/influxdb/telegraf/plugins/postgresql" + _ "github.com/influxdb/telegraf/plugins/prometheus" _ "github.com/influxdb/telegraf/plugins/redis" _ "github.com/influxdb/telegraf/plugins/system" ) diff --git a/plugins/prometheus/prometheus.go b/plugins/prometheus/prometheus.go new file mode 100644 index 000000000..4029e9932 --- /dev/null +++ b/plugins/prometheus/prometheus.go @@ -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{} + }) +} diff --git a/plugins/prometheus/prometheus_test.go b/plugins/prometheus/prometheus_test.go new file mode 100644 index 000000000..be2b190e7 --- /dev/null +++ b/plugins/prometheus/prometheus_test.go @@ -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)) + } +} From 45d9aec62ef377a7f95180658682be5a89430d1b Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sat, 20 Jun 2015 15:00:49 +0100 Subject: [PATCH 3/3] Add optional Prometheus exporter to telegraf. This will allow Telegraf plugins to be useful with Prometheus. This is disabled by default. --- agent.go | 62 +++++++++++++++++++++++++++++++++++++++- cmd/telegraf/telegraf.go | 18 ++++++++++++ config.go | 23 +++++++++++++-- 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/agent.go b/agent.go index 7ac924f63..00c8d3bc6 100644 --- a/agent.go +++ b/agent.go @@ -5,18 +5,21 @@ import ( "log" "net/url" "os" + "regexp" "sort" "sync" "time" "github.com/influxdb/influxdb/client" "github.com/influxdb/telegraf/plugins" + "github.com/prometheus/client_golang/prometheus" ) type runningPlugin struct { name string plugin plugins.Plugin config *ConfiguredPlugin + mu sync.Mutex } type Agent struct { @@ -53,6 +56,7 @@ func NewAgent(config *Config) (*Agent, error) { } config.Tags["host"] = agent.Hostname + prometheus.MustRegister(agent) return agent, nil } @@ -97,7 +101,7 @@ func (a *Agent) LoadPlugins() ([]string, error) { return nil, err } - a.plugins = append(a.plugins, &runningPlugin{name, plugin, config}) + a.plugins = append(a.plugins, &runningPlugin{name, plugin, config, sync.Mutex{}}) names = append(names, name) } @@ -125,7 +129,9 @@ func (a *Agent) crankParallel() error { acc.Prefix = plugin.name + "_" acc.Config = plugin.config + plugin.mu.Lock() plugin.plugin.Gather(&acc) + plugin.mu.Unlock() points <- &acc }(plugin) @@ -156,7 +162,9 @@ func (a *Agent) crank() error { for _, plugin := range a.plugins { acc.Prefix = plugin.name + "_" acc.Config = plugin.config + plugin.mu.Lock() err := plugin.plugin.Gather(&acc) + plugin.mu.Unlock() if err != nil { return err } @@ -180,7 +188,9 @@ func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) err acc.Prefix = plugin.name + "_" acc.Config = plugin.config + plugin.mu.Lock() err := plugin.plugin.Gather(&acc) + plugin.mu.Unlock() if err != nil { return err } @@ -200,6 +210,56 @@ func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) err } } +// Implements prometheus.Collector +func (a *Agent) Describe(ch chan<- *prometheus.Desc) { + prometheus.NewGauge(prometheus.GaugeOpts{Name: "Dummy", Help: "Dummy"}).Describe(ch) +} + +// Implements prometheus.Collector +func (a *Agent) Collect(ch chan<- prometheus.Metric) { + var wg sync.WaitGroup + + invalidNameCharRE := regexp.MustCompile(`[^a-zA-Z0-9_]`) + + for _, plugin := range a.plugins { + wg.Add(1) + go func(plugin *runningPlugin) { + defer wg.Done() + var acc BatchPoints + acc.Prefix = plugin.name + "_" + + plugin.mu.Lock() + err := plugin.plugin.Gather(&acc) + plugin.mu.Unlock() + if err != nil { + return + } + + for _, point := range acc.Points { + var value float64 + switch point.Fields["value"].(type) { + case float64, int64: + value = point.Fields["value"].(float64) + default: + continue + } + tags := map[string]string{} + for k, v := range point.Tags { + tags[invalidNameCharRE.ReplaceAllString(k, "_")] = v + } + desc := prometheus.NewDesc(invalidNameCharRE.ReplaceAllString(point.Measurement, "_"), point.Measurement, nil, tags) + metric, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, value) + if err == nil { + ch <- metric + } + } + + }(plugin) + } + + wg.Wait() +} + func (a *Agent) TestAllPlugins() error { var names []string diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index f36693ebc..f4cd6c693 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -4,12 +4,14 @@ import ( "flag" "fmt" "log" + "net/http" "os" "os/signal" "strings" "github.com/influxdb/telegraf" _ "github.com/influxdb/telegraf/plugins/all" + "github.com/prometheus/client_golang/prometheus" ) var fDebug = flag.Bool("debug", false, "show metrics as they're generated to stdout") @@ -117,5 +119,21 @@ func main() { f.Close() } + pc := &telegraf.PrometheusCollector{ListenAddress: ""} + err = config.ApplyPrometheusCollector(pc) + if err != nil { + log.Fatal(err) + } + if pc.ListenAddress != "" { + http.Handle("/metrics", prometheus.Handler()) + log.Printf("Listening on %s for Prometheus", pc.ListenAddress) + go func() { + err := http.ListenAndServe(pc.ListenAddress, nil) + if err != nil { + log.Fatal(err) + } + }() + } + ag.Run(shutdown) } diff --git a/config.go b/config.go index 013b8d281..6953195ba 100644 --- a/config.go +++ b/config.go @@ -36,8 +36,13 @@ type Config struct { UserAgent string Tags map[string]string - agent *ast.Table - plugins map[string]*ast.Table + agent *ast.Table + plugins map[string]*ast.Table + prometheusCollector *ast.Table +} + +type PrometheusCollector struct { + ListenAddress string } func (c *Config) Plugins() map[string]*ast.Table { @@ -85,6 +90,14 @@ func (c *Config) ApplyAgent(v interface{}) error { return nil } +func (c *Config) ApplyPrometheusCollector(v interface{}) error { + if c.prometheusCollector != nil { + return toml.UnmarshalTable(c.prometheusCollector, v) + } + + return nil +} + func (c *Config) ApplyPlugin(name string, v interface{}) (*ConfiguredPlugin, error) { cp := &ConfiguredPlugin{Name: name} @@ -183,6 +196,8 @@ func LoadConfig(path string) (*Config, error) { } case "agent": c.agent = subtbl + case "prometheus_collector": + c.prometheusCollector = subtbl default: c.plugins[name] = subtbl } @@ -261,6 +276,10 @@ database = "telegraf" # required. # debug = false # hostname = "prod3241" +[prometheus_collector] +# If set, expose all metrics on this address for Prometheus +# listen_address = ":9115" + # PLUGINS `