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?
Context: Akka.NET
Counters
Name | Tags | Value |
---|---|---|
akka.actor.created | actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Subscriber |
4 |
akka.messages.recv | actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Subscriber messagetype: string |
40 |
akka.actor.created | actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Publisher |
2 |
akka.messages.recv | actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Publisher messagetype: string |
20 |
Histograms
Name | Tags | Value |
---|---|---|
akka.messages.latency | actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Subscriber messagetype: string |
40 |
akka.messages.latency | actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Publisher messagetype: string |
20 |
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:
sequenceDiagram
/system/testActor->>/user/publisher: akka.msg.recv string
Note over /system/testActor,/user/publisher: SpanIndex: 0
ParentIndex: none
Tags:
akka.actor.recv.msgType: string
Logs:
message
content: 0
/user/publisher->>/user/subscriber1: akka.msg.recv string Note over /user/publisher,/user/subscriber1: SpanIndex: 1
ParentIndex: 0
Tags:
akka.actor.recv.msgType: string
Logs:
waiting
message
content: 0
/user/publisher->>/user/subscriber2: akka.msg.recv string Note over /user/publisher,/user/subscriber2: SpanIndex: 2
ParentIndex: 0
Tags:
akka.actor.recv.msgType: string
Logs:
waiting
message
content: 0
ParentIndex: none
Tags:
akka.actor.recv.msgType: string
Logs:
message
content: 0
/user/publisher->>/user/subscriber1: akka.msg.recv string Note over /user/publisher,/user/subscriber1: SpanIndex: 1
ParentIndex: 0
Tags:
akka.actor.recv.msgType: string
Logs:
waiting
message
content: 0
/user/publisher->>/user/subscriber2: akka.msg.recv string Note over /user/publisher,/user/subscriber2: SpanIndex: 2
ParentIndex: 0
Tags:
akka.actor.recv.msgType: string
Logs:
waiting
message
content: 0
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.