Traces and Metrics for ActorSystem.EventStream
Scenario
On this page we will see how Phobos handles local publisher-subscriber scenario - using Event Source.
Code
Here is a code sample we will use:
// create publisher and subscribers
var publisher = Sys.ActorOf<Publisher>("publisher");
var subscriber1 = Sys.ActorOf(Props.Create(() => new Subscriber(TestActor)), "subscriber1");
var subscriber2 = Sys.ActorOf(Props.Create(() => new Subscriber(TestActor)), "subscriber2");
// subscribe to events
Sys.EventStream.Subscribe(subscriber1, typeof(string));
Sys.EventStream.Subscribe(subscriber2, typeof(string));
// publish 10 messages
for (var i = 0; i < 10; ++i)
publisher.Tell(i.ToString());
// wait for messages - two "0", two "1", etc
ExpectMsgAllOf(Enumerable.Range(0, 10).SelectMany(i => new[] { i.ToString(), i.ToString() }).ToArray());
private class Publisher : ReceiveActor
{
public Publisher()
{
ReceiveAny(m => Context.System.EventStream.Publish(m));
}
}
private class Subscriber : ReceiveActor
{
public Subscriber(IActorRef target)
{
ReceiveAny(m => target.Tell(m));
}
}
Different messages are published to multiple subscribers. What data will be collected?
Metrics
Automatically collected metrics here:
akka.actor.created
counter - how many actors of given type were created at that moment?akka.messages.recv
meters - how many messages of given type arrived into actor's mailboxes?
From the metrics above it is absolutely clear that we have 2 subscribers generated per single publisher, and 20 messages were delivered from 10 messages published - exactly what we would expect.
Traces
For each messages published, we have full-tracked message trace from publishing to subscriber's mailboxes. Here is a trace for the first message:
[!include[Sample](../../../../projects/phobos/2.x/src/end2end/Phobos.Actor.End2End.Tests/Standalone/EventStreamSpecs.EventSteam_publishing_with_2_subscribers.approved.mermaid)]
Here we can see that:
- Test actor is sending a message
0
to the publisher - Publisher is sending same message to both subscribers.
Note that we do not have any intermediate actors between publisher and subscribers - that is how
EventSource
works, it's a singleton connecting publishers and subscribers via theirIActorRef
s.
That's pretty much it. Let's see, how similar scenario will look like in clustering mode, using Distributed Pub-Sub feature.