* add event register package

* added the stylecop

* add all misc files
This commit is contained in:
Sergey Kanzhelev 2017-05-04 21:48:51 -07:00 коммит произвёл Jinhuafei
Родитель 8b59db4fd2
Коммит 050d93817c
16 изменённых файлов: 292 добавлений и 102 удалений

4
CONTRIBUTING.md Normal file
Просмотреть файл

@ -0,0 +1,4 @@
Contributing
======
Information on contributing to this repo is in the [Contributing Guide](https://github.com/aspnet/Home/blob/dev/CONTRIBUTING.md) in the Home repo.

12
LICENSE.txt Normal file
Просмотреть файл

@ -0,0 +1,12 @@
Copyright (c) .NET Foundation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
these files except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

13
README.md Normal file
Просмотреть файл

@ -0,0 +1,13 @@
# Telemetry correlation http module
Telemetry correlation http module enables cross tier telemetry tracking.
- Reads http headers
- Start/Stops Activity for the http request
- Ensure the Activity ambient state is transferred thru the IIS callbacks
See http protocol [specifications](https://github.com/lmolkova/corefx/blob/80e8a8d767a71f413fd7b2f11b507cd395110e8d/src/System.Diagnostics.DiagnosticSource/src/FlatRequestId.md) for details.
This http module is used by Application Insights. See [documentation](https://docs.microsoft.com/en-us/azure/application-insights/application-insights-correlation) and [code](https://github.com/Microsoft/ApplicationInsights-dotnet-server).

Просмотреть файл

@ -1,4 +1,7 @@
using System;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
@ -12,7 +15,14 @@ namespace Microsoft.AspNet.TelemetryCorrelation
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ActivityExtensions
{
/// <summary>
/// Http header name to carry the Request ID.
/// </summary>
internal const string RequestIDHeaderName = "Request-Id";
/// <summary>
/// Http header name to carry the correlation context.
/// </summary>
internal const string CorrelationContextHeaderName = "Correlation-Context";
/// <summary>
@ -20,23 +30,24 @@ namespace Microsoft.AspNet.TelemetryCorrelation
/// </summary>
/// <param name="activity">Instance of activity that has not been started yet.</param>
/// <param name="requestHeaders">Request headers collection.</param>
/// <returns>true if request was parsed successfully, false - otherwise.</returns>
public static bool Extract(this Activity activity, NameValueCollection requestHeaders)
{
if (activity == null)
{
AspNetTelemetryCorrelaitonEventSource.Log.ActvityExtractionError("activity is null");
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("activity is null");
return false;
}
if (activity.ParentId != null)
{
AspNetTelemetryCorrelaitonEventSource.Log.ActvityExtractionError("ParentId is already set on activity");
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("ParentId is already set on activity");
return false;
}
if (activity.Id != null)
{
AspNetTelemetryCorrelaitonEventSource.Log.ActvityExtractionError("Activity is already started");
AspNetTelemetryCorrelationEventSource.Log.ActvityExtractionError("Activity is already started");
return false;
}
@ -46,23 +57,22 @@ namespace Microsoft.AspNet.TelemetryCorrelation
// there may be several Request-Id header, but we only read the first one
activity.SetParentId(requestIDs[0]);
// Header format - Correlation-Context: key1=value1, key2=value2
// Header format - Correlation-Context: key1=value1, key2=value2
var baggages = requestHeaders.GetValues(CorrelationContextHeaderName);
if (baggages != null)
{
// there may be several Correlation-Context header
// there may be several Correlation-Context header
foreach (var item in baggages)
{
foreach (var pair in item.Split(','))
{
NameValueHeaderValue baggageItem;
if (NameValueHeaderValue.TryParse(pair, out baggageItem))
if (NameValueHeaderValue.TryParse(pair, out NameValueHeaderValue baggageItem))
{
activity.AddBaggage(baggageItem.Name, baggageItem.Value);
}
else
{
AspNetTelemetryCorrelaitonEventSource.Log.HeaderParsingError(CorrelationContextHeaderName, pair);
AspNetTelemetryCorrelationEventSource.Log.HeaderParsingError(CorrelationContextHeaderName, pair);
}
}
}
@ -74,10 +84,12 @@ namespace Microsoft.AspNet.TelemetryCorrelation
return false;
}
/// <summary>
///
/// Reads Request-Id and Correlation-Context headers and sets ParentId and Baggage on Activity.
/// </summary>
/// <param name="activity">Instance of activity that has not been started yet.</param>
/// <param name="requestHeaders">Request headers collection.</param>
/// <returns>true if request was parsed successfully, false - otherwise.</returns>
[Obsolete("Method is obsolete, use Extract method instead", true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool TryParse(this Activity activity, NameValueCollection requestHeaders)

Просмотреть файл

@ -1,4 +1,7 @@
using System.Diagnostics;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Web;
namespace Microsoft.AspNet.TelemetryCorrelation
@ -8,13 +11,32 @@ namespace Microsoft.AspNet.TelemetryCorrelation
/// </summary>
internal static class ActivityHelper
{
/// <summary>
/// Listener name.
/// </summary>
public const string AspNetListenerName = "Microsoft.AspNet.TelemetryCorrelation";
/// <summary>
/// Activity name for http request.
/// </summary>
public const string AspNetActivityName = "Microsoft.AspNet.HttpReqIn";
/// <summary>
/// Event name for the activity start event.
/// </summary>
public const string AspNetActivityStartName = "Microsoft.AspNet.HttpReqIn.Start";
/// <summary>
/// Event name for the activity stop event.
/// </summary>
public const string AspNetActivityLostStopName = "Microsoft.AspNet.HttpReqIn.ActivityLost.Stop";
/// <summary>
/// Key to store the activity in HttpContext.
/// </summary>
public const string ActivityKey = "__AspnetActivity__";
private static readonly DiagnosticListener s_aspNetListener = new DiagnosticListener(AspNetListenerName);
private static readonly DiagnosticListener AspNetListener = new DiagnosticListener(AspNetListenerName);
/// <summary>
/// It's possible that a request is executed in both native threads and managed threads,
@ -22,6 +44,7 @@ namespace Microsoft.AspNet.TelemetryCorrelation
/// This method is intended to restore the current activity in order to correlate the child
/// activities with the root activity of the request.
/// </summary>
/// <param name="root">Root activity id for the current request.</param>
/// <returns>If it returns an activity, it will be silently stopped with the parent activity</returns>
public static Activity RestoreCurrentActivity(Activity root)
{
@ -32,13 +55,14 @@ namespace Microsoft.AspNet.TelemetryCorrelation
var childActivity = new Activity(root.OperationName);
childActivity.SetParentId(root.Id);
childActivity.SetStartTime(root.StartTimeUtc);
foreach(var item in root.Baggage)
foreach (var item in root.Baggage)
{
childActivity.AddBaggage(item.Key, item.Value);
}
childActivity.Start();
AspNetTelemetryCorrelaitonEventSource.Log.ActivityStarted(childActivity.Id);
AspNetTelemetryCorrelationEventSource.Log.ActivityStarted(childActivity.Id);
return childActivity;
}
@ -55,9 +79,9 @@ namespace Microsoft.AspNet.TelemetryCorrelation
// if activity is in the stack, stop it with Stop event
if (Activity.Current != null)
{
s_aspNetListener.StopActivity(Activity.Current, new { });
AspNetListener.StopActivity(Activity.Current, new { });
RemoveCurrentActivity(context);
AspNetTelemetryCorrelaitonEventSource.Log.ActivityStopped(activity.Id);
AspNetTelemetryCorrelationEventSource.Log.ActivityStopped(activity.Id);
return true;
}
}
@ -69,15 +93,15 @@ namespace Microsoft.AspNet.TelemetryCorrelation
{
if (activity != null)
{
s_aspNetListener.Write(AspNetActivityLostStopName, new { activity });
AspNetListener.Write(AspNetActivityLostStopName, new { activity });
RemoveCurrentActivity(context);
AspNetTelemetryCorrelaitonEventSource.Log.ActivityStopped(activity.Id, true);
AspNetTelemetryCorrelationEventSource.Log.ActivityStopped(activity.Id, true);
}
}
public static Activity CreateRootActivity(HttpContext context)
{
if (s_aspNetListener.IsEnabled() && s_aspNetListener.IsEnabled(AspNetActivityName))
if (AspNetListener.IsEnabled() && AspNetListener.IsEnabled(AspNetActivityName))
{
var rootActivity = new Activity(ActivityHelper.AspNetActivityName);
@ -85,25 +109,27 @@ namespace Microsoft.AspNet.TelemetryCorrelation
if (StartAspNetActivity(rootActivity))
{
SaveCurrentActivity(context, rootActivity);
AspNetTelemetryCorrelaitonEventSource.Log.ActivityStarted(rootActivity.Id);
AspNetTelemetryCorrelationEventSource.Log.ActivityStarted(rootActivity.Id);
return rootActivity;
}
}
return null;
}
private static bool StartAspNetActivity(Activity activity)
{
if (s_aspNetListener.IsEnabled(AspNetActivityName, activity, new { }))
if (AspNetListener.IsEnabled(AspNetActivityName, activity, new { }))
{
if (s_aspNetListener.IsEnabled(AspNetActivityStartName))
if (AspNetListener.IsEnabled(AspNetActivityStartName))
{
s_aspNetListener.StartActivity(activity, new { });
AspNetListener.StartActivity(activity, new { });
}
else
{
activity.Start();
}
return true;
}
@ -111,9 +137,10 @@ namespace Microsoft.AspNet.TelemetryCorrelation
}
/// <summary>
/// This should be called after the Activity starts
/// and only for root activity of a request
/// This should be called after the Activity starts and only for root activity of a request.
/// </summary>
/// <param name="context">Context to save context to.</param>
/// <param name="activity">Activity to save.</param>
private static void SaveCurrentActivity(HttpContext context, Activity activity)
{
Debug.Assert(context != null);

Просмотреть файл

@ -1,4 +1,7 @@
using System.Diagnostics.Tracing;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.Tracing;
namespace Microsoft.AspNet.TelemetryCorrelation
{
@ -6,12 +9,12 @@ namespace Microsoft.AspNet.TelemetryCorrelation
/// ETW EventSource tracing class.
/// </summary>
[EventSource(Name = "Microsoft-AspNet-Telemetry-Correlation", Guid = "ace2021e-e82c-5502-d81d-657f27612673")]
internal sealed class AspNetTelemetryCorrelaitonEventSource : EventSource
internal sealed class AspNetTelemetryCorrelationEventSource : EventSource
{
/// <summary>
/// Instance of the PlatformEventSource class.
/// </summary>
public static readonly AspNetTelemetryCorrelaitonEventSource Log = new AspNetTelemetryCorrelaitonEventSource();
public static readonly AspNetTelemetryCorrelationEventSource Log = new AspNetTelemetryCorrelationEventSource();
[Event(1, Message = "Callback='{0}'", Level = EventLevel.Verbose)]
public void TraceCallback(string callback)

Просмотреть файл

@ -23,6 +23,8 @@
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -31,6 +33,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>Microsoft.AspNet.TelemetryCorrelation.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -38,6 +42,8 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>Microsoft.AspNet.TelemetryCorrelation.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -55,11 +61,24 @@
<Compile Include="ActivityExtensions.cs" />
<Compile Include="ActivityHelper.cs" />
<Compile Include="TelemetryCorrelationHttpModule.cs" />
<Compile Include="AspNetDiagnosticsEventSource.cs" />
<Compile Include="AspNetTelemetryCorrelationEventSource.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Microsoft.AspNet.TelemetryCorrelation.ruleset" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\Newtonsoft.Json.dll" />
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\Microsoft.Diagnostics.Tracing.EventRegister.1.1.28\build\Microsoft.Diagnostics.Tracing.EventRegister.targets" Condition="Exists('..\..\packages\Microsoft.Diagnostics.Tracing.EventRegister.1.1.28\build\Microsoft.Diagnostics.Tracing.EventRegister.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Diagnostics.Tracing.EventRegister.1.1.28\build\Microsoft.Diagnostics.Tracing.EventRegister.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Diagnostics.Tracing.EventRegister.1.1.28\build\Microsoft.Diagnostics.Tracing.EventRegister.targets'))" />
</Target>
</Project>

Просмотреть файл

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Rules for Microsoft.AspNet.TelemetryCorrelation" Description="Code analysis rules for Microsoft.AspNet.TelemetryCorrelation.csproj." ToolsVersion="15.0">
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1001" Action="Warning" />
<Rule Id="CA1009" Action="Warning" />
<Rule Id="CA1016" Action="Warning" />
<Rule Id="CA1033" Action="Warning" />
<Rule Id="CA1049" Action="Warning" />
<Rule Id="CA1060" Action="Warning" />
<Rule Id="CA1061" Action="Warning" />
<Rule Id="CA1063" Action="Warning" />
<Rule Id="CA1065" Action="Warning" />
<Rule Id="CA1301" Action="Warning" />
<Rule Id="CA1400" Action="Warning" />
<Rule Id="CA1401" Action="Warning" />
<Rule Id="CA1403" Action="Warning" />
<Rule Id="CA1404" Action="Warning" />
<Rule Id="CA1405" Action="Warning" />
<Rule Id="CA1410" Action="Warning" />
<Rule Id="CA1415" Action="Warning" />
<Rule Id="CA1821" Action="Warning" />
<Rule Id="CA1900" Action="Warning" />
<Rule Id="CA1901" Action="Warning" />
<Rule Id="CA2002" Action="Warning" />
<Rule Id="CA2100" Action="Warning" />
<Rule Id="CA2101" Action="Warning" />
<Rule Id="CA2108" Action="Warning" />
<Rule Id="CA2111" Action="Warning" />
<Rule Id="CA2112" Action="Warning" />
<Rule Id="CA2114" Action="Warning" />
<Rule Id="CA2116" Action="Warning" />
<Rule Id="CA2117" Action="Warning" />
<Rule Id="CA2122" Action="Warning" />
<Rule Id="CA2123" Action="Warning" />
<Rule Id="CA2124" Action="Warning" />
<Rule Id="CA2126" Action="Warning" />
<Rule Id="CA2131" Action="Warning" />
<Rule Id="CA2132" Action="Warning" />
<Rule Id="CA2133" Action="Warning" />
<Rule Id="CA2134" Action="Warning" />
<Rule Id="CA2137" Action="Warning" />
<Rule Id="CA2138" Action="Warning" />
<Rule Id="CA2140" Action="Warning" />
<Rule Id="CA2141" Action="Warning" />
<Rule Id="CA2146" Action="Warning" />
<Rule Id="CA2147" Action="Warning" />
<Rule Id="CA2149" Action="Warning" />
<Rule Id="CA2200" Action="Warning" />
<Rule Id="CA2202" Action="Warning" />
<Rule Id="CA2207" Action="Warning" />
<Rule Id="CA2212" Action="Warning" />
<Rule Id="CA2213" Action="Warning" />
<Rule Id="CA2214" Action="Warning" />
<Rule Id="CA2216" Action="Warning" />
<Rule Id="CA2220" Action="Warning" />
<Rule Id="CA2229" Action="Warning" />
<Rule Id="CA2231" Action="Warning" />
<Rule Id="CA2232" Action="Warning" />
<Rule Id="CA2235" Action="Warning" />
<Rule Id="CA2236" Action="Warning" />
<Rule Id="CA2237" Action="Warning" />
<Rule Id="CA2238" Action="Warning" />
<Rule Id="CA2240" Action="Warning" />
<Rule Id="CA2241" Action="Warning" />
<Rule Id="CA2242" Action="Warning" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1101" Action="None" />
<Rule Id="SA1200" Action="None" />
<Rule Id="SA1405" Action="None" />
<Rule Id="SA1633" Action="None" />
<Rule Id="SX1101" Action="Warning" />
</Rules>
</RuleSet>

Просмотреть файл

@ -22,15 +22,4 @@ using System.Runtime.InteropServices;
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4c8e592c-c532-4cf2-80ef-3bdd0d788d12")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: InternalsVisibleTo("Microsoft.AspNet.TelemetryCorrelation.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]

Просмотреть файл

@ -1,17 +1,25 @@
using System;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Web;
namespace Microsoft.AspNet.TelemetryCorrelation
{
class TelemetryCorrelationHttpModule : IHttpModule
/// <summary>
/// Http Module sets ambient state using Activity API from DiagnosticsSource package.
/// </summary>
public class TelemetryCorrelationHttpModule : IHttpModule
{
private const string BeginCalledFlag = "Microsoft.AspNet.TelemetryCorrelation.BeginCalled";
/// <inheritdoc />
public void Dispose()
{
}
/// <inheritdoc />
public void Init(HttpApplication context)
{
context.BeginRequest += Application_BeginRequest;
@ -22,17 +30,17 @@ namespace Microsoft.AspNet.TelemetryCorrelation
private void Application_BeginRequest(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
AspNetTelemetryCorrelaitonEventSource.Log.TraceCallback("Application_BeginRequest");
AspNetTelemetryCorrelationEventSource.Log.TraceCallback("Application_BeginRequest");
ActivityHelper.CreateRootActivity(context);
context.Items[BeginCalledFlag] = true;
}
private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
AspNetTelemetryCorrelaitonEventSource.Log.TraceCallback("Application_PreRequestHandlerExecute");
AspNetTelemetryCorrelationEventSource.Log.TraceCallback("Application_PreRequestHandlerExecute");
var context = ((HttpApplication)sender).Context;
var rootActivity = (Activity) context.Items[ActivityHelper.ActivityKey];
var rootActivity = (Activity)context.Items[ActivityHelper.ActivityKey];
if (Activity.Current == null && rootActivity != null)
{
ActivityHelper.RestoreCurrentActivity(rootActivity);
@ -41,7 +49,7 @@ namespace Microsoft.AspNet.TelemetryCorrelation
private void Application_EndRequest(object sender, EventArgs e)
{
AspNetTelemetryCorrelaitonEventSource.Log.TraceCallback("Application_EndRequest");
AspNetTelemetryCorrelationEventSource.Log.TraceCallback("Application_EndRequest");
var context = ((HttpApplication)sender).Context;
@ -56,6 +64,7 @@ namespace Microsoft.AspNet.TelemetryCorrelation
else
{
var activity = (Activity)context.Items[ActivityHelper.ActivityKey];
// try to stop activity if it's in the Current stack
if (!ActivityHelper.StopAspNetActivity(activity, context))
{
@ -66,6 +75,6 @@ namespace Microsoft.AspNet.TelemetryCorrelation
}
}
}
}
}
}
}

Просмотреть файл

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Diagnostics.Tracing.EventRegister" version="1.1.28" targetFramework="net45" />
<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="net45" developmentDependency="true" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.0-preview1-25214-03" targetFramework="net45" />
</packages>

Просмотреть файл

@ -1,4 +1,7 @@
using System.Collections.Generic;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
@ -26,10 +29,11 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public void Can_Restore_First_RequestId_When_Multiple_RequestId_In_Headers()
{
var activity = new Activity(TestActivityName);
var requestHeaders = new NameValueCollection();
requestHeaders.Add(ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1");
requestHeaders.Add(ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b22222.1");
var requestHeaders = new NameValueCollection
{
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1" },
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b22222.1" }
};
Assert.True(activity.Extract(requestHeaders));
Assert.Equal("|aba2f1e978b11111.1", activity.ParentId);
@ -40,9 +44,10 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public void Restore_Empty_RequestId_Should_Not_Throw_Exception()
{
var activity = new Activity(TestActivityName);
var requestHeaders = new NameValueCollection();
requestHeaders.Add(ActivityExtensions.RequestIDHeaderName, "");
var requestHeaders = new NameValueCollection
{
{ ActivityExtensions.RequestIDHeaderName, "" }
};
Assert.False(activity.Extract(requestHeaders));
Assert.Null(activity.ParentId);
@ -53,17 +58,20 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public void Can_Restore_Baggages_When_CorrelationContext_In_Headers()
{
var activity = new Activity(TestActivityName);
var requestHeaders = new NameValueCollection();
requestHeaders.Add(ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key1=123,key2=456,key3=789");
var requestHeaders = new NameValueCollection
{
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1" },
{ ActivityExtensions.CorrelationContextHeaderName, "key1=123,key2=456,key3=789" }
};
Assert.True(activity.Extract(requestHeaders));
Assert.Equal("|aba2f1e978b11111.1", activity.ParentId);
var baggageItems = new List<KeyValuePair<string, string>>();
baggageItems.Add(new KeyValuePair<string, string>("key1", "123"));
baggageItems.Add(new KeyValuePair<string, string>("key2", "456"));
baggageItems.Add(new KeyValuePair<string, string>("key3", "789"));
var baggageItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("key1", "123"),
new KeyValuePair<string, string>("key2", "456"),
new KeyValuePair<string, string>("key3", "789")
};
var expectedBaggage = baggageItems.OrderBy(kvp => kvp.Key);
var actualBaggage = activity.Baggage.OrderBy(kvp => kvp.Key);
Assert.Equal(expectedBaggage, actualBaggage);
@ -73,22 +81,25 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public void Can_Restore_Baggages_When_Multiple_CorrelationContext_In_Headers()
{
var activity = new Activity(TestActivityName);
var requestHeaders = new NameValueCollection();
requestHeaders.Add(ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key1=123,key2=456,key3=789");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key4=abc,key5=def");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key6=xyz");
var requestHeaders = new NameValueCollection
{
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1" },
{ ActivityExtensions.CorrelationContextHeaderName, "key1=123,key2=456,key3=789" },
{ ActivityExtensions.CorrelationContextHeaderName, "key4=abc,key5=def" },
{ ActivityExtensions.CorrelationContextHeaderName, "key6=xyz" }
};
Assert.True(activity.Extract(requestHeaders));
Assert.Equal("|aba2f1e978b11111.1", activity.ParentId);
var baggageItems = new List<KeyValuePair<string, string>>();
baggageItems.Add(new KeyValuePair<string, string>("key1", "123"));
baggageItems.Add(new KeyValuePair<string, string>("key2", "456"));
baggageItems.Add(new KeyValuePair<string, string>("key3", "789"));
baggageItems.Add(new KeyValuePair<string, string>("key4", "abc"));
baggageItems.Add(new KeyValuePair<string, string>("key5", "def"));
baggageItems.Add(new KeyValuePair<string, string>("key6", "xyz"));
var baggageItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("key1", "123"),
new KeyValuePair<string, string>("key2", "456"),
new KeyValuePair<string, string>("key3", "789"),
new KeyValuePair<string, string>("key4", "abc"),
new KeyValuePair<string, string>("key5", "def"),
new KeyValuePair<string, string>("key6", "xyz")
};
var expectedBaggage = baggageItems.OrderBy(kvp => kvp.Key);
var actualBaggage = activity.Baggage.OrderBy(kvp => kvp.Key);
Assert.Equal(expectedBaggage, actualBaggage);
@ -98,20 +109,23 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public void Can_Restore_Baggages_When_Some_MalFormat_CorrelationContext_In_Headers()
{
var activity = new Activity(TestActivityName);
var requestHeaders = new NameValueCollection();
requestHeaders.Add(ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key1=123,key2=456,key3=789");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key4=abc;key5=def");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key6????xyz");
requestHeaders.Add(ActivityExtensions.CorrelationContextHeaderName, "key7=123=456");
var requestHeaders = new NameValueCollection
{
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b11111.1" },
{ ActivityExtensions.CorrelationContextHeaderName, "key1=123,key2=456,key3=789" },
{ ActivityExtensions.CorrelationContextHeaderName, "key4=abc;key5=def" },
{ ActivityExtensions.CorrelationContextHeaderName, "key6????xyz" },
{ ActivityExtensions.CorrelationContextHeaderName, "key7=123=456" }
};
Assert.True(activity.Extract(requestHeaders));
Assert.Equal("|aba2f1e978b11111.1", activity.ParentId);
var baggageItems = new List<KeyValuePair<string, string>>();
baggageItems.Add(new KeyValuePair<string, string>("key1", "123"));
baggageItems.Add(new KeyValuePair<string, string>("key2", "456"));
baggageItems.Add(new KeyValuePair<string, string>("key3", "789"));
var baggageItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("key1", "123"),
new KeyValuePair<string, string>("key2", "456"),
new KeyValuePair<string, string>("key3", "789")
};
var expectedBaggage = baggageItems.OrderBy(kvp => kvp.Key);
var actualBaggage = activity.Baggage.OrderBy(kvp => kvp.Key);
Assert.Equal(expectedBaggage, actualBaggage);

Просмотреть файл

@ -1,4 +1,7 @@
using System;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
@ -15,14 +18,17 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public class ActivityHelperTest
{
private const string TestActivityName = "Activity.Test";
private List<KeyValuePair<string, string>> _baggageItems = new List<KeyValuePair<string, string>>();
private string _baggageInHeader;
private readonly List<KeyValuePair<string, string>> _baggageItems;
private readonly string _baggageInHeader;
public ActivityHelperTest()
{
_baggageItems.Add(new KeyValuePair<string, string>("TestKey1", "123"));
_baggageItems.Add(new KeyValuePair<string, string>("TestKey2", "456"));
_baggageItems.Add(new KeyValuePair<string, string>("TestKey1", "789"));
_baggageItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("TestKey1", "123"),
new KeyValuePair<string, string>("TestKey2", "456"),
new KeyValuePair<string, string>("TestKey1", "789")
};
_baggageInHeader = "TestKey1=123,TestKey2=456,TestKey1=789";
// reset static fields
@ -30,7 +36,7 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
GetField("s_allListenerObservable", BindingFlags.Static | BindingFlags.NonPublic);
allListenerField.SetValue(null, null);
var aspnetListenerField = typeof(ActivityHelper).
GetField("s_aspNetListener", BindingFlags.Static | BindingFlags.NonPublic);
GetField("AspNetListener", BindingFlags.Static | BindingFlags.NonPublic);
aspnetListenerField.SetValue(null, new DiagnosticListener(ActivityHelper.AspNetListenerName));
}
@ -266,10 +272,7 @@ namespace Microsoft.AspNet.TelemetryCorrelation.Tests
public void OnNext(KeyValuePair<string, object> value)
{
if(_onNextCallBack != null)
{
_onNextCallBack(value);
}
_onNextCallBack?.Invoke(value);
}
}

Просмотреть файл

@ -1,4 +1,7 @@
using System.Collections.Generic;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;

Просмотреть файл

@ -1,4 +1,7 @@
using System.IO;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Xml.Linq;
using Microsoft.Web.XmlTransform;
using Xunit;

Просмотреть файл

@ -1,4 +1,7 @@
using System.IO;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Xml.Linq;
using Microsoft.Web.XmlTransform;
using Xunit;