-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): added /metrics endpoint. Fixes MEMB-604
- Loading branch information
1 parent
0d2347e
commit acfcfd9
Showing
6 changed files
with
156 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { | ||
Gauge, | ||
register | ||
} = require('prom-client'); | ||
|
||
const { | ||
Event, | ||
Application | ||
} = require('../models'); | ||
const helpers = require('./helpers'); | ||
|
||
const createGauge = (name, help, labels = []) => new Gauge({ | ||
name, | ||
help, | ||
labelNames: labels | ||
}); | ||
|
||
const gaugesList = { | ||
eventsTotal: createGauge('events_total', 'Total amount of events'), | ||
eventsByType: createGauge('events_by_type', 'Amount of events by type', ['type']), | ||
eventsByTypeAndStatus: createGauge('events_by_type_and_status', 'Amount of events by type and status', ['type', 'status']), | ||
applicationsTotal: createGauge('applications_total', 'Total amount of applications'), | ||
applicationsByEvent: createGauge('applications_by_event', 'Amount of applications by event', ['event_name']), | ||
applicationsByEventAndStatus: createGauge('applications_by_event_and_status', 'Amount of applications by event and status', ['event_name', 'status']), | ||
applicationsByEventAndBody: createGauge('applications_by_event_and_body', 'Amount of applications by event and body', ['event_name', 'body_name']), | ||
}; | ||
|
||
exports.getMetrics = async (req, res) => { | ||
let [ | ||
events, | ||
applications, | ||
] = await Promise.all([ | ||
Event.findAll(), | ||
Application.findAll({ include: [Event] }), | ||
]); | ||
|
||
events = events.map((event) => event.toJSON()); | ||
applications = applications.map((application) => Object.assign(application.toJSON(), { event_name: application.event.name })); | ||
|
||
// setting gauges with real data | ||
helpers.addGaugeData(gaugesList.eventsTotal, helpers.countByFields(events, [])); | ||
helpers.addGaugeData(gaugesList.eventsByType, helpers.countByFields(events, ['type'])); | ||
helpers.addGaugeData(gaugesList.eventsByTypeAndStatus, helpers.countByFields(events, ['type', 'status'])); | ||
|
||
helpers.addGaugeData(gaugesList.applicationsTotal, helpers.countByFields(applications, [])); | ||
helpers.addGaugeData(gaugesList.applicationsByEvent, helpers.countByFields(applications, ['event_name'])); | ||
helpers.addGaugeData(gaugesList.applicationsByEventAndBody, helpers.countByFields(applications, ['event_name', 'body_name'])); | ||
helpers.addGaugeData(gaugesList.applicationsByEventAndStatus, helpers.countByFields(applications, ['event_name', 'status'])); | ||
|
||
res.set('Content-Type', register.contentType); | ||
res.end(register.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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
const { startServer, stopServer } = require('../../lib/server.js'); | ||
const { request } = require('../scripts/helpers'); | ||
const generator = require('../scripts/generator'); | ||
const mock = require('../scripts/mock-core-registry'); | ||
|
||
describe('Metrics requests', () => { | ||
beforeEach(async () => { | ||
mock.mockAll(); | ||
await startServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await stopServer(); | ||
mock.cleanAll(); | ||
}); | ||
|
||
test('should return data correctly', async () => { | ||
const event = await generator.createEvent({}); | ||
await generator.createApplication({ user_id: 1, status: 'accepted' }, event); | ||
await generator.createApplication({ user_id: 2, status: 'accepted' }, event); | ||
await generator.createApplication({ user_id: 3, status: 'pending' }, event); | ||
|
||
|
||
const res = await request({ | ||
uri: '/metrics', | ||
method: 'GET', | ||
json: false | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
}); | ||
}); |