public void RddTestHttpProcessingFrameworkNoDuplication()
        {
            Stopwatch      stopwatch    = Stopwatch.StartNew();
            HttpWebRequest request      = (HttpWebRequest)WebRequest.Create(this.testUrl);
            var            id           = ClientServerDependencyTracker.GetIdForRequestObject(request);
            var            returnObject = TestUtils.GenerateHttpWebResponse(HttpStatusCode.BadRequest);

            this.httpProcessingFramework.OnRequestSend(request);
            this.httpProcessingFramework.OnRequestSend(request);
            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrl.OriginalString);
            this.httpProcessingFramework.OnRequestSend(request);
            this.httpProcessingFramework.OnRequestSend(request);
            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrl.OriginalString);
            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrl.OriginalString);
            this.httpProcessingFramework.OnRequestSend(request);
            Thread.Sleep(this.sleepTimeMsecBetweenBeginAndEnd);
            Assert.AreEqual(0, this.sendItems.Count, "No telemetry item should be processed without calling End");
            this.httpProcessingFramework.OnResponseReceive(request, returnObject);
            this.httpProcessingFramework.OnEndHttpCallback(id, false, false, 409);
            this.httpProcessingFramework.OnEndHttpCallback(id, true, false, 304);
            this.httpProcessingFramework.OnResponseReceive(request, returnObject);
            this.httpProcessingFramework.OnEndHttpCallback(id, true, false, 200);
            this.httpProcessingFramework.OnResponseReceive(request, returnObject);
            this.httpProcessingFramework.OnEndHttpCallback(id, false, false, 400);
            stopwatch.Stop();

            Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent");
            ValidateTelemetryPacketForOnRequestSend(this.sendItems[0] as DependencyTelemetry, this.testUrl, RemoteDependencyConstants.HTTP, false, stopwatch.Elapsed.TotalMilliseconds, "409");
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method gets called once for each event from the Http DiagnosticSource.
        /// </summary>
        /// <param name="value">The pair containing the event name, and an object representing the payload. The payload
        /// is essentially a dynamic object that contain different properties depending on the event.</param>
        public void OnNext(KeyValuePair <string, object> value)
        {
            try
            {
                switch (value.Key)
                {
                case "System.Net.Http.Desktop.HttpRequestOut.Start":
                {
                    var request = (HttpWebRequest)this.requestFetcherRequestEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.HttpDesktopBeginCallbackCalled(ClientServerDependencyTracker.GetIdForRequestObject(request), request.RequestUri.ToString());

                    // With this event, DiagnosticSource injects headers himself (after the event)
                    // ApplicationInsights must not do this
                    this.httpDesktopProcessing.OnBegin(request, false);
                    break;
                }

                case "System.Net.Http.Desktop.HttpRequestOut.Stop":
                {
                    // request is never null
                    var request = this.requestFetcherResponseEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.HttpDesktopEndCallbackCalled(ClientServerDependencyTracker.GetIdForRequestObject(request));
                    var response = this.responseFetcher.Fetch(value.Value);
                    this.httpDesktopProcessing.OnEndResponse(request, response);
                    break;
                }

                case "System.Net.Http.Desktop.HttpRequestOut.Ex.Stop":
                {
                    // request is never null
                    var request = this.requestFetcherResponseExEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.HttpDesktopEndCallbackCalled(ClientServerDependencyTracker.GetIdForRequestObject(request));
                    object statusCode = this.responseExStatusFetcher.Fetch(value.Value);
                    object headers    = this.responseExHeadersFetcher.Fetch(value.Value);
                    this.httpDesktopProcessing.OnEndResponse(request, statusCode, headers);
                    break;
                }

                case "System.Net.Http.InitializationFailed":
                {
                    DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false;

                    Exception ex = (Exception)value.Value.GetType().GetProperty("Exception")?.GetValue(value.Value);
                    DependencyCollectorEventSource.Log.HttpHandlerDiagnosticListenerFailedToInitialize(ex?.ToInvariantString());
                    break;
                }

                default:
                {
                    DependencyCollectorEventSource.Log.NotExpectedCallback(value.GetHashCode(), value.Key, "unknown key");
                    break;
                }
                }
            }
            catch (Exception exc)
            {
                DependencyCollectorEventSource.Log.CallbackError(0, "OnNext", exc);
            }
        }
        public void FrameworkHttpProcessingIsDisabledWhenHttpDesktopDiagSourceIsEnabled()
        {
            DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = true;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.testUrl);
            var            id      = ClientServerDependencyTracker.GetIdForRequestObject(request);

            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrl.ToString());
            this.httpProcessingFramework.OnEndHttpCallback(id, null, false, 200);

            Assert.AreEqual(0, this.sendItems.Count, "No telemetry item should be sent");
        }
        public void RddTestHttpProcessingFrameworkOnEndHttpCallbackInvalidId()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.testUrl);
            var            id1     = ClientServerDependencyTracker.GetIdForRequestObject(request);
            var            id2     = 200;

            this.httpProcessingFramework.OnBeginHttpCallback(id1, this.testUrl.ToString());
            Thread.Sleep(this.sleepTimeMsecBetweenBeginAndEnd);
            Assert.AreEqual(0, this.sendItems.Count, "No telemetry item should be processed without calling End");

            this.httpProcessingFramework.OnEndHttpCallback(id2, true, true, null);
            Assert.AreEqual(0, this.sendItems.Count, "No telemetry item should be processed as invalid id is passed");
        }
        public void HttpProcessorSetsTargetForNonStandardPort()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.testUrlNonStandardPort);
            var            id      = ClientServerDependencyTracker.GetIdForRequestObject(request);

            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrlNonStandardPort.ToString());
            this.httpProcessingFramework.OnEndHttpCallback(id, null, false, 500);

            Assert.AreEqual(1, this.sendItems.Count, "Exactly one telemetry item should be sent");
            DependencyTelemetry receivedItem = (DependencyTelemetry)this.sendItems[0];
            string expectedTarget            = this.testUrlNonStandardPort.Host + ":" + this.testUrlNonStandardPort.Port;

            Assert.AreEqual(expectedTarget, receivedItem.Target, "HttpProcessingFramework returned incorrect target for non standard port.");
        }
        public void OnEndHttpCallbackSetsSuccessToTrueForLessThan400()
        {
            int statusCode = 399;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.testUrl);
            var            id      = ClientServerDependencyTracker.GetIdForRequestObject(request);

            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrl.ToString());
            this.httpProcessingFramework.OnEndHttpCallback(id, null, false, statusCode);

            Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent");
            var actual = this.sendItems[0] as DependencyTelemetry;

            Assert.IsTrue(actual.Success.Value);
        }
        public void OnEndHttpCallbackSetsSuccessToFalseForNegativeStatusCode()
        {
            // -1 StatusCode is returned in case of no response
            int statusCode = -1;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.testUrl);
            var            id      = ClientServerDependencyTracker.GetIdForRequestObject(request);

            this.httpProcessingFramework.OnBeginHttpCallback(id, this.testUrl.ToString());
            this.httpProcessingFramework.OnEndHttpCallback(id, null, false, statusCode);

            Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent");
            var actual = this.sendItems[0] as DependencyTelemetry;

            Assert.IsFalse(actual.Success.Value);
        }
        public void RddTestHttpProcessingFrameworkStartTimeFromGetRequestStreamAsync()
        {
            Stopwatch      stopwatch = Stopwatch.StartNew();
            HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(this.testUrl);
            var            id1       = ClientServerDependencyTracker.GetIdForRequestObject(request);

            this.httpProcessingFramework.OnBeginHttpCallback(id1, this.testUrl.ToString());
            Thread.Sleep(this.sleepTimeMsecBetweenBeginAndEnd);
            this.httpProcessingFramework.OnBeginHttpCallback(id1, this.testUrl.ToString());
            Thread.Sleep(this.sleepTimeMsecBetweenBeginAndEnd);
            Assert.AreEqual(0, this.sendItems.Count, "No telemetry item should be processed without calling End");
            this.httpProcessingFramework.OnEndHttpCallback(id1, true, false, 200);
            stopwatch.Stop();

            Assert.AreEqual(1, this.sendItems.Count, "Exactly one telemetry item should be sent");
            ValidateTelemetryPacketForOnBeginHttpCallback(this.sendItems[0] as DependencyTelemetry, this.testUrl, RemoteDependencyConstants.HTTP, true, stopwatch.Elapsed.TotalMilliseconds, "200");
        }
        public void RddTestHttpProcessingFrameworkOnEndHttpCallback()
        {
            var       request            = WebRequest.Create(this.testUrl);
            var       returnObjectPassed = TestUtils.GenerateHttpWebResponse(HttpStatusCode.OK);
            Stopwatch stopwatch          = new Stopwatch();

            stopwatch.Start();
            this.httpProcessingFramework.OnRequestSend(request);
            Thread.Sleep(this.sleepTimeMsecBetweenBeginAndEnd);
            Assert.AreEqual(0, this.sendItems.Count, "No telemetry item should be processed without calling End");
            this.httpProcessingFramework.OnResponseReceive(request, returnObjectPassed);
            this.httpProcessingFramework.OnEndHttpCallback(ClientServerDependencyTracker.GetIdForRequestObject(request), true, true, 200);
            stopwatch.Stop();

            Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent");
            ValidateTelemetryPacketForOnRequestSend(this.sendItems[0] as DependencyTelemetry, this.testUrl, RemoteDependencyConstants.HTTP, true, stopwatch.Elapsed.TotalMilliseconds, "200");
        }
        private void SimulateWebRequestResponseWithAppId(string appId)
        {
            var request = WebRequest.Create(this.testUrl);

            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(appId));

            var returnObjectPassed = TestUtils.GenerateHttpWebResponse(HttpStatusCode.OK, headers);

            this.httpProcessingFramework.OnBeginHttpCallback(ClientServerDependencyTracker.GetIdForRequestObject(request), this.testUrl.OriginalString);
            this.httpProcessingFramework.OnRequestSend(request);
            this.httpProcessingFramework.OnResponseReceive(request, returnObjectPassed);
            this.httpProcessingFramework.OnEndHttpCallback(
                ClientServerDependencyTracker.GetIdForRequestObject(request),
                true,
                true,
                (int)HttpStatusCode.OK);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Implemented by the derived class for removing the tuple from its specific cache.
 /// </summary>
 /// <param name="webRequest">The request which acts as the key.</param>
 protected override void RemoveTupleForWebDependencies(WebRequest webRequest)
 {
     this.TelemetryTable.Remove(ClientServerDependencyTracker.GetIdForRequestObject(webRequest));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Implemented by the derived class for getting the tuple from its specific cache.
 /// </summary>
 /// <param name="webRequest">The request which acts as the key.</param>
 /// <returns>The tuple for the given request.</returns>
 protected override Tuple <DependencyTelemetry, bool> GetTupleForWebDependencies(WebRequest webRequest)
 {
     return(this.TelemetryTable.Get(ClientServerDependencyTracker.GetIdForRequestObject(webRequest)));
 }
Exemplo n.º 13
0
        /// <summary>
        /// Implemented by the derived class for adding the tuple to its specific cache.
        /// </summary>
        /// <param name="webRequest">The request which acts the key.</param>
        /// <param name="telemetry">The dependency telemetry for the tuple.</param>
        /// <param name="isCustomCreated">Boolean value that tells if the current telemetry item is being added by the customer or not.</param>
        protected override void AddTupleForWebDependencies(WebRequest webRequest, DependencyTelemetry telemetry, bool isCustomCreated)
        {
            var telemetryTuple = new Tuple <DependencyTelemetry, bool>(telemetry, isCustomCreated);

            this.TelemetryTable.Store(ClientServerDependencyTracker.GetIdForRequestObject(webRequest), telemetryTuple);
        }