toddg
toddg

Reputation: 2916

See how long a Heroku request has run for

Is it possible to determine how long a Heroku Node.js request has run for?

At the end of every Heroku request, I get the following line written to my log:

2016-08-22T22:00:05.410367+00:00 heroku[router]: at=info method=POST path="xxx" host=xxx request_id=xxx fwd="54.85.230.45" dyno=web.1 connect=1ms service=2759ms status=200 bytes=669

The service var tells me how long the request took to run (as detailed here). Is it possible to access this variable while the request is running?

Upvotes: 0

Views: 38

Answers (1)

hunterloftis
hunterloftis

Reputation: 13799

Sure, though measuring request times has nothing specifically to do with Heroku.

Just add some sort of timer to your request handlers. For example, in express, you can dynamically see how long a request has taken (so far) by doing something like:

const express = require('express');
const app = express();
app
  .use((req, res, next) => {
    req.start = Date.now();
    next();
  })
  .get('/', (req, res) => {
    res.send('ok');
    console.log(Date.now() - req.start);
  })
  .listen(process.env.PORT || 3000, () => {
    console.log('Listening');
  });

By recording the time at which a request starts, you can always later compute how long that request has taken.

Upvotes: 1

Related Questions