-
Notifications
You must be signed in to change notification settings - Fork 174
/
main.go
162 lines (139 loc) · 4.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"flag"
"fmt"
"io"
"math"
"net/http"
"regexp"
"strings"
"time"
"github.com/DataDog/datadog-go/statsd"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/sirupsen/logrus"
)
var (
debug = flag.Bool("d", false, "Enable debug mode")
metricsHost = flag.String("h", "http://localhost:9090/metrics", "The full URL — like 'http://localhost:9090/metrics' to query for Prometheus metrics.")
interval = flag.String("i", "10s", "The interval at which to query. Value must be parseable by time.ParseDuration (https://golang.org/pkg/time/#ParseDuration).")
ignoredLabelsStr = flag.String("ignored-labels", "", "A comma-seperated list of label name regexes to not export")
ignoredMetricsStr = flag.String("ignored-metrics", "", "A comma-seperated list of metric name regexes to not export")
prefix = flag.String("p", "", "A prefix to append to any metrics emitted. Do not include a trailing period.")
statsHost = flag.String("s", "127.0.0.1:8126", "The host and port — like '127.0.0.1:8126' — to send our metrics to.")
)
func main() {
flag.Parse()
c, _ := statsd.New(*statsHost)
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
i, err := time.ParseDuration(*interval)
if err != nil {
logrus.WithError(err).Fatalf("Failed to parse interval '%s'", *interval)
}
ignoredLabels := []*regexp.Regexp{}
ignoredMetrics := []*regexp.Regexp{}
for _, ignoredLabelStr := range strings.Split(*ignoredLabelsStr, ",") {
if len(ignoredLabelStr) > 0 {
ignoredLabels = append(ignoredLabels, regexp.MustCompile(ignoredLabelStr))
}
}
for _, ignoredMetricStr := range strings.Split(*ignoredMetricsStr, ",") {
if len(ignoredMetricStr) > 0 {
ignoredMetrics = append(ignoredMetrics, regexp.MustCompile(ignoredMetricStr))
}
}
ticker := time.NewTicker(i)
for _ = range ticker.C {
collect(c, ignoredLabels, ignoredMetrics)
}
if *prefix != "" {
c.Namespace = *prefix
}
}
func collect(c *statsd.Client, ignoredLabels []*regexp.Regexp, ignoredMetrics []*regexp.Regexp) {
logrus.WithFields(logrus.Fields{
"stats_host": *statsHost,
"metrics_host": *metricsHost,
"ignored_labels": ignoredLabels,
"ignored_metrics": ignoredMetrics,
}).Debug("Beginning collection")
resp, _ := http.Get(*metricsHost)
d := expfmt.NewDecoder(resp.Body, expfmt.FmtText)
var mf dto.MetricFamily
for {
err := d.Decode(&mf)
if err == io.EOF {
// We've hit the end, break out!
break
} else if err != nil {
c.Count("veneur.prometheus.decode_errors_total", 1, nil, 1.0)
logrus.WithError(err).Warn("Failed to decode a metric")
break
}
if !shouldExportMetric(mf, ignoredMetrics) {
continue
}
var metricCount int64
switch mf.GetType() {
case dto.MetricType_COUNTER:
for _, counter := range mf.GetMetric() {
tags := getTags(counter.GetLabel(), ignoredLabels)
c.Count(mf.GetName(), int64(counter.GetCounter().GetValue()), tags, 1.0)
metricCount++
}
case dto.MetricType_GAUGE:
for _, gauge := range mf.GetMetric() {
tags := getTags(gauge.GetLabel(), ignoredLabels)
c.Gauge(mf.GetName(), float64(gauge.GetGauge().GetValue()), tags, 1.0)
metricCount++
}
case dto.MetricType_HISTOGRAM, dto.MetricType_SUMMARY:
for _, histo := range mf.GetMetric() {
tags := getTags(histo.GetLabel(), ignoredLabels)
hname := mf.GetName()
summ := histo.GetSummary()
c.Gauge(fmt.Sprintf("%s.sum", hname), summ.GetSampleSum(), tags, 1.0)
c.Gauge(fmt.Sprintf("%s.count", hname), float64(summ.GetSampleCount()), tags, 1.0)
for _, quantile := range summ.GetQuantile() {
v := quantile.GetValue()
if !math.IsNaN(v) {
c.Gauge(fmt.Sprintf("%s.%dpercentile", hname, int(quantile.GetQuantile()*100)), v, tags, 1.0)
metricCount++
}
}
}
default:
c.Count("veneur.prometheus.unknown_metric_type_total", 1, nil, 1.0)
}
c.Count("veneur.prometheus.metrics_flushed_total", metricCount, nil, 1.0)
}
}
func getTags(labels []*dto.LabelPair, ignoredLabels []*regexp.Regexp) []string {
var tags []string
for _, pair := range labels {
labelName := pair.GetName()
labelValue := pair.GetValue()
include := true
for _, ignoredLabel := range ignoredLabels {
if ignoredLabel.MatchString(labelName) {
include = false
break
}
}
if include {
tags = append(tags, fmt.Sprintf("%s:%s", labelName, labelValue))
}
}
return tags
}
func shouldExportMetric(mf dto.MetricFamily, ignoredMetrics []*regexp.Regexp) bool {
for _, ignoredMetric := range ignoredMetrics {
metricName := mf.GetName()
if ignoredMetric.MatchString(metricName) {
return false
}
}
return true
}