From ca3259f7034f459cc683a9f2d888105429f3a1b6 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Mon, 2 Mar 2015 16:52:56 -0800 Subject: [PATCH] Implementing IRequestIdentifierFeature Using code from HttpListener codebase to generate trace ids just to be consistent with other code. --- .../FeatureContext.cs | 14 ++++++++++++-- .../RequestProcessing/RequestContext.cs | 13 +++++++++++++ .../RequestTests.cs | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs b/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs index 8a2e919c6a..2d1e40c6b5 100644 --- a/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs +++ b/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs @@ -22,7 +22,6 @@ using System.Net; using System.Net.WebSockets; using System.Security.Claims; using System.Security.Cryptography.X509Certificates; -using System.Security.Principal; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.FeatureModel; @@ -42,7 +41,8 @@ namespace Microsoft.AspNet.Server.WebListener IHttpRequestLifetimeFeature, IHttpWebSocketFeature, IHttpAuthenticationFeature, - IHttpUpgradeFeature + IHttpUpgradeFeature, + IRequestIdentifierFeature { private RequestContext _requestContext; private FeatureCollection _features; @@ -110,6 +110,8 @@ namespace Microsoft.AspNet.Server.WebListener _features.Add(typeof(IHttpWebSocketFeature), this); } + _features.Add(typeof(IRequestIdentifierFeature), this); + // TODO: /* Server @@ -432,5 +434,13 @@ namespace Microsoft.AspNet.Server.WebListener get { return _authHandler; } set { _authHandler = value; } } + + Guid IRequestIdentifierFeature.TraceIdentifier + { + get + { + return _requestContext.TraceIdentifier; + } + } } } diff --git a/src/Microsoft.Net.Http.Server/RequestProcessing/RequestContext.cs b/src/Microsoft.Net.Http.Server/RequestProcessing/RequestContext.cs index 4a5d7058a4..b2232554ca 100644 --- a/src/Microsoft.Net.Http.Server/RequestProcessing/RequestContext.cs +++ b/src/Microsoft.Net.Http.Server/RequestProcessing/RequestContext.cs @@ -132,6 +132,19 @@ namespace Microsoft.Net.Http.Server } } + public unsafe Guid TraceIdentifier + { + get + { + // This is the base GUID used by HTTP.SYS for generating the activity ID. + // HTTP.SYS overwrites the first 8 bytes of the base GUID with RequestId to generate ETW activity ID. + + var guid = new Guid(0xffcb4c93, 0xa57f, 0x453c, 0xb6, 0x3f, 0x84, 0x71, 0xc, 0x79, 0x67, 0xbb); + *((ulong*)&guid) = Request.RequestId; + return guid; + } + } + /// /// The authentication challengest that will be added to the response if the status code is 401. /// This must be a subset of the AuthenticationSchemes enabled on the server. diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs index 3730f1ec1e..47bbb4d63d 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs @@ -63,6 +63,11 @@ namespace Microsoft.AspNet.Server.WebListener Assert.NotEqual(0, connectionInfo.LocalPort); Assert.True(connectionInfo.IsLocal); + // Trace identifier + var requestIdentifierFeature = httpContext.GetFeature(); + Assert.NotNull(requestIdentifierFeature); + Assert.NotNull(requestIdentifierFeature.TraceIdentifier); + // Note: Response keys are validated in the ResponseTests } catch (Exception ex) @@ -95,12 +100,17 @@ namespace Microsoft.AspNet.Server.WebListener { var requestInfo = httpContext.GetFeature(); var connectionInfo = httpContext.GetFeature(); + var requestIdentifierFeature = httpContext.GetFeature(); // Request Keys Assert.Equal("http", requestInfo.Scheme); Assert.Equal(expectedPath, requestInfo.Path); Assert.Equal(expectedPathBase, requestInfo.PathBase); Assert.Equal(string.Empty, requestInfo.QueryString); + + // Trace identifier + Assert.NotNull(requestIdentifierFeature); + Assert.NotNull(requestIdentifierFeature.TraceIdentifier); } catch (Exception ex) { @@ -135,10 +145,15 @@ namespace Microsoft.AspNet.Server.WebListener { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var requestInfo = httpContext.GetFeature(); + var requestIdentifierFeature = httpContext.GetFeature(); try { Assert.Equal(expectedPath, requestInfo.Path); Assert.Equal(expectedPathBase, requestInfo.PathBase); + + // Trace identifier + Assert.NotNull(requestIdentifierFeature); + Assert.NotNull(requestIdentifierFeature.TraceIdentifier); } catch (Exception ex) {