2016-06-02 14:06:49 -05:00
|
|
|
package metrics
|
2016-06-02 07:32:17 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-08-09 04:07:26 -05:00
|
|
|
"strings"
|
2016-06-02 07:32:17 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GraphitePublisher struct {
|
2016-06-03 08:06:57 -05:00
|
|
|
address string
|
|
|
|
protocol string
|
|
|
|
prefix string
|
|
|
|
prevCounts map[string]int64
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateGraphitePublisher() (*GraphitePublisher, error) {
|
|
|
|
graphiteSection, err := setting.Cfg.GetSection("metrics.graphite")
|
|
|
|
if err != nil {
|
2016-08-11 10:00:09 -05:00
|
|
|
return nil, nil
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2016-08-15 02:37:24 -05:00
|
|
|
address := graphiteSection.Key("address").String()
|
|
|
|
if address == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:56:07 -05:00
|
|
|
publisher := &GraphitePublisher{}
|
2016-06-03 08:06:57 -05:00
|
|
|
publisher.prevCounts = make(map[string]int64)
|
|
|
|
publisher.protocol = "tcp"
|
2016-08-17 02:53:46 -05:00
|
|
|
publisher.prefix = graphiteSection.Key("prefix").MustString("prod.grafana.%(instance_name)s")
|
2016-08-15 02:37:24 -05:00
|
|
|
publisher.address = address
|
2016-08-09 04:07:26 -05:00
|
|
|
|
|
|
|
safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1)
|
2016-08-10 04:15:27 -05:00
|
|
|
prefix := graphiteSection.Key("prefix").Value()
|
2016-08-09 04:07:26 -05:00
|
|
|
|
2016-08-10 04:15:27 -05:00
|
|
|
if prefix == "" {
|
2016-08-17 02:53:46 -05:00
|
|
|
prefix = "prod.grafana.%(instance_name)s."
|
2016-08-10 04:15:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
publisher.prefix = strings.Replace(prefix, "%(instance_name)s", safeInstanceName, -1)
|
2016-06-02 09:56:07 -05:00
|
|
|
return publisher, nil
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2016-06-02 14:06:49 -05:00
|
|
|
func (this *GraphitePublisher) Publish(metrics []Metric) {
|
2016-06-03 08:06:57 -05:00
|
|
|
conn, err := net.DialTimeout(this.protocol, this.address, time.Second*5)
|
2016-06-02 07:32:17 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(3, "Metrics: GraphitePublisher: Failed to connect to %s!", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
now := time.Now().Unix()
|
2016-06-03 02:17:36 -05:00
|
|
|
|
2016-06-02 14:06:49 -05:00
|
|
|
for _, m := range metrics {
|
2016-06-03 08:06:57 -05:00
|
|
|
metricName := this.prefix + m.Name() + m.StringifyTags()
|
2016-06-02 14:06:49 -05:00
|
|
|
|
|
|
|
switch metric := m.(type) {
|
|
|
|
case Counter:
|
2016-06-03 08:06:57 -05:00
|
|
|
this.addCount(buf, metricName+".count", metric.Count(), now)
|
2016-09-22 04:16:19 -05:00
|
|
|
case Gauge:
|
|
|
|
this.addCount(buf, metricName, metric.Value(), now)
|
2016-06-03 02:17:36 -05:00
|
|
|
case Timer:
|
2016-06-03 05:50:51 -05:00
|
|
|
percentiles := metric.Percentiles([]float64{0.25, 0.75, 0.90, 0.99})
|
2016-06-03 08:06:57 -05:00
|
|
|
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)
|
2016-06-02 14:06:49 -05:00
|
|
|
}
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Metrics: GraphitePublisher.Publish() \n%s", buf)
|
|
|
|
_, err = conn.Write(buf.Bytes())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(3, "Metrics: GraphitePublisher: Failed to send metrics! %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 08:06:57 -05:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|