Skip to content

Commit

Permalink
Fix prometheus label names, and dont panic if invalid
Browse files Browse the repository at this point in the history
fixes #907
  • Loading branch information
sparrc committed Mar 22, 2016
1 parent 7f65ffc commit 69606a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [#884](https://github.com/influxdata/telegraf/issues/884): Do not call write method if there are 0 metrics to write.
- [#898](https://github.com/influxdata/telegraf/issues/898): Put database name in quotes, fixes special characters in the database name.
- [#656](https://github.com/influxdata/telegraf/issues/656): No longer run `lsof` on linux to get netstat data, fixes permissions issue.
- [#907](https://github.com/influxdata/telegraf/issues/907): Fix prometheus invalid label/measurement name key.

## v0.11.1 [2016-03-17]

Expand Down
2 changes: 0 additions & 2 deletions plugins/inputs/udp_listener/udp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ func (u *UdpListener) Start(acc telegraf.Accumulator) error {
}

func (u *UdpListener) Stop() {
u.Lock()
defer u.Unlock()
close(u.done)
u.listener.Close()
u.wg.Wait()
Expand Down
46 changes: 36 additions & 10 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ import (
"fmt"
"log"
"net/http"
"regexp"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/prometheus/client_golang/prometheus"
)

var (
sanitizedChars = strings.NewReplacer("/", "_", "@", "_", " ", "_", "-", "_", ".", "_")

// Prometheus metric names must match this regex
// see https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
metricName = regexp.MustCompile("^[a-zA-Z_:][a-zA-Z0-9_:]*$")

// Prometheus labels must match this regex
// see https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
labelName = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
)

type PrometheusClient struct {
Listen string
metrics map[string]*prometheus.UntypedVec
Expand Down Expand Up @@ -64,27 +78,36 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
}

for _, point := range metrics {
var labels []string
key := point.Name()
key = sanitizedChars.Replace(key)

for k, _ := range point.Tags() {
if len(k) > 0 {
labels = append(labels, k)
}
}

var labels []string
l := prometheus.Labels{}
for tk, tv := range point.Tags() {
l[tk] = tv
for k, v := range point.Tags() {
k = sanitizedChars.Replace(k)
if len(k) == 0 {
continue
}
if !labelName.MatchString(k) {
continue
}
labels = append(labels, k)
l[k] = v
}

for n, val := range point.Fields() {
n = sanitizedChars.Replace(n)
var mname string
if n == "value" {
mname = key
} else {
mname = fmt.Sprintf("%s_%s", key, n)
}

if !metricName.MatchString(mname) {
continue
}

if _, ok := p.metrics[mname]; !ok {
p.metrics[mname] = prometheus.NewUntypedVec(
prometheus.UntypedOpts{
Expand All @@ -93,7 +116,10 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
},
labels,
)
prometheus.MustRegister(p.metrics[mname])
if err := prometheus.Register(p.metrics[mname]); err != nil {
log.Printf("prometheus_client: Metric failed to register with prometheus, %s", err)
continue
}
}

switch val := val.(type) {
Expand Down

0 comments on commit 69606a4

Please sign in to comment.