-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatadog.go
183 lines (159 loc) · 6.09 KB
/
datadog.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2022 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"context"
"fmt"
"net/http"
"sort"
datadog "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
)
// Sort old-to-new
type statOccurrence []AggregatedStat
func (list statOccurrence) Len() int { return len(list) }
func (list statOccurrence) Swap(i, j int) { list[i], list[j] = list[j], list[i] }
func (list statOccurrence) Less(i, j int) bool {
var si = list[i]
var sj = list[j]
return si.Time < sj.Time
}
// Write new stats to DataDog
func datadogUploadStats(hostname string, bucketSecs int64, addedStats map[string][]StatsStat) (err error) {
// Generate the list of aggregated stats
aggregatedStats := statsAggregate(addedStats, bucketSecs)
if len(aggregatedStats) == 0 {
return
}
// Sort stats as old-to-new
sort.Sort(statOccurrence(aggregatedStats))
// Create the metrics
var series datadog.Series
seriesArray := []datadog.Series{}
series = datadog.Series{Metric: "notehub." + hostname + ".disk.reads", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.DiskReads)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".disk.writes", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.DiskWrites)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".net.received", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.NetReceived)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".net.sent", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.NetSent)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".http.conn", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.HttpConnTotal)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".http.connreused", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.HttpConnReused)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".handlers", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.HandlersDiscovery + stat.HandlersContinuous)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".events.received", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.EventsReceived)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".events.routed", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.EventsRouted)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".database.reads", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.DatabaseReads)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".database.writes", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.DatabaseWrites)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
series = datadog.Series{Metric: "notehub." + hostname + ".api.calls", Type: datadog.PtrString("gauge")}
for _, stat := range aggregatedStats {
point := []*float64{
datadog.PtrFloat64(float64(stat.Time)),
datadog.PtrFloat64(float64(stat.APITotal)),
}
series.Points = append(series.Points, point)
}
seriesArray = append(seriesArray, series)
// Submit the metrics
ctx := context.Background()
ctx = context.WithValue(ctx, datadog.ContextServerVariables, map[string]string{"site": Config.DatadogSite})
keys := make(map[string]datadog.APIKey)
keys["apiKeyAuth"] = datadog.APIKey{Key: Config.DatadogAPIKey}
keys["appKeyAuth"] = datadog.APIKey{Key: Config.DatadogAppKey}
ctx = context.WithValue(ctx, datadog.ContextAPIKeys, keys)
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
body := datadog.MetricsPayload{Series: seriesArray}
var r *http.Response
_, r, err = apiClient.MetricsApi.SubmitMetrics(ctx, body, *datadog.NewSubmitMetricsOptionalParameters())
if err != nil {
fmt.Printf("datadog: error submitting metrics: %s\n", err)
fmt.Printf("%v\n", r)
}
// Done
return
}