This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
stats.go
133 lines (115 loc) · 3.55 KB
/
stats.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package datadog
import (
"fmt"
"github.com/DataDog/datadog-go/statsd"
"go.opencensus.io/stats/view"
"sync"
)
const (
// DefaultStatsAddrUDP specifies the default protocol (UDP) and address
// for the DogStatsD service.
DefaultStatsAddrUDP = "localhost:8125"
// DefaultStatsAddrUDS specifies the default socket address for the
// DogStatsD service over UDS. Only useful for platforms supporting unix
// sockets.
DefaultStatsAddrUDS = "unix:///var/run/datadog/dsd.socket"
)
// collector implements statsd.Client
type statsExporter struct {
opts Options
client *statsd.Client
mu sync.Mutex // mu guards viewData
viewData map[string]*view.Data
countData map[string]int64
}
func newStatsExporter(o Options) (*statsExporter, error) {
endpoint := o.StatsAddr
if endpoint == "" {
endpoint = DefaultStatsAddrUDP
}
client, err := statsd.New(endpoint, o.StatsdOptions...)
if err != nil {
return nil, err
}
return &statsExporter{
opts: o,
viewData: make(map[string]*view.Data),
countData: make(map[string]int64),
client: client,
}, nil
}
func (s *statsExporter) addViewData(vd *view.Data) {
sig := viewSignature(s.opts.Namespace, s.opts.TagMetricNames, vd.View)
s.mu.Lock()
s.viewData[sig] = vd
s.mu.Unlock()
var lastErr error
for _, row := range vd.Rows {
if err := s.submitMetric(vd.View, row, sig); err != nil {
lastErr = err
}
}
if lastErr != nil {
s.opts.onError(lastErr) // Only report last error.
}
}
func (s *statsExporter) submitMetric(v *view.View, row *view.Row, metricName string) error {
var err error
const rate = float64(1)
client := s.client
opt := s.opts
tags := []string{}
switch data := row.Data.(type) {
case *view.CountData:
// get a unique string for metric and associated tags
metricID := metricRowID(row, metricName)
// compute the difference of now and last collected period
submitData := data.Value - s.countData[metricID]
// update map with current value
s.countData[metricID] = data.Value
return client.Count(metricName, submitData, opt.tagMetrics(row.Tags, tags), rate)
case *view.SumData:
return client.Gauge(metricName, float64(data.Value), opt.tagMetrics(row.Tags, tags), rate)
case *view.LastValueData:
return client.Gauge(metricName, float64(data.Value), opt.tagMetrics(row.Tags, tags), rate)
case *view.DistributionData:
var metrics = map[string]float64{
"min": data.Min,
"max": data.Max,
"count": float64(data.Count),
"avg": data.Mean,
"squared_dev_sum": data.SumOfSquaredDev,
}
for name, value := range metrics {
err = client.Gauge(metricName+"."+name, value, opt.tagMetrics(row.Tags, tags), rate)
}
if !s.opts.DisableCountPerBuckets {
for x := range data.CountPerBucket {
addlTags := []string{"bucket_idx:" + fmt.Sprint(x)}
err = client.Gauge(metricName+".count_per_bucket", float64(data.CountPerBucket[x]), opt.tagMetrics(row.Tags, addlTags), rate)
}
}
return err
default:
return fmt.Errorf("aggregation %T is not supported", v.Aggregation)
}
}
func (s *statsExporter) stop() {
if err := s.client.Close(); err != nil {
s.opts.onError(err)
}
}
func metricRowID(row *view.Row, metricName string) string{
tgs := ""
for _,tag := range row.Tags{
tgs += tag.Key.Name() + ":" + tag.Value
}
if tgs != ""{
metricName += "|" + tgs
}
return metricName
}