API Application Insights good practice to use

前端 未结 2 1104
梦谈多话
梦谈多话 2021-01-19 11:42

I read this documentation: https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics

There are many different API method to

2条回答
  •  不思量自难忘°
    2021-01-19 12:21

    This is the best practice for 2.5.1 AI SDK. Will highlight parts which might not be required in upcoming AI SDK releases.

    The right way to do end-to-end tracing is to rely on new Activity class in .NET framework. Until AI supports Activity.Tags (https://github.com/Microsoft/ApplicationInsights-dotnet/issues/562) you need to propagate them manually using TelemetryInitializer:

    public class ActvityTagsTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            Activity current = Activity.Current;
    
            if (current == null)
            {
                current = (Activity)HttpContext.Current?.Items["__AspnetActivity__"];
            }
    
            while (current != null)
            {
                foreach (var tag in current.Tags)
                {
                    if (!telemetry.Context.Properties.ContainsKey(tag.Key))
                    {
                        telemetry.Context.Properties.Add(tag.Key, tag.Value);
                    }
                }
    
                current = current.Parent;
            }
        }
    }
    

    Then register it in ApplicationInsights.config:

      
        ...
        
      
    

    Then you can populate proper tags:

    [AjaxErrorHandling]
    [HttpPost]
    public async Task SyncDriverToVistracks(int DriverID)
    {
        Activity.Current.AddTag("DriverID", DriverID.ToString());
        Activity.Current.AddTag("UserID", User.Identity.Name);
    
        try
        {
            if ([condition])
            {
                // some actions here
    
                try
                {
                    // If below call is HTTP then no need to use StartOperation
                    using (telemetryClient.StartOperation("AddNewDriverToVistrackAsync"))
                    {
                        driver.VistrackId = await _vistracksService.AddNewDriverToVistrackAsync(domain);
                    }
    
                    // If below call is HTTP then no need to use StartOperation
                    using (telemetryClient.StartOperation("SaveChanges"))
                    {
                        await db.SaveChangesAsync();
                    }
                }
                catch (VistracksApiException api_ex)
                {
                    // external service throws exception type VistracksApiException 
                    throw new AjaxException("vistracksApiClient", api_ex.Response.Message);
                }
                catch (VistracksApiCommonException common_ex)
                {
                    // external service throws exception type VistracksApiCommonException 
                    throw new AjaxException("vistracksApiServer", "3MD HOS server is not available");
                }
                catch (Exception ex)
                {
                    // something wrong at all
                    throw new AjaxException("General", ex.Message);
                }
            }
            else
            {
                // condition is not valid
                throw new AjaxException("General", "AccountId is not found");
            }
        }
        catch (Exception ex)
        {
            // Upcoming 2.6 AI SDK will track exceptions for MVC apps automatically.
            telemetryClient.TrackException(ex);
            throw;
        }
    }
    

    You should have the following telemetry:

    1. Incoming request
    2. Outgoing requests (dependencies)
    3. Exceptions for failed requests

    All telemetry will be stamped with ChangedBy and DriverID

提交回复
热议问题