This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: basic metrics Signed-off-by: Moritz Johner <[email protected]> * chore: update chart docs & env vars
- Loading branch information
Showing
14 changed files
with
715 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
const express = require('express') | ||
const Prometheus = require('prom-client') | ||
|
||
/** MetricsServer class. */ | ||
class MetricsServer { | ||
/** | ||
* Create Metrics Server | ||
* @param {number} port - the port to listen on | ||
* @param {Object} logger - Logger for logging stuff | ||
* @param {Object} register - Prometheus registry that holds metric data | ||
*/ | ||
constructor ({ port, logger, registry }) { | ||
this._port = port | ||
this._logger = logger | ||
this._registry = registry | ||
|
||
this._app = express() | ||
this._app.get('/metrics', (req, res) => { | ||
res.set('Content-Type', Prometheus.register.contentType) | ||
res.end(this._registry.metrics()) | ||
}) | ||
} | ||
|
||
/** | ||
* Start the metrics server: Listen on a TCP port and serve metrics over HTTP | ||
*/ | ||
start () { | ||
return new Promise((resolve, reject) => { | ||
this._server = this._app.listen(this._port, () => { | ||
this._logger.info(`MetricsServer listening on port ${this._port}`) | ||
resolve() | ||
}) | ||
this._app.on('error', err => reject(err)) | ||
}) | ||
} | ||
|
||
/** | ||
* Stop the metrics server | ||
*/ | ||
stop () { | ||
return new Promise((resolve, reject) => { | ||
this._server.close(err => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = MetricsServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const Prometheus = require('prom-client') | ||
const request = require('supertest') | ||
|
||
const MetricsServer = require('./metrics-server') | ||
const Metrics = require('./metrics') | ||
|
||
describe('MetricsServer', () => { | ||
let server | ||
let loggerMock | ||
let registry | ||
let metrics | ||
|
||
beforeEach(async () => { | ||
loggerMock = sinon.mock() | ||
loggerMock.info = sinon.stub() | ||
registry = new Prometheus.Registry() | ||
metrics = new Metrics({ registry }) | ||
|
||
server = new MetricsServer({ | ||
logger: loggerMock, | ||
registry: registry, | ||
port: 3918 | ||
}) | ||
|
||
await server.start() | ||
}) | ||
|
||
afterEach(async () => { | ||
sinon.restore() | ||
await server.stop() | ||
}) | ||
|
||
it('start server to serve metrics', async () => { | ||
metrics.observeSync({ | ||
name: 'foo', | ||
namespace: 'example', | ||
backend: 'foo', | ||
status: 'success' | ||
}) | ||
|
||
metrics.observeSync({ | ||
name: 'bar', | ||
namespace: 'example', | ||
backend: 'foo', | ||
status: 'failed' | ||
}) | ||
|
||
const res = await request('http://localhost:3918') | ||
.get('/metrics') | ||
.expect('Content-Type', Prometheus.register.contentType) | ||
.expect(200) | ||
|
||
expect(res.text).to.have.string('sync_calls{name="foo",namespace="example",backend="foo",status="success"} 1') | ||
expect(res.text).to.have.string('sync_calls{name="bar",namespace="example",backend="foo",status="failed"} 1') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict' | ||
|
||
const Prometheus = require('prom-client') | ||
|
||
/** Metrics class. */ | ||
class Metrics { | ||
/** | ||
* Create Metrics object | ||
*/ | ||
constructor ({ registry }) { | ||
this._registry = registry | ||
this._syncCalls = new Prometheus.Counter({ | ||
name: 'sync_calls', | ||
help: 'number of sync operations', | ||
labelNames: ['name', 'namespace', 'backend', 'status'], | ||
registers: [registry] | ||
}) | ||
} | ||
|
||
/** | ||
* Observe the result a sync process | ||
* @param {String} name - the name of the externalSecret | ||
* @param {String} namespace - the namespace of the externalSecret | ||
* @param {String} backend - the backend used to fetch the externalSecret | ||
* @param {String} status - the result of the sync process: error|success | ||
*/ | ||
observeSync ({ name, namespace, backend, status }) { | ||
this._syncCalls.inc({ | ||
name, | ||
namespace, | ||
backend, | ||
status | ||
}) | ||
} | ||
} | ||
|
||
module.exports = Metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const Prometheus = require('prom-client') | ||
|
||
const Metrics = require('./metrics') | ||
|
||
describe('Metrics', () => { | ||
let registry | ||
let metrics | ||
|
||
beforeEach(async () => { | ||
registry = new Prometheus.Registry() | ||
metrics = new Metrics({ registry }) | ||
}) | ||
|
||
afterEach(async () => { | ||
sinon.restore() | ||
}) | ||
|
||
it('should store metrics', async () => { | ||
metrics.observeSync({ | ||
name: 'foo', | ||
namespace: 'example', | ||
backend: 'foo', | ||
status: 'success' | ||
}) | ||
expect(registry.metrics()).to.have.string('sync_calls{name="foo",namespace="example",backend="foo",status="success"} 1') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.