Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer.OnInitializeTelemetry C# (CSharp) Method

OnInitializeTelemetry() protected method

Implements initialization logic.
protected OnInitializeTelemetry ( HttpContext platformContext, Microsoft.ApplicationInsights.DataContracts.RequestTelemetry requestTelemetry, ITelemetry telemetry ) : void
platformContext System.Web.HttpContext Http context.
requestTelemetry Microsoft.ApplicationInsights.DataContracts.RequestTelemetry Request telemetry object associated with the current request.
telemetry ITelemetry Telemetry item to initialize.
return void
        protected override void OnInitializeTelemetry(
            HttpContext platformContext,
            RequestTelemetry requestTelemetry,
            ITelemetry telemetry)
        {
            OperationContext parentContext = requestTelemetry.Context.Operation;

            // Make sure that RequestTelemetry is initialized.
            if (string.IsNullOrEmpty(parentContext.ParentId))
            {
                if (!string.IsNullOrWhiteSpace(this.ParentOperationIdHeaderName))
                {
                    var parentId = platformContext.Request.UnvalidatedGetHeader(this.ParentOperationIdHeaderName);
                    if (!string.IsNullOrEmpty(parentId))
                    {
                        parentContext.ParentId = parentId;
                    }
                }
            }

            if (string.IsNullOrEmpty(parentContext.Id))
            {
                if (!string.IsNullOrWhiteSpace(this.RootOperationIdHeaderName))
                {
                    var rootId = platformContext.Request.UnvalidatedGetHeader(this.RootOperationIdHeaderName);
                    if (!string.IsNullOrEmpty(rootId))
                    {
                        parentContext.Id = rootId;
                    }
                }

                if (string.IsNullOrEmpty(parentContext.Id))
                {
                    parentContext.Id = requestTelemetry.Id;
                }
            }

            if (telemetry != requestTelemetry)
            {
                if (string.IsNullOrEmpty(telemetry.Context.Operation.ParentId))
                {
                    telemetry.Context.Operation.ParentId = requestTelemetry.Id;
                }

                if (string.IsNullOrEmpty(telemetry.Context.Operation.Id))
                {
                    telemetry.Context.Operation.Id = parentContext.Id;
                }
            }
        }