Skip to content

Commit a0d2e61

Browse files
authored
Add soure label to callback duration metric (#178)
Add rules count to engine logging
1 parent 6cb84c1 commit a0d2e61

File tree

8 files changed

+20
-19
lines changed

8 files changed

+20
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SHELL = /bin/bash
22

33
export
4-
LINT_VERSION="1.59.0"
4+
LINT_VERSION="1.59.1"
55

66
.PHONY: all
77
all: deps lint test

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
go.etcd.io/etcd/api/v3 v3.5.14
1010
go.etcd.io/etcd/client/v3 v3.5.14
1111
go.uber.org/zap v1.27.0
12-
golang.org/x/net v0.25.0
12+
golang.org/x/net v0.26.0
1313
)
1414

1515
require (
@@ -26,8 +26,8 @@ require (
2626
github.com/prometheus/procfs v0.14.0 // indirect
2727
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
2828
go.uber.org/multierr v1.10.0 // indirect
29-
golang.org/x/sys v0.20.0 // indirect
30-
golang.org/x/text v0.15.0 // indirect
29+
golang.org/x/sys v0.21.0 // indirect
30+
golang.org/x/text v0.16.0 // indirect
3131
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
3232
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
3333
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
6060
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6161
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6262
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
63-
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
64-
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
63+
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
64+
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
6565
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6666
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6767
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6868
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6969
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7070
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
71-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
72-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
71+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
72+
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7373
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
7474
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
75-
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
76-
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
75+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
76+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
7777
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
7878
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
7979
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=

metrics/prometheus.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
Namespace: "rules",
4747
Help: "etcd rules engine callback duration in ms",
4848
Buckets: []float64{1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 30000, 60000, 300000, 600000},
49-
}, []string{"pattern", "rule"})
49+
}, []string{"pattern", "rule", "source"})
5050
rulesEngineKeyProcessBufferCap = prometheus.NewGauge(prometheus.GaugeOpts{
5151
Name: "key_process_buffer_cap",
5252
Subsystem: "etcd",
@@ -123,8 +123,8 @@ func WorkBufferWaitTime(methodName, pattern string, startTime time.Time) {
123123
}
124124

125125
// CallbackWaitTime tracks how much time elapsed between when the rule was evaluated and the callback called.
126-
func CallbackWaitTime(pattern, ruleID string, startTime time.Time) {
127-
rulesEngineCallbackWaitTime.WithLabelValues(pattern, ruleID).Observe(float64(time.Since(startTime).Nanoseconds() / 1e6))
126+
func CallbackWaitTime(pattern, ruleID, source string, startTime time.Time) {
127+
rulesEngineCallbackWaitTime.WithLabelValues(pattern, ruleID, source).Observe(float64(time.Since(startTime).Nanoseconds() / 1e6))
128128
}
129129

130130
// KeyProcessBufferCap tracks the capacity of the key processor buffer.

metrics/prometheus_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestWorkBufferWaitTime(t *testing.T) {
6969
}
7070

7171
func TestCallbackWaitTime(t *testing.T) {
72-
CallbackWaitTime("/desired/key/pattern", "ruleID", time.Now())
72+
CallbackWaitTime("/desired/key/pattern", "ruleID", "source", time.Now())
7373
checkMetrics(t, `rules_etcd_callback_wait_ms_count{pattern="/desired/key/pattern",rule="ruleID"} 1`)
7474
}
7575

rules/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (e *baseEngine) addRule(rule DynamicRule,
292292
}
293293

294294
func (e *v3Engine) Run() {
295-
e.logger.Info("Rules engine options", zap.Object("options", &e.options))
295+
e.logger.Info("Rules engine options", zap.Object("options", &e.options), zap.Int("rules", len(e.ruleMgr.rules)))
296296
prefixSlice := []string{}
297297
prefixes := e.ruleMgr.prefixes
298298
// This is a map; used to ensure there are no duplicates

rules/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (w *watcher) singleRun() {
7070
time.Sleep(delay) // TODO ideally a context should be used for fast shutdown, e.g. select { case <-ctx.Done(); case <-time.After(delay) }
7171
}
7272
w.logger.Debug("Calling process key", zap.String("key", key))
73-
w.kp.processKey(key, value, w.api, w.logger, map[string]string{}, incRuleProcessedCount)
73+
w.kp.processKey(key, value, w.api, w.logger, map[string]string{"source": "watcher"}, incRuleProcessedCount)
7474
}
7575

7676
func incRuleProcessedCount(ruleID string) {

rules/worker.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (bw *baseWorker) isStopped() bool {
7272

7373
func (bw *baseWorker) doWork(loggerPtr **zap.Logger,
7474
rulePtr *staticRule, lockTTL int, callback workCallback,
75-
metricsInfo metricsInfo, lockKey string, ruleID string) {
75+
metricsInfo metricsInfo, lockKey string, ruleID string, source string) {
7676
logger := *loggerPtr
7777
logger = logger.With(zap.String("ruleID", ruleID), zap.String("mutex", lockKey))
7878
rule := *rulePtr
@@ -131,7 +131,7 @@ func (bw *baseWorker) doWork(loggerPtr **zap.Logger,
131131
logger.Info("callback started", zap.Any("attributes", attrMap))
132132
startTime := time.Now()
133133
callback()
134-
metrics.CallbackWaitTime(metricsInfo.keyPattern, ruleID, startTime)
134+
metrics.CallbackWaitTime(metricsInfo.keyPattern, ruleID, source, startTime)
135135
if bw.callbackListener != nil {
136136
bw.callbackListener.callbackDone(ruleID, attributes)
137137
}
@@ -164,6 +164,7 @@ func (w *v3Worker) singleRun() {
164164
}
165165
w.addWorkerID(newMetadata)
166166
task.Metadata = newMetadata
167+
source := task.Metadata["source"]
167168
task.Logger = task.Logger.With(zap.String("worker", w.workerID))
168169
// Use wait group and go routine to prevent killing of workers
169170
var wg sync.WaitGroup
@@ -180,7 +181,7 @@ func (w *v3Worker) singleRun() {
180181
task.Context = context
181182
task.cancel = cancelFunc
182183
metricsInfo := newMetricsInfo(context, work.keyPattern, work.metricsStartTime)
183-
w.doWork(&task.Logger, &work.rule, w.engine.getLockTTLForRule(work.ruleIndex), func() { work.ruleTaskCallback(&task) }, metricsInfo, work.lockKey, work.ruleID)
184+
w.doWork(&task.Logger, &work.rule, w.engine.getLockTTLForRule(work.ruleIndex), func() { work.ruleTaskCallback(&task) }, metricsInfo, work.lockKey, work.ruleID, source)
184185
}()
185186
wg.Wait()
186187
}

0 commit comments

Comments
 (0)