forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
2f08577
commit d05bc7f
Showing
3 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters