private RequestTelemetry InitializeRequestTelemetry(HttpContext httpContext, Activity activity, bool isActivityCreatedFromRequestIdHeader, long timestamp)
        {
            var requestTelemetry = new RequestTelemetry();

            StringValues standardParentId;

            if (isActivityCreatedFromRequestIdHeader)
            {
                requestTelemetry.Context.Operation.ParentId = activity.ParentId;

                foreach (var prop in activity.Baggage)
                {
                    if (!requestTelemetry.Context.Properties.ContainsKey(prop.Key))
                    {
                        requestTelemetry.Context.Properties[prop.Key] = prop.Value;
                    }
                }
            }
            else if (httpContext.Request.Headers.TryGetValue(RequestResponseHeaders.StandardParentIdHeader, out standardParentId))
            {
                standardParentId = StringUtilities.EnforceMaxLength(standardParentId, InjectionGuardConstants.RequestHeaderMaxLength);
                requestTelemetry.Context.Operation.ParentId = standardParentId;
            }

            requestTelemetry.Id = activity.Id;
            requestTelemetry.Context.Operation.Id = activity.RootId;

            this.client.Initialize(requestTelemetry);

            // set Source
            string headerCorrelationId = HttpHeadersUtilities.GetRequestContextKeyValue(httpContext.Request.Headers, RequestResponseHeaders.RequestContextSourceKey);

            string applicationId = null;

            // If the source header is present on the incoming request, and it is an external component (not the same ikey as the one used by the current component), populate the source field.
            if (!string.IsNullOrEmpty(headerCorrelationId))
            {
                headerCorrelationId = StringUtilities.EnforceMaxLength(headerCorrelationId, InjectionGuardConstants.AppIdMaxLengeth);
                if (string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey))
                {
                    requestTelemetry.Source = headerCorrelationId;
                }

                else if ((this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false) &&
                         applicationId != headerCorrelationId)
                {
                    requestTelemetry.Source = headerCorrelationId;
                }
            }

            requestTelemetry.Start(timestamp);
            httpContext.Features.Set(requestTelemetry);

            return(requestTelemetry);
        }
        private string GetAppIdFromRequestHeader(IHeaderDictionary requestHeaders, string instrumentationKey)
        {
            // set Source
            string headerCorrelationId = HttpHeadersUtilities.GetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextSourceKey);

            // If the source header is present on the incoming request, and it is an external component (not the same ikey as the one used by the current component), populate the source field.
            if (!string.IsNullOrEmpty(headerCorrelationId))
            {
                headerCorrelationId = StringUtilities.EnforceMaxLength(headerCorrelationId, InjectionGuardConstants.AppIdMaxLength);
                if (string.IsNullOrEmpty(instrumentationKey))
                {
                    return(headerCorrelationId);
                }

                string applicationId = null;
                if ((this.applicationIdProvider?.TryGetApplicationId(instrumentationKey, out applicationId) ?? false) &&
                    applicationId != headerCorrelationId)
                {
                    return(headerCorrelationId);
                }
            }

            return(null);
        }