Skip to content

Commit

Permalink
fix: reload query targets (#23)
Browse files Browse the repository at this point in the history
* fix: reload query collectors - part 2
* deps: update dependencies
* fix: address security recommendations
* chore: update CodeQL workflow
* chore: small wording changes
  • Loading branch information
burningalchemist authored Apr 4, 2021
1 parent 7e2410d commit 80abbf2
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 65 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ jobs:
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.8.2
27 changes: 18 additions & 9 deletions cmd/sql_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
)

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

func init() {
Expand Down Expand Up @@ -80,12 +80,21 @@ func main() {
func reloadCollectors(e sql_exporter.Exporter) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
klog.Infof("Reloading the collectors...")
err := e.Config().ReloadCollectorFiles()
if err != nil {
klog.Errorf("Error reloading collectors - %v", err)
config := e.Config()
if err := config.ReloadCollectorFiles(); err != nil {
klog.Errorf("Error reloading collector configs - %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
http.Error(w, `Query collectors have been reloaded`, http.StatusOK)

// FIXME: Should be t.Collectors() instead of config.Collectors
target, err := sql_exporter.NewTarget("", "", string(config.Target.DSN), config.Collectors, nil, config.Globals)
if err != nil {
klog.Errorf("Error creating a new target - %v", err)
}
e.UpdateTarget([]sql_exporter.Target{target})

klog.Infof("Query collectors have been successfuly reloaded")
w.WriteHeader(http.StatusNoContent)
}
}

Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"k8s.io/klog/v2"
)

const MaxInt32 int = 1<<31 - 1

// Load attempts to parse the given config file and return a Config object.
func Load(configFile string) (*Config, error) {
klog.Infof("Loading configuration from %s", configFile)
Expand Down Expand Up @@ -266,6 +268,7 @@ func (j *JobConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return checkOverflow(j.XXX, "job")
}

//lint:ignore U1000 - it's unused so far
// checkLabelCollisions checks for label collisions between StaticConfig labels and Metric labels.
func (j *JobConfig) checkLabelCollisions() error {
sclabels := make(map[string]interface{})
Expand Down
11 changes: 10 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sql_exporter

import (
"context"
"errors"
"flag"
"fmt"
"sync"
Expand All @@ -22,6 +23,7 @@ type Exporter interface {
WithContext(context.Context) Exporter
// Config returns the Exporter's underlying Config object.
Config() *config.Config
UpdateTarget([]Target)
}

type exporter struct {
Expand All @@ -41,7 +43,7 @@ func NewExporter(configFile string) (Exporter, error) {
// Override the DSN if requested (and in single target mode).
if *dsnOverride != "" {
if len(c.Jobs) > 0 {
return nil, fmt.Errorf("The config.data-source-name flag (value %q) only applies in single target mode", *dsnOverride)
return nil, fmt.Errorf("the config.data-source-name flag (value %q) only applies in single target mode", *dsnOverride)
}
c.Target.DSN = config.Secret(*dsnOverride)
}
Expand All @@ -54,6 +56,9 @@ func NewExporter(configFile string) (Exporter, error) {
}
targets = []Target{target}
} else {
if len(c.Jobs) > (config.MaxInt32 / 3) {
return nil, errors.New("'jobs' list is too large")
}
targets = make([]Target, 0, len(c.Jobs)*3)
for _, jc := range c.Jobs {
job, err := NewJob(jc, c.Globals)
Expand Down Expand Up @@ -147,3 +152,7 @@ func (e *exporter) Gather() ([]*dto.MetricFamily, error) {
func (e *exporter) Config() *config.Config {
return e.config
}

func (e *exporter) UpdateTarget(target []Target) {
e.targets = target
}
28 changes: 15 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ go 1.14

require (
github.com/ClickHouse/clickhouse-go v1.4.3
github.com/apache/arrow/go/arrow v0.0.0-20210312155728-26fc75157af2 // indirect
github.com/apache/arrow/go/arrow v0.0.0-20210404094439-beb1c1b35be1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.1.4 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/denisenkom/go-mssqldb v0.9.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/flatbuffers v1.12.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jackc/pgx/v4 v4.10.1
github.com/jackc/pgx/v4 v4.11.0
github.com/kardianos/minwinsvc v1.0.0
github.com/lib/pq v1.10.0
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/client_golang v1.10.0
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.18.0
github.com/prometheus/procfs v0.6.0 // indirect
github.com/snowflakedb/gosnowflake v1.3.12
github.com/prometheus/common v0.20.0
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/snowflakedb/gosnowflake v1.4.2
github.com/stretchr/testify v1.6.2-0.20200803095430-a3bed97cf337 // indirect
github.com/vertica/vertica-sql-go v1.1.1
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.1-0.20200728163639-5d63473da8fb
golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
k8s.io/klog/v2 v2.7.0
k8s.io/klog/v2 v2.8.0
)
Loading

0 comments on commit 80abbf2

Please sign in to comment.