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

Commit 5b5a00f

Browse files
authored
feat(metrics): add metrics names following Prometheus best practices, **deprecating** old metrics names! (#540)
1 parent d85311b commit 5b5a00f

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,10 @@ The secrets will persist even if the helm installation is removed, although they
615615

616616
kubernetes-external-secrets exposes the following metrics over a prometheus endpoint:
617617

618-
| Metric | Description | Example |
619-
| ----------------------------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
620-
| `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` |
621-
| `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` |
618+
| Metric | Type | Description | Example |
619+
| -------------------------------------------------- | ------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
620+
| `kubernetes_external_secrets_sync_calls_count` | Counter | Number of sync operations by backend, secret name and status | `kubernetes_external_secrets_sync_calls_count{name="foo",namespace="example",backend="foo",status="success"} 1` |
621+
| `kubernetes_external_secrets_last_sync_call_state` | Gauge | State of last sync call of external secert, where -1 means the last sync_call was an error and 1 means the last sync_call was a success | `kubernetes_external_secrets_last_sync_call_state{name="foo",namespace="example",backend="foo"} 1` |
622622

623623

624624
## Development

lib/metrics-server.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ describe('MetricsServer', () => {
5555
.expect('Content-Type', Prometheus.register.contentType)
5656
.expect(200)
5757

58+
expect(res.text).to.have.string('kubernetes_external_secrets_sync_calls_count{name="foo",namespace="example",backend="foo",status="success"} 1')
59+
expect(res.text).to.have.string('kubernetes_external_secrets_sync_calls_count{name="bar",namespace="example",backend="foo",status="failed"} 1')
60+
expect(res.text).to.have.string('kubernetes_external_secrets_last_sync_call_state{name="foo",namespace="example",backend="foo"} 1')
61+
expect(res.text).to.have.string('kubernetes_external_secrets_last_sync_call_state{name="bar",namespace="example",backend="foo"} -1')
62+
63+
// Deprecated metrics.
5864
expect(res.text).to.have.string('sync_calls{name="foo",namespace="example",backend="foo",status="success"} 1')
5965
expect(res.text).to.have.string('sync_calls{name="bar",namespace="example",backend="foo",status="failed"} 1')
6066
expect(res.text).to.have.string('last_state{name="foo",namespace="example",backend="foo"} 1')

lib/metrics.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,27 @@ class Metrics {
99
*/
1010
constructor ({ registry }) {
1111
this._registry = registry
12+
this._syncCallsCount = new Prometheus.Counter({
13+
name: 'kubernetes_external_secrets_sync_calls_count',
14+
help: 'Number of sync operations',
15+
labelNames: ['name', 'namespace', 'backend', 'status'],
16+
registers: [registry]
17+
})
1218
this._syncCalls = new Prometheus.Counter({
1319
name: 'sync_calls',
14-
help: 'number of sync operations',
20+
help: '(Deprecated since 0.6.1, please use kubernetes_external_secrets_sync_calls_count) Number of sync operations',
1521
labelNames: ['name', 'namespace', 'backend', 'status'],
1622
registers: [registry]
1723
})
24+
this._lastSyncCallState = new Prometheus.Gauge({
25+
name: 'kubernetes_external_secrets_last_sync_call_state',
26+
help: 'State of last sync call of external secert. Value -1 if the last sync was a failure, 1 otherwise',
27+
labelNames: ['name', 'namespace', 'backend'],
28+
registers: [registry]
29+
})
1830
this._lastState = new Prometheus.Gauge({
1931
name: 'last_state',
20-
help: 'Value -1 if the last sync was a failure, 1 otherwise.',
32+
help: '(Deprecated since 0.6.1, please use kubernetes_external_secrets_last_sync_call_state) Value -1 if the last sync was a failure, 1 otherwise',
2133
labelNames: ['name', 'namespace', 'backend'],
2234
registers: [registry]
2335
})
@@ -31,19 +43,35 @@ class Metrics {
3143
* @param {String} status - the result of the sync process: error|success
3244
*/
3345
observeSync ({ name, namespace, backend, status }) {
46+
this._syncCallsCount.inc({
47+
name,
48+
namespace,
49+
backend,
50+
status
51+
})
3452
this._syncCalls.inc({
3553
name,
3654
namespace,
3755
backend,
3856
status
3957
})
4058
if (status === 'success') {
59+
this._lastSyncCallState.set({
60+
name,
61+
namespace,
62+
backend
63+
}, 1)
4164
this._lastState.set({
4265
name,
4366
namespace,
4467
backend
4568
}, 1)
4669
} else {
70+
this._lastSyncCallState.set({
71+
name,
72+
namespace,
73+
backend
74+
}, -1)
4775
this._lastState.set({
4876
name,
4977
namespace,

lib/metrics.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe('Metrics', () => {
2727
backend: 'foo',
2828
status: 'success'
2929
})
30+
expect(registry.metrics()).to.have.string('kubernetes_external_secrets_sync_calls_count{name="foo",namespace="example",backend="foo",status="success"} 1')
31+
// Deprecated metric.
3032
expect(registry.metrics()).to.have.string('sync_calls{name="foo",namespace="example",backend="foo",status="success"} 1')
3133
})
3234
})

0 commit comments

Comments
 (0)