Skip to content

Commit 80abbf2

Browse files
fix: reload query targets (#23)
* fix: reload query collectors - part 2 * deps: update dependencies * fix: address security recommendations * chore: update CodeQL workflow * chore: small wording changes
1 parent 7e2410d commit 80abbf2

File tree

9 files changed

+150
-65
lines changed

9 files changed

+150
-65
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ jobs:
3131
# a pull request then we can checkout the head.
3232
fetch-depth: 2
3333

34-
# If this run was triggered by a pull request event, then checkout
35-
# the head of the pull request instead of the merge commit.
36-
- run: git checkout HEAD^2
37-
if: ${{ github.event_name == 'pull_request' }}
38-
3934
# Initializes the CodeQL tools for scanning.
4035
- name: Initialize CodeQL
4136
uses: github/codeql-action/init@v1

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.8.1
1+
0.8.2

cmd/sql_exporter/main.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
)
1919

2020
var (
21-
showVersion = flag.Bool("version", false, "Print version information.")
22-
listenAddress = flag.String("web.listen-address", ":9399", "Address to listen on for web interface and telemetry.")
23-
metricsPath = flag.String("web.metrics-path", "/metrics", "Path under which to expose metrics.")
24-
enableReload = flag.Bool("web.enable-reload", false, "Enable reload collector data handler.")
25-
configFile = flag.String("config.file", "sql_exporter.yml", "SQL Exporter configuration file name.")
21+
showVersion = flag.Bool("version", false, "Print version information")
22+
listenAddress = flag.String("web.listen-address", ":9399", "Address to listen on for web interface and telemetry")
23+
metricsPath = flag.String("web.metrics-path", "/metrics", "Path under which to expose metrics")
24+
enableReload = flag.Bool("web.enable-reload", false, "Enable reload collector data handler")
25+
configFile = flag.String("config.file", "sql_exporter.yml", "SQL Exporter configuration filename")
2626
)
2727

