-
Notifications
You must be signed in to change notification settings - Fork 4
/
metric_wavefront.go
93 lines (76 loc) · 2.28 KB
/
metric_wavefront.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
package owl
import (
"net"
"os"
"strings"
"time"
"github.com/rcrowley/go-metrics"
"github.com/wavefronthq/go-metrics-wavefront"
)
var wavefrontConfig *wavefront.WavefrontConfig
func initMetricWavefront() error {
url := extractWavefrontUrl(config.Metric.WavefrontUrl)
if url == "" {
useMetric = false
return Error("owl: invalid Wavefront proxy address (%s): should be URL:PORT or $URL:$PORT", config.Metric.WavefrontUrl)
}
addr, err := net.ResolveTCPAddr("tcp", url)
if err != nil {
useMetric = false
return Error("owl: cannot resolve Wavefront proxy address (%s): %s", config.Metric.WavefrontUrl, err)
}
var hostTags map[string]string
if GitTag == "" {
hostTags = nil
} else {
hostTags = map[string]string{"git_tag": GitTag}
}
wavefrontConfig = &wavefront.WavefrontConfig{
Addr: addr,
Registry: metrics.DefaultRegistry,
DurationUnit: time.Nanosecond,
Prefix: config.AppName,
HostTags: hostTags,
Percentiles: []float64{0.5, 0.75, 0.95, 0.99, 0.999},
}
return nil
}
func extractWavefrontUrl(wavefrontUrl string) string {
splits := strings.Split(wavefrontUrl, ":")
if len(splits) != 2 {
useMetric = false
return ""
}
address, port := splits[0], splits[1]
if address == "" || port == "" {
return ""
}
if strings.HasPrefix(address, "$") && strings.HasPrefix(port, "$") {
// Start with $ -> Load from the environment
address = os.Getenv(address[1:])
port = os.Getenv(port[1:])
if address != "" && port != "" {
return address + ":" + port
}
} else if !strings.HasPrefix(address, "$") && !strings.HasPrefix(port, "$") {
// Start without $ -> Use the URL directly
return wavefrontUrl
}
return ""
}
func metricIncWavefront(stat string, value int64, tags map[string]string) {
if !useMetric || wavefrontConfig == nil {
return
}
counter := wavefront.GetOrRegisterMetric(stat, metrics.NewCounter(), tags).(metrics.Counter)
counter.Inc(value)
wavefront.WavefrontSingleMetric(wavefrontConfig, stat, counter, tags)
}
func metricGaugeWavefront(stat string, value int64, tags map[string]string) {
if !useMetric || wavefrontConfig == nil {
return
}
gauge := wavefront.GetOrRegisterMetric(stat, metrics.NewGauge(), tags).(metrics.Gauge)
gauge.Update(value)
wavefront.WavefrontSingleMetric(wavefrontConfig, stat, gauge, tags)
}