Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Counter metric support for ingestion response time #1796

Merged
merged 19 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Microsoft.ApplicationInsights.TestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.CodeDom;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
using System.Diagnostics.Tracing;

public class TransmissionTest
{
Expand Down Expand Up @@ -86,6 +88,7 @@ public void SetsTimeoutToSpecifiedValue()
public class SendAsync
{
private readonly Uri testUri = new Uri("https://127.0.0.1/");
private const long AllKeywords = -1;

[TestMethod]
public async Task SendAsyncUsesPostMethodToSpecifiedHttpEndpoint()
Expand Down Expand Up @@ -363,6 +366,85 @@ public async Task SendAsyncReturnsCorrectHttpResponseWrapperWithRetryHeaderWhenN
}

}

#if NETCOREAPP2_1
[TestMethod]
public async Task SendAsyncLogsIngestionReponseTimeEventCounter()
{
var handler = new HandlerForFakeHttpClient
{
InnerHandler = new HttpClientHandler(),
OnSendAsync = (req, cancellationToken) =>
{
return Task.FromResult<HttpResponseMessage>(new HttpResponseMessage());
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
}
};

using (var fakeHttpClient = new HttpClient(handler))
{
// Instantiate Transmission with the mock HttpClient
Transmission transmission = new Transmission(testUri, new byte[] { 1, 2, 3, 4, 5 }, fakeHttpClient, string.Empty, string.Empty);

using (var listener = new TestEventListener())
{
var eventCounterArguments = new Dictionary<string, string>
{
{"EventCounterIntervalSec", "1"}
};

listener.EnableEvents(CoreEventSource.Log, EventLevel.LogAlways, (EventKeywords)AllKeywords, eventCounterArguments);

HttpWebResponseWrapper result = await transmission.SendAsync();

// VERIFY
// We validate by checking SDK traces.
var allTraces = listener.Messages.ToList();
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
var traces = allTraces.Where(item => item.EventName == "EventCounters").ToList();
Assert.AreEqual(1, traces.Count);
var payload = (IDictionary<string, object>)traces[0].Payload[0];
Assert.AreEqual("IngestionEndpoint-ResponseTimeMsec", payload["Name"].ToString());
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
Assert.AreNotEqual(0, (float)payload["IntervalSec"]);
}
}
}
#endif
[TestMethod]
public async Task SendAsyncLogsIngestionReponseTimeAndStatusCode()
{
var handler = new HandlerForFakeHttpClient
{
InnerHandler = new HttpClientHandler(),
OnSendAsync = (req, cancellationToken) =>
{
return Task.FromResult<HttpResponseMessage>(new HttpResponseMessage());
}
};

using (var fakeHttpClient = new HttpClient(handler))
{
// Instantiate Transmission with the mock HttpClient
Transmission transmission = new Transmission(testUri, new byte[] { 1, 2, 3, 4, 5 }, fakeHttpClient, string.Empty, string.Empty);

using (var listener = new TestEventListener())
{
var eventCounterArguments = new Dictionary<string, string>
{
{"EventCounterIntervalSec", "1"}
};

listener.EnableEvents(CoreEventSource.Log, EventLevel.LogAlways, (EventKeywords)AllKeywords, eventCounterArguments);

HttpWebResponseWrapper result = await transmission.SendAsync();

// VERIFY
// We validate by checking SDK traces.
var allTraces = listener.Messages.ToList();
// Event 67 is logged after response from breeze.
var traces = allTraces.Where(item => item.EventId == 67).ToList();
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
Assert.AreEqual(1, traces.Count);
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -159,9 +160,16 @@ public virtual async Task<HttpWebResponseWrapper> SendAsync()
// "The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout."
// i.e for Server errors (500 status code), no exception is thrown. Hence this method should read the response and status code,
// and return correct HttpWebResponseWrapper to give any Retry policies a chance to retry as needed.
var stopwatch = new Stopwatch();
stopwatch.Start();

using (var response = await client.SendAsync(request, ct.Token).ConfigureAwait(false))
{
stopwatch.Stop();
CoreEventSource.Log.BreezeResponseTime(response != null ? (int)response.StatusCode : -1, stopwatch.ElapsedMilliseconds);
// Log breeze respose time as event counter metric.
CoreEventSource.Log.BreezeResponseTimeEventCounter(stopwatch.ElapsedMilliseconds);

if (response != null)
{
wrapper = new HttpWebResponseWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ internal sealed class CoreEventSource : EventSource
{
public static readonly CoreEventSource Log = new CoreEventSource();

#if NETSTANDARD2_0
public EventCounter BreezeResponseTimeCounter;
#endif

private readonly ApplicationNameProvider nameProvider = new ApplicationNameProvider();

#if NETSTANDARD2_0
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
private CoreEventSource()
{
this.BreezeResponseTimeCounter = new EventCounter("IngestionEndpoint-ResponseTimeMsec", this);
}
#endif

public static bool IsVerboseEnabled
{
[NonEvent]
Expand Down Expand Up @@ -620,6 +631,17 @@ public void InitializationIsSkippedForSampledItem(string appDomainName = "Incorr

#endregion

[Event(67, Message = "Backend has responded with {0} status code in {1}ms.", Level = EventLevel.Informational)]
public void BreezeResponseTime(int responseCode, float responseDurationInMs, string appDomainName = "Incorrect") => this.WriteEvent(67, responseCode, responseDurationInMs, this.nameProvider.Name);

[NonEvent]
public void BreezeResponseTimeEventCounter(float responseDurationInMs)
{
#if NETSTANDARD2_0
this.BreezeResponseTimeCounter.WriteMetric(responseDurationInMs);
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

/// <summary>
/// Keywords for the PlatformEventSource.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## VNext
[WorkerService package is modified to depend on 2.1.1 on Microsoft.Extensions.DependencyInjection so that it can be used in .NET Core 2.1 projects without nuget errors.](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1677)

- [New: EventCounter to track Ingestion Endpoint Response Time] (https://github.com/microsoft/ApplicationInsights-dotnet/pull/1796)

## Version 2.14.0
- no changes since beta.
Expand Down