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

Commit

Permalink
Merge pull request #472 from Microsoft/dev/DepColNoReactiveCore
Browse files Browse the repository at this point in the history
Add Core Http DiagnosticListener to DependencyCollector.NET45
  • Loading branch information
cijothomas authored Apr 25, 2017
2 parents 06ef428 + b0f225f commit df9c452
Show file tree
Hide file tree
Showing 22 changed files with 1,223 additions and 344 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'Test.props'))\Test.props" />
<PropertyGroup>
Expand Down Expand Up @@ -80,6 +80,21 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\Shared.Tests\Implementation\DependencyCollectorDiagnosticListenerTests.Netstandard16.cs" >
<Link>DependencyCollectorDiagnosticListenerTests.Netstandard16.cs</Link>
</Compile>
<Compile Include="..\Shared.Tests\Implementation\DependencyCollectorDiagnosticListenerTests.Netstandard20.cs" >
<Link>DependencyCollectorDiagnosticListenerTests.Netstandard20.cs</Link>
</Compile>
<Compile Include="..\Shared.Tests\Implementation\EnumerableAssert.cs" >
<Link>EnumerableAssert.cs</Link>
</Compile>
<Compile Include="..\Shared.Tests\Implementation\HttpHeadersUtilitiesTests.cs" >
<Link>HttpHeadersUtilitiesTests.cs</Link>
</Compile>
<Compile Include="..\Shared.Tests\Implementation\MockCorrelationIdLookupHelper.cs" >
<Link>MockCorrelationIdLookupHelper.cs</Link>
</Compile>
<Compile Include="HttpWebRequestUtils.cs" />
<Compile Include="DependencyTrackingTelemetryModuleTest.cs" />
</ItemGroup>
Expand All @@ -94,4 +109,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\..\..\packages\StyleCop.MSBuild.4.7.54.0\build\StyleCop.MSBuild.Targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\StyleCop.MSBuild.4.7.54.0\build\StyleCop.MSBuild.Targets'))" />
</Target>
</Project>
</Project>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public void OnActivityStartInjectsHeaders()
activity.AddBaggage("k", "v");
activity.Start();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrlWithScheme);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme);
this.listener.OnActivityStart(request);

// Request-Id and Correlation-Context are injected by HttpClient
// check only legacy headers here
Assert.AreEqual(activity.RootId, request.Headers.GetValues(RequestResponseHeaders.StandardRootIdHeader).Single());
Assert.AreEqual(activity.Id, request.Headers.GetValues(RequestResponseHeaders.StandardParentIdHeader).Single());
Assert.AreEqual(mockAppId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey));
Assert.AreEqual(MockAppId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey));
}

/// <summary>
/// Tests that OnStopActivity tracks telemetry
/// Tests that OnStopActivity tracks telemetry.
/// </summary>
[TestMethod]
public void OnActivityStopTracksTelemetry()
Expand All @@ -50,7 +50,7 @@ public void OnActivityStopTracksTelemetry()
activity.SetStartTime(startTime);
activity.Start();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrlWithScheme);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme);
this.listener.OnActivityStart(request);

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
Expand All @@ -60,9 +60,9 @@ public void OnActivityStopTracksTelemetry()
var telemetry = sentTelemetry.Single() as DependencyTelemetry;

Assert.AreEqual("POST /", telemetry.Name);
Assert.AreEqual(requestUrl, telemetry.Target);
Assert.AreEqual(RequestUrl, telemetry.Target);
Assert.AreEqual(RemoteDependencyConstants.HTTP, telemetry.Type);
Assert.AreEqual(requestUrlWithScheme, telemetry.Data);
Assert.AreEqual(RequestUrlWithScheme, telemetry.Data);
Assert.AreEqual("200", telemetry.ResultCode);
Assert.AreEqual(true, telemetry.Success);

Expand All @@ -83,12 +83,12 @@ public void OnActivityStopTracksTelemetryForCanceledRequest()
var activity = new Activity("System.Net.Http.HttpRequestOut");
activity.Start();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrlWithScheme);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme);
this.listener.OnActivityStart(request);

this.listener.OnActivityStop(null, request, TaskStatus.Canceled);

var telemetry = sentTelemetry.Single() as DependencyTelemetry;
var telemetry = this.sentTelemetry.Single() as DependencyTelemetry;

Assert.AreEqual("Canceled", telemetry.ResultCode);
Assert.AreEqual(false, telemetry.Success);
Expand All @@ -103,37 +103,37 @@ public void OnActivityStopTracksTelemetryForFaultedRequest()
var activity = new Activity("System.Net.Http.HttpRequestOut");
activity.Start();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrlWithScheme);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme);
this.listener.OnActivityStart(request);

this.listener.OnActivityStop(null, request, TaskStatus.Faulted);

var telemetry = sentTelemetry.Single() as DependencyTelemetry;
var telemetry = this.sentTelemetry.Single() as DependencyTelemetry;

Assert.AreEqual("Faulted", telemetry.ResultCode);
Assert.AreEqual(false, telemetry.Success);
}

/// <summary>
/// Tests that exception during request processing os tracked with correct context.
/// Tests that exception during request processing is tracked with correct context.
/// </summary>
[TestMethod]
public void OnExceptionTracksException()
{
var activity = new Activity("System.Net.Http.HttpRequestOut");
activity.Start();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrlWithScheme);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme);
this.listener.OnActivityStart(request);

