Skip to main content
Version: 4.10.0

Custom Lifecycle Events

Implementing Custom Lifecycle Events

In addition to the built-in lifecycle events, you can add your own using the process.recordStartupTime(label) function. The label will then be used in the JSON output of the startup-times command.

You can use this to record the times at various stages of your application's startup. For instance:

  • when a database connection is requested
  • when the database connection is returned
  • when an http server is ready for incoming events

Using the CLI

To obtain the startup timing values, you can use the nsolid-cli startup-times command.

N|Solid provides three startup times by default via startup-times:

ParameterDescription
initialized_nodeThe time it takes to initialize the Node internals, reported in [seconds, nanoseconds]
initialized_v8The time it takes to initialize the V8 engine, reported in [seconds, nanoseconds]
loaded_environmentThe time it takes to complete all initialization, which includes running some of Node's internal JavaScript code, and your main module's top-level code, reported in [seconds, nanoseconds]

Usage

$ nsolid-cli startup-times

Example JSON Result

{
"id": "bf24f4ed072b3bb4b220aa81fa3a73fde8038409",
"app": "MyApp",
"hostname": "myApp.example.com",
"initialized_node": [ 0, 130404 ],
"initialized_v8": [ 0, 482651 ],
"loaded_environment": [ 0, 620207709 ]
}

This indicates that Node was initialized in 130,404 nanoseconds (which is 130 microseconds, 0.130 milliseconds, or 0.000130 seconds).

Note: The timing information is provided in hrtime format, which is a two element array of [seconds, nanoseconds]. A nanosecond is one billionth (1,000,000,000th) of a second. The time values are the elapsed time since the process was started.

Example: Adding A Custom Timer

Below is an example web server instrumented to provide the time it takes for the web server to start listening to connections:

# http_sample.js

const http = require("http")

const server = http.createServer(onRequest)

server.listen(3000, onListen)

function onListen() {
console.log("server listening on http://localhost:3000")
process.recordStartupTime("http_listen")
}

function onRequest(request, response) {
response.end("Hello, world!")
}

To start this program with the N|Solid agent listening on port 5000, use the following command:

$ NSOLID_APPNAME=http-sample nsolid http_sample.js

To obtain the startup times, including the custom timer, use the following command:

$ nsolid-cli startup-times --app http-sample

Example JSON Result

{
"id": "bf24f4ed072b3bb4b220aa81fa3a73fde8038409",
"app": "http-sample",
"hostname": "http-sample.example.com",
"initialized_node": [ 0, 129554 ],
"initialized_v8": [ 0, 460521 ],
"loaded_environment": [ 0, 95201339 ],
"http_listen": [ 0, 94902772 ]
}