2828
func init() {
@@ -80,12 +80,21 @@ func main() {
8080
func reloadCollectors(e sql_exporter.Exporter) func(http.ResponseWriter, *http.Request) {
8181
return func(w http.ResponseWriter, r *http.Request) {
8282
klog.Infof("Reloading the collectors...")
83-
err := e.Config().ReloadCollectorFiles()
84-
if err != nil {
85-
klog.Errorf("Error reloading collectors - %v", err)
83+
config := e.Config()
84+
if err := config.ReloadCollectorFiles(); err != nil {
85+
klog.Errorf("Error reloading collector configs - %v", err)
8686
http.Error(w, err.Error(), http.StatusInternalServerError)
8787
}
88-
http.Error(w, `Query collectors have been reloaded`, http.StatusOK)
88+
89+
// FIXME: Should be t.Collectors() instead of config.Collectors
90+
target, err := sql_exporter.NewTarget("", "", string(config.Target.DSN), config.Collectors, nil, config.Globals)
91+
if err != nil {
92+
klog.Errorf("Error creating a new target - %v", err)
93+
}
94+
e.UpdateTarget([]sql_exporter.Target{target})
95+
96+
klog.Infof("Query collectors have been successfuly reloaded")
97+
w.WriteHeader(http.StatusNoContent)
8998
}
9099
}
91100

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"k8s.io/klog/v2"
1414
)
1515

16+
const MaxInt32 int = 1<<31 - 1
17+
1618
// Load attempts to parse the given config file and return a Config object.
1719
func Load(configFile string) (*Config, error) {
1820
klog.Infof("Loading configuration from %s", configFile)
@@ -266,6 +268,7 @@ func (j *JobConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
266268
return checkOverflow(j.XXX, "job")
267269
}
268270

271+
//lint:ignore U1000 - it's unused so far
269272
// checkLabelCollisions checks for label collisions between StaticConfig labels and Metric labels.
270273
func (j *JobConfig) checkLabelCollisions() error {
271274
sclabels := make(map[string]interface{})

exporter.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sql_exporter
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
"sync"
@@ -22,6 +23,7 @@ type Exporter interface {
2223
WithContext(context.Context) Exporter
2324
// Config returns the Exporter's underlying Config object.
2425
Config() *config.Config
26+
UpdateTarget([]Target)
2527
}
2628

2729
type exporter struct {
@@ -41,7 +43,7 @@ func NewExporter(configFile string) (Exporter, error) {
4143
// Override the DSN if requested (and in single target mode).
4244
if *dsnOverride != "" {
4345
if len(c.Jobs) > 0 {
44-
return nil, fmt.Errorf("The config.data-source-name flag (value %q) only applies in single target mode", *dsnOverride)
46+
return nil, fmt.Errorf("the config.data-source-name flag (value %q) only applies in single target mode", *dsnOverride)
4547
}
4648
c.Target.DSN = config.Secret(*dsnOverride)
4749
}
@@ -54,6 +56,9 @@ func NewExporter(configFile string) (Exporter, error) {
5456
}
5557
targets = []Target{target}
5658
} else {
59+
if len(c.Jobs) > (config.MaxInt32 / 3) {
60+
return nil, errors.New("'jobs' list is too large")
61+
}
5762
targets = make([]Target, 0, len(c.Jobs)*3)
5863
for _, jc := range c.Jobs {
5964
job, err := NewJob(jc, c.Globals)
@@ -147,3 +152,7 @@ func (e *exporter) Gather() ([]*dto.MetricFamily, error) {
147152
func (e *exporter) Config() *config.Config {
148153
return e.config
149154
}
155+
156+
func (e *exporter) UpdateTarget(target []Target) {
157+
e.targets = target
158+
}

go.mod

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@ go 1.14
44

55
require (
66
github.com/ClickHouse/clickhouse-go v1.4.3
7-
github.com/apache/arrow/go/arrow v0.0.0-20210312155728-26fc75157af2 // indirect
7+
github.com/apache/arrow/go/arrow v0.0.0-20210404094439-beb1c1b35be1 // indirect
8+
github.com/aws/aws-sdk-go-v2/credentials v1.1.4 // indirect
9+
github.com/aws/aws-sdk-go-v2/service/s3 v1.4.0 // indirect
810
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
911
github.com/denisenkom/go-mssqldb v0.9.0
10-
github.com/go-sql-driver/mysql v1.5.0
12+
github.com/go-sql-driver/mysql v1.6.0
13+
github.com/golang/protobuf v1.5.2 // indirect
1114
github.com/google/flatbuffers v1.12.0 // indirect
1215
github.com/google/uuid v1.2.0 // indirect
1316
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
14-
github.com/jackc/pgx/v4 v4.10.1
17+
github.com/jackc/pgx/v4 v4.11.0
1518
github.com/kardianos/minwinsvc v1.0.0
1619
github.com/lib/pq v1.10.0
1720
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
1821
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect
1922
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
20-
github.com/prometheus/client_golang v1.9.0
23+
github.com/prometheus/client_golang v1.10.0
2124
github.com/prometheus/client_model v0.2.0
22-
github.com/prometheus/common v0.18.0
23-
github.com/prometheus/procfs v0.6.0 // indirect
24-
github.com/snowflakedb/gosnowflake v1.3.12
25+
github.com/prometheus/common v0.20.0
26+
github.com/sirupsen/logrus v1.8.1 // indirect
27+
github.com/snowflakedb/gosnowflake v1.4.2
2528
github.com/stretchr/testify v1.6.2-0.20200803095430-a3bed97cf337 // indirect
2629
github.com/vertica/vertica-sql-go v1.1.1
27-
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
28-
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
29-
golang.org/x/text v0.3.5 // indirect
30-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
31-
google.golang.org/protobuf v1.25.1-0.20200728163639-5d63473da8fb
30+
golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c // indirect
31+
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
32+
golang.org/x/text v0.3.6 // indirect
33+
google.golang.org/protobuf v1.26.0
3234
gopkg.in/yaml.v2 v2.4.0
3335
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
34-
k8s.io/klog/v2 v2.7.0
36+
k8s.io/klog/v2 v2.8.0
3537
)

0 commit comments

Comments
 (0)