Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 6e6bec6

Browse files
committed
Added EventCounters to Hosting
1 parent d5ec085 commit 6e6bec6

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/Microsoft.AspNetCore.Hosting/Internal/HostingApplication.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ public Context CreateContext(IFeatureCollection contextFeatures)
4242
{
4343
var httpContext = _httpContextFactory.Create(contextFeatures);
4444

45+
var eventLogEnabled = HostingEventSource.Log.IsEnabled();
46+
4547
// These enabled checks are virtual dispatch and used twice and so cache to locals
4648
var diagnoticsEnabled = _diagnosticSource.IsEnabled(DiagnosticsBeginRequestKey);
4749
var loggingEnabled = _logger.IsEnabled(LogLevel.Information);
4850

49-
if (HostingEventSource.Log.IsEnabled())
51+
if (eventLogEnabled)
5052
{
5153
// To keep the hot path short we defer logging in this function to non-inlines
5254
RecordRequestStartEventLog(httpContext);
5355
}
5456

5557
// Only make call GetTimestamp if its value will be used, i.e. of the listenters is enabled
56-
var startTimestamp = (diagnoticsEnabled || loggingEnabled) ? Stopwatch.GetTimestamp() : 0;
58+
var startTimestamp = (diagnoticsEnabled || loggingEnabled || eventLogEnabled) ? Stopwatch.GetTimestamp() : 0;
5759

5860
// Scope may be relevant for a different level of logging, so we always create it
5961
// see: https://github.com/aspnet/Hosting/pull/944
@@ -107,7 +109,7 @@ public void DisposeContext(Context context, Exception exception)
107109
// Diagnostics is enabled for EndRequest, but it may not be for BeginRequest
108110
// so call GetTimestamp if currentTimestamp is zero (from above)
109111
RecordEndRequestDiagnostics(
110-
httpContext,
112+
httpContext,
111113
(currentTimestamp != 0) ? currentTimestamp : Stopwatch.GetTimestamp());
112114
}
113115
}
@@ -119,8 +121,8 @@ public void DisposeContext(Context context, Exception exception)
119121
// Diagnostics is enabled for UnhandledException, but it may not be for BeginRequest
120122
// so call GetTimestamp if currentTimestamp is zero (from above)
121123
RecordUnhandledExceptionDiagnostics(
122-
httpContext,
123-
(currentTimestamp != 0) ? currentTimestamp : Stopwatch.GetTimestamp(),
124+
httpContext,
125+
(currentTimestamp != 0) ? currentTimestamp : Stopwatch.GetTimestamp(),
124126
exception);
125127
}
126128

@@ -146,7 +148,7 @@ public void DisposeContext(Context context, Exception exception)
146148
if (eventLogEnabled)
147149
{
148150
// Non-inline
149-
hostingEventLog.RequestStop();
151+
hostingEventLog.RequestStop(startTimestamp, currentTimestamp);
150152
}
151153
}
152154

src/Microsoft.AspNetCore.Hosting/Internal/HostingEventSource.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,22 @@ public sealed class HostingEventSource : EventSource
1111
{
1212
public static readonly HostingEventSource Log = new HostingEventSource();
1313

14-
private HostingEventSource() { }
14+
#if NETSTANDARD1_5
15+
private EventCounter _requestCounter = null;
16+
private EventCounter _successfulRequestCounter = null;
17+
private EventCounter _failedRequestCounter = null;
18+
private EventCounter _requestExecutionTimeCounter = null;
19+
#endif
20+
21+
private HostingEventSource()
22+
{
23+
#if NETSTANDARD1_5
24+
_requestCounter = new EventCounter("Request", this);
25+
_successfulRequestCounter = new EventCounter("SuccessfulRequest", this);
26+
_failedRequestCounter = new EventCounter("FailedRequest", this);
27+
_requestExecutionTimeCounter = new EventCounter("RequestExecutionTime", this);
28+
#endif
29+
}
1530

1631
// NOTE
1732
// - The 'Start' and 'Stop' suffixes on the following event names have special meaning in EventSource. They
@@ -35,12 +50,29 @@ public void HostStop()
3550
[Event(3, Level = EventLevel.Informational)]
3651
public void RequestStart(string method, string path)
3752
{
53+
#if NETSTANDARD1_5
54+
_requestCounter.WriteMetric(1);
55+
#endif
3856
WriteEvent(3, method, path);
3957
}
4058

4159
[MethodImpl(MethodImplOptions.NoInlining)]
60+
[NonEvent]
61+
public void RequestStop(long startTimestamp, long endTimestamp)
62+
{
63+
#if NETSTANDARD1_5
64+
_successfulRequestCounter.WriteMetric(1);
65+
66+
if (endTimestamp != 0)
67+
{
68+
_requestExecutionTimeCounter.WriteMetric(endTimestamp - startTimestamp);
69+
}
70+
#endif
71+
RequestStop();
72+
}
73+
4274
[Event(4, Level = EventLevel.Informational)]
43-
public void RequestStop()
75+
private void RequestStop()
4476
{
4577
WriteEvent(4);
4678
}
@@ -49,7 +81,11 @@ public void RequestStop()
4981
[Event(5, Level = EventLevel.Error)]
5082
public void UnhandledException()
5183
{
84+
#if NETSTANDARD1_5
85+
_failedRequestCounter.WriteMetric(1);
86+
#endif
5287
WriteEvent(5);
5388
}
89+
5490
}
55-
}
91+
}

src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public WebHost(
7070
_hostingServiceProvider = hostingServiceProvider;
7171
_applicationServiceCollection.AddSingleton<IApplicationLifetime, ApplicationLifetime>();
7272
_applicationServiceCollection.AddSingleton<HostedServiceExecutor>();
73+
74+
// This is here for the sole reason to load it as fast as possible in the application domain and be found
75+
// by the EventSource.GetSources() api.
76+
var eventSource = HostingEventSource.Log;
7377
}
7478

7579
public IServiceProvider Services

test/Microsoft.AspNetCore.Hosting.Tests/Internal/HostingEventSourceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void RequestStop()
151151
eventListener.EnableEvents(hostingEventSource, EventLevel.Informational);
152152

153153
// Act
154-
hostingEventSource.RequestStop();
154+
hostingEventSource.RequestStop(startTimestamp: 0, endTimestamp: 0);
155155

156156
// Assert
157157
var eventData = eventListener.EventData;

0 commit comments

Comments
 (0)