Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the time Node.js spends to send an HTTP response body?

My current setup involves a Node.js web application using Express.js.
I am using DataDog's dd-tracer to measure the time Node.js spends for particular method invocations as part of my APM solution.

I would like to know if it is possible to measure the portion of time an incoming HTTP request is busy sending data back to the client as HTTP response body.

Are there any pitfalls or inaccuracies involved when trying to do this kind of instrumentation?
Does anybody know why this is not measured by APM client libraries by default?

like image 279
Martin Löper Avatar asked May 27 '20 11:05

Martin Löper


People also ask

How do I find HTTP response time?

The response time is determined by making an HTTP GET request to the web page URL and measuring the time it takes for the first object to be returned.

What is process Hrtime?

The process. hrtime() method to measure code execution time which returns array which include current high-resolution real time in a [seconds, nanoseconds].


1 Answers

I would like to know if it is possible to measure the portion of time an incoming HTTP request is busy sending data back to the client as HTTP response body.

You could wrap calls to res.write manually to create additional spans in the request trace. I would only recommend this if there are not many calls to the method within a request, and otherwise I would recommend to capture just a metric instead.

Alternatively, profiling might be an option that would give you a lot more information about exactly what is taking time within the res.write calls.

I look for a "global" solution which can be integrated into a Nest.js application without instrumenting each call to res.write manually.

As described above, you can simply wrap res.write directly at the start of every request. Using the tracer, this can be achieved like this:

res.write = tracer.wrap('http.write', res.write)

This should be done before any other middleware has the chance to write data.

Example middleware:

app.use((req, res) => {
  res.write = tracer.wrap('http.write', res.write)
})

Are there any pitfalls or inaccuracies involved when trying to do this kind of instrumentation?

Nothing major that I can think of.

Does anybody know why this is not measured by APM client libraries by default?

The main issue for doing this out of the box is that creating a span for every call to res.write may be expensive if there are too many calls. If you think it would make sense to have an option to do this out of the box, we can definitely consider adding that.

Hope this helps!

like image 170
rochdev Avatar answered Nov 01 '22 10:11

rochdev