-
Notifications
You must be signed in to change notification settings - Fork 816
Expand file tree
/
Copy pathheartbeat.go
More file actions
149 lines (128 loc) · 3.96 KB
/
Copy pathheartbeat.go
File metadata and controls
149 lines (128 loc) · 3.96 KB
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
// Copyright 2018 The Prometheus 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
//
// http://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.
// Scrape heartbeat data.
package collector
import (
"context"
"database/sql"
"strconv"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
const (
// heartbeat is the Metric subsystem we use.
heartbeat = "heartbeat"
// heartbeatQuery is the query used to fetch the stored and current
// timestamps. %s will be replaced by the database and table name.
// The second column allows gets the server timestamp at the exact same
// time the query is run.
heartbeatQuery = "SELECT UNIX_TIMESTAMP(ts), UNIX_TIMESTAMP(?), server_id from ?.?"
)
var (
collectHeartbeatDatabase = kingpin.Flag(
"collect.heartbeat.database",
"Database from where to collect heartbeat data",
).Default("heartbeat").String()
collectHeartbeatTable = kingpin.Flag(
"collect.heartbeat.table",
"Table from where to collect heartbeat data",
).Default("heartbeat").String()
collectHeartbeatUtc = kingpin.Flag(
"collect.heartbeat.utc",
"Use UTC for timestamps of the current server (`pt-heartbeat` is called with `--utc`)",
).Bool()
)
// Metric descriptors.
var (
HeartbeatStoredDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, heartbeat, "stored_timestamp_seconds"),
"Timestamp stored in the heartbeat table.",
[]string{"server_id"}, nil,
)
HeartbeatNowDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, heartbeat, "now_timestamp_seconds"),
"Timestamp of the current server.",
[]string{"server_id"}, nil,
)
)
// ScrapeHeartbeat scrapes from the heartbeat table.
// This is mainly targeting pt-heartbeat, but will work with any heartbeat
// implementation that writes to a table with two columns:
// CREATE TABLE heartbeat (
//
// ts varchar(26) NOT NULL,
// server_id int unsigned NOT NULL PRIMARY KEY,
//
// );
type ScrapeHeartbeat struct{}
// Name of the Scraper. Should be unique.
func (ScrapeHeartbeat) Name() string {
return "heartbeat"
}
// Help describes the role of the Scraper.
func (ScrapeHeartbeat) Help() string {
return "Collect from heartbeat"
}
// Version of MySQL from which scraper is available.
func (ScrapeHeartbeat) Version() float64 {
return 5.1
}
// nowExpr returns a current timestamp expression.
func nowExpr() string {
if *collectHeartbeatUtc {
return "UTC_TIMESTAMP(6)"
}
return "NOW(6)"
}
// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeHeartbeat) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error {
heartbeatRows, err := db.QueryContext(ctx, heartbeatQuery, nowExpr(), *collectHeartbeatDatabase, *collectHeartbeatTable)
if err != nil {
return err
}
defer heartbeatRows.Close()
var (
now, ts sql.RawBytes
serverId int
)
for heartbeatRows.Next() {
if err := heartbeatRows.Scan(&ts, &now, &serverId); err != nil {
return err
}
tsFloatVal, err := strconv.ParseFloat(string(ts), 64)
if err != nil {
return err
}
nowFloatVal, err := strconv.ParseFloat(string(now), 64)
if err != nil {
return err
}
serverId := strconv.Itoa(serverId)
ch <- prometheus.MustNewConstMetric(
HeartbeatNowDesc,
prometheus.GaugeValue,
nowFloatVal,
serverId,
)
ch <- prometheus.MustNewConstMetric(
HeartbeatStoredDesc,
prometheus.GaugeValue,
tsFloatVal,
serverId,
)
}
return nil
}
// check interface
var _ Scraper = ScrapeHeartbeat{}