-
Notifications
You must be signed in to change notification settings - Fork 19
/
metrics.go
363 lines (311 loc) · 10.4 KB
/
metrics.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Package metrics contains all the code that is used to collect metrics from the target
package metrics
import (
"fmt"
"io/ioutil"
"reflect"
"strconv"
"sync"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/infra-integrations-sdk/integration"
"github.com/newrelic/infra-integrations-sdk/log"
"github.com/newrelic/nri-mssql/src/args"
"github.com/newrelic/nri-mssql/src/connection"
"github.com/newrelic/nri-mssql/src/database"
"gopkg.in/yaml.v2"
)
type customQuery struct {
Query string
Prefix string
Name string `yaml:"metric_name"`
Type string `yaml:"metric_type"`
Database string
}
// PopulateInstanceMetrics creates instance-level metrics
func PopulateInstanceMetrics(instanceEntity *integration.Entity, connection *connection.SQLConnection, arguments args.ArgumentList) {
metricSet := instanceEntity.NewMetricSet("MssqlInstanceSample",
metric.Attribute{Key: "displayName", Value: instanceEntity.Metadata.Name},
metric.Attribute{Key: "entityName", Value: instanceEntity.Metadata.Namespace + ":" + instanceEntity.Metadata.Name},
metric.Attribute{Key: "host", Value: connection.Host},
)
collectionList := instanceDefinitions
if arguments.EnableBufferMetrics {
collectionList = append(collectionList, instanceBufferDefinitions...)
}
for _, queryDef := range instanceDefinitions {
models := queryDef.GetDataModels()
if err := connection.Query(models, queryDef.GetQuery()); err != nil {
log.Error("Could not execute instance query: %s", err.Error())
continue
}
vp := reflect.Indirect(reflect.ValueOf(models))
// Nothing was returned
if vp.Len() == 0 {
log.Debug("No data returned from instance query '%s'", queryDef.GetQuery())
continue
}
vpInterface := vp.Index(0).Interface()
err := metricSet.MarshalMetrics(vpInterface)
if err != nil {
log.Error("Could not parse metrics from instance query result: %s", err.Error())
}
}
populateWaitTimeMetrics(instanceEntity, connection)
if len(arguments.CustomMetricsQuery) > 0 {
populateCustomMetrics(instanceEntity, connection, customQuery{Query: arguments.CustomMetricsQuery})
} else if len(arguments.CustomMetricsConfig) > 0 {
queries, err := parseCustomQueries(arguments)
if err != nil {
log.Error("Failed to parse custom queries: %s", err)
}
var wg sync.WaitGroup
for _, query := range queries {
wg.Add(1)
go func(query customQuery) {
defer wg.Done()
populateCustomMetrics(instanceEntity, connection, query)
}(query)
}
wg.Wait()
}
}
func parseCustomQueries(arguments args.ArgumentList) ([]customQuery, error) {
// load YAML config file
b, err := ioutil.ReadFile(arguments.CustomMetricsConfig)
if err != nil {
return nil, fmt.Errorf("failed to read custom_metrics_config: %s", err)
}
// parse
var c struct{ Queries []customQuery }
err = yaml.Unmarshal(b, &c)
if err != nil {
return nil, fmt.Errorf("failed to parse custom_metrics_config: %s", err)
}
return c.Queries, nil
}
func populateWaitTimeMetrics(instanceEntity *integration.Entity, connection *connection.SQLConnection) {
models := make([]waitTimeModel, 0)
if err := connection.Query(&models, waitTimeQuery); err != nil {
log.Error("Could not execute query: %s", err.Error())
return
}
for _, model := range models {
metricSet := instanceEntity.NewMetricSet("MssqlWaitSample",
metric.Attribute{Key: "displayName", Value: instanceEntity.Metadata.Name},
metric.Attribute{Key: "entityName", Value: instanceEntity.Metadata.Namespace + ":" + instanceEntity.Metadata.Name},
metric.Attribute{Key: "waitType", Value: *model.WaitType},
metric.Attribute{Key: "host", Value: connection.Host},
metric.Attribute{Key: "instance", Value: instanceEntity.Metadata.Name},
)
metrics := []struct {
metricName string
metricValue int
metricType metric.SourceType
}{
{
"system.waitTimeCount", *model.WaitCount, metric.GAUGE,
},
{
"system.waitTimeInMillisecondsPerSecond", *model.WaitTime, metric.GAUGE,
},
}
for _, metric := range metrics {
err := metricSet.SetMetric(metric.metricName, metric.metricValue, metric.metricType)
if err != nil {
log.Error("Could not set wait time metric '%s' for wait type '%s': %s", metric.metricName, model.WaitType, err.Error())
}
}
}
}
// Execute one or more custom queries
func populateCustomMetrics(instanceEntity *integration.Entity, connection *connection.SQLConnection, query customQuery) {
var prefix string
if len(query.Database) > 0 {
prefix = "USE " + query.Database + "; "
}
rows, err := connection.Queryx(prefix + query.Query)
if err != nil {
log.Error("Could not execute custom query: %s", err)
return
}
defer func() {
_ = rows.Close()
}()
for rows.Next() {
row := make(map[string]interface{})
err := rows.MapScan(row)
if err != nil {
log.Error("Failed to scan custom query row: %s", err)
return
}
nameInterface, ok := row["metric_name"]
var name string
if !ok {
if len(query.Name) > 0 {
name = query.Name
}
} else {
name, ok = nameInterface.(string)
if !ok {
log.Error("Non-string type %T for custom query 'metric_name' column", nameInterface)
return
}
}
value, ok := row["metric_value"]
var valueString string
if !ok {
if len(name) > 0 {
log.Error("Missing 'metric_value' for %s in custom query", name)
return
}
} else {
valueString = fmt.Sprintf("%v", value)
if len(name) == 0 {
log.Error("Missing 'metric_name' for %s in custom query", valueString)
return
}
}
if len(query.Prefix) > 0 {
name = query.Prefix + name
}
var metricType metric.SourceType
metricTypeInterface, ok := row["metric_type"]
if !ok {
if len(query.Type) > 0 {
metricType, err = metric.SourceTypeForName(query.Type)
if err != nil {
log.Error("Invalid metric type %s in YAML: %s", query.Type, err)
return
}
} else {
metricType = detectMetricType(valueString)
}
} else {
// metric type was specified
metricTypeString, ok := metricTypeInterface.(string)
if !ok {
log.Error("Non-string type %T for custom query 'metric_type' column", metricTypeInterface)
return
}
metricType, err = metric.SourceTypeForName(metricTypeString)
if err != nil {
log.Error("Invalid metric type %s in query 'metric_type' column: %s", metricTypeString, err)
return
}
}
attributes := []metric.Attribute{
{Key: "displayName", Value: instanceEntity.Metadata.Name},
{Key: "entityName", Value: instanceEntity.Metadata.Namespace + ":" + instanceEntity.Metadata.Name},
{Key: "host", Value: connection.Host},
{Key: "instance", Value: instanceEntity.Metadata.Name},
}
if len(query.Database) > 0 {
attributes = append(attributes, metric.Attribute{Key: "database", Value: query.Database})
}
ms := instanceEntity.NewMetricSet("MssqlCustomQuerySample", attributes...)
for k, v := range row {
if k == "metric_name" || k == "metric_type" || k == "metric_value" {
continue
}
vString := fmt.Sprintf("%v", v)
if len(query.Prefix) > 0 {
k = query.Prefix + k
}
err = ms.SetMetric(k, vString, detectMetricType(vString))
if err != nil {
log.Error("Failed to set metric: %s", err)
continue
}
}
if len(valueString) > 0 {
err = ms.SetMetric(name, valueString, metricType)
if err != nil {
log.Error("Failed to set metric: %s", err)
}
}
}
}
// PopulateDatabaseMetrics collects per-database metrics
func PopulateDatabaseMetrics(i *integration.Integration, instanceName string, connection *connection.SQLConnection, arguments args.ArgumentList) error {
// create database entities
dbEntities, err := database.CreateDatabaseEntities(i, connection, instanceName)
if err != nil {
return err
}
// create database entities lookup for fast metric set
dbSetLookup := database.CreateDBEntitySetLookup(dbEntities, instanceName, connection.Host)
modelChan := make(chan interface{}, 10)
var wg sync.WaitGroup
wg.Add(1)
go dbMetricPopulator(dbSetLookup, modelChan, &wg)
// run queries that are not specific to a database
processGeneralDBDefinitions(connection, modelChan)
// run queries that are not specific to a database
if arguments.EnableBufferMetrics {
processDBBufferDefinitions(connection, modelChan)
}
// run queries that are specific to a database
if arguments.EnableDatabaseReserveMetrics {
processSpecificDBDefinitions(connection, dbSetLookup.GetDBNames(), modelChan)
}
close(modelChan)
wg.Wait()
return nil
}
func processGeneralDBDefinitions(con *connection.SQLConnection, modelChan chan<- interface{}) {
for _, queryDef := range databaseDefinitions {
makeDBQuery(con, queryDef.GetQuery(), queryDef.GetDataModels(), modelChan)
}
}
func processDBBufferDefinitions(con *connection.SQLConnection, modelChan chan<- interface{}) {
for _, queryDef := range databaseBufferDefinitions {
makeDBQuery(con, queryDef.GetQuery(), queryDef.GetDataModels(), modelChan)
}
}
func processSpecificDBDefinitions(con *connection.SQLConnection, dbNames []string, modelChan chan<- interface{}) {
for _, queryDef := range specificDatabaseDefinitions {
for _, dbName := range dbNames {
query := queryDef.GetQuery(dbNameReplace(dbName))
makeDBQuery(con, query, queryDef.GetDataModels(), modelChan)
}
}
}
func makeDBQuery(con *connection.SQLConnection, query string, models interface{}, modelChan chan<- interface{}) {
if err := con.Query(models, query); err != nil {
log.Error("Encountered the following error: %s. Running query '%s'", err.Error(), query)
return
}
// Send models off to populator
sendModelsToPopulator(modelChan, models)
}
func sendModelsToPopulator(modelChan chan<- interface{}, models interface{}) {
v := reflect.ValueOf(models)
vp := reflect.Indirect(v)
// because all data models are hard coded we can ensure they are all slices and not type check
for i := 0; i < vp.Len(); i++ {
modelChan <- vp.Index(i).Interface()
}
}
func dbMetricPopulator(dbSetLookup database.DBMetricSetLookup, modelChan <-chan interface{}, wg *sync.WaitGroup) {
defer wg.Done()
for {
model, ok := <-modelChan
if !ok {
return
}
metricSet, ok := dbSetLookup.MetricSetFromModel(model)
if !ok {
log.Error("Unable to determine database name, %+v", model)
continue
}
if err := metricSet.MarshalMetrics(model); err != nil {
log.Error("Error setting database metrics: %s", err.Error())
}
}
}
func detectMetricType(value string) metric.SourceType {
if _, err := strconv.ParseFloat(value, 64); err != nil {
return metric.ATTRIBUTE
}
return metric.GAUGE
}