|
|
@@ -4,7 +4,6 @@ import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"net"
|
|
|
- "reflect"
|
|
|
"time"
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
@@ -12,9 +11,10 @@ import (
|
|
|
)
|
|
|
|
|
|
type GraphitePublisher struct {
|
|
|
- Address string
|
|
|
- Protocol string
|
|
|
- Prefix string
|
|
|
+ address string
|
|
|
+ protocol string
|
|
|
+ prefix string
|
|
|
+ prevCounts map[string]int64
|
|
|
}
|
|
|
|
|
|
func CreateGraphitePublisher() (*GraphitePublisher, error) {
|
|
|
@@ -24,15 +24,16 @@ func CreateGraphitePublisher() (*GraphitePublisher, error) {
|
|
|
}
|
|
|
|
|
|
publisher := &GraphitePublisher{}
|
|
|
- publisher.Protocol = "tcp"
|
|
|
- publisher.Address = graphiteSection.Key("address").MustString("localhost:2003")
|
|
|
- publisher.Prefix = graphiteSection.Key("prefix").MustString("service.grafana.%(instance_name)s")
|
|
|
+ publisher.prevCounts = make(map[string]int64)
|
|
|
+ publisher.protocol = "tcp"
|
|
|
+ publisher.address = graphiteSection.Key("address").MustString("localhost:2003")
|
|
|
+ publisher.prefix = graphiteSection.Key("prefix").MustString("service.grafana.%(instance_name)s")
|
|
|
|
|
|
return publisher, nil
|
|
|
}
|
|
|
|
|
|
func (this *GraphitePublisher) Publish(metrics []Metric) {
|
|
|
- conn, err := net.DialTimeout(this.Protocol, this.Address, time.Second*5)
|
|
|
+ conn, err := net.DialTimeout(this.protocol, this.address, time.Second*5)
|
|
|
|
|
|
if err != nil {
|
|
|
log.Error(3, "Metrics: GraphitePublisher: Failed to connect to %s!", err)
|
|
|
@@ -41,36 +42,24 @@ func (this *GraphitePublisher) Publish(metrics []Metric) {
|
|
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
now := time.Now().Unix()
|
|
|
- addIntToBuf := func(metric string, value int64) {
|
|
|
- buf.WriteString(fmt.Sprintf("%s %d %d\n", metric, value, now))
|
|
|
- }
|
|
|
- addFloatToBuf := func(metric string, value float64) {
|
|
|
- buf.WriteString(fmt.Sprintf("%s %f %d\n", metric, value, now))
|
|
|
- }
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
- log.Info("metric: %v, %v", m, reflect.TypeOf(m))
|
|
|
- metricName := this.Prefix + m.Name() + m.StringifyTags()
|
|
|
+ metricName := this.prefix + m.Name() + m.StringifyTags()
|
|
|
|
|
|
switch metric := m.(type) {
|
|
|
case Counter:
|
|
|
- addIntToBuf(metricName+".count", metric.Count())
|
|
|
- case SimpleTimer:
|
|
|
- addIntToBuf(metricName+".count", metric.Count())
|
|
|
- addIntToBuf(metricName+".max", metric.Max())
|
|
|
- addIntToBuf(metricName+".min", metric.Min())
|
|
|
- addFloatToBuf(metricName+".mean", metric.Mean())
|
|
|
+ this.addCount(buf, metricName+".count", metric.Count(), now)
|
|
|
case Timer:
|
|
|
percentiles := metric.Percentiles([]float64{0.25, 0.75, 0.90, 0.99})
|
|
|
- addIntToBuf(metricName+".count", metric.Count())
|
|
|
- addIntToBuf(metricName+".max", metric.Max())
|
|
|
- addIntToBuf(metricName+".min", metric.Min())
|
|
|
- addFloatToBuf(metricName+".mean", metric.Mean())
|
|
|
- addFloatToBuf(metricName+".std", metric.StdDev())
|
|
|
- addFloatToBuf(metricName+".p25", percentiles[0])
|
|
|
- addFloatToBuf(metricName+".p75", percentiles[1])
|
|
|
- addFloatToBuf(metricName+".p90", percentiles[2])
|
|
|
- addFloatToBuf(metricName+".p99", percentiles[3])
|
|
|
+ this.addCount(buf, metricName+".count", metric.Count(), now)
|
|
|
+ this.addInt(buf, metricName+".max", metric.Max(), now)
|
|
|
+ this.addInt(buf, metricName+".min", metric.Min(), now)
|
|
|
+ this.addFloat(buf, metricName+".mean", metric.Mean(), now)
|
|
|
+ this.addFloat(buf, metricName+".std", metric.StdDev(), now)
|
|
|
+ this.addFloat(buf, metricName+".p25", percentiles[0], now)
|
|
|
+ this.addFloat(buf, metricName+".p75", percentiles[1], now)
|
|
|
+ this.addFloat(buf, metricName+".p90", percentiles[2], now)
|
|
|
+ this.addFloat(buf, metricName+".p99", percentiles[3], now)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -82,3 +71,22 @@ func (this *GraphitePublisher) Publish(metrics []Metric) {
|
|
|
log.Error(3, "Metrics: GraphitePublisher: Failed to send metrics! %s", err)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func (this *GraphitePublisher) addInt(buf *bytes.Buffer, metric string, value int64, now int64) {
|
|
|
+ buf.WriteString(fmt.Sprintf("%s %d %d\n", metric, value, now))
|
|
|
+}
|
|
|
+
|
|
|
+func (this *GraphitePublisher) addFloat(buf *bytes.Buffer, metric string, value float64, now int64) {
|
|
|
+ buf.WriteString(fmt.Sprintf("%s %f %d\n", metric, value, now))
|
|
|
+}
|
|
|
+
|
|
|
+func (this *GraphitePublisher) addCount(buf *bytes.Buffer, metric string, value int64, now int64) {
|
|
|
+ delta := value
|
|
|
+
|
|
|
+ if last, ok := this.prevCounts[metric]; ok {
|
|
|
+ delta = calculateDelta(last, value)
|
|
|
+ }
|
|
|
+
|
|
|
+ this.prevCounts[metric] = value
|
|
|
+ buf.WriteString(fmt.Sprintf("%s %d %d\n", metric, delta, now))
|
|
|
+}
|