Skip to content
This repository was archived by the owner on Jul 26, 2022. It is now read-only.

Commit 1d9d237

Browse files
authored
feat: add last_state metric (#357)
It is a bit difficult to alert on sync_calls since they trigger into an error state and may stay there. This adds the gauge last_state. With last_state, people can easily alert on a value < 0 to indicate failure.
1 parent b345ffd commit 1d9d237

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ kubernetes-external-secrets exposes the following metrics over a prometheus endp
435435
| Metric | Description | Example |
436436
| ----------------------------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
437437
| `sync_calls` | This metric counts the number of sync calls by backend, secret name and status | `sync_calls{name="foo",namespace="example",backend="foo",status="success"} 1` |
438+
| `last_state` | A value of -1 or 1 where -1 means the last sync_call was an error and 1 means the last sync_call was a success | `last_state{name="foo",namespace="example",backend="foo"} 1` |
438439

439440

440441
## Development

lib/metrics-server.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,7 @@ describe('MetricsServer', () => {
5757

5858
expect(res.text).to.have.string('sync_calls{name="foo",namespace="example",backend="foo",status="success"} 1')
5959
expect(res.text).to.have.string('sync_calls{name="bar",namespace="example",backend="foo",status="failed"} 1')
60+
expect(res.text).to.have.string('last_state{name="foo",namespace="example",backend="foo"} 1')
61+
expect(res.text).to.have.string('last_state{name="bar",namespace="example",backend="foo"} -1')
6062
})
6163
})

lib/metrics.js

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ class Metrics {
1515
labelNames: ['name', 'namespace', 'backend', 'status'],
1616
registers: [registry]
1717
})
18+
this._lastState = new Prometheus.Gauge({
19+
name: 'last_state',
20+
help: 'Value -1 if the last sync was a failure, 1 otherwise.',
21+
labelNames: ['name', 'namespace', 'backend'],
22+
registers: [registry]
23+
})
1824
}
1925

2026
/**
@@ -31,6 +37,19 @@ class Metrics {
3137
backend,
3238
status
3339
})
40+
if (status === 'success') {
41+
this._lastState.set({
42+
name,
43+
namespace,
44+
backend
45+
}, 1)
46+
} else {
47+
this._lastState.set({
48+
name,
49+
namespace,
50+
backend
51+
}, -1)
52+
}
3453
}
3554
}
3655

0 commit comments

Comments
 (0)