Edit

Share via


Enable telemetry for feature flags in a Node.js application

In this tutorial, you use telemetry in your Node.js application to track feature flag evaluations and custom events. Telemetry allows you to make informed decisions about your feature management strategy. You utilize the feature flag with telemetry enabled created in the overview for enabling telemetry for feature flags. Before proceeding, ensure that you create a feature flag named Greeting in your Configuration store with telemetry enabled. This tutorial builds on top of the tutorial for using variant feature flags in a Node.js application.

Prerequisites

Add telemetry to your Node.js application

  1. Install the following packages.

    npm install @microsoft/feature-management-applicationinsights-node
    
  2. Open server.js and add the following code in the beginning to connect to Application Insights to publish telemetry.

    const appInsightsConnectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING;
    const applicationInsights = require("applicationinsights");
    applicationInsights.setup(appInsightsConnectionString).start();
    
    const express = require("express");
    const server = express();
    // existing code ...
    
  3. Add the following import.

    const { createTelemetryPublisher, trackEvent } = require("@microsoft/feature-management-applicationinsights-node");
    
  4. Create and register the telemetry publisher when initializing the FeatureManager.

    // existing code ...
    const featureFlagProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
    const publishTelemetry = createTelemetryPublisher(applicationInsights.defaultClient);
    featureManager = new FeatureManager(featureFlagProvider, {
        onFeatureEvaluated: publishTelemetry
    });
    // existing code ...
    

    The publishTelemetry callback will send telemetry data each time a feature flag is evaluated.

  5. Track custom events for user interactions. Modify the /api/like endpoint to send telemetry data to Application Insights whenever a user likes content. This helps you analyze which feature variants perform better.

    server.post("/api/like", (req, res) => {
        const { UserId } = req.body;
        if (UserId === undefined) {
            return res.status(400).send({ error: "UserId is required" });
        }
        trackEvent(applicationInsights.defaultClient, UserId, { name: "Liked" });
        res.status(200).send();
    });
    

Run the application

  1. Application insights requires a connection string to connect to your Application Insights resource. Set the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable to the connection string for your Application Insights resource.

    setx APPLICATIONINSIGHTS_CONNECTION_STRING "applicationinsights-connection-string"
    

    If you use PowerShell, run the following command:

    $Env:APPLICATIONINSIGHTS_CONNECTION_STRING = "applicationinsights-connection-string"
    

    If you use macOS or Linux, run the following command:

    export APPLICATIONINSIGHTS_CONNECTION_STRING='applicationinsights-connection-string'
    

Collect telemetry

Deploy your application to begin collecting telemetry from your users. To test its functionality, you can simulate user activity by creating many test users. Each user will experience a different variant of greeting messages, and they can interact with the application by clicking the heart button to like a quote. As your user base grows, you can monitor the increasing volume of telemetry data collected in Azure App Configuration. Additionally, you can drill down into the data to analyze how each variant of the feature flag influences user behavior.

Additional resources