Example #1
0
    static void Main(string[] args)
    {
        var tracer = new Tracer.Builder("Proto.Chat.Client")
                     .WithSampler(new ConstSampler(true))
                     .Build();

        SpanSetup spanSetup             = (span, message) => span.Log(message?.ToString());
        var       openTracingMiddleware = OpenTracingExtensions.OpenTracingSenderMiddleware(tracer);

        Serialization.RegisterFileDescriptor(ChatReflection.Descriptor);
        Remote.Start("127.0.0.1", 0);
        var server  = new PID("127.0.0.1:8000", "chatserver");
        var context = new RootContext(default, openTracingMiddleware);
        public ActorManager(IActorFactory actorFactory, IProvider persistenceProvider, IOptions <ActorSettings> actorSettings, ITracer tracer, ILoggerFactory loggerFactory)
        {
            _actorFactory = actorFactory;
            var settings = actorSettings.Value;
            var logger   = loggerFactory.CreateLogger <ActorManager>();

            // Configure OpenTracing
            Context = new RootContext(new MessageHeader(), OpenTracingExtensions.OpenTracingSenderMiddleware())
                      .WithOpenTracing();

            _actorFactory.RegisterActor(new RequestActor(this, persistenceProvider, TimeSpan.FromMilliseconds(settings.ChildActorTimeoutInMilliseconds), loggerFactory, tracer), ActorNames.RequestActor);

            EventStream.Instance.Subscribe <DeadLetterEvent>(dl =>
            {
                logger.LogWarning($"DeadLetter from {dl.Sender} to {dl.Pid} : {dl.Message?.GetType().Name} = '{dl.Message?.ToString()}'");
            });
        }
Example #3
0
        public void RootContextOpenTracingSenderTest()
        {
            var root = new RootContext(System, new MessageHeader(), OpenTracingExtensions.OpenTracingSenderMiddleware(_tracer))
                       .WithOpenTracing(tracer: _tracer);

            var messages = new List <object>();

            var actorProps = Props
                             .FromFunc(ctx => { messages.Add(ctx.Message); return(Actor.Done); })
                             .WithMailbox(() => new TestMailbox())
            ;
            var actor = System.Root.Spawn(actorProps);

            root.Send(actor, "test_message");

            Assert.Equal(2, messages.Count); // Started & "test_message"
            _tracer.Received(1).BuildSpan("Send String");

            _tracer.ClearReceivedCalls();

            root.Request(actor, "test_message_2");
            Assert.Equal(3, messages.Count); // Started & "test_message" & "test_message_2"
            _tracer.Received(1).BuildSpan("Request String");
        }
Example #4
0
    static void Main(string[] args)
    {
        var tracer = new Tracer.Builder("Proto.Example.LifecycleEvents")
                     .WithSampler(new ConstSampler(true))
                     .Build();

        GlobalTracer.Register(tracer);

        EventStream.Instance.Subscribe <DeadLetterEvent>(
            dl =>
        {
            Console.WriteLine($"DeadLetter from {dl.Sender} to {dl.Pid} : {dl.Message?.GetType().Name} = '{dl.Message?.ToString()}'");
        });

        var context = new RootContext(new MessageHeader(), OpenTracingExtensions.OpenTracingSenderMiddleware());

        var props = Props
                    .FromProducer(() => new ChildActor())
                    .WithOpenTracing();

        var actor = context.Spawn(props);

        context.Send(actor, new Hello
        {
            Who = "Alex"
        });

        //why wait?
        //Stop is a system message and is not processed through the user message mailbox
        //thus, it will be handled _before_ any user message
        //we only do this to show the correct order of events in the console
        Thread.Sleep(TimeSpan.FromSeconds(1));
        actor.StopAsync().Wait();

        Console.ReadLine();
    }
        static async Task Main()
        {
            var serviceName = $"DrawNiceTrace{Guid.NewGuid()}";

            var tracer = new Tracer.Builder(serviceName)
                         .WithSampler(new ConstSampler(true))
                         .Build();

            GlobalTracer.Register(tracer);
            var system      = new ActorSystem();
            var rootContext = new RootContext(system, MessageHeader.Empty, OpenTracingExtensions.OpenTracingSenderMiddleware())
                              .WithOpenTracing();

            var bankProps = Props.FromFunc(
                ctx =>
            {
                if (ctx.Message is ProcessPayment _)
                {
                    ctx.Respond(new ProcessPaymentResponse {
                        Ok = true
                    });
                }
                return(Task.CompletedTask);
            }
                )
                            .WithOpenTracing();
            var bank = rootContext.Spawn(bankProps);

            var restaurantProps = Props.FromFunc(
                async ctx =>
            {
                if (ctx.Message is TriggerFood trigger)
                {
                    using (GlobalTracer.Instance.BuildSpan("Preparing food !").StartActive())
                    {
                        await Task.Delay(1000);
                    }

                    ctx.Send(trigger.Customer, new DeliverFood());
                }
            }
                )
                                  .WithOpenTracing();
            var restaurant = rootContext.Spawn(restaurantProps);

            var  cartProps      = Props.FromProducer(() => new CartActor(bank)).WithOpenTracing();
            long nextCartNumber = 1;

            string getActorName(long number) => $"cart-{number}";

            var dinnerProps = Props.FromFunc(
                async ctx =>
            {
                switch (ctx.Message)
                {
                case AddItem addItem:
                    PID cartPID;

                    if (addItem.CartNumber == default)
                    {
                        var cartNumber = nextCartNumber++;
                        cartPID        = ctx.SpawnNamed(cartProps, getActorName(cartNumber));
                        ctx.Send(cartPID, new AssignCartNumber {
                            CartNumber = cartNumber
                        });
                    }
                    else
                    {
                        var cartActorName = getActorName(addItem.CartNumber);
                        cartPID           = ctx.Children.First(p => p.Id.EndsWith(cartActorName));
                    }

                    ctx.Forward(cartPID);
                    break;

                case ConfirmOrder confirmOrder:
                    var orderPid = ctx.Children.First(p => p.Id.EndsWith(getActorName(confirmOrder.CartNumber)));
                    ctx.Forward(orderPid);
                    break;

                case SendPaymentDetails sendPaymentDetails:
                    var orderPid2 = ctx.Children.First(p => p.Id.EndsWith(getActorName(sendPaymentDetails.OrderNumber)));
                    var resp      = await ctx.RequestAsync <ProcessPaymentResponse>(orderPid2, sendPaymentDetails);

                    if (resp.Ok)             // it will always, we have super rich customers
                    {
                        ctx.Send(
                            restaurant,
                            new TriggerFood {
                            Customer = sendPaymentDetails.Customer, OrderNumber = sendPaymentDetails.OrderNumber
                        }
                            );
                    }
                    break;
                }
            }
                )
                              .WithOpenTracing();
            var dinner = rootContext.Spawn(dinnerProps);

            var customerProps = Props.FromFunc(
                async ctx =>
            {
                switch (ctx.Message)
                {
                case Started _:
                    var cartNumberResponse = await ctx.RequestAsync <CartNumberResponse>(dinner, new AddItem {
                        ProductName = "Beer!"
                    });
                    var cartNumber = cartNumberResponse.CartNumber;

                    await Task.Delay(100);
                    await ctx.RequestAsync <CartNumberResponse>(dinner, new AddItem {
                        CartNumber = cartNumber, ProductName = "Pizza."
                    });
                    await Task.Delay(100);
                    await ctx.RequestAsync <CartNumberResponse>(dinner, new AddItem {
                        CartNumber = cartNumber, ProductName = "Ice cream."
                    });

                    await Task.Delay(200);
                    var confirmed = await ctx.RequestAsync <ConfirmOrderResponse>(dinner, new ConfirmOrder {
                        CartNumber = cartNumber
                    });

                    await Task.Delay(300);
                    ctx.Send(dinner, new SendPaymentDetails {
                        Customer = ctx.Self, OrderNumber = confirmed.OrderNumber
                    });

                    break;

                case DeliverFood deliver:
                    throw new Exception("Food Delivered !");
                }
            }
                )
                                .WithOpenTracing()
                                .WithGuardianSupervisorStrategy(new OneForOneStrategy((pid, reason) => SupervisorDirective.Stop, 0, null));

            rootContext.Spawn(customerProps);

            await Task.Delay(TimeSpan.FromSeconds(10));

            Console.WriteLine("Done! Go to jaeger : http://localhost:16686/search?service=" + serviceName);
            Console.WriteLine("Press [Enter]");
            Console.ReadLine();
        }