private void SetAppIdInResponseHeader(HttpContext httpContext, RequestTelemetry requestTelemetry) { if (this.injectResponseHeaders) { IHeaderDictionary responseHeaders = httpContext.Response?.Headers; if (responseHeaders != null && !string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && (!responseHeaders.ContainsKey(RequestResponseHeaders.RequestContextHeader) || HttpHeadersUtilities.ContainsRequestContextKeyValue( responseHeaders, RequestResponseHeaders.RequestContextTargetKey))) { if (this.lastIKeyLookedUp != requestTelemetry.Context.InstrumentationKey) { var appIdResolved = this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out this.lastAppIdUsed); if (appIdResolved.HasValue && appIdResolved.Value) { this.lastIKeyLookedUp = requestTelemetry.Context.InstrumentationKey; } } HttpHeadersUtilities.SetRequestContextKeyValue( responseHeaders, RequestResponseHeaders.RequestContextTargetKey, this.lastAppIdUsed); } } }
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 void SetAppIdInResponseHeader(HttpContext httpContext, RequestTelemetry requestTelemetry) { IHeaderDictionary responseHeaders = httpContext.Response?.Headers; if (responseHeaders != null && !string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && (!responseHeaders.ContainsKey(RequestResponseHeaders.RequestContextHeader) || HttpHeadersUtilities.ContainsRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey))) { string applicationId = null; if (this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false) { HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, applicationId); } } }
private void SetAppIdInResponseHeader(HttpContext httpContext, RequestTelemetry requestTelemetry) { IHeaderDictionary responseHeaders = httpContext.Response?.Headers; if (responseHeaders != null && !string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && (!responseHeaders.ContainsKey(RequestResponseHeaders.RequestContextHeader) || HttpHeadersUtilities.ContainsRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey))) { string correlationId = null; if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out correlationId)) { HttpHeadersUtilities.SetRequestContextKeyValue(responseHeaders, RequestResponseHeaders.RequestContextTargetKey, correlationId); } } }
private void SetW3CContext(IHeaderDictionary requestHeaders, Activity activity, out string sourceAppId) { sourceAppId = null; if (requestHeaders.TryGetValue(W3C.W3CConstants.TraceParentHeader, out StringValues traceParentValues)) { var parentTraceParent = StringUtilities.EnforceMaxLength( traceParentValues.First(), InjectionGuardConstants.TraceParentHeaderMaxLength); activity.SetTraceparent(parentTraceParent); } string[] traceStateValues = HttpHeadersUtilities.SafeGetCommaSeparatedHeaderValues( requestHeaders, W3C.W3CConstants.TraceStateHeader, InjectionGuardConstants.TraceStateHeaderMaxLength, InjectionGuardConstants.TraceStateMaxPairs); if (traceStateValues != null && traceStateValues.Any()) { var pairsExceptAz = new StringBuilder(); foreach (var t in traceStateValues) { if (t.StartsWith(W3C.W3CConstants.AzureTracestateNamespace + "=", StringComparison.Ordinal)) { // start after 'az=' TryExtractAppIdFromAzureTracestate(t.Substring(3), out sourceAppId); } else { pairsExceptAz.Append(t).Append(','); } } if (pairsExceptAz.Length > 0) { // remove last comma var tracestateStr = pairsExceptAz.ToString(0, pairsExceptAz.Length - 1); activity.SetTracestate(StringUtilities.EnforceMaxLength(tracestateStr, InjectionGuardConstants.TraceStateHeaderMaxLength)); } } ReadCorrelationContext(requestHeaders, activity); }
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); }