private void ParseResponse(HttpResponseMessage response, DependencyTelemetry telemetry)
        {
            try
            {
                string targetApplicationId = HttpHeadersUtilities.GetRequestContextKeyValue(response.Headers, RequestResponseHeaders.RequestContextCorrelationTargetKey);
                if (!string.IsNullOrEmpty(targetApplicationId) && !string.IsNullOrEmpty(telemetry.Context.InstrumentationKey))
                {
                    // We only add the cross component correlation key if the key does not represent the current component.
                    string sourceApplicationId;
                    if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) &&
                        targetApplicationId != sourceApplicationId)
                    {
                        telemetry.Type    = RemoteDependencyConstants.AI;
                        telemetry.Target += " | " + targetApplicationId;
                    }
                }
            }
            catch (Exception e)
            {
                AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
            }

            int statusCode = (int)response.StatusCode;

            telemetry.ResultCode = (statusCode > 0) ? statusCode.ToString(CultureInfo.InvariantCulture) : string.Empty;
            telemetry.Success    = (statusCode > 0) && (statusCode < 400);
        }
Ejemplo n.º 2
0
        private void InjectRequestHeaders(HttpRequestMessage request, string instrumentationKey)
        {
            try
            {
                HttpRequestHeaders requestHeaders = request.Headers;
                if (requestHeaders != null && this.setComponentCorrelationHttpHeaders && !this.correlationDomainExclusionList.Contains(request.RequestUri.Host))
                {
                    string sourceApplicationId = null;
                    try
                    {
                        if (!string.IsNullOrEmpty(instrumentationKey) &&
                            !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey) &&
                            (this.configuration.ApplicationIdProvider?.TryGetApplicationId(instrumentationKey, out sourceApplicationId) ?? false))
                        {
                            HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId);
                        }
                    }
                    catch (Exception e)
                    {
                        AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
                    }

                    var currentActivity = Activity.Current;

                    switch (this.httpInstrumentationVersion)
                    {
                    case HttpInstrumentationVersion.V1:
                        // HttpClient does not add any headers
                        // add W3C or Request-Id depending on Activity format
                        // add correlation-context anyway
                        if (currentActivity.IdFormat == ActivityIdFormat.W3C)
                        {
                            InjectW3CHeaders(currentActivity, requestHeaders);
                            if (this.injectRequestIdInW3CMode)
                            {
                                InjectBackCompatibleRequestId(currentActivity, requestHeaders);
                            }
                        }
                        else
                        {
                            if (!requestHeaders.Contains(RequestResponseHeaders.RequestIdHeader))
                            {
                                requestHeaders.Add(RequestResponseHeaders.RequestIdHeader, currentActivity.Id);
                            }
                        }

                        InjectCorrelationContext(requestHeaders, currentActivity);
                        break;

                    case HttpInstrumentationVersion.V2:
                        // On V2, HttpClient adds Request-Id and Correlation-Context
                        // but not W3C
                        if (currentActivity.IdFormat == ActivityIdFormat.W3C)
                        {
                            // we are going to add W3C and Request-Id (in W3C-compatible format)
                            // as a result HttpClient will not add Request-Id AND Correlation-Context
                            InjectW3CHeaders(currentActivity, requestHeaders);
                            if (this.injectRequestIdInW3CMode)
                            {
                                InjectBackCompatibleRequestId(currentActivity, requestHeaders);
                            }

                            InjectCorrelationContext(requestHeaders, currentActivity);
                        }

                        break;

                    case HttpInstrumentationVersion.V3:
                        // on V3, HttpClient adds either W3C or Request-Id depending on Activity format
                        // and adds Correlation-Context
                        if (currentActivity.IdFormat == ActivityIdFormat.W3C && this.injectRequestIdInW3CMode)
                        {
                            // we are going to override Request-Id to be in W3C compatible mode
                            InjectBackCompatibleRequestId(currentActivity, requestHeaders);
                        }

                        break;
                    }

                    if (this.injectLegacyHeaders)
                    {
                        // Add the root ID (Activity.RootId works with W3C and Hierarchical format)
                        string rootId = currentActivity.RootId;
                        if (!string.IsNullOrEmpty(rootId) && !requestHeaders.Contains(RequestResponseHeaders.StandardRootIdHeader))
                        {
                            requestHeaders.Add(RequestResponseHeaders.StandardRootIdHeader, rootId);
                        }

                        // Add the parent ID
                        string parentId = currentActivity.IdFormat == ActivityIdFormat.W3C ?
                                          W3CUtilities.FormatTelemetryId(rootId, currentActivity.SpanId.ToHexString()) :
                                          currentActivity.Id;

                        if (!string.IsNullOrEmpty(parentId) && !requestHeaders.Contains(RequestResponseHeaders.StandardParentIdHeader))
                        {
                            requestHeaders.Add(RequestResponseHeaders.StandardParentIdHeader, parentId);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
            }
        }
        private void InjectRequestHeaders(HttpRequestMessage request, string instrumentationKey, bool isLegacyEvent = false)
        {
            try
            {
                var currentActivity = Activity.Current;

                HttpRequestHeaders requestHeaders = request.Headers;
                if (requestHeaders != null && this.setComponentCorrelationHttpHeaders && !this.correlationDomainExclusionList.Contains(request.RequestUri.Host))
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(instrumentationKey) && !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey))
                        {
                            string sourceApplicationId;
                            if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumentationKey, out sourceApplicationId))
                            {
                                HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
                    }

                    // Add the root ID
                    string rootId = currentActivity.RootId;
                    if (!string.IsNullOrEmpty(rootId) &&
                        !requestHeaders.Contains(RequestResponseHeaders.StandardRootIdHeader))
                    {
                        requestHeaders.Add(RequestResponseHeaders.StandardRootIdHeader, rootId);
                    }

                    // Add the parent ID
                    string parentId = currentActivity.Id;
                    if (!string.IsNullOrEmpty(parentId) &&
                        !requestHeaders.Contains(RequestResponseHeaders.StandardParentIdHeader))
                    {
                        requestHeaders.Add(RequestResponseHeaders.StandardParentIdHeader, parentId);
                        if (isLegacyEvent)
                        {
                            requestHeaders.Add(RequestResponseHeaders.RequestIdHeader, parentId);
                        }
                    }

                    if (isLegacyEvent)
                    {
                        // we expect baggage to be empty or contain a few items
                        using (IEnumerator <KeyValuePair <string, string> > e = currentActivity.Baggage.GetEnumerator())
                        {
                            if (e.MoveNext())
                            {
                                var baggage = new List <string>();
                                do
                                {
                                    KeyValuePair <string, string> item = e.Current;
                                    baggage.Add(new NameValueHeaderValue(item.Key, item.Value).ToString());
                                }while (e.MoveNext());
                                request.Headers.Add(RequestResponseHeaders.CorrelationContextHeader, baggage);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
            }
        }
#pragma warning disable 612, 618
        private void InjectRequestHeaders(HttpRequestMessage request, string instrumentationKey, bool isLegacyEvent = false)
        {
            try
            {
                HttpRequestHeaders requestHeaders = request.Headers;
                if (requestHeaders != null && this.setComponentCorrelationHttpHeaders && !this.correlationDomainExclusionList.Contains(request.RequestUri.Host))
                {
                    string sourceApplicationId = null;
                    try
                    {
                        if (!string.IsNullOrEmpty(instrumentationKey) &&
                            !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey) &&
                            (this.configuration.ApplicationIdProvider?.TryGetApplicationId(instrumentationKey, out sourceApplicationId) ?? false))
                        {
                            HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId);
                        }
                    }
                    catch (Exception e)
                    {
                        AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
                    }

                    var currentActivity = Activity.Current;
                    if (isLegacyEvent)
                    {
                        if (!requestHeaders.Contains(RequestResponseHeaders.RequestIdHeader))
                        {
                            requestHeaders.Add(RequestResponseHeaders.RequestIdHeader, currentActivity.Id);
                        }

                        InjectCorrelationContext(requestHeaders, currentActivity);
                    }

                    if (this.injectLegacyHeaders)
                    {
                        // Add the root ID
                        string rootId = currentActivity.RootId;
                        if (!string.IsNullOrEmpty(rootId) && !requestHeaders.Contains(RequestResponseHeaders.StandardRootIdHeader))
                        {
                            requestHeaders.Add(RequestResponseHeaders.StandardRootIdHeader, rootId);
                        }

                        // Add the parent ID
                        string parentId = currentActivity.Id;
                        if (!string.IsNullOrEmpty(parentId) && !requestHeaders.Contains(RequestResponseHeaders.StandardParentIdHeader))
                        {
                            requestHeaders.Add(RequestResponseHeaders.StandardParentIdHeader, parentId);
                        }
                    }

                    if (this.injectW3CHeaders)
                    {
                        currentActivity.UpdateContextOnActivity();
                        string traceParent = currentActivity.GetTraceparent();
                        if (traceParent != null && !requestHeaders.Contains(W3CConstants.TraceParentHeader))
                        {
                            requestHeaders.Add(W3CConstants.TraceParentHeader, traceParent);
                        }

                        string traceState = currentActivity.GetTracestate();
                        if (!requestHeaders.Contains(W3CConstants.TraceStateHeader))
                        {
                            if (sourceApplicationId != null)
                            {
                                // TODO: there could be another az in the state.
                                // last updated state should appear first in the tracestate
                                string appIdPair = StringUtilities.FormatAzureTracestate(sourceApplicationId);
                                if (traceState == null)
                                {
                                    traceState = appIdPair;
                                }
                                else
                                {
                                    traceState = appIdPair + "," + traceState;
                                }
                            }

                            if (traceState != null)
                            {
                                requestHeaders.Add(W3CConstants.TraceStateHeader, traceState);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e));
            }
        }