Skip to content

Commit 0a09fce

Browse files
authored
Implemented InternalServerErrorOccurred event (#27)
1 parent 4827552 commit 0a09fce

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

src/Http.Tests/RequestActivityEventSourceTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Diagnostics.Tracing;
34
using System.Linq;
45
using Thor.Analyzer;
@@ -242,6 +243,34 @@ public void Stop_HttpResponse_Null()
242243

243244
#endregion
244245

246+
#region Outer Activity Warning
247+
248+
[Fact(DisplayName = "OuterActivityNotAllowed: Should log a outer activity is not allowed")]
249+
public void OuterActivityNotAllowed()
250+
{
251+
RequestActivityEventSource.Log.Listen((listener) =>
252+
{
253+
// arrange
254+
const string expectedMessage = "Outer activities are not allowed when creating a server-side message activity.";
255+
Guid activityId = Guid.NewGuid();
256+
257+
// act
258+
RequestActivityEventSource.Log.OuterActivityNotAllowed(activityId);
259+
260+
// assert
261+
TelemetryEvent firstItem = listener
262+
.OrderedEvents
263+
.Select(e => e.Map("7778"))
264+
.FirstOrDefault(e => e.Message == expectedMessage);
265+
266+
Assert.NotNull(firstItem);
267+
Assert.Equal(EventLevel.Warning, firstItem.Level);
268+
AssertItem(firstItem, 0, activityId, expectedMessage);
269+
});
270+
}
271+
272+
#endregion
273+
245274
#region Begin/End Transfer Events
246275

247276
[Fact(DisplayName = "BeginTransfer: Should log a begin transfer")]
@@ -296,6 +325,44 @@ public void EndTransfer()
296325

297326
#endregion
298327

328+
#region Internal Server Error
329+
330+
[Fact(DisplayName = "InternalServerErrorOccurred: Should log a internal server error exception")]
331+
public void InternalServerErrorOccurred()
332+
{
333+
RequestActivityEventSource.Log.Listen((listener) =>
334+
{
335+
// arrange
336+
ConcurrentQueue<AttachmentDescriptor> attachments = new ConcurrentQueue<AttachmentDescriptor>();
337+
const string expectedMessage = "Internal server error occurred.";
338+
Exception exception = new Exception();
339+
AttachmentDispatcher.Instance.Attach(d => attachments.Enqueue(d));
340+
341+
// act
342+
RequestActivityEventSource.Log.InternalServerErrorOccurred(exception);
343+
344+
// assert
345+
AttachmentDispatcher.Instance.Detach(d => attachments.Enqueue(d));
346+
TelemetryEvent firstItem = listener
347+
.OrderedEvents
348+
.Select(e => e.Map("7779"))
349+
.FirstOrDefault(e => e.Message == expectedMessage);
350+
351+
Assert.NotNull(firstItem);
352+
Assert.Equal(EventLevel.Error, firstItem.Level);
353+
AssertItem(firstItem, 0, Guid.Empty, expectedMessage);
354+
Assert.Collection(attachments,
355+
d =>
356+
{
357+
Assert.Matches("^\\d{8}\\-[a-f\\d]{32}$", d.Id);
358+
Assert.Equal("exception", d.Name);
359+
Assert.Equal(nameof(Exception), d.TypeName);
360+
});
361+
});
362+
}
363+
364+
#endregion
365+
299366
private static void AssertItem(TelemetryEvent item, int expectedCount,
300367
Guid expectedActivityId, Guid? expectedUserId, string expectedMessage)
301368
{

src/Http/RequestActivityEventSource.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal sealed class RequestActivityEventSource
1515
private const int _beginTransferEventId = 5;
1616
private const int _endTransferEventId = 6;
1717
private const int _outerActivityWarningEventId = 7;
18+
private const int _internalServerErrorEventId = 8;
1819

1920
public static RequestActivityEventSource Log { get; } = new RequestActivityEventSource();
2021

@@ -143,7 +144,8 @@ private void Stop(int applicationId, Guid activityId, Guid userId, int statusCod
143144
#region Outer Activity Warning
144145

145146
/// <summary>
146-
/// A warning regarding outer activities which were not allowed when opening a new server-side message activity.
147+
/// A warning regarding outer activities which are not allowed when opening a new
148+
/// server-side message activity.
147149
/// </summary>
148150
[NonEvent]
149151
public void OuterActivityNotAllowed(Guid activityId)
@@ -201,6 +203,58 @@ private void EndTransfer(Guid relatedActivityId, int applicationId, Guid activit
201203

202204
#endregion
203205

206+
#region Internal Server Error
207+
208+
/// <summary>
209+
/// A internal server error occurred during request/response processing.
210+
/// </summary>
211+
[NonEvent]
212+
public void InternalServerErrorOccurred(Exception exception)
213+
{
214+
if (IsEnabled())
215+
{
216+
AttachmentId attachmentId = AttachmentId.NewId();
217+
ExceptionAttachment attachment = AttachmentFactory.Create(attachmentId,
218+
nameof(exception), exception);
219+
220+
AttachmentDispatcher.Instance.Dispatch(attachment);
221+
InternalServerErrorOccurred(Application.Id, ActivityStack.Id, attachmentId);
222+
}
223+
}
224+
225+
[Event(_internalServerErrorEventId, Level = EventLevel.Error,
226+
Message = "Internal server error occurred.", Version = 1)]
227+
private void InternalServerErrorOccurred(int applicationId, Guid activityId,
228+
string attachmentId)
229+
{
230+
InternalServerErrorOccurredCore(_internalServerErrorEventId, applicationId, activityId,
231+
attachmentId);
232+
}
233+
234+
[NonEvent]
235+
private unsafe void InternalServerErrorOccurredCore(int eventId, int applicationId,
236+
Guid activityId, string attachmentId)
237+
{
238+
StringExtensions.SetToEmptyIfNull(ref attachmentId);
239+
240+
fixed (char* attachmentIdBytes = attachmentId)
241+
{
242+
const short dataCount = 3;
243+
EventData* data = stackalloc EventData[dataCount];
244+
245+
data[0].DataPointer = (IntPtr)(&applicationId);
246+
data[0].Size = 4;
247+
data[1].DataPointer = (IntPtr)(&activityId);
248+
data[1].Size = 16;
249+
data[2].DataPointer = (IntPtr)attachmentIdBytes;
250+
data[2].Size = ((attachmentId.Length + 1) * 2);
251+
252+
WriteEventCore(eventId, dataCount, data);
253+
}
254+
}
255+
256+
#endregion
257+
204258
#region Tasks and Opcodes
205259

206260
public static class Tasks

0 commit comments

Comments
 (0)