How to log backend errors with prometheus?

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:

app.use(
  prometheusMiddleware({
    metricsPath: '/metrics',
    collectDefaultMetrics: true,
    metricsApp,
  })
)

fly.toml:

[metrics]
  port = 8081
  path = "/metrics"

Thanks

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.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.