Show / Hide Table of Contents

    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());
    
    
    class Publisher : ReceiveActor
    {
        public Publisher() => ReceiveAny(m => Context.System.EventStream.Publish(m));
    }
    
    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+Publisher 1
    akka.actor.created actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Subscriber 2

    Meters

    Name Tags Value
    akka.messages.recv actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Publisher
    messagetype: string
    10
    akka.messages.recv actortype: Phobos.Actor.End2End.Tests.Standalone.EventStreamSpecs+Subscriber
    messagetype: string
    20

    Context: Test

    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 sys/.../testActor->>/user/publisher: akka.msg.recv String Note over sys/.../testActor,/user/publisher: SpanIndex: 0
    ParentIndex: none
    Tags:
      akka.actor.recv.msgType: string
    Logs:
      message: 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
    References:
      child_of: SpanIndex=0
    Logs:
      message: 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
    References:
      child_of: SpanIndex=0
    Logs:
      message: 0

    Here we can see that:

    1. Test actor is sending a message 0 to the publisher
    2. 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 their IActorRefs.

    That's pretty much it. Let's see, how similar scenario will look like in clustering mode, using Distributed Pub-Sub feature.

    Back to top Generated by DocFX