Skip to content

Commit

Permalink
Add prometheus_client output module
Browse files Browse the repository at this point in the history
Adds a client implementation using the prometheus go_client library
that exposes metrics. Currently only supports Gauge type events.

X-Github-Issue: influxdata#306
  • Loading branch information
oldmantaiter committed Oct 22, 2015
1 parent 2f08577 commit d05bc7f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions outputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import (
_ "github.com/influxdb/telegraf/outputs/kafka"
_ "github.com/influxdb/telegraf/outputs/mqtt"
_ "github.com/influxdb/telegraf/outputs/opentsdb"
_ "github.com/influxdb/telegraf/outputs/prometheus"
)
102 changes: 102 additions & 0 deletions outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package prometheus

import (
"fmt"
"github.com/influxdb/influxdb/client/v2"
"github.com/influxdb/telegraf/outputs"
"github.com/prometheus/client_golang/prometheus"
"net/http"
)

type PrometheusClient struct {
Listen string
server *http.Server
gauges map[string]*prometheus.GaugeVec
}

var sampleConfig = `
# Address to listen on
listen = ":8000"
`

func (p *PrometheusClient) Connect() error {
if p.Listen == "" {
p.Listen = ":8000"
}
http.Handle("/metrics", prometheus.Handler())
server := &http.Server{
Addr: p.Listen,
Handler: prometheus.Handler(),
}
p.server = server
p.gauges = make(map[string]*prometheus.GaugeVec)
go p.server.ListenAndServe()
return nil
}

func (p *PrometheusClient) Close() error {
// TODO: Add a proper close function
return nil
}

func (p *PrometheusClient) SampleConfig() string {
return sampleConfig
}

func (p *PrometheusClient) Description() string {
return "Configuration for the Prometheus client to spawn"
}

func (p *PrometheusClient) Write(points []*client.Point) error {
if len(points) == 0 {
return nil
}

for _, point := range points {
var labels []string
name := point.Name()
key := name

for k, _ := range point.Tags() {
if len(k) > 0 {
labels = append(labels, k)
// key += "_" + k
}
}
// sort.Strings(labels)

if _, ok := p.gauges[key]; !ok {
// Setup a new gauge vector
p.gauges[key] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: key,
Help: fmt.Sprintf("Telegraf collected point '%s'", name),
},
labels,
)
prometheus.MustRegister(p.gauges[key])
}

l := prometheus.Labels{}
for tk, tv := range point.Tags() {
l[tk] = tv
}

for _, val := range point.Fields() {
switch val.(type) {
case int64:
ival := val.(int64)
p.gauges[key].With(l).Set(float64(ival))
case float64:
p.gauges[key].With(l).Set(val.(float64))
}
}
}
return nil
}

func init() {
outputs.Add("prometheus_client", func() outputs.Output {
return &PrometheusClient{}
})
}
3 changes: 2 additions & 1 deletion plugins/aerospike/aerospike.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ func get(key []byte, host string) (map[string]string, error) {
func readAerospikeStats(stats map[string]string, acc plugins.Accumulator, host, namespace string) {
for key, value := range stats {
tags := map[string]string{
"host": host,
"host": host,
"namespace": "_service",
}

if namespace != "" {
Expand Down

0 comments on commit d05bc7f

Please sign in to comment.