Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

Track dependency instrumented with HttpDesktop DiagnosticSource in Response event (#509) #516

Merged
merged 11 commits into from
May 4, 2017
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
@@ -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;

Expand All @@ -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 =>
{
Expand All @@ -35,31 +40,38 @@ public void TestDependencyCollectionNoParentActivity()
if (depTelemetry != null &&
!depTelemetry.Data.StartsWith(FakeProfileApiEndpoint, StringComparison.OrdinalIgnoreCase))
{
Copy link
Contributor

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.

Copy link
Member Author

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

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why test initialize need to explicitly mark this false?

Copy link
Member Author

Choose a reason for hiding this comment

The 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())
{
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
Expand All @@ -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:"),
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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"]);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Compile Include="..\Shared.Tests\Implementation\DependencyCollectorDiagnosticListenerTests.Netstandard20.cs" Link="DependencyCollectorDiagnosticListenerTests.Netstandard20.cs" />
<Compile Include="..\Shared.Tests\Implementation\EnumerableAssert.cs" Link="EnumerableAssert.cs" />
<Compile Include="..\Shared.Tests\Implementation\MockCorrelationIdLookupHelper.cs" Link="MockCorrelationIdLookupHelper.cs" />
<Compile Include="..\..\TestFramework\Shared\SdkVersionHelper.cs" Link="SdkVersionHelper.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>Copyright © Microsoft. All Rights Reserved.</Copyright>
Expand Down Expand Up @@ -29,6 +29,7 @@
<Compile Include="..\Shared\Implementation\HttpCoreDiagnosticSourceListener.cs" Link="HttpCoreDiagnosticSourceListener.cs" />
<Compile Include="..\Shared\Implementation\HttpHeadersUtilities.cs" Link="HttpHeadersUtilities.cs" />
<Compile Include="..\Shared\Implementation\PropertyFetcher.cs" Link="PropertyFetcher.cs" />
<Compile Include="..\Shared\Implementation\RDDSource.cs" Link="RDDSource.cs" />
<Compile Include="..\Shared\Implementation\HttpParsers\AzureBlobHttpParser.cs" Link="AzureBlobHttpParser.cs" />
<Compile Include="..\Shared\Implementation\HttpParsers\AzureIotHubHttpParser.cs" Link="AzureIotHubHttpParser.cs" />
<Compile Include="..\Shared\Implementation\HttpParsers\AzureQueueHttpParser.cs" Link="AzureQueueHttpParser.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="$(MSBuildThisFileDirectory)HttpDependenciesParsingTelemetryInitializerTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\ClientServerDependencyTrackerTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\DependencyTargetNameHelperTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\DesktopDiagnosticSourceHttpProcessingTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\HttpParsers\AzureBlobHttpParserTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\HttpParsers\AzureServiceBusHttpParserTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\HttpParsers\DocumentDbHttpParserTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Microsoft.ApplicationInsights.DependencyCollector
using Microsoft.ApplicationInsights.Common;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.TestFramework;
#if !NETCORE
using Microsoft.ApplicationInsights.Web.TestFramework;
#endif
Expand Down Expand Up @@ -203,6 +205,10 @@ public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndNoTargetI
Assert.AreEqual(RequestUrl, telemetry.Target);
Assert.AreEqual(HttpOkResultCode, telemetry.ResultCode);
Assert.AreEqual(true, telemetry.Success);

string expectedVersion =
SdkVersionHelper.GetExpectedSdkVersion(typeof(DependencyTrackingTelemetryModule), prefix: "rdddsc:");
Assert.AreEqual(expectedVersion, telemetry.Context.GetInternalContext().SdkVersion);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Microsoft.ApplicationInsights.DependencyCollector
using Microsoft.ApplicationInsights.Common;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.TestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
Expand Down Expand Up @@ -72,6 +74,10 @@ public void OnActivityStopTracksTelemetry()
Assert.AreEqual(activity.ParentId, telemetry.Context.Operation.ParentId);
Assert.AreEqual(activity.Id, telemetry.Id);
Assert.AreEqual("v", telemetry.Context.Properties["k"]);

string expectedVersion =
SdkVersionHelper.GetExpectedSdkVersion(typeof(DependencyTrackingTelemetryModule), prefix: "rdddsc:");
Assert.AreEqual(expectedVersion, telemetry.Context.GetInternalContext().SdkVersion);
}

/// <summary>
Expand Down
Loading