public static async Task PerformRequestAsync()

in RESTProxy/Models/ProxyManager.cs [165:240]


        public static async Task<HttpResponseMessage> PerformRequestAsync(
            string pathAndQuery,
            HttpMethod method,
            IPrincipal onBehalfOf,
            string body = null,
            string tenantId = null,
            string tenantName = null,
            EndpointType endpointType = EndpointType.Prod,
            string correlationId = null,
            string clientRequestId = null,
            string clientName = null)
        {
            // We'll track how long this takes, for telemetry purposes.
            Stopwatch stopwatch = Stopwatch.StartNew();

            // Assume ok unless we find out otherwise.
            HttpStatusCode statusCode = HttpStatusCode.OK;

            // We want to record the request header for every request in case we need to look up failures later.
            string requestId = string.Empty;

            // We also want to store the ClientId so that it's easier to see distribution of requests across Clients.
            string clientId = string.Empty;

            try
            {
                Endpoint endpoint = null;
                HttpResponseMessage response;
                if (ProxyManager.TryGetEndpoint(tenantId, tenantName, endpointType, out endpoint, out response))
                {
                    clientId = endpoint.ClientId;
                    response = await endpoint.PerformRequestAsync(pathAndQuery, method, onBehalfOf, body, correlationId, clientRequestId, clientName);
                }

                // We'll capture the status code for use in the finally block.
                statusCode = response.StatusCode;

                // Get any of the request ID headers that can be used for post-mortem diagnostics.  We'll use them in the finally block.
                IEnumerable<string> headerValues;
                if (response.Headers.TryGetValues(ProxyManager.MSCorrelationIdHeader, out headerValues))
                {
                    // If the client supplied a correlationId, the value we're getting back from the API should be identical.
                    correlationId = headerValues.FirstOrDefault();
                }

                if (response.Headers.TryGetValues(ProxyManager.MSRequestIdHeader, out headerValues))
                {
                    requestId = headerValues.FirstOrDefault();
                }

                if (response.Headers.TryGetValues(ProxyManager.MSClientRequestIdHeader, out headerValues))
                {
                    // If the client supplied a clientRequestId, the value we're getting back from the API should be identical.
                    clientRequestId = headerValues.FirstOrDefault();
                }

                return response;
            }
            finally
            {
                stopwatch.Stop();
                ProxyManager.LogTelemetryEvent(
                    onBehalfOf.Identity.Name,
                    pathAndQuery,
                    method,
                    tenantId,
                    tenantName,
                    clientId,
                    endpointType,
                    statusCode,
                    correlationId,
                    requestId,
                    clientRequestId,
                    stopwatch.Elapsed.TotalSeconds);
            }
        }