-
Notifications
You must be signed in to change notification settings - Fork 67
Track dependency instrumented with HttpDesktop DiagnosticSource in Response event (#509) #516
Changes from 9 commits
004ee6f
7305e11
b97d60a
e233d62
5e60b26
886a9e2
7d313e5
4f7d151
0827d68
e973cdd
5c8a03c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
namespace Microsoft.ApplicationInsights.DependencyCollector | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net; | ||
|
||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.DependencyCollector.Implementation; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.Extensibility.Implementation; | ||
using Microsoft.ApplicationInsights.TestFramework; | ||
using Microsoft.ApplicationInsights.Web.TestFramework; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
|
@@ -19,14 +22,16 @@ public class DependencyTrackingTelemetryModuleTestNet46 | |
{ | ||
private const string IKey = "F8474271-D231-45B6-8DD4-D344C309AE69"; | ||
private const string FakeProfileApiEndpoint = "http://www.microsoft.com"; | ||
private StubTelemetryChannel channel; | ||
private TelemetryConfiguration config; | ||
private List<DependencyTelemetry> sentTelemetry; | ||
|
||
[TestMethod] | ||
[Timeout(5000)] | ||
public void TestDependencyCollectionNoParentActivity() | ||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
ITelemetry sentTelemetry = null; | ||
this.sentTelemetry = new List<DependencyTelemetry>(); | ||
|
||
var channel = new StubTelemetryChannel | ||
this.channel = new StubTelemetryChannel | ||
{ | ||
OnSend = telemetry => | ||
{ | ||
|
@@ -35,31 +40,38 @@ public void TestDependencyCollectionNoParentActivity() | |
if (depTelemetry != null && | ||
!depTelemetry.Data.StartsWith(FakeProfileApiEndpoint, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Assert.IsNull(sentTelemetry); | ||
sentTelemetry = telemetry; | ||
this.sentTelemetry.Add(depTelemetry); | ||
} | ||
} | ||
}; | ||
|
||
var config = new TelemetryConfiguration | ||
this.config = new TelemetryConfiguration | ||
{ | ||
InstrumentationKey = IKey, | ||
TelemetryChannel = channel | ||
TelemetryChannel = this.channel | ||
}; | ||
|
||
config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); | ||
this.config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); | ||
DependencyTableStore.Instance.IsDesktopHttpDiagnosticSourceActivated = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why test initialize need to explicitly mark this false? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to clean up previous tests results. Added the same in Cleanup |
||
} | ||
|
||
[TestMethod] | ||
[Timeout(5000)] | ||
public void TestDependencyCollectionDiagnosticSourceNoParentActivity() | ||
{ | ||
using (var module = new DependencyTrackingTelemetryModule()) | ||
{ | ||
module.EnableDiagnosticSourceInstrumentation = true; | ||
module.ProfileQueryEndpoint = FakeProfileApiEndpoint; | ||
module.Initialize(config); | ||
module.Initialize(this.config); | ||
|
||
var url = new Uri("http://bing.com"); | ||
var url = new Uri("https://www.bing.com/"); | ||
HttpWebRequest request = WebRequest.CreateHttp(url); | ||
request.GetResponse(); | ||
using (request.GetResponse()) | ||
{ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please retain the test for 'GetResponse() but no subsequent stream read' to test that we wont collect dependencies in that case, and having a test with comments will be easier for future reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
Assert.IsNotNull(sentTelemetry); | ||
var item = (DependencyTelemetry)sentTelemetry; | ||
var item = this.sentTelemetry.Single(); | ||
Assert.AreEqual(url, item.Data); | ||
Assert.AreEqual(url.Host, item.Target); | ||
Assert.AreEqual("GET " + url.AbsolutePath, item.Name); | ||
|
@@ -74,6 +86,9 @@ public void TestDependencyCollectionNoParentActivity() | |
-TimeSpan.FromMinutes(1).TotalMilliseconds, | ||
"now - 1 min < timestamp"); | ||
Assert.AreEqual("200", item.ResultCode); | ||
Assert.AreEqual( | ||
SdkVersionHelper.GetExpectedSdkVersion(typeof(DependencyTrackingTelemetryModule), "rdddsd:"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test uses DependencyTrackingTelemetryModule() with the config you created above. The config by default should have EnableDiagnosticSourceInstrumentation as false right? If thats the case, then how did we end up with 'rddsd' as version prefix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never mind - i didn't see you are setting EnableDiagnosticSourceInstrumentation = true at the beginning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test explicitly enables instrumentation |
||
item.Context.GetInternalContext().SdkVersion); | ||
|
||
var requestId = item.Id; | ||
Assert.AreEqual(requestId, request.Headers["Request-Id"]); | ||
|
@@ -86,47 +101,89 @@ public void TestDependencyCollectionNoParentActivity() | |
|
||
[TestMethod] | ||
[Timeout(5000)] | ||
public void TestDependencyCollectionWithParentActivity() | ||
public void TestDependencyCollectionEventSource() | ||
{ | ||
ITelemetry sentTelemetry = null; | ||
|
||
var channel = new StubTelemetryChannel | ||
using (var module = new DependencyTrackingTelemetryModule()) | ||
{ | ||
OnSend = telemetry => | ||
module.ProfileQueryEndpoint = FakeProfileApiEndpoint; | ||
module.Initialize(this.config); | ||
|
||
var url = new Uri("https://www.bing.com/"); | ||
HttpWebRequest request = WebRequest.CreateHttp(url); | ||
using (request.GetResponse()) | ||
{ | ||
// The correlation id lookup service also makes http call, just make sure we skip that | ||
DependencyTelemetry depTelemetry = telemetry as DependencyTelemetry; | ||
if (depTelemetry != null && | ||
!depTelemetry.Data.StartsWith(FakeProfileApiEndpoint, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Assert.IsNull(sentTelemetry); | ||
sentTelemetry = telemetry; | ||
} | ||
} | ||
}; | ||
|
||
var config = new TelemetryConfiguration | ||
var item = this.sentTelemetry.Single(); | ||
Assert.AreEqual(url, item.Data); | ||
Assert.AreEqual(url.Host, item.Target); | ||
Assert.AreEqual(url.AbsolutePath, item.Name); | ||
Assert.IsTrue(item.Duration > TimeSpan.FromMilliseconds(0), "Duration has to be positive"); | ||
Assert.AreEqual(RemoteDependencyConstants.HTTP, item.Type, "HttpAny has to be dependency kind as it includes http and azure calls"); | ||
Assert.IsTrue( | ||
DateTime.UtcNow.Subtract(item.Timestamp.UtcDateTime).TotalMilliseconds < | ||
TimeSpan.FromMinutes(1).TotalMilliseconds, | ||
"timestamp < now"); | ||
Assert.IsTrue( | ||
item.Timestamp.Subtract(DateTime.UtcNow).TotalMilliseconds > | ||
-TimeSpan.FromMinutes(1).TotalMilliseconds, | ||
"now - 1 min < timestamp"); | ||
Assert.AreEqual("200", item.ResultCode); | ||
Assert.AreEqual( | ||
SdkVersionHelper.GetExpectedSdkVersion(typeof(DependencyTrackingTelemetryModule), "rddf:"), | ||
item.Context.GetInternalContext().SdkVersion); | ||
|
||
Assert.IsNull(request.Headers["Request-Id"]); | ||
Assert.IsNull(request.Headers["x-ms-request-id"]); | ||
Assert.IsNull(request.Headers["Correlation-Context"]); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[Timeout(5000)] | ||
public void TestNoDependencyCollectionDiagnosticSourceNoResponseClose() | ||
{ | ||
using (var module = new DependencyTrackingTelemetryModule()) | ||
{ | ||
InstrumentationKey = IKey, | ||
TelemetryChannel = channel | ||
}; | ||
module.EnableDiagnosticSourceInstrumentation = true; | ||
module.ProfileQueryEndpoint = FakeProfileApiEndpoint; | ||
module.Initialize(this.config); | ||
|
||
var url = new Uri("https://www.bing.com/"); | ||
HttpWebRequest request = WebRequest.CreateHttp(url); | ||
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
|
||
config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); | ||
Assert.IsFalse(this.sentTelemetry.Any()); | ||
var requestId = request.Headers["Request-Id"]; | ||
var rootId = request.Headers["x-ms-request-root-id"]; | ||
Assert.IsNotNull(requestId); | ||
Assert.IsNotNull(rootId); | ||
Assert.AreEqual(requestId, request.Headers["x-ms-request-id"]); | ||
Assert.IsTrue(requestId.StartsWith('|' + rootId + '.')); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[Timeout(5000)] | ||
public void TestDependencyCollectionDiagnosticSourceWithParentActivity() | ||
{ | ||
using (var module = new DependencyTrackingTelemetryModule()) | ||
{ | ||
module.EnableDiagnosticSourceInstrumentation = true; | ||
module.ProfileQueryEndpoint = FakeProfileApiEndpoint; | ||
module.Initialize(config); | ||
module.Initialize(this.config); | ||
|
||
var url = new Uri("http://bing.com"); | ||
var url = new Uri("https://www.bing.com/"); | ||
HttpWebRequest request = WebRequest.CreateHttp(url); | ||
|
||
var parent = new Activity("parent").AddBaggage("k", "v").SetParentId("|guid.").Start(); | ||
request.GetResponse(); | ||
using (request.GetResponse()) | ||
{ | ||
} | ||
|
||
parent.Stop(); | ||
|
||
Assert.IsNotNull(sentTelemetry); | ||
var item = (DependencyTelemetry)sentTelemetry; | ||
var item = this.sentTelemetry.Single(); | ||
Assert.AreEqual("200", item.ResultCode); | ||
|
||
var requestId = item.Id; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this check "!depTelemetry.Data.StartsWith(FakeProfileApiEndpoint" - DependencyCollector by default will not be tracking dependency calls made to any AI endpoints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it's a fake endpoint here: microsoft.com