# Observing Node.js ### Efforts to improve the observability of Node.js @jasnell https://jasnell.github.io/nodesummit2018/observing/#/
## What's happening inside?

Monitoring

to watch, observe, or check closely
or continuously.

Source: Merriam-Webster

Profiling

to give a brief description that provides
information about [something].

Source: Merriam-Webster

Tracing

a mark left by something that has passed or is past.

to follow the footprints of, track, or trail of.

Source: Merriam-Webster

Monitoring

Monitoring Node.js applications is
a production concern...

  • Security
  • Performance
  • Health
  • Service Level

Monitoring

There are new Node.js core modules
intended to help make monitoring
easier in the future.

  • async_hooks
  • perf_hooks
  • trace_events

async_hooks

Tracking asynchronous context...


const { createHook } = require('async_hooks')

const asyncHook = createHook({
  init(id, type, triggerAsyncId, resource) { },
  before(id) { },
  after(id) { },
  destroy(id) { },
  promiseResolve(id) { }
})

asyncHook.enable()

async_hooks

Provides an AsyncResource class.
There are two kinds of resources:

  • A handle is a long-lived thing
    that has async activity over time.
  • A request is a short-lived request
    to do one asynchronous thing.

async_hooks

Demo!

perf_hooks

Ad hoc timing metrics


const { PerformanceObserver, performance } = require('perf_hooks')

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration)
})

obs.observe({ entryTypes: ['measure'] })

performance.mark('A')

doSomeLongRunningProcess(() => {
  performance.mark('B')
  performance.measure('A to B', 'A', 'B')
})

perf_hooks

Demo!

perf_hooks

This is the same Performance API
implemented by browsers, with a few
Node.js specific additions.

  • User timing marks
  • Node.js startup timing
  • Garbage collection timing
  • HTTP2 performance
  • More to come...

trace_events


const { createTracing } = require('trace_events')
const categories = [ 'node.perf', 'node.async_hooks' ]

const tracing = createTracing({ categories })

tracing.enable()
// do stuff
tracing.disable()

trace_events


const CDP = require('chrome-remote-interface')

const client = await CDP({port: args.port})
client.NodeTracing.tracingComplete(() => client.close())
client.on('event', (message) => { })

await client.NodeTracing.start({
  traceConfig: {
    recordMode: 'recordContinuously',
    includedCategories: ['node'],
  }
})

setTimeout(() => client.NodeTracing.stop(), 10000).unref()

Profiling

Profiling involves collecting
and analyzing performance and
operation metrics for discreet
parts of a Node.js app within
a fixed period of time.

Clinic Flamegraphs









$ npm i -g clinic
$ clinic flame -- node some.application.js

--prof
--prof-process

















$ npm i -g clinic
$ clinic doctor -- node some.application.js








$ npm i -g clinic
$ clinic bubbleprof -- node some.application.js

Profiling

Profiling tools build on primitives
like async_hooks, trace events,
and --prof to build a snapshot of
what your application is doing
at a given point in time.

Tracing

Tracing is a record of what is happening
within the process.

Current efforts with Node.js are focusing
on the platform independent
V8 Trace Events Format.

Tracing


{ traceEvents: [{
    pid: 22893,
    tid: 22893,
    ts: 110377229940,
    tts: 164935,
    ph: 'b',
    cat: 'node,node.async_hooks',
    name: 'TickObject_CALLBACK',
    dur: 0,
    tdur: 0,
    id: '0x25',
    args: {}
  ]
},

Tracing

We are actively expanding the
information emitted as trace events.

  • Process and Thread metadata
  • Async flow (using async_hooks)
  • Transaction detail (e.g. http requests, dns queries)
  • Sync operations
  • Performance

Tracing

Demo!

Come Help Us!

Node.js Diagnostics Working Group

http://github.com/nodejs/diagnostics

Thank you.

Come talk to us if you need help with your Node.js

- Twitter: [@jasnell](https://twitter.com/jasnell) - GitHub: [@jasnell](https://github.com/jasnell)