Ejemplo n.º 1
0
        public Statsd(string serviceName = null)
        {
            LoggingBootstrap.Configure();
            _log.Info("statsd.net starting.");
            _tokenSource      = new CancellationTokenSource();
            _shutdownComplete = new ManualResetEvent(false);

            SuperCheapIOC.Add(_log);
            var systemInfoService = new SystemInfoService();

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

            SuperCheapIOC.Add(systemMetricsService as ISystemMetricsService);

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

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

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

            _backends  = new List <IBackend>();
            _listeners = new List <IListener>();
        }
Ejemplo n.º 2
0
 public void AddBackend(IBackend backend, string name = "")
 {
     _log.InfoFormat("Adding backend {0} named '{1}'", backend.GetType().Name, name);
     _backends.Add(backend);
     SuperCheapIOC.Add(backend, name);
     _messageBroadcaster.LinkTo(backend);
     backend.Completion.LogAndContinueWith(_log, name, () =>
     {
         if (_backends.All(q => !q.IsActive))
         {
             _shutdownComplete.Set();
         }
     });
 }
Ejemplo n.º 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);
        }