DEV Community

Cover image for Application Insights SDK for Node.js part 4 : Out of box telemetries - Basics and Console
Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Application Insights SDK for Node.js part 4 : Out of box telemetries - Basics and Console

In the previous article, I looked into Application Map which illustrate my entire application components. In this article, I go back to Node.js SDK and look into out of box telemetries.

setup/start function

At the beginning of the application, I initialize the application insight client instance and call setup and start function.

var appInsights = require("applicationinsights");

When I look at GitHub: ApplicationInsights.ts, it setup out of box telemetries and starts colleting them.

export function setup(instrumentationKey?: string) {
    if(!defaultClient) {
        defaultClient = new TelemetryClient(instrumentationKey);
        _console = new AutoCollectConsole(defaultClient);
        _exceptions = new AutoCollectExceptions(defaultClient);
        _performance = new AutoCollectPerformance(defaultClient);
        _serverRequests = new AutoCollectHttpRequests(defaultClient);
        _clientRequests = new AutoCollectHttpDependencies(defaultClient);
        if (!_nativePerformance) {
            _nativePerformance = new AutoCollectNativePerformance(defaultClient);
    } else {"The default client is already setup");

    if (defaultClient && {, _diskRetryInterval, _diskRetryMaxBytes);

    return Configuration;

export function start() {
    if(!!defaultClient) {
        _isStarted = true;
        _console.enable(_isConsole, _isConsoleLog);
        _nativePerformance.enable(_isNativePerformance, _disabledExtendedMetrics);
        _serverRequests.useAutoCorrelation(_isCorrelating, _forceClsHooked);
        if (liveMetricsClient && _isSendingLiveMetrics) {
    } else {
        Logging.warn("Start cannot be called before setup");

    return Configuration;

Basically, there are six sets of auto collect.

The class also provides setAutoCollectXXX functions enable each module.
One thing to note is that "console collect" is not enabled by default and you have to explicitly enables it. I will explain this later.

Before looking into each of them, there is one environment settings I need to check.

Enable diagnostic

According to README, I can set APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL environment variable to true to enable third-party instrumentations.

These is an interesting bug in Visual Studio Code which always sets the value to true. You can find the issue here

So I explicitly delete the environment variable at the beginning of my code to enable the diagnostic. Don't set it false, as it only set the value as string and it never meet the if statement. Simply delete it will do the trick.



In the enable function of Console, it basically enables some of "diagnostic-channel" modules which I can find here.

This uses Diagnostic Channel Publishers which let client subscribe events.


Console.sub.ts collects event messages, which TelemetryClient uses trackTrace to trach them. But as I said earlier, I need to explicitly call setAutoCollectConsole function to enable this.

So I changed my code like below.

var appInsights = require("applicationinsights");
    .setAutoCollectConsole(true, true)

Once I enable this, any function such as console.log() are tracked to Application Insights.

I then added console.log to one of endpoint.

app.get('/', (req, res) => {
    let customMetric = Math.random() * 50 + 50;
    client.trackEvent({ name: "my custom event", properties: { customProperty: "my custom event" } });
    client.trackMetric({ name: "custom metric", value: customMetric}); // generate metric [50, 100]
    console.log("/ was called");

When I execute it, then I can see the log in Application Insights.
Alt Text

bunyan and winston

Simlar to console, bunyan and winston are both node.js logging library. I installed bunyan and added logger and log information in "ordersfail" endpoint.

var bunyan = require("bunyan");
var log = bunyan.createLogger({ name: "bunynLogger" });

app.get("/ordersfail", (req, res) => {
    });"logged in bunyan.");
    res.send("something went wrong :(");

And it logs like below.
Alt Text

Console logging : Traces and Errors

According to the console.sub.ts in develop branch, when Application Insights tracks console events, it tracks as either Trace or Exception. However when I look publisher, it always publish the message content as string object, thus I don't know if out of box console has feature to log it as Exception. So far all the traces in my environment goes to Traces.


It's handy to see console.log (or any other logging tool output) information in Azure Monitor for monitoring and analyzing to see how is my application doing. I can also set Alert from there when specific message is logged.

In the next article, I will explain Exception log.

Go to next article

Top comments (0)