Skip to content

Commit

Permalink
Fix call pattern that throws. (#6626)
Browse files Browse the repository at this point in the history
* Fix call pattern that throws.
Change from related activity ids to actually setting the activity id for the current event being logged.

* Avoid allocations in EventSourceEvents

Co-authored-by: ReubenBond <rebond@microsoft.com>
(cherry picked from commit 29400a2)
  • Loading branch information
rrector authored and sergeybykov committed Jul 1, 2020
1 parent eed1501 commit a1ea247
Showing 1 changed file with 115 additions and 57 deletions.
172 changes: 115 additions & 57 deletions src/Orleans.Core/Diagnostics/EventSourceEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,45 @@ public void OnTimeout(Message message)
{
if (this.IsEnabled())
{
OnTimeout(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.OnTimeout();
}
}
}

[Event(1)]
public void OnTimeout(Guid activityId)
{
this.WriteEventWithRelatedActivityId(1, activityId);
}
[Event(1, Level = EventLevel.Warning)]
private void OnTimeout() => this.WriteEvent(1);

[NonEvent]
public void OnTargetSiloFail(Message message)
{
if (this.IsEnabled())
{
OnTargetSiloFail(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.OnTargetSiloFail();
}
}
}

[Event(2)]
public void OnTargetSiloFail(Guid activityId)
{
this.WriteEventWithRelatedActivityId(2, activityId);
}
[Event(2, Level = EventLevel.Warning)]
private void OnTargetSiloFail() => this.WriteEvent(2);

[NonEvent]
public void DoCallback(Message message)
{
if (this.IsEnabled())
{
DoCallback(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.DoCallback();
}
}
}

[Event(3)]
public void DoCallback(Guid activityId)
{
WriteEventWithRelatedActivityId(3, activityId);
}
[Event(3, Level = EventLevel.Verbose)]
private void DoCallback() => this.WriteEvent(3);
}

[EventSource(Name = "Microsoft-Orleans-OutsideRuntimeClient")]
Expand All @@ -64,45 +64,45 @@ public void SendRequest(Message message)
{
if (this.IsEnabled())
{
SendRequest(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.SendRequest();
}
}
}

[Event(1)]
public void SendRequest(Guid activityId)
{
WriteEventWithRelatedActivityId(1, activityId);
}
[Event(1, Level = EventLevel.Verbose)]
private void SendRequest() => this.WriteEvent(1);

[NonEvent]
public void ReceiveResponse(Message message)
{
if (this.IsEnabled())
{
ReceiveResponse(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.ReceiveResponse();
}
}
}

[Event(2)]
public void ReceiveResponse(Guid activityId)
{
WriteEventWithRelatedActivityId(2, activityId);
}
[Event(2, Level = EventLevel.Verbose)]
private void ReceiveResponse() => this.WriteEvent(2);

[NonEvent]
public void SendResponse(Message message)
{
if (this.IsEnabled())
{
SendResponse(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.SendResponse();
}
}
}

[Event(3)]
public void SendResponse(Guid activityId)
{
WriteEventWithRelatedActivityId(3, activityId);
}
[Event(3, Level = EventLevel.Verbose)]
private void SendResponse() => this.WriteEvent(3);
}

[EventSource(Name = "Microsoft-Orleans-Dispatcher")]
Expand All @@ -115,12 +115,15 @@ public void ReceiveMessage(Message message)
{
if (this.IsEnabled())
{
ReceiveMessage(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.ReceiveMessage();
}
}
}

[Event(1)]
public void ReceiveMessage(Guid activityId) => WriteEventWithRelatedActivityId(1, activityId);
[Event(1, Level = EventLevel.Verbose)]
private void ReceiveMessage() => WriteEvent(1);
}

[EventSource(Name = "Microsoft-Orleans-InsideRuntimeClient")]
Expand All @@ -133,36 +136,45 @@ public void SendRequest(Message message)
{
if (this.IsEnabled())
{
SendRequest(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.SendRequest();
}
}
}

[Event(1)]
public void SendRequest(Guid activityId) => WriteEventWithRelatedActivityId(1, activityId);
[Event(1, Level = EventLevel.Verbose)]
private void SendRequest() => WriteEvent(1);

[NonEvent]
public void ReceiveResponse(Message message)
{
if (this.IsEnabled())
{
ReceiveResponse(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.ReceiveResponse();
}
}
}

[Event(2)]
public void ReceiveResponse(Guid activityId) => WriteEventWithRelatedActivityId(2, activityId);
[Event(2, Level = EventLevel.Verbose)]
private void ReceiveResponse() => WriteEvent(2);

[NonEvent]
public void SendResponse(Message message)
{
if (this.IsEnabled())
{
SendResponse(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.SendResponse();
}
}
}

[Event(3)]
public void SendResponse(Guid activityId) => WriteEventWithRelatedActivityId(3, activityId);
[Event(3, Level = EventLevel.Verbose)]
private void SendResponse() => WriteEvent(3);
}

[EventSource(Name = "Microsoft-Orleans-GatewayAcceptor")]
Expand All @@ -175,12 +187,15 @@ public void HandleMessage(Message message)
{
if (this.IsEnabled())
{
HandleMessage(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.HandleMessage();
}
}
}

[Event(1)]
public void HandleMessage(Guid activityId) => WriteEventWithRelatedActivityId(1, activityId);
[Event(1, Level = EventLevel.Verbose)]
private void HandleMessage() => WriteEvent(1);

}

Expand All @@ -194,12 +209,15 @@ public void HandleMessage(Message message)
{
if (this.IsEnabled())
{
HandleMessage(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.HandleMessage();
}
}
}

[Event(1)]
public void HandleMessage(Guid activityId) => WriteEventWithRelatedActivityId(1, activityId);
[Event(1, Level = EventLevel.Verbose)]
private void HandleMessage() => WriteEvent(1);
}

[EventSource(Name = "Microsoft-Orleans-IncomingMessageAgent")]
Expand All @@ -212,11 +230,51 @@ public void ReceiveMessage(Message message)
{
if (this.IsEnabled())
{
ReceiveMessage(message.TraceContext?.ActivityId ?? Guid.Empty);
using (message.SetThreadActivityId())
{
this.ReceiveMessage();
}
}
}

[Event(1)]
public void ReceiveMessage(Guid activityId) => WriteEventWithRelatedActivityId(1, activityId);
[Event(1, Level = EventLevel.Verbose)]
private void ReceiveMessage() => WriteEvent(1);
}

internal static class EventSourceMessageExtensions
{
public static ActivityIdScope SetThreadActivityId(this Message message)
{
var activityId = message?.TraceContext?.ActivityId;

if (!(activityId is Guid messageActivityId) || messageActivityId == Guid.Empty)
{
return new ActivityIdScope(Guid.Empty, shouldReset: false);
}

EventSource.SetCurrentThreadActivityId(messageActivityId, out var oldActivity);
return new ActivityIdScope(oldActivity, shouldReset: messageActivityId != oldActivity);

}

internal readonly ref struct ActivityIdScope
{
private readonly Guid oldActivity;
private readonly bool shouldReset;

public ActivityIdScope(Guid oldActivity, bool shouldReset)
{
this.oldActivity = oldActivity;
this.shouldReset = shouldReset;
}

public void Dispose()
{
if (shouldReset)
{
EventSource.SetCurrentThreadActivityId(oldActivity);
}
}
}
}
}

0 comments on commit a1ea247

Please sign in to comment.