DEV Community

Cover image for Application Insights SDK for Node.js part 2 : Track events
Kenichiro Nakamura
Kenichiro Nakamura

Posted on • Edited on

Application Insights SDK for Node.js part 2 : Track events

In the previous article, I explain how Application Insights SDK for Node.js sends telemetry data to servers. In this article, I demonstrate it.

Sample application

It's super straight forward sample. Just create new node.js app and use following code as index.js

var appInsights = require('applicationinsights');
appInsights.setup('<instrumentation_key>').start();
var client = appInsights.defaultClient;
appInsights.defaultClient.addTelemetryProcessor(envelope => {
    envelope.tags["ai.cloud.role"] = "myapp";
    envelope.tags["ai.cloud.roleInstance"] = "myapp1"
});

var express = require('express');
var app = express();

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]

    res.send(customMetric.toString());
});

app.listen(8888);

process.on('SIGINT', (code) => {
    setTimeout(() => {
        client.flush({ isAppCrashing: true });
        console.log("flushed");
        process.exit();
    }, 0);
});

Once you start the application, access to http://localhost:8888 and you will see the result.

Normal behavior

When everything goes fine, both event log and metric are sent and stored in the Application Insights Server. As you see in the screenshot below, you can filter results by | where cloud_RoleName contains "myapp"

Alt Text

When terminate the application by Ctrl+C

As explained in the previous article, the SDK doesn't send each event immediately. This infers there is some chance to loose the data if the Node.js application is exit and if you don't flush the data.

I use process.on to test the behavior.

1. Start the application in console.

2. Go to http://localhost:8888 and generate some events.

3. Terminate the application by Ctrl+C.

4. You see cache is written to disk. The path is path.join(os.tmpdir(), Sender.TEMPDIR_PREFIX + this._config.instrumentationKey)

5. Now restart the Node.js application to see the data is saved to cloud later. It may take a bit of time depending on how many data you cached.

When network is down

Even though my wifi is off, I still can access to my server locally, which generates telemetry.

In this case, the behavior is similar to previous scenario, which cache the data to disk.

Re-write the data

While SDK cannot access to the internet, I have an opportunity to manually tweak the file. This time, I just modified the data from "my custom event" to "my custom event offline" for fun. After reconnecting to the internet, the data is stored as expected. *Don't mind the timestamp as I tested this earlier than previous scenario :)

Summary

The SDK behave exactly as I expected. Still there are several things to keep in mind.

  • Handle some event explicitly (Exception will be handled by default)
  • There is a minute interval for each file to be uploaded by default, so it may take a bit of time.

I talk about application map next.

Go to next article

Top comments (0)