Skip to content

Heartbeat collector query using palceholders #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -18,7 +18,6 @@ package collector
import (
"context"
"database/sql"
"fmt"
"strconv"

"github.com/alecthomas/kingpin/v2"
@@ -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 (
@@ -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
}
11 changes: 6 additions & 5 deletions collector/heartbeat_test.go
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ package collector

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

@@ -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{
@@ -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"},
},
}

@@ -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() {