Example #1
0
 public void Initialise()
 {
     _systemMetrics = new Mock <ISystemMetricsService>();
     _log           = new Mock <ILog>();
     _block         = MessageParserBlockFactory.CreateMessageParserBlock(new CancellationToken(),
                                                                         _systemMetrics.Object,
                                                                         _log.Object);
 }
Example #2
0
        public Statsd(string serviceName = null)
        {
            _log.Info("statsd.net starting.");
            _tokenSource      = new CancellationTokenSource();
            _shutdownComplete = new ManualResetEvent(false);

            var systemInfoService = new SystemInfoService();

            var container = TinyIoCContainer.Current;

            container.Register <ISystemInfoService, SystemInfoService>(systemInfoService);


            serviceName = serviceName ?? systemInfoService.HostName;
            var systemMetricsService = new SystemMetricsService("statsdnet", serviceName);

            container.Register <ISystemMetricsService, SystemMetricsService>(systemMetricsService);

            /**
             * The flow is:
             *  Listeners ->
             *    Message Parser ->
             *      router ->
             *        Aggregator ->
             *          Broadcaster ->
             *            Backends
             */

            // Initialise the core blocks
            _router        = new StatsdMessageRouterBlock();
            _messageParser = MessageParserBlockFactory.CreateMessageParserBlock(_tokenSource.Token, container.Resolve <ISystemMetricsService>());
            _messageParser.LinkTo(_router);
            _messageParser.Completion.LogAndContinueWith("MessageParser", () =>
            {
                _log.Info("MessageParser: Completion signaled. Notifying the MessageBroadcaster.");
                _messageBroadcaster.Complete();
            });
            _messageBroadcaster = new BroadcastBlock <Bucket>(Bucket.Clone);
            _messageBroadcaster.Completion.LogAndContinueWith("MessageBroadcaster", () =>
            {
                _log.Info("MessageBroadcaster: Completion signaled. Notifying all backends.");
                _backends.ForEach(q => q.Complete());
            });

            // Add the broadcaster to the IOC container
            container.Register <BroadcastBlock <Bucket> >(_messageBroadcaster);
            systemMetricsService.SetTarget(_messageBroadcaster);

            _backends  = new List <IBackend>();
            _listeners = new List <IListener>();
        }
Example #3
0
        public Relay(dynamic config)
        {
            LoggingBootstrap.Configure();
            _log.Info("statsdrelay is starting up.");
            _tokenSource      = new CancellationTokenSource();
            _shutdownComplete = new ManualResetEvent(false);
            _listeners        = new List <IListener>();

            var systemInfoService = new SystemInfoService();
            var relayMetrics      = new RelayMetricsService("relay", _tokenSource.Token, systemInfoService.HostName);

            SuperCheapIOC.Add(relayMetrics as ISystemMetricsService);

            /* Pipeline is
             *  UDPStatsListener
             *  HTTPStatsListener
             *  TCPStatsListener
             *    ->  MessageParserBlock
             *      ->  BatchBlock
             *        -> UDPRawStatsSender
             */

            var udpSender   = new UDPRawStatsSender(config.target.host, (int)config.target.port, relayMetrics);
            var outputBlock = new ActionBlock <StatsdMessage[]>((lines) =>
            {
                // Only send valid lines
                _log.InfoFormat("Forwarding {0} lines.", lines.Length);
                udpSender.Send(lines.Where(p => !(p is InvalidMessage)).ToArray());
            },
                                                                new ExecutionDataflowBlockOptions()
            {
                BoundedCapacity   = ExecutionDataflowBlockOptions.Unbounded,
                CancellationToken = _tokenSource.Token
            });
            var batchBlock = new BatchBlock <StatsdMessage>(10, new GroupingDataflowBlockOptions()
            {
                CancellationToken = _tokenSource.Token,
                BoundedCapacity   = GroupingDataflowBlockOptions.Unbounded
            });

            batchBlock.LinkTo(outputBlock);
            var messageParserBlock = MessageParserBlockFactory.CreateMessageParserBlock(_tokenSource.Token, relayMetrics, _log);

            messageParserBlock.LinkTo(batchBlock);

            // Completion chain
            messageParserBlock.Completion.LogAndContinueWith(_log, "MessageParserBlock",
                                                             () =>
            {
                _log.Info("MessageParserBlock: Completion signalled. Notifying BatchBlock.");
                batchBlock.Complete();
            });

            batchBlock.Completion.LogAndContinueWith(_log, "BatchBlock",
                                                     () =>
            {
                _log.Info("BatchBlock: Completion signalled. Notifying OutputBlock.");
                outputBlock.Complete();
            });
            outputBlock.Completion.LogAndContinueWith(_log, "OutputBlock",
                                                      () =>
            {
                // Last one to leave the room turns out the lights.
                _log.Info("OutputBlock: Completion signalled. Shutting down.");
                _shutdownComplete.Set();
            });

            // Listeners
            if (config.listeners.udp.enabled)
            {
                var udpListener = new UdpStatsListener((int)config.listeners.udp.port, relayMetrics);
                udpListener.LinkTo(messageParserBlock, _tokenSource.Token);
                _listeners.Add(udpListener);
            }
            if (config.listeners.http.enabled)
            {
                var httpListener = new HttpStatsListener((int)config.listeners.http.port, relayMetrics);
                httpListener.LinkTo(messageParserBlock, _tokenSource.Token);
                _listeners.Add(httpListener);
            }
            if (config.listeners.tcp.enabled)
            {
                var tcpListener = new TcpStatsListener((int)config.listeners.tcp.port, relayMetrics);
                tcpListener.LinkTo(messageParserBlock, _tokenSource.Token);
                _listeners.Add(tcpListener);
            }

            // Set the system metrics target
            relayMetrics.SetTarget(batchBlock);
        }
Example #4
0
 public MessageParserBlockTests()
 {
     _systemMetrics = Substitute.For <ISystemMetricsService>();
     _block         = MessageParserBlockFactory.CreateMessageParserBlock(new CancellationToken(),
                                                                         _systemMetrics);
 }