Add router and search result metrics

This commit is contained in:
Rogerio Chaves 2020-04-15 20:02:13 +02:00
parent 4c9d834c56
commit 0d49dcbe05
No known key found for this signature in database
GPG Key ID: E6AF5440509B1D94
4 changed files with 38 additions and 33 deletions

View File

@ -20,6 +20,7 @@ if (process.env.NODE_ENV != "production") {
watcher.on("all", () => {
console.log("Clearing /lib/ module cache from server");
Object.keys(require.cache).forEach((id) => {
if (id.includes("metrics")) return;
if (/[\/\\]lib[\/\\]/.test(id)) delete require.cache[id];
});
if (server) server.close();

View File

@ -408,6 +408,7 @@ router.get("/search", async (req, res) => {
const query = req.query.query;
const people = await queries.searchPeople(ssbServer, query);
metrics.searchResults.observe(people.length);
res.render("search", { people, query });
});

View File

@ -4,36 +4,37 @@ const collectDefaultMetrics = prometheus.collectDefaultMetrics;
const register = prometheus.register;
collectDefaultMetrics({ register });
const ssbProgressRate = new prometheus.Gauge({
name: "ssb_progress_rate",
help: "Tracks ssb syncing progress rate",
});
const ssbProgressFeeds = new prometheus.Gauge({
name: "ssb_progress_feeds",
help: "Tracks ssb syncing progress feeds",
});
const ssbProgressIncompleteFeeds = new prometheus.Gauge({
name: "ssb_progress_incomplete_feeds",
help: "Tracks ssb syncing progress incomplete feeds",
});
const ssbProgressProgress = new prometheus.Gauge({
name: "ssb_progress_progress",
help: "Tracks ssb syncing progress progress",
});
const ssbProgressTotal = new prometheus.Gauge({
name: "ssb_progress_total",
help: "Tracks ssb syncing progress total",
});
const { Gauge, Summary, Counter } = prometheus;
module.exports = {
register,
ssbProgressRate,
ssbProgressFeeds,
ssbProgressIncompleteFeeds,
ssbProgressProgress,
ssbProgressTotal,
router: new Counter({
name: "router",
help: "Routes accessed by users",
labelNames: ["method", "path"],
}),
ssbProgressRate: new Gauge({
name: "ssb_progress_rate",
help: "Tracks ssb syncing progress rate",
}),
ssbProgressFeeds: new Gauge({
name: "ssb_progress_feeds",
help: "Tracks ssb syncing progress feeds",
}),
ssbProgressIncompleteFeeds: new Gauge({
name: "ssb_progress_incomplete_feeds",
help: "Tracks ssb syncing progress incomplete feeds",
}),
ssbProgressProgress: new Gauge({
name: "ssb_progress_progress",
help: "Tracks ssb syncing progress progress",
}),
ssbProgressTotal: new Gauge({
name: "ssb_progress_total",
help: "Tracks ssb syncing progress total",
}),
searchResults: new Summary({
name: "search_results",
help: "Amount of results returned from search",
}),
};

View File

@ -2,13 +2,15 @@ const fs = require("fs");
const leftpad = require("left-pad"); // I don't believe I'm depending on this
const pull = require("pull-stream");
const split = require("split-buffer");
const metrics = require("./metrics");
module.exports.asyncRouter = (app) => {
const debug = require("debug")("router");
let wrapper = (debugMsg, fn) => async (req, res, next) => {
let wrapper = (method, path, fn) => async (req, res, next) => {
try {
debug(debugMsg);
debug(`${method} ${path}`);
metrics.router.inc({ method, path });
await fn(req, res);
} catch (e) {
next(e);
@ -16,11 +18,11 @@ module.exports.asyncRouter = (app) => {
};
return {
get: (path, fn) => {
app.get(path, wrapper(`GET ${path}`, fn));
app.get(path, wrapper("GET", path, fn));
},
post: (path, fn) => {
debug(`POST ${path}`);
app.post(path, wrapper(`POST ${path}`, fn));
app.post(path, wrapper("POST", path, fn));
},
};
};