var exception = new HttpRequestException("message", new Exception("The server name or address could not be resolved"));
this.listener.OnException(exception, request);
this.listener.OnException(exception);
this.listener.OnActivityStop(null, request, TaskStatus.Faulted);

var dependencyTelemetry = sentTelemetry.Single(t => t is DependencyTelemetry) as DependencyTelemetry;
var exceptionTelemetry = sentTelemetry.Single(t => t is ExceptionTelemetry) as ExceptionTelemetry;
var dependencyTelemetry = this.sentTelemetry.Single(t => t is DependencyTelemetry) as DependencyTelemetry;
var exceptionTelemetry = this.sentTelemetry.Single(t => t is ExceptionTelemetry) as ExceptionTelemetry;

Assert.AreEqual(2, sentTelemetry.Count);
Assert.AreEqual(2, this.sentTelemetry.Count);
Assert.AreEqual(exception, exceptionTelemetry.Exception);
Assert.AreEqual(exceptionTelemetry.Context.Operation.Id, dependencyTelemetry.Context.Operation.Id);
Assert.AreEqual(exceptionTelemetry.Context.Operation.ParentId, dependencyTelemetry.Id);
Expand Down
14 changes: 12 additions & 2 deletions Src/DependencyCollector/Net45/DependencyCollector.Net45.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\..\..\packages\MicroBuild.Core.0.2.0\build\MicroBuild.Core.props" Condition="Exists('..\..\..\..\packages\MicroBuild.Core.0.2.0\build\MicroBuild.Core.props')" />
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'Product.props'))\Product.props" />
Expand Down Expand Up @@ -39,6 +39,7 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
Expand All @@ -49,6 +50,15 @@
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<Compile Include="..\Shared\Implementation\HttpCoreDiagnosticSourceListener.cs">
<Link>HttpCoreDiagnosticSourceListener.cs</Link>
</Compile>
<Compile Include="..\Shared\Implementation\HttpHeadersUtilities.cs">
<Link>HttpHeadersUtilities.cs</Link>
</Compile>
<Compile Include="..\Shared\Implementation\PropertyFetcher.cs">
<Link>PropertyFetcher.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\TestFramework\Net45\TestFramework.Net45.csproj">
Expand Down Expand Up @@ -79,4 +89,4 @@
<Import Project="..\..\..\..\packages\MicroBuild.Core.0.2.0\build\MicroBuild.Core.targets" Condition="Exists('..\..\..\..\packages\MicroBuild.Core.0.2.0\build\MicroBuild.Core.targets')" />
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'Common.targets'))\Common.targets" />
<Import Project="..\..\..\..\packages\StyleCop.MSBuild.4.7.54.0\build\StyleCop.MSBuild.Targets" Condition="Exists('..\..\..\..\packages\StyleCop.MSBuild.4.7.54.0\build\StyleCop.MSBuild.Targets')" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void TestDependencyCollectionNoParentActivity()
TelemetryChannel = channel
};

config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

using (var module = new DependencyTrackingTelemetryModule())
{
module.ProfileQueryEndpoint = FakeProfileApiEndpoint;
Expand All @@ -71,6 +73,7 @@ public void TestDependencyCollectionNoParentActivity()
item.Timestamp.Subtract(DateTime.UtcNow).TotalMilliseconds >
-TimeSpan.FromMinutes(1).TotalMilliseconds,
"now - 1 min < timestamp");
Assert.AreEqual("200", item.ResultCode);

var requestId = item.Id;
Assert.AreEqual(requestId, request.Headers["Request-Id"]);
Expand Down Expand Up @@ -108,6 +111,8 @@ public void TestDependencyCollectionWithParentActivity()
TelemetryChannel = channel
};

config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

using (var module = new DependencyTrackingTelemetryModule())
{
module.ProfileQueryEndpoint = FakeProfileApiEndpoint;
Expand All @@ -122,12 +127,14 @@ public void TestDependencyCollectionWithParentActivity()

Assert.IsNotNull(sentTelemetry);
var item = (DependencyTelemetry)sentTelemetry;
Assert.AreEqual("200", item.ResultCode);

var requestId = item.Id;
Assert.AreEqual(requestId, request.Headers["Request-Id"]);
Assert.AreEqual(requestId, request.Headers["x-ms-request-id"]);
Assert.IsTrue(requestId.StartsWith(parent.Id));
Assert.AreNotEqual(parent.Id, requestId);
Assert.AreEqual("v", item.Context.Properties["k"]);

Assert.AreEqual("k=v", request.Headers["Correlation-Context"]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<DelaySign>true</DelaySign>
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Microsoft.AI.DependencyCollector.Tests</AssemblyName>
<AssemblyOriginatorKeyFile>../../../Keys/35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
Expand All @@ -16,6 +17,13 @@
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Shared.Tests\Implementation\DependencyCollectorDiagnosticListenerTests.Netstandard16.cs" Link="DependencyCollectorDiagnosticListenerTests.Netstandard16.cs" />
<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" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetCore\DependencyCollector.NetCore.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit df9c452

Please sign in to comment.