Setting cloud role name for Application Insights customEvent (health check, snapshot collector) and customMetric telemetry events

Matt Buckleman 0 Reputation points
2023-10-05T19:54:20.2466667+00:00

Situation:

  • Azure App Service on Windows, running .net core 3.1 apps (web and API)
  • Running on 4 separate app service plans in 4 regions behind Azure FD
  • Logging app insights through the package in our actual code (not just turning it on in the app service itself)

Background

  • We have our own ITelemetryInitializer in our code and are setting the role name via telemetry.Context.Cloud.RoleName
  • Most app insights events respect and use the role name we set, including any custom events we send

Problem

  • However, there are things that the app service itself seems to send that do not. Specifically, the app insights data for customMetrics (e.g. "System.Runtime|CPU Usage" and other performance metrics) and customEvents (e.g. "AppInsightsSnapshotCollectorLogs") that are Not sent by our own code do not use the role name we have set
  • This makes sense as they would not go through our telemetry initializer, but we still need to be able to set the role name somehow
  • I need to be able to do this via env vars so I can set them on a per-slot basis. This has already been a real headache because our staging slots were pinging our production app insights instance with lots of data.
  • There is no documentation on how to disable, for example, snapshot debugger on a per-slot basis

Attempted solutions:

I have tried setting environment vars in the azure portal using these vars:

  • ApplicationInsights_CloudRoleName
  • WEBSITE_CLOUD_ROLENAME
  • APPLICATIONINSIGHTS_ROLE_NAME

I am assuming it is the app service framework that is logging these, and it is therefore using WEBSITE_HOSTNAME - but that cannot be set via the azure portal.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,037 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,408 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 27,111 Reputation points Microsoft Employee
    2023-10-12T02:00:47.2366667+00:00

    Hi @Matt Buckleman

    I think you should be able to disable the snapshot debugger via slot through configuration settings. Easiest way is add:

    {
      "ApplicationInsights": {
        "SnapshotCollector": {
          "IsEnabled": false
        }
      }
    }
    

    to an appsettings.<slot>.json file or pin ApplicationInsights:SnapshotCollector:IsEnabled app setting to the preferred slot. customMetrics and customEvent is not part of the ITelemetryInitializer pipeline. To still use a cloud role name, you need to go the SDK route and disable auto instrumentation. Use TelemetryClient.Track() to send the custom metric:

    var telemetry = new MetricTelemetry("MyCustomMetric", 42);
    telemetry.Context.Cloud.RoleName = "MyCustomRoleName";
    TelemetryClient.Track(telemetry);
    
    0 comments No comments