Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Insights - how to use ISupportProperties in TelemetryInitializer

I have a custom AI Telemetry Initializer. Recently, I have started getting a compiler warning, saying Context.Properties is obsolete: "TelemetryContext.Properties is obsolete. Use GlobalProperties to set global level properties. For properties at item level, use ISupportProperties.Properties. 1. How do I do that? Use ISupportProperties.Properties? 2. I am logging the tenant Id, and deviceID, from claims in the principal, per request. Is that considered app global or a support property?

    public class JsonTelemetryInitializer : ITelemetryInitializer
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly SystemOptions _systemOptions;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="accessor">For accessing the http context</param>
        /// <param name="systemOptions">System options</param>
        public JsonTelemetryInitializer(IHttpContextAccessor accessor, IOptions<SystemOptions> systemOptions)
        {
            _httpContextAccessor = accessor;
            _systemOptions = systemOptions.Value;
        }

        /// <summary>
        /// Initialize the custom telemetry initializer
        /// </summary>
        /// <param name="telemetry">Telemetry</param>
        public void Initialize(ITelemetry telemetry)
        {
            if (_httpContextAccessor.HttpContext == null)
            {
                return;
            }

            if (_httpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
            {
                const string tenantId = "northstar_tenantid";
                if (!telemetry.Context.Properties.ContainsKey(tenantId))
                {
                    var user = _httpContextAccessor.HttpContext.User;
                    telemetry.Context.Properties[tenantId] =
                        ClaimsHelper.GetClaim<int>(user, TokenNames.TenantId).ToString();

                    var userId = ClaimsHelper.GetClaim<int>(user, TokenNames.UserId).ToString();

                    telemetry.Context.Properties["northstar_userid"] = userId;
                    var deviceId = ClaimsHelper.GetClaim<string>(user, TokenNames.DeviceId);
                    if (deviceId != null)
                    {
                        telemetry.Context.Properties["northstar_deviceid"] = deviceId;
                    }

                    telemetry.Context.User.Id = userId;

                    var sessionId = ClaimsHelper.GetClaim<string>(user, TokenNames.SessionId);
                    if (!string.IsNullOrEmpty(sessionId))
                    {
                        telemetry.Context.Session.Id = sessionId;
                    }
                }
            }
        }
    }
like image 906
John Waters Avatar asked Oct 15 '18 17:10

John Waters


2 Answers

I had a similar problem and find this post. They added documentation for adding properties to TelemetryContext. Simply cast ITelemetry to RequestTelemetry.

public void Initialize(ITelemetry telemetry)
{
    var requestTelemetry = telemetry as RequestTelemetry;
    
    if (requestTelemetry == null) return;

    requestTelemetry.Properties["customProp"] = "test";
}

https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer

like image 187
Ivana Trošić Avatar answered Sep 24 '22 13:09

Ivana Trošić


1.How do I do that? Use ISupportProperties.Properties?

Just cast the ITelemetry to ISupportProperties.

My example code as below:

  using Microsoft.ApplicationInsights.Channel;
  using Microsoft.ApplicationInsights.DataContracts;
  using Microsoft.ApplicationInsights.Extensibility;

  public class MyTelemetryInitializer:ITelemetryInitializer
  {

     public void Initialize(ITelemetry telemetry)
     {
          //cast ITelemetry to ISupportProperties
          ISupportProperties propTelemetry = (ISupportProperties)telemetry;

          if (!propTelemetry.Properties.ContainsKey("isuport_key"))
          {
              propTelemetry.Properties.Add("isuport_key", "isupport_value");
          }

      }
  }

Then after execution, the property is shown in azure portal, screenshot as below: enter image description here

2.I am logging the tenant Id, and deviceID, from claims in the principal, per request. Is that considered app global or a support property?

As per my understanding, you can use support property(item level) since it's similar with you did previously using like this telemetry.Context.Properties["northstar_deviceid"] = deviceId;

like image 41
Ivan Yang Avatar answered Sep 24 '22 13:09

Ivan Yang