Skip to content

Commit 3b0185d

Browse files
Mikhail-msftLiudmila Molkova
authored andcommitted
parseHeaders flag support (#47)
* parseHeaders flag support
1 parent 2bee47d commit 3b0185d

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/Microsoft.AspNet.TelemetryCorrelation/ActivityHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,19 @@ public static void StopRestoredActivity(Activity activity, HttpContext context)
141141
/// Creates root (first level) activity that describes incoming request.
142142
/// </summary>
143143
/// <param name="context">Current HttpContext.</param>
144+
/// <param name="parseHeaders">Determines if headers should be parsed get correlation ids.</param>
144145
/// <returns>New root activity.</returns>
145-
public static Activity CreateRootActivity(HttpContext context)
146+
public static Activity CreateRootActivity(HttpContext context, bool parseHeaders)
146147
{
147148
if (AspNetListener.IsEnabled() && AspNetListener.IsEnabled(AspNetActivityName))
148149
{
149150
var rootActivity = new Activity(AspNetActivityName);
150151

151-
rootActivity.Extract(context.Request.Unvalidated.Headers);
152+
if (parseHeaders)
153+
{
154+
rootActivity.Extract(context.Request.Unvalidated.Headers);
155+
}
156+
152157
if (StartAspNetActivity(rootActivity))
153158
{
154159
context.Items[ActivityKey] = rootActivity;

src/Microsoft.AspNet.TelemetryCorrelation/TelemetryCorrelationHttpModule.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.ComponentModel;
56
using System.Diagnostics;
67
using System.Reflection;
78
using System.Web;
@@ -21,6 +22,12 @@ static TelemetryCorrelationHttpModule()
2122
onStepMethodInfo = typeof(HttpApplication).GetMethod("OnExecuteRequestStep");
2223
}
2324

25+
/// <summary>
26+
/// Gets or sets a value indicating whether TelemetryCorrelationHttpModule should parse headers to get correlation ids.
27+
/// </summary>
28+
[EditorBrowsable(EditorBrowsableState.Never)]
29+
public bool ParseHeaders { get; set; } = true;
30+
2431
/// <inheritdoc />
2532
public void Dispose()
2633
{
@@ -78,7 +85,7 @@ private void Application_BeginRequest(object sender, EventArgs e)
7885
{
7986
var context = ((HttpApplication)sender).Context;
8087
AspNetTelemetryCorrelationEventSource.Log.TraceCallback("Application_BeginRequest");
81-
ActivityHelper.CreateRootActivity(context);
88+
ActivityHelper.CreateRootActivity(context, ParseHeaders);
8289
context.Items[BeginCalledFlag] = true;
8390
}
8491

@@ -99,7 +106,7 @@ private void Application_EndRequest(object sender, EventArgs e)
99106
if (!context.Items.Contains(BeginCalledFlag))
100107
{
101108
// Activity has never been started
102-
var activity = ActivityHelper.CreateRootActivity(context);
109+
var activity = ActivityHelper.CreateRootActivity(context, ParseHeaders);
103110
ActivityHelper.StopAspNetActivity(activity, context.Items);
104111
}
105112
else

test/Microsoft.AspNet.TelemetryCorrelation.Tests/ActivityHelperTest.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public void Stop_Root_Activity_With_129_Nesting_Depth()
259259
public void Should_Not_Create_RootActivity_If_AspNetListener_Not_Enabled()
260260
{
261261
var context = HttpContextHelper.GetFakeHttpContext();
262-
var rootActivity = ActivityHelper.CreateRootActivity(context);
262+
var rootActivity = ActivityHelper.CreateRootActivity(context, true);
263263

264264
Assert.Null(rootActivity);
265265
}
@@ -269,7 +269,7 @@ public void Should_Not_Create_RootActivity_If_AspNetActivity_Not_Enabled()
269269
{
270270
var context = HttpContextHelper.GetFakeHttpContext();
271271
this.EnableAspNetListenerOnly();
272-
var rootActivity = ActivityHelper.CreateRootActivity(context);
272+
var rootActivity = ActivityHelper.CreateRootActivity(context, true);
273273

274274
Assert.Null(rootActivity);
275275
}
@@ -279,7 +279,7 @@ public void Should_Not_Create_RootActivity_If_AspNetActivity_Not_Enabled_With_Ar
279279
{
280280
var context = HttpContextHelper.GetFakeHttpContext();
281281
this.EnableAspNetListenerAndDisableActivity();
282-
var rootActivity = ActivityHelper.CreateRootActivity(context);
282+
var rootActivity = ActivityHelper.CreateRootActivity(context, true);
283283

284284
Assert.Null(rootActivity);
285285
}
@@ -289,27 +289,45 @@ public void Can_Create_RootActivity_And_Restore_Info_From_Request_Header()
289289
{
290290
var requestHeaders = new Dictionary<string, string>
291291
{
292-
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b2cab6.1" },
292+
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b2cab6.1." },
293293
{ ActivityExtensions.CorrelationContextHeaderName, this.baggageInHeader }
294294
};
295295

296296
var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);
297297
this.EnableAspNetListenerAndActivity();
298-
var rootActivity = ActivityHelper.CreateRootActivity(context);
298+
var rootActivity = ActivityHelper.CreateRootActivity(context, true);
299299

300300
Assert.NotNull(rootActivity);
301-
Assert.True(rootActivity.ParentId == "|aba2f1e978b2cab6.1");
301+
Assert.True(rootActivity.ParentId == "|aba2f1e978b2cab6.1.");
302302
var expectedBaggage = this.baggageItems.OrderBy(item => item.Value);
303303
var actualBaggage = rootActivity.Baggage.OrderBy(item => item.Value);
304304
Assert.Equal(expectedBaggage, actualBaggage);
305305
}
306306

307+
[Fact]
308+
public void Can_Create_RootActivity_And_Ignore_Info_From_Request_Header_If_ParseHeaders_Is_False()
309+
{
310+
var requestHeaders = new Dictionary<string, string>
311+
{
312+
{ ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b2cab6.1." },
313+
{ ActivityExtensions.CorrelationContextHeaderName, this.baggageInHeader }
314+
};
315+
316+
var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);
317+
this.EnableAspNetListenerAndActivity();
318+
var rootActivity = ActivityHelper.CreateRootActivity(context, parseHeaders: false);
319+
320+
Assert.NotNull(rootActivity);
321+
Assert.Null(rootActivity.ParentId);
322+
Assert.Empty(rootActivity.Baggage);
323+
}
324+
307325
[Fact]
308326
public void Can_Create_RootActivity_And_Start_Activity()
309327
{
310328
var context = HttpContextHelper.GetFakeHttpContext();
311329
this.EnableAspNetListenerAndActivity();
312-
var rootActivity = ActivityHelper.CreateRootActivity(context);
330+
var rootActivity = ActivityHelper.CreateRootActivity(context, true);
313331

314332
Assert.NotNull(rootActivity);
315333
Assert.True(!string.IsNullOrEmpty(rootActivity.Id));
@@ -320,7 +338,7 @@ public void Can_Create_RootActivity_And_Saved_In_HttContext()
320338
{
321339
var context = HttpContextHelper.GetFakeHttpContext();
322340
this.EnableAspNetListenerAndActivity();
323-
var rootActivity = ActivityHelper.CreateRootActivity(context);
341+
var rootActivity = ActivityHelper.CreateRootActivity(context, true);
324342

325343
Assert.NotNull(rootActivity);
326344
Assert.Same(rootActivity, context.Items[ActivityHelper.ActivityKey]);

0 commit comments

Comments
 (0)