Skip to content
Merged
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
3 changes: 2 additions & 1 deletion metric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sql_exporter

import (
"database/sql"
"fmt"
"sort"

Expand Down Expand Up @@ -76,7 +77,7 @@ func NewMetricFamily(logContext string, mc *config.MetricConfig, constLabels []*
func (mf MetricFamily) Collect(row map[string]any, ch chan<- Metric) {
labelValues := make([]string, len(mf.labels))
for i, label := range mf.config.KeyLabels {
labelValues[i] = row[label].(string)
labelValues[i] = row[label].(sql.NullString).String
}
for _, v := range mf.config.Values {
if mf.config.ValueLabel != "" {
Expand Down
13 changes: 8 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,23 @@ func (q *Query) scanDest(rows *sql.Rows) ([]any, errors.WithContext) {
if err != nil {
return nil, errors.Wrap(q.logContext, err)
}
klog.V(3).Infof(`returned_columns="%v"%v`, columns, q.logContext)
klog.V(3).Infof("[%s] Returned columns: %q", q.logContext, columns)
// Create the slice to scan the row into, with strings for keys and float64s for values.
dest := make([]any, 0, len(columns))
have := make(map[string]bool, len(q.columnTypes))
for i, column := range columns {
switch q.columnTypes[column] {
case columnTypeKey:
dest = append(dest, new(string))
dest = append(dest, new(sql.NullString))
have[column] = true
case columnTypeValue:
dest = append(dest, new(float64))
have[column] = true
default:
if column == "" {
klog.Warningf("[%s] Unnamed column %d returned by query", q.logContext, i)
klog.Infof("[%s] Unnamed column %d returned by query", q.logContext, i)
} else {
klog.Warningf("[%s] Extra column %q returned by query", q.logContext, column)
klog.Infof("[%s] Extra column %q returned by query", q.logContext, column)
}
dest = append(dest, new(any))
}
Expand Down Expand Up @@ -190,7 +190,10 @@ func (q *Query) scanRow(rows *sql.Rows, dest []any) (map[string]any, errors.With
for i, column := range columns {
switch q.columnTypes[column] {
case columnTypeKey:
result[column] = *dest[i].(*string)
if !dest[i].(*sql.NullString).Valid {
klog.V(3).Infof("[%s] Key column %q is NULL, return empty string", q.logContext, column)
}
result[column] = *dest[i].(*sql.NullString)
case columnTypeValue:
result[column] = *dest[i].(*float64)
}
Expand Down