Permalink
Browse files

Use go-kit levels and its level filter (#6)

* Use go-kit levels and its level filter
* Use filtered logger and make all default filter
  • Loading branch information...
metalmatze authored and dominikschulz committed Apr 7, 2017
1 parent 5389806 commit 15609d17d1350b86e313c6246093475f963f338e
Showing with 299 additions and 132 deletions.
  1. +3 −2 exporter.go
  2. +12 −11 job.go
  3. +0 −107 leveled/logger.go
  4. +20 −9 main.go
  5. +10 −3 query.go
  6. +22 −0 vendor/github.com/go-kit/kit/log/level/doc.go
  7. +226 −0 vendor/github.com/go-kit/kit/log/level/level.go
  8. +6 −0 vendor/vendor.json
@@ -2,6 +2,7 @@ package main
import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -34,7 +35,7 @@ func NewExporter(logger log.Logger, configFile string) (*Exporter, error) {
continue
}
if err := job.Init(logger); err != nil {
logger.Log("level", "warning", "msg", "Skipping job. Failed to initialize", "err", err, "job", job.Name)
level.Warn(logger).Log("msg", "Skipping job. Failed to initialize", "err", err, "job", job.Name)
continue
}
exp.jobs = append(exp.jobs, job)
@@ -55,7 +56,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
continue
}
if query.desc == nil {
e.logger.Log("level", "error", "msg", "Query has no descriptor", "query", query.Name)
level.Error(e.logger).Log("msg", "Query has no descriptor", "query", query.Name)
continue
}
ch <- query.desc
23 job.go
@@ -10,6 +10,7 @@ import (
"github.com/cenkalti/backoff"
_ "github.com/denisenkom/go-mssqldb" // register the MS-SQL driver
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
_ "github.com/go-sql-driver/mysql" // register the MySQL driver
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq" // register the PostgreSQL driver
@@ -28,7 +29,7 @@ func (j *Job) Init(logger log.Logger) error {
// register each query as an metric
for _, q := range j.Queries {
if q == nil {
j.log.Log("level", "warning", "msg", "Skipping invalid query")
level.Warn(j.log).Log("msg", "Skipping invalid query")
continue
}
if q.metrics == nil {
@@ -64,7 +65,7 @@ func (j *Job) Run() {
}
// if there are no connection URLs for this job it can't be run
if j.Connections == nil {
j.log.Log("level", "error", "msg", "No conenctions for job", "job", j.Name)
level.Error(j.log).Log("msg", "No conenctions for job", "job", j.Name)
return
}
// make space for the connection objects
@@ -76,7 +77,7 @@ func (j *Job) Run() {
for _, conn := range j.Connections {
u, err := url.Parse(conn)
if err != nil {
j.log.Log("level", "error", "msg", "Failed to parse URL", "url", conn, "err", err)
level.Error(j.log).Log("msg", "Failed to parse URL", "url", conn, "err", err)
continue
}
user := ""
@@ -95,17 +96,17 @@ func (j *Job) Run() {
})
}
}
j.log.Log("level", "debug", "msg", "Starting")
level.Debug(j.log).Log("msg", "Starting")
// enter the run loop
// tries to run each query on each connection at approx the interval
for {
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = j.Interval
if err := backoff.Retry(j.runOnce, bo); err != nil {
j.log.Log("level", "error", "msg", "Failed to run", "err", err)
level.Error(j.log).Log("msg", "Failed to run", "err", err)
}
j.log.Log("level", "debug", "msg", "Sleeping until next run", "sleep", j.Interval.String())
level.Debug(j.log).Log("msg", "Sleeping until next run", "sleep", j.Interval.String())
time.Sleep(j.Interval)
}
}
@@ -118,7 +119,7 @@ func (j *Job) runOnceConnection(conn *connection, done chan int) {
// connect to DB if not connected already
if err := conn.connect(j.Interval); err != nil {
j.log.Log("level", "warn", "msg", "Failed to connect", "err", err)
level.Warn(j.log).Log("msg", "Failed to connect", "err", err)
return
}
@@ -128,16 +129,16 @@ func (j *Job) runOnceConnection(conn *connection, done chan int) {
}
if q.desc == nil {
// this may happen if the metric registration failed
q.log.Log("level", "warning", "msg", "Skipping query. Collector is nil")
level.Warn(q.log).Log("msg", "Skipping query. Collector is nil")
continue
}
q.log.Log("level", "debug", "msg", "Running Query")
level.Debug(q.log).Log("msg", "Running Query")
// execute the query on the connection
if err := q.Run(conn); err != nil {
q.log.Log("level", "warning", "msg", "Failed to run query", "err", err)
level.Warn(q.log).Log("msg", "Failed to run query", "err", err)
continue
}
q.log.Log("level", "debug", "msg", "Query finished")
level.Debug(q.log).Log("msg", "Query finished")
updated++
}
}

This file was deleted.

Oops, something went wrong.
29 main.go
@@ -5,10 +5,10 @@ import (
"fmt"
"net/http"
"os"
"github.com/justwatchcom/sql_exporter/leveled"
"strings"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
@@ -35,18 +35,29 @@ func main() {
// init logger
logger := log.NewJSONLogger(os.Stdout)
logger = log.With(
logger,
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)
logger = leveled.NewFromEnv(logger)
logger = log.With(logger, "caller", log.DefaultCaller)
// set the allowed log level filter
switch strings.ToLower(os.Getenv("LOGLEVEL")) {
case "debug":
logger = level.NewFilter(logger, level.AllowDebug())
case "info":
logger = level.NewFilter(logger, level.AllowInfo())
case "warn":
logger = level.NewFilter(logger, level.AllowWarn())
case "error":
logger = level.NewFilter(logger, level.AllowError())
default:
logger = level.NewFilter(logger, level.AllowAll())
}
logger.Log("msg", "Starting sql_exporter", "version_info", version.Info(), "build_context", version.BuildContext())
exporter, err := NewExporter(logger, *configFile)
if err != nil {
logger.Log("level", "error", "msg", "Error starting exporter", "err", err)
level.Error(logger).Log("msg", "Error starting exporter", "err", err)
os.Exit(1)
}
prometheus.MustRegister(exporter)
@@ -65,9 +76,9 @@ func main() {
`))
})
logger.Log("level", "info", "msg", "Listening", "listenAddress", *listenAddress)
level.Info(logger).Log("msg", "Listening", "listenAddress", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
logger.Log("level", "error", "msg", "Error starting HTTP server:", "err", err)
level.Error(logger).Log("msg", "Error starting HTTP server:", "err", err)
os.Exit(1)
}
}
@@ -5,6 +5,7 @@ import (
"strconv"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@@ -35,12 +36,12 @@ func (q *Query) Run(conn *connection) error {
res := make(map[string]interface{})
err := rows.MapScan(res)
if err != nil {
q.log.Log("level", "error", "msg", "Failed to scan", "err", err, "host", conn.host, "db", conn.database)
level.Error(q.log).Log("msg", "Failed to scan", "err", err, "host", conn.host, "db", conn.database)
continue
}
m, err := q.updateMetrics(conn, res)
if err != nil {
q.log.Log("level", "error", "msg", "Failed to update metrics", "err", err, "host", conn.host, "db", conn.database)
level.Error(q.log).Log("msg", "Failed to update metrics", "err", err, "host", conn.host, "db", conn.database)
continue
}
metrics = append(metrics, m...)
@@ -66,7 +67,13 @@ func (q *Query) updateMetrics(conn *connection, res map[string]interface{}) ([]p
for _, valueName := range q.Values {
m, err := q.updateMetric(conn, res, valueName)
if err != nil {
q.log.Log("level", "error", "msg", "Failed to update metric", "value", valueName, "err", err, "host", conn.host, "db", conn.database)
level.Error(q.log).Log(
"msg", "Failed to update metric",
"value", valueName,
"err", err,
"host", conn.host,
"db", conn.database,
)
continue
}
metrics = append(metrics, m)

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 15609d1

Please sign in to comment.