Skip to content

Commit 15b9a45

Browse files
Add example config and fix stat update (#31)
* Add debug logging * Fix: update cached FileInfo if modified * Exit on register failure
1 parent 93a1e59 commit 15b9a45

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
103103
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
104104
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
105105
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
106+
github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfEUpgAwUN0o=
106107
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
107108
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
108109
github.com/kabukky/httpscerts v0.0.0-20150320125433-617593d7dcb3/go.mod h1:BYpt4ufZiIGv2nXn4gMxnfKV306n3mWXgNu/d2TqdTU=
@@ -133,6 +134,7 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
133134
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
134135
github.com/prometheus/client_golang v1.6.0 h1:YVPodQOcK15POxhgARIvnDRVpLcuK8mglnMrWfyrw6A=
135136
github.com/prometheus/client_golang v1.6.0/go.mod h1:ZLOG9ck3JLRdB5MgO8f+lLTe83AXG6ro35rLTxvnIl4=
137+
github.com/prometheus/client_golang v1.9.0 h1:Rrch9mh17XcxvEu9D9DEpb4isxjGBtcevQjKvxPRQIU=
136138
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
137139
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
138140
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -151,6 +153,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
151153
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
152154
github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI=
153155
github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
156+
github.com/prometheus/prometheus v2.5.0+incompatible h1:7QPitgO2kOFG8ecuRn9O/4L9+10He72rVRJvMXrE9Hg=
154157
github.com/prometheus/prometheus v2.5.0+incompatible/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s=
155158
github.com/prometheus/promu v0.5.0 h1:q7GkmIdBZ+ulL+6v4EDsZL+cW9UCW9J3DHA89bFI83c=
156159
github.com/prometheus/promu v0.5.0/go.mod h1:sXydR89lpo0YkCrYK1EhYjaJUesenzLhd9CNRAwN+bI=
@@ -180,6 +183,7 @@ golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxT
180183
golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek=
181184
golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY=
182185
golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
186+
golang.org/x/exp v0.0.0-20191227195350-da58074b4299 h1:zQpM52jfKHG6II1ISZY1ZcpygvuSFZpLwfluuF89XOg=
183187
golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
184188
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
185189
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
@@ -191,6 +195,7 @@ golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jK
191195
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
192196
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
193197
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
198+
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE=
194199
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
195200
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
196201
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
@@ -277,6 +282,7 @@ golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtn
277282
golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
278283
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
279284
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
285+
golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4 h1:Toz2IK7k8rbltAXwNAxKcn9OzqyNfMUhUNjz3sL0NMk=
280286
golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
281287
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
282288
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -346,5 +352,6 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
346352
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a h1:LJwr7TCTghdatWv40WobzlKXc9c4s8oGa7QKJUtHhWA=
347353
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
348354
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
355+
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
349356
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
350357
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=

internal/setup/setup.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"os"
77

8+
"github.com/m-lab/go/logx"
89
"github.com/m-lab/prometheus-bigquery-exporter/sql"
910
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/spf13/afero"
@@ -26,6 +27,7 @@ func (f *File) IsModified() (bool, error) {
2627
var err error
2728
if f.stat == nil {
2829
f.stat, err = fs.Stat(f.Name)
30+
logx.Debug.Println("IsModified:stat1:", f.Name, err)
2931
// Return true on the first successful Stat(), or the error otherwise.
3032
return err == nil, err
3133
}
@@ -34,7 +36,14 @@ func (f *File) IsModified() (bool, error) {
3436
log.Printf("Failed to stat %q: %v", f.Name, err)
3537
return false, err
3638
}
37-
return curr.ModTime().After(f.stat.ModTime()), nil
39+
logx.Debug.Println("IsModified:stat2:", f.Name, curr.ModTime(), f.stat.ModTime(),
40+
curr.ModTime().After(f.stat.ModTime()))
41+
modified := curr.ModTime().After(f.stat.ModTime())
42+
if modified {
43+
// Update the stat cache to the latest version.
44+
f.stat = curr
45+
}
46+
return modified, nil
3847
}
3948

4049
// Register the given collector. If a collector was previously registered with
@@ -43,6 +52,7 @@ func (f *File) IsModified() (bool, error) {
4352
func (f *File) Register(c *sql.Collector) error {
4453
if f.c != nil {
4554
ok := prometheus.Unregister(f.c)
55+
logx.Debug.Println("Unregister:", ok)
4656
if !ok {
4757
// This is a fatal error. If the
4858
return fmt.Errorf("Failed to unregister %q", f.Name)
@@ -55,6 +65,7 @@ func (f *File) Register(c *sql.Collector) error {
5565
// While collector Update could fail transiently, this may be a fatal error.
5666
return err
5767
}
68+
logx.Debug.Println("Register: success:", f.Name)
5869
// Save the registered collector.
5970
f.c = c
6071
return nil

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ func reloadRegisterUpdate(client *bigquery.Client, files []setup.File, vars map[
8181
fileToMetric(f.Name), fileToQuery(f.Name, vars))
8282

8383
log.Println("Registering:", fileToMetric(f.Name))
84-
err = f.Register(c)
84+
// NOTE: prometheus collector registration will fail when a file
85+
// uses the same name but changes the metrics reported. Because
86+
// this cannot be recovered, we use rtx.Must to exit and allow
87+
// the runtime environment to restart.
88+
rtx.Must(f.Register(c), "Failed to register collector: aborting")
8589
} else {
86-
log.Println("Updating:", fileToMetric(f.Name))
90+
start := time.Now()
8791
err = f.Update()
92+
log.Println("Updating:", fileToMetric(f.Name), time.Since(start))
8893
}
8994
if err != nil {
9095
log.Println("Error:", f.Name, err)

sql/collector.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package sql
44
import (
55
"log"
66
"sync"
7+
"time"
8+
9+
"github.com/m-lab/go/logx"
710

811
"github.com/prometheus/client_golang/prometheus"
912
)
@@ -66,6 +69,7 @@ func NewCollector(runner QueryRunner, valType prometheus.ValueType, metricName,
6669
// Describe satisfies the prometheus.Collector interface. Describe is called
6770
// immediately after registering the collector.
6871
func (col *Collector) Describe(ch chan<- *prometheus.Desc) {
72+
logx.Debug.Println("Describe:", time.Now())
6973
if col.descs == nil {
7074
// TODO: collect metrics for query exec time.
7175
col.descs = make(map[string]*prometheus.Desc, 1)
@@ -84,13 +88,16 @@ func (col *Collector) Describe(ch chan<- *prometheus.Desc) {
8488
// Collect satisfies the prometheus.Collector interface. Collect reports values
8589
// from cached metrics.
8690
func (col *Collector) Collect(ch chan<- prometheus.Metric) {
91+
logx.Debug.Println("Collect:", time.Now())
8792
col.mux.Lock()
8893
// Get reference to current metrics slice to allow Update to run concurrently.
8994
metrics := col.metrics
9095
col.mux.Unlock()
9196

9297
for i := range col.metrics {
9398
for k, desc := range col.descs {
99+
logx.Debug.Printf("%s labels:%#v values:%#v",
100+
col.metricName, metrics[i].LabelValues, metrics[i].Values[k])
94101
ch <- prometheus.MustNewConstMetric(
95102
desc, col.valType, metrics[i].Values[k], metrics[i].LabelValues...)
96103
}
@@ -105,8 +112,10 @@ func (col *Collector) String() string {
105112
// Update runs the collector query and atomically updates the cached metrics.
106113
// Update is called automaticlly after the collector is registered.
107114
func (col *Collector) Update() error {
115+
logx.Debug.Println("Update:", col.metricName)
108116
metrics, err := col.runner.Query(col.query)
109117
if err != nil {
118+
logx.Debug.Println("Failed to run query:", err)
110119
return err
111120
}
112121
// Swap the cached metrics.

0 commit comments

Comments
 (0)