Enriching AppInsights Telemetry with additional information

We are running Sitecore 10 in containers, and we have multiple environments. We’ve also got multiple server roles. We’d like our telemetry to App Insights to tell us a) what instance is this for, and b) what role is this for.

We can achieve this with a TelemetryInitializer…

public class TelemetryEnrichment : ITelemetryInitializer
{
  private static Dictionary<string, string> values = null;

  public void Initialize(ITelemetry telemetry)
  {
    ISupportProperties props = telemetry as ISupportProperties;

    if (props != null) {
      if (values == null)
      {
        values = new Dictionary<string, string>();
        values.Add("Sitecore_Role", System.Configuration.ConfigurationManager.AppSettings["role:define"]);
        values.Add("Sitecore_Environment", Environment.GetEnvironmentVariable("SITECORE_ENVIRONMENT_TYPE") ?? "None");
      }
      foreach(KeyValuePair<string,string>kv in values) 
      {
        if (!props.Properties.ContainsKey(kv.Key))
        {
          props.Properties.Add(kv.Key, kv.Value);
        }
      }
    }
  }
}

This class will populate a dictionary with some additional properties, and then add those to telemetry being processed.

Enriching AppInsights Telemetry with additional information

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.