dd-trace-js
                                
                                
                                
                                    dd-trace-js copied to clipboard
                            
                            
                            
                        move runtime metrics to a worker thread
What does this PR do?
First, all the existing thread-specific runtime metrics that were
retrieved via JS were moved to C++, so that we're avoiding any usage of
the VM on the main thread when we can avoid it. These metrics are then
retrieved via stats() the same way the other C++ metrics were already
being retrieved.
Next, all the metric retrieval (e.g. the call to stats()) and flushing
logic is done on a worker thread when possible. Metrics that are not
thread-specific are retrieved via JS calls on the worker thread where
possible.
When main-thread code adds metric data, it's sent to the worker thread for inclusing in guages, counters and histograms, since that's where that data will be flushed anyway.
The worker thread dies on stop(). If worker threads aren't available,
all the logic happens on the main thread.
Motivation
The primary effect of this change is that if the event loop is blocked on the main thread, the flushing of runtime metrics is not blocked, though collection may be, because V8 Isolate data APIs are not thread safe, so they're done on the main thread. Since the encoding and flushing of metrics is now done on the worker thread, we can reduce the interval length between metric collections, so now when the worker thread is used, the interval is 1 second rather than the previous 10 seconds.