Say I have a logger helper that I call in a catch, something like this:
export async function logger(error, message) {
if (process.env.NODE_ENV === 'development') {
console.error(error)
} else {
// Can I do this with the provided Prometheus/Grafana setup?
await sendToGrafanaSomehow(error, message)
}
}
I can’t quire figure it out.
I know I can setup a /metrics endpoint in my Express to send custom metrics, but I’m not quite sure if that’s doing what I want:
So, all of this seems like it’s on the right track.
Based on the code snippet, it looks like you’re using express-prometheus-middleware, so just as a word of caution, that seems abandoned. shouldn’t really matter, just letting you know.
To actually track your own metrics, I believe you’ll have to use prom-client (the library used by express-prometheus-middleware) directly.
E.g.
const Prometheus = require('prom-client');
let some_counter = Prometheus.Counter({
name: `myapp_errors_logged`,
help: 'Counter for total errors logged',
["error_type"],
});
// in error handler
let error_type = ...;
some_counter.inc({"error_type": error_type})
This should, if I read correctly, automatically end up exposed to prometheusMiddleware.
From this point, just make sure the port defined in the [metrics] section of your fly.toml is the port that express is listening on, and it should Just Work, available at fly-metrics.net.