-
Notifications
You must be signed in to change notification settings - Fork 1
/
statsd.go
50 lines (38 loc) · 1.14 KB
/
statsd.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/DataDog/datadog-go/statsd"
)
func giveMetricsToDatadogStatsd(debug bool, metrics []string, model string, statsdipport string) {
client, err := statsd.New(statsdipport) // udp
if err != nil {
logFatalf("Error creating StatsD client: %v", err)
}
defer client.Close()
retrievedFloatMetrics := map[string]float64{}
for _, metric := range metrics {
metric = strings.ToLower(strings.TrimSpace(metric))
retrievedMetric := strings.Split(metric, "=")[0]
retrievedMetricValue := strings.Split(metric, "=")[1]
if strings.Contains(retrievedMetricValue, ".0") {
retrievedMetricFloat, err := strconv.ParseFloat(retrievedMetricValue, 64)
if err != nil {
fmt.Println("Error:", err)
return
}
retrievedFloatMetrics[retrievedMetric] = retrievedMetricFloat
}
}
for key, value := range retrievedFloatMetrics {
if debug {
logWithTimestamp("Metric sent: " + key + "=" + strconv.FormatFloat(value, 'f', -1, 64))
}
err = client.Gauge(key, value, []string{"gateway:" + model}, 1)
if err != nil {
log.Printf("Error sending %s to statsd: %v", key, err)
}
}
}