forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
417 lines (366 loc) · 17.6 KB
/
scraper.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver"
import (
"context"
"errors"
"strconv"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver/internal/metadata"
)
const (
picosecondsInNanoseconds int64 = 1000
)
type mySQLScraper struct {
sqlclient client
logger *zap.Logger
config *Config
mb *metadata.MetricsBuilder
}
func newMySQLScraper(
settings component.ReceiverCreateSettings,
config *Config,
) *mySQLScraper {
return &mySQLScraper{
logger: settings.Logger,
config: config,
mb: metadata.NewMetricsBuilder(config.Metrics, settings.BuildInfo),
}
}
// start starts the scraper by initializing the db client connection.
func (m *mySQLScraper) start(_ context.Context, host component.Host) error {
sqlclient := newMySQLClient(m.config)
err := sqlclient.Connect()
if err != nil {
return err
}
m.sqlclient = sqlclient
return nil
}
// shutdown closes the db connection
func (m *mySQLScraper) shutdown(context.Context) error {
if m.sqlclient == nil {
return nil
}
return m.sqlclient.Close()
}
// scrape scrapes the mysql db metric stats, transforms them and labels them into a metric slices.
func (m *mySQLScraper) scrape(context.Context) (pmetric.Metrics, error) {
if m.sqlclient == nil {
return pmetric.Metrics{}, errors.New("failed to connect to http client")
}
now := pcommon.NewTimestampFromTime(time.Now())
// collect innodb metrics.
innodbStats, innoErr := m.sqlclient.getInnodbStats()
if innoErr != nil {
m.logger.Error("Failed to fetch InnoDB stats", zap.Error(innoErr))
}
errs := &scrapererror.ScrapeErrors{}
for k, v := range innodbStats {
if k != "buffer_pool_size" {
continue
}
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolLimitDataPoint(now, v))
}
// collect io_waits metrics.
m.scrapeTableIoWaitsStats(now, errs)
m.scrapeIndexIoWaitsStats(now, errs)
// collect global status metrics.
m.scrapeGlobalStats(now, errs)
m.mb.EmitForResource(metadata.WithMysqlInstanceEndpoint(m.config.Endpoint))
return m.mb.Emit(), errs.Combine()
}
func (m *mySQLScraper) scrapeGlobalStats(now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
globalStats, err := m.sqlclient.getGlobalStats()
if err != nil {
m.logger.Error("Failed to fetch global stats", zap.Error(err))
errs.AddPartial(66, err)
return
}
m.recordDataPages(now, globalStats, errs)
m.recordDataUsage(now, globalStats, errs)
for k, v := range globalStats {
switch k {
// buffer_pool.pages
case "Innodb_buffer_pool_pages_data":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolPagesDataPoint(now, v,
metadata.AttributeBufferPoolPagesData))
case "Innodb_buffer_pool_pages_free":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolPagesDataPoint(now, v,
metadata.AttributeBufferPoolPagesFree))
case "Innodb_buffer_pool_pages_misc":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolPagesDataPoint(now, v,
metadata.AttributeBufferPoolPagesMisc))
// buffer_pool.page_flushes
case "Innodb_buffer_pool_pages_flushed":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolPageFlushesDataPoint(now, v))
// buffer_pool.operations
case "Innodb_buffer_pool_read_ahead_rnd":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsReadAheadRnd))
case "Innodb_buffer_pool_read_ahead":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsReadAhead))
case "Innodb_buffer_pool_read_ahead_evicted":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsReadAheadEvicted))
case "Innodb_buffer_pool_read_requests":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsReadRequests))
case "Innodb_buffer_pool_reads":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsReads))
case "Innodb_buffer_pool_wait_free":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsWaitFree))
case "Innodb_buffer_pool_write_requests":
addPartialIfError(errs, m.mb.RecordMysqlBufferPoolOperationsDataPoint(now, v,
metadata.AttributeBufferPoolOperationsWriteRequests))
// commands
case "Com_stmt_execute":
addPartialIfError(errs, m.mb.RecordMysqlCommandsDataPoint(now, v, metadata.AttributeCommandExecute))
case "Com_stmt_close":
addPartialIfError(errs, m.mb.RecordMysqlCommandsDataPoint(now, v, metadata.AttributeCommandClose))
case "Com_stmt_fetch":
addPartialIfError(errs, m.mb.RecordMysqlCommandsDataPoint(now, v, metadata.AttributeCommandFetch))
case "Com_stmt_prepare":
addPartialIfError(errs, m.mb.RecordMysqlCommandsDataPoint(now, v, metadata.AttributeCommandPrepare))
case "Com_stmt_reset":
addPartialIfError(errs, m.mb.RecordMysqlCommandsDataPoint(now, v, metadata.AttributeCommandReset))
case "Com_stmt_send_long_data":
addPartialIfError(errs, m.mb.RecordMysqlCommandsDataPoint(now, v, metadata.AttributeCommandSendLongData))
// created tmps
case "Created_tmp_disk_tables":
addPartialIfError(errs, m.mb.RecordMysqlTmpResourcesDataPoint(now, v, metadata.AttributeTmpResourceDiskTables))
case "Created_tmp_files":
addPartialIfError(errs, m.mb.RecordMysqlTmpResourcesDataPoint(now, v, metadata.AttributeTmpResourceFiles))
case "Created_tmp_tables":
addPartialIfError(errs, m.mb.RecordMysqlTmpResourcesDataPoint(now, v, metadata.AttributeTmpResourceTables))
// handlers
case "Handler_commit":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerCommit))
case "Handler_delete":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerDelete))
case "Handler_discover":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerDiscover))
case "Handler_external_lock":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerExternalLock))
case "Handler_mrr_init":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerMrrInit))
case "Handler_prepare":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerPrepare))
case "Handler_read_first":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadFirst))
case "Handler_read_key":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadKey))
case "Handler_read_last":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadLast))
case "Handler_read_next":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadNext))
case "Handler_read_prev":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadPrev))
case "Handler_read_rnd":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadRnd))
case "Handler_read_rnd_next":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerReadRndNext))
case "Handler_rollback":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerRollback))
case "Handler_savepoint":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerSavepoint))
case "Handler_savepoint_rollback":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerSavepointRollback))
case "Handler_update":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerUpdate))
case "Handler_write":
addPartialIfError(errs, m.mb.RecordMysqlHandlersDataPoint(now, v, metadata.AttributeHandlerWrite))
// double_writes
case "Innodb_dblwr_pages_written":
addPartialIfError(errs, m.mb.RecordMysqlDoubleWritesDataPoint(now, v, metadata.AttributeDoubleWritesPagesWritten))
case "Innodb_dblwr_writes":
addPartialIfError(errs, m.mb.RecordMysqlDoubleWritesDataPoint(now, v, metadata.AttributeDoubleWritesWrites))
// log_operations
case "Innodb_log_waits":
addPartialIfError(errs, m.mb.RecordMysqlLogOperationsDataPoint(now, v, metadata.AttributeLogOperationsWaits))
case "Innodb_log_write_requests":
addPartialIfError(errs, m.mb.RecordMysqlLogOperationsDataPoint(now, v, metadata.AttributeLogOperationsWriteRequests))
case "Innodb_log_writes":
addPartialIfError(errs, m.mb.RecordMysqlLogOperationsDataPoint(now, v, metadata.AttributeLogOperationsWrites))
// operations
case "Innodb_data_fsyncs":
addPartialIfError(errs, m.mb.RecordMysqlOperationsDataPoint(now, v, metadata.AttributeOperationsFsyncs))
case "Innodb_data_reads":
addPartialIfError(errs, m.mb.RecordMysqlOperationsDataPoint(now, v, metadata.AttributeOperationsReads))
case "Innodb_data_writes":
addPartialIfError(errs, m.mb.RecordMysqlOperationsDataPoint(now, v, metadata.AttributeOperationsWrites))
// page_operations
case "Innodb_pages_created":
addPartialIfError(errs, m.mb.RecordMysqlPageOperationsDataPoint(now, v, metadata.AttributePageOperationsCreated))
case "Innodb_pages_read":
addPartialIfError(errs, m.mb.RecordMysqlPageOperationsDataPoint(now, v,
metadata.AttributePageOperationsRead))
case "Innodb_pages_written":
addPartialIfError(errs, m.mb.RecordMysqlPageOperationsDataPoint(now, v,
metadata.AttributePageOperationsWritten))
// row_locks
case "Innodb_row_lock_waits":
addPartialIfError(errs, m.mb.RecordMysqlRowLocksDataPoint(now, v, metadata.AttributeRowLocksWaits))
case "Innodb_row_lock_time":
addPartialIfError(errs, m.mb.RecordMysqlRowLocksDataPoint(now, v, metadata.AttributeRowLocksTime))
// row_operations
case "Innodb_rows_deleted":
addPartialIfError(errs, m.mb.RecordMysqlRowOperationsDataPoint(now, v, metadata.AttributeRowOperationsDeleted))
case "Innodb_rows_inserted":
addPartialIfError(errs, m.mb.RecordMysqlRowOperationsDataPoint(now, v, metadata.AttributeRowOperationsInserted))
case "Innodb_rows_read":
addPartialIfError(errs, m.mb.RecordMysqlRowOperationsDataPoint(now, v,
metadata.AttributeRowOperationsRead))
case "Innodb_rows_updated":
addPartialIfError(errs, m.mb.RecordMysqlRowOperationsDataPoint(now, v,
metadata.AttributeRowOperationsUpdated))
// locks
case "Table_locks_immediate":
addPartialIfError(errs, m.mb.RecordMysqlLocksDataPoint(now, v, metadata.AttributeLocksImmediate))
case "Table_locks_waited":
addPartialIfError(errs, m.mb.RecordMysqlLocksDataPoint(now, v, metadata.AttributeLocksWaited))
// locked_connects
case "Locked_connects":
addPartialIfError(errs, m.mb.RecordMysqlLockedConnectsDataPoint(now, v))
// sorts
case "Sort_merge_passes":
addPartialIfError(errs, m.mb.RecordMysqlSortsDataPoint(now, v, metadata.AttributeSortsMergePasses))
case "Sort_range":
addPartialIfError(errs, m.mb.RecordMysqlSortsDataPoint(now, v, metadata.AttributeSortsRange))
case "Sort_rows":
addPartialIfError(errs, m.mb.RecordMysqlSortsDataPoint(now, v, metadata.AttributeSortsRows))
case "Sort_scan":
addPartialIfError(errs, m.mb.RecordMysqlSortsDataPoint(now, v, metadata.AttributeSortsScan))
// threads
case "Threads_cached":
addPartialIfError(errs, m.mb.RecordMysqlThreadsDataPoint(now, v, metadata.AttributeThreadsCached))
case "Threads_connected":
addPartialIfError(errs, m.mb.RecordMysqlThreadsDataPoint(now, v, metadata.AttributeThreadsConnected))
case "Threads_created":
addPartialIfError(errs, m.mb.RecordMysqlThreadsDataPoint(now, v, metadata.AttributeThreadsCreated))
case "Threads_running":
addPartialIfError(errs, m.mb.RecordMysqlThreadsDataPoint(now, v, metadata.AttributeThreadsRunning))
// opened resources
case "Opened_files":
addPartialIfError(errs, m.mb.RecordMysqlOpenedResourcesDataPoint(now, v, metadata.AttributeOpenedResourcesFile))
case "Opened_tables":
addPartialIfError(errs, m.mb.RecordMysqlOpenedResourcesDataPoint(now, v, metadata.AttributeOpenedResourcesTable))
case "Opened_table_definitions":
addPartialIfError(errs, m.mb.RecordMysqlOpenedResourcesDataPoint(now, v, metadata.AttributeOpenedResourcesTableDefinition))
}
}
}
func (m *mySQLScraper) scrapeTableIoWaitsStats(now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
tableIoWaitsStats, err := m.sqlclient.getTableIoWaitsStats()
if err != nil {
m.logger.Error("Failed to fetch table io_waits stats", zap.Error(err))
errs.AddPartial(8, err)
return
}
for i := 0; i < len(tableIoWaitsStats); i++ {
s := tableIoWaitsStats[i]
// counts
m.mb.RecordMysqlTableIoWaitCountDataPoint(now, s.countDelete, metadata.AttributeIoWaitsOperationsDelete, s.name, s.schema)
m.mb.RecordMysqlTableIoWaitCountDataPoint(now, s.countFetch, metadata.AttributeIoWaitsOperationsFetch, s.name, s.schema)
m.mb.RecordMysqlTableIoWaitCountDataPoint(now, s.countInsert, metadata.AttributeIoWaitsOperationsInsert, s.name, s.schema)
m.mb.RecordMysqlTableIoWaitCountDataPoint(now, s.countUpdate, metadata.AttributeIoWaitsOperationsUpdate, s.name, s.schema)
// times
m.mb.RecordMysqlTableIoWaitTimeDataPoint(
now, s.timeDelete/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsDelete, s.name, s.schema,
)
m.mb.RecordMysqlTableIoWaitTimeDataPoint(
now, s.timeFetch/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsFetch, s.name, s.schema,
)
m.mb.RecordMysqlTableIoWaitTimeDataPoint(
now, s.timeInsert/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsInsert, s.name, s.schema,
)
m.mb.RecordMysqlTableIoWaitTimeDataPoint(
now, s.timeUpdate/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsUpdate, s.name, s.schema,
)
}
}
func (m *mySQLScraper) scrapeIndexIoWaitsStats(now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
indexIoWaitsStats, err := m.sqlclient.getIndexIoWaitsStats()
if err != nil {
m.logger.Error("Failed to fetch index io_waits stats", zap.Error(err))
errs.AddPartial(8, err)
return
}
for i := 0; i < len(indexIoWaitsStats); i++ {
s := indexIoWaitsStats[i]
// counts
m.mb.RecordMysqlIndexIoWaitCountDataPoint(now, s.countDelete, metadata.AttributeIoWaitsOperationsDelete, s.name, s.schema, s.index)
m.mb.RecordMysqlIndexIoWaitCountDataPoint(now, s.countFetch, metadata.AttributeIoWaitsOperationsFetch, s.name, s.schema, s.index)
m.mb.RecordMysqlIndexIoWaitCountDataPoint(now, s.countInsert, metadata.AttributeIoWaitsOperationsInsert, s.name, s.schema, s.index)
m.mb.RecordMysqlIndexIoWaitCountDataPoint(now, s.countUpdate, metadata.AttributeIoWaitsOperationsUpdate, s.name, s.schema, s.index)
// times
m.mb.RecordMysqlIndexIoWaitTimeDataPoint(
now, s.timeDelete/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsDelete, s.name, s.schema, s.index,
)
m.mb.RecordMysqlIndexIoWaitTimeDataPoint(
now, s.timeFetch/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsFetch, s.name, s.schema, s.index,
)
m.mb.RecordMysqlIndexIoWaitTimeDataPoint(
now, s.timeInsert/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsInsert, s.name, s.schema, s.index,
)
m.mb.RecordMysqlIndexIoWaitTimeDataPoint(
now, s.timeUpdate/picosecondsInNanoseconds, metadata.AttributeIoWaitsOperationsUpdate, s.name, s.schema, s.index,
)
}
}
func addPartialIfError(errors *scrapererror.ScrapeErrors, err error) {
if err != nil {
errors.AddPartial(1, err)
}
}
func (m *mySQLScraper) recordDataPages(now pcommon.Timestamp, globalStats map[string]string, errors *scrapererror.ScrapeErrors) {
dirty, err := parseInt(globalStats["Innodb_buffer_pool_pages_dirty"])
if err != nil {
errors.AddPartial(2, err) // we need dirty to calculate free, so 2 data points lost here
return
}
m.mb.RecordMysqlBufferPoolDataPagesDataPoint(now, dirty, metadata.AttributeBufferPoolDataDirty)
data, err := parseInt(globalStats["Innodb_buffer_pool_pages_data"])
if err != nil {
errors.AddPartial(1, err)
return
}
m.mb.RecordMysqlBufferPoolDataPagesDataPoint(now, data-dirty, metadata.AttributeBufferPoolDataClean)
}
func (m *mySQLScraper) recordDataUsage(now pcommon.Timestamp, globalStats map[string]string, errors *scrapererror.ScrapeErrors) {
dirty, err := parseInt(globalStats["Innodb_buffer_pool_bytes_dirty"])
if err != nil {
errors.AddPartial(2, err) // we need dirty to calculate free, so 2 data points lost here
return
}
m.mb.RecordMysqlBufferPoolUsageDataPoint(now, dirty, metadata.AttributeBufferPoolDataDirty)
data, err := parseInt(globalStats["Innodb_buffer_pool_bytes_data"])
if err != nil {
errors.AddPartial(1, err)
return
}
m.mb.RecordMysqlBufferPoolUsageDataPoint(now, data-dirty, metadata.AttributeBufferPoolDataClean)
}
// parseInt converts string to int64.
func parseInt(value string) (int64, error) {
return strconv.ParseInt(value, 10, 64)
}