Skip to content

Commit

Permalink
made IActorTelemetry events nullable (#7478)
Browse files Browse the repository at this point in the history
And don't emit these events when they are `null`
  • Loading branch information
Aaronontheweb authored Jan 22, 2025
1 parent f80a2b1 commit a64d954
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace Akka.Actor
public void CheckReceiveTimeout(bool reschedule = True) { }
protected void ClearActor(Akka.Actor.ActorBase actor) { }
protected void ClearActorCell() { }
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
protected virtual Akka.Actor.ActorRestarted CreateActorRestartedEvent(System.Exception cause) { }
protected virtual Akka.Actor.ActorStarted CreateActorStartedEvent() { }
protected virtual Akka.Actor.ActorStopped CreateActorStoppedEvent() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace Akka.Actor
public void CheckReceiveTimeout(bool reschedule = True) { }
protected void ClearActor(Akka.Actor.ActorBase actor) { }
protected void ClearActorCell() { }
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
protected virtual Akka.Actor.ActorRestarted CreateActorRestartedEvent(System.Exception cause) { }
protected virtual Akka.Actor.ActorStarted CreateActorStartedEvent() { }
protected virtual Akka.Actor.ActorStopped CreateActorStoppedEvent() { }
Expand Down
6 changes: 2 additions & 4 deletions src/core/Akka/Actor/ActorCell.Children.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ private string GetRandomActorName(string prefix = "$")
/// <param name="child">The child.</param>
public void Stop(IActorRef child)
{
ChildRestartStats stats;
if (ChildrenContainer.TryGetByRef(child, out stats))
if (ChildrenContainer.TryGetByRef(child, out _))
{
var repointableActorRef = child as RepointableActorRef;
if (repointableActorRef == null || repointableActorRef.IsStarted)
if (child is not RepointableActorRef repointableActorRef || repointableActorRef.IsStarted)
{
while (true)
{
Expand Down
13 changes: 9 additions & 4 deletions src/core/Akka/Actor/ActorCell.DefaultMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ public void Restart(Exception cause)
/// <summary>
/// Overrideable in order to support issues such as https://github.com/petabridge/phobos-issues/issues/82
/// </summary>
protected virtual ActorStarted CreateActorStartedEvent()
protected virtual ActorStarted? CreateActorStartedEvent()
{
return new ActorStarted(Self, Props.Type);
}

/// <summary>
/// Overrideable in order to support issues such as https://github.com/petabridge/phobos-issues/issues/82
/// </summary>
protected virtual ActorStopped CreateActorStoppedEvent()
protected virtual ActorStopped? CreateActorStoppedEvent()
{
return new ActorStopped(Self, Props.Type);
}
Expand All @@ -466,8 +466,13 @@ private void Create(Exception? failure)
CheckReceiveTimeout();
if (System.Settings.DebugLifecycle)
Publish(new Debug(Self.Path.ToString(), created.GetType(), "Started (" + created + ")"));
if(System.Settings.EmitActorTelemetry)
System.EventStream.Publish(CreateActorStartedEvent());
if (System.Settings.EmitActorTelemetry)
{
var actorStarted = CreateActorStartedEvent();
if(actorStarted != null)
System.EventStream.Publish(actorStarted);
}

}
catch (Exception e)
{
Expand Down
20 changes: 14 additions & 6 deletions src/core/Akka/Actor/ActorCell.FaultHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,13 @@ private void FinishTerminate()
var pipeline = SystemImpl.ActorPipelineResolver.ResolvePipeline(a.GetType());
pipeline.BeforeActorIncarnated(a, this);
}

if(System.Settings.EmitActorTelemetry)
System.EventStream.Publish(CreateActorStoppedEvent());

if (System.Settings.EmitActorTelemetry)
{
var stopEvent = CreateActorStoppedEvent();
if (stopEvent is not null)
System.EventStream.Publish(stopEvent);
}
}
catch (Exception x)
{
Expand Down Expand Up @@ -350,8 +354,12 @@ private void FinishRecreate(Exception cause, ActorBase? failedActor)

if (System.Settings.DebugLifecycle)
Publish(new Debug(_self.Path.ToString(), freshActor.GetType(), "Restarted (" + freshActor + ")"));
if(System.Settings.EmitActorTelemetry)
System.EventStream.Publish(CreateActorRestartedEvent(cause));
if (System.Settings.EmitActorTelemetry)
{
var restartEvent = CreateActorRestartedEvent(cause);
if (restartEvent is not null)
System.EventStream.Publish(restartEvent);
}

// only after parent is up and running again do restart the children which were not stopped
foreach (var survivingChild in survivors)
Expand All @@ -378,7 +386,7 @@ private void FinishRecreate(Exception cause, ActorBase? failedActor)
/// <summary>
/// Overrideable in order to support issues such as https://github.com/petabridge/phobos-issues/issues/82
/// </summary>
protected virtual ActorRestarted CreateActorRestartedEvent(Exception cause)
protected virtual ActorRestarted? CreateActorRestartedEvent(Exception cause)
{
return new ActorRestarted(Self, Props.Type, cause);
}
Expand Down

0 comments on commit a64d954

Please sign in to comment.