Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions collector/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package collector
import (
"context"
"database/sql"
"fmt"
"strconv"

"github.com/alecthomas/kingpin/v2"
Expand All @@ -33,7 +32,7 @@ const (
// 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(%s), server_id from `%s`.`%s`"
heartbeatQuery = "SELECT UNIX_TIMESTAMP(ts), UNIX_TIMESTAMP(?), server_id from ?.?"
)

var (
Expand Down Expand Up @@ -101,8 +100,7 @@ func nowExpr() string {

// 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 {
query := fmt.Sprintf(heartbeatQuery, nowExpr(), *collectHeartbeatDatabase, *collectHeartbeatTable)
heartbeatRows, err := db.QueryContext(ctx, query)
heartbeatRows, err := db.QueryContext(ctx, heartbeatQuery, nowExpr(), *collectHeartbeatDatabase, *collectHeartbeatTable)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions collector/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package collector

import (
"context"
"database/sql/driver"
"fmt"
"testing"

Expand All @@ -29,17 +30,17 @@ import (
type ScrapeHeartbeatTestCase struct {
Args []string
Columns []string
Query string
SQLArgs []driver.Value
}

var ScrapeHeartbeatTestCases = []ScrapeHeartbeatTestCase{
{
[]string{
"--collect.heartbeat.database", "heartbeat-test",
"--collect.heartbeat.table", "heartbeat-test",
"--collect.heartbeat.table", "heartbeat-test' OR 1=1;",
},
[]string{"UNIX_TIMESTAMP(ts)", "UNIX_TIMESTAMP(NOW(6))", "server_id"},
"SELECT UNIX_TIMESTAMP(ts), UNIX_TIMESTAMP(NOW(6)), server_id from `heartbeat-test`.`heartbeat-test`",
[]driver.Value{"NOW(6)", "heartbeat-test", "heartbeat-test' OR 1=1;"},
},
{
[]string{
Expand All @@ -48,7 +49,7 @@ var ScrapeHeartbeatTestCases = []ScrapeHeartbeatTestCase{
"--collect.heartbeat.utc",
},
[]string{"UNIX_TIMESTAMP(ts)", "UNIX_TIMESTAMP(UTC_TIMESTAMP(6))", "server_id"},
"SELECT UNIX_TIMESTAMP(ts), UNIX_TIMESTAMP(UTC_TIMESTAMP(6)), server_id from `heartbeat-test`.`heartbeat-test`",
[]driver.Value{"UTC_TIMESTAMP(6)", "heartbeat-test", "heartbeat-test"},
},
}

Expand All @@ -68,7 +69,7 @@ func TestScrapeHeartbeat(t *testing.T) {

rows := sqlmock.NewRows(tt.Columns).
AddRow("1487597613.001320", "1487598113.448042", 1)
mock.ExpectQuery(sanitizeQuery(tt.Query)).WillReturnRows(rows)
mock.ExpectQuery("SELECT").WithArgs(tt.SQLArgs...).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
Expand Down