From 8d6e3a9c9ea3889a398cbac8f78a624294734672 Mon Sep 17 00:00:00 2001 From: Rajkumar Rangaraj Date: Fri, 22 Jan 2021 12:42:25 -0800 Subject: [PATCH] Add response duration to TransmissionStatusEventArgs --- .../net452/PublicAPI.Unshipped.txt | 2 ++ .../net46/PublicAPI.Unshipped.txt | 2 ++ .../netstandard2.0/PublicAPI.Unshipped.txt | 2 ++ .../Channel/TransmissionTest.cs | 3 +++ .../Channel/Transmission.cs | 6 ++++-- .../Channel/TransmissionStatusEventArgs.cs | 18 +++++++++++++++++- 6 files changed, 30 insertions(+), 3 deletions(-) diff --git a/.publicApi/Microsoft.ApplicationInsights.dll/net452/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.dll/net452/PublicAPI.Unshipped.txt index e69de29bb2..b0e05c8ccd 100644 --- a/.publicApi/Microsoft.ApplicationInsights.dll/net452/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.dll/net452/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +Microsoft.ApplicationInsights.Channel.TransmissionStatusEventArgs.ResponseDurationInMs.get -> long +Microsoft.ApplicationInsights.Channel.TransmissionStatusEventArgs.TransmissionStatusEventArgs(Microsoft.ApplicationInsights.Extensibility.Implementation.HttpWebResponseWrapper response, long responseDurationInMs) -> void \ No newline at end of file diff --git a/.publicApi/Microsoft.ApplicationInsights.dll/net46/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.dll/net46/PublicAPI.Unshipped.txt index e69de29bb2..b0e05c8ccd 100644 --- a/.publicApi/Microsoft.ApplicationInsights.dll/net46/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.dll/net46/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +Microsoft.ApplicationInsights.Channel.TransmissionStatusEventArgs.ResponseDurationInMs.get -> long +Microsoft.ApplicationInsights.Channel.TransmissionStatusEventArgs.TransmissionStatusEventArgs(Microsoft.ApplicationInsights.Extensibility.Implementation.HttpWebResponseWrapper response, long responseDurationInMs) -> void \ No newline at end of file diff --git a/.publicApi/Microsoft.ApplicationInsights.dll/netstandard2.0/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.dll/netstandard2.0/PublicAPI.Unshipped.txt index e69de29bb2..b0e05c8ccd 100644 --- a/.publicApi/Microsoft.ApplicationInsights.dll/netstandard2.0/PublicAPI.Unshipped.txt +++ b/.publicApi/Microsoft.ApplicationInsights.dll/netstandard2.0/PublicAPI.Unshipped.txt @@ -0,0 +1,2 @@ +Microsoft.ApplicationInsights.Channel.TransmissionStatusEventArgs.ResponseDurationInMs.get -> long +Microsoft.ApplicationInsights.Channel.TransmissionStatusEventArgs.TransmissionStatusEventArgs(Microsoft.ApplicationInsights.Extensibility.Implementation.HttpWebResponseWrapper response, long responseDurationInMs) -> void \ No newline at end of file diff --git a/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Channel/TransmissionTest.cs b/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Channel/TransmissionTest.cs index d1b4c922bb..ffd396876b 100644 --- a/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Channel/TransmissionTest.cs +++ b/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Channel/TransmissionTest.cs @@ -528,6 +528,7 @@ public async Task TestTransmissionStatusEventHandlerWithSuccessTransmission() { Assert.IsTrue(sender is Transmission); Assert.AreEqual((int)HttpStatusCode.OK, args.Response.StatusCode); + Assert.AreNotEqual(0, args.ResponseDurationInMs); }; // ACT @@ -559,6 +560,7 @@ public async Task TestTransmissionStatusEventHandlerWithKnownFailureTransmission transmission.TransmissionStatusEvent += delegate (object sender, TransmissionStatusEventArgs args) { Assert.AreEqual((int)HttpStatusCode.RequestTimeout, args.Response.StatusCode); + Assert.AreEqual(0, args.ResponseDurationInMs); }; // ACT @@ -589,6 +591,7 @@ public async Task TestTransmissionStatusEventHandlerWithUnKnownFailureTransmissi transmission.TransmissionStatusEvent += delegate (object sender, TransmissionStatusEventArgs args) { Assert.AreEqual(999, args.Response.StatusCode); + Assert.AreEqual(0, args.ResponseDurationInMs); }; // ACT diff --git a/BASE/src/Microsoft.ApplicationInsights/Channel/Transmission.cs b/BASE/src/Microsoft.ApplicationInsights/Channel/Transmission.cs index 56d8d3dcbb..100e165b3b 100644 --- a/BASE/src/Microsoft.ApplicationInsights/Channel/Transmission.cs +++ b/BASE/src/Microsoft.ApplicationInsights/Channel/Transmission.cs @@ -156,6 +156,7 @@ public virtual async Task SendAsync() { HttpRequestMessage request = this.CreateRequestMessage(this.EndpointAddress, contentStream); HttpWebResponseWrapper wrapper = null; + long responseDurationInMs = 0; try { @@ -171,7 +172,8 @@ public virtual async Task SendAsync() using (var response = await client.SendAsync(request, ct.Token).ConfigureAwait(false)) { stopwatch.Stop(); - CoreEventSource.Log.IngestionResponseTime(response != null ? (int)response.StatusCode : -1, stopwatch.ElapsedMilliseconds); + responseDurationInMs = stopwatch.ElapsedMilliseconds; + CoreEventSource.Log.IngestionResponseTime(response != null ? (int)response.StatusCode : -1, responseDurationInMs); // Log ingestion respose time as event counter metric. CoreEventSource.Log.IngestionResponseTimeEventCounter(stopwatch.ElapsedMilliseconds); @@ -225,7 +227,7 @@ public virtual async Task SendAsync() try { // Initiates event notification to subscriber with Transmission and TransmissionStatusEventArgs. - this.TransmissionStatusEvent?.Invoke(this, new TransmissionStatusEventArgs(wrapper ?? new HttpWebResponseWrapper() { StatusCode = 999 })); + this.TransmissionStatusEvent?.Invoke(this, new TransmissionStatusEventArgs(wrapper ?? new HttpWebResponseWrapper() { StatusCode = 999 }, responseDurationInMs)); } catch (Exception ex) { diff --git a/BASE/src/Microsoft.ApplicationInsights/Channel/TransmissionStatusEventArgs.cs b/BASE/src/Microsoft.ApplicationInsights/Channel/TransmissionStatusEventArgs.cs index e1c914eddd..c4e59891b0 100644 --- a/BASE/src/Microsoft.ApplicationInsights/Channel/TransmissionStatusEventArgs.cs +++ b/BASE/src/Microsoft.ApplicationInsights/Channel/TransmissionStatusEventArgs.cs @@ -12,14 +12,30 @@ public class TransmissionStatusEventArgs : EventArgs /// Initializes a new instance of the class. /// /// Response from ingestion endpoint. - public TransmissionStatusEventArgs(HttpWebResponseWrapper response) + [ObsoleteAttribute("This constructor is deprecated. Please use a constructor that accepts response and responseDurationInMs instead.", false)] + public TransmissionStatusEventArgs(HttpWebResponseWrapper response) : this(response, default) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Response from ingestion endpoint. + /// Response duration in milliseconds. + public TransmissionStatusEventArgs(HttpWebResponseWrapper response, long responseDurationInMs) { this.Response = response; + this.ResponseDurationInMs = responseDurationInMs; } /// /// Gets the response from ingestion endpoint. /// public HttpWebResponseWrapper Response { get; } + + /// + /// Gets response duration in milliseconds. + /// + public long ResponseDurationInMs { get; } } }