Example #1
0
    protected override void OnStop()
    {
      if ( _statsd != null )
      {
        _statsd.Stop();
        _statsd.ShutdownWaitHandle.WaitOne();
      }

      if (_config != null)
      {
        _config.Dispose();
        _config = null;
      }
    }
Example #2
0
    public void Start(bool waitForCompletion = true)
    {
      //TODO : JV IS CONFIG FILE A ACTUAL FILE PATH?  IF SO THEN ITS MISLEADING SHOULD BE CONFIGFILEPATH??
      LoggingBootstrap.Configure();

      var configFile = ResolveConfigFile(_configFile);
      if (!File.Exists(configFile))
      {
        throw new FileNotFoundException("Could not find the statsd.net config file. I looked here: " + configFile);
      }
      _config = ConfigurationFactory.Parse(configFile);
      _statsd = new Statsd(_config);
      if (waitForCompletion)
      {
        _statsd.ShutdownWaitHandle.WaitOne();
      }
    }
Example #3
0
 private void LoadListeners(StatsdnetConfiguration config, 
     CancellationToken cancellationToken,
     ISystemMetricsService systemMetrics)
 {
     // Load listeners - done last and once the rest of the chain is in place
     foreach (var listenerConfig in config.Listeners)
     {
         if (listenerConfig is UDPListenerConfiguration)
         {
             var udpConfig = listenerConfig as UDPListenerConfiguration;
             AddListener(new UdpStatsListener(udpConfig.Port, systemMetrics));
             systemMetrics.LogCount("startup.listener.udp." + udpConfig.Port);
         }
         else if (listenerConfig is TCPListenerConfiguration)
         {
             var tcpConfig = listenerConfig as TCPListenerConfiguration;
             AddListener(new TcpStatsListener(tcpConfig.Port, systemMetrics));
             systemMetrics.LogCount("startup.listener.tcp." + tcpConfig.Port);
         }
         else if (listenerConfig is HTTPListenerConfiguration)
         {
             var httpConfig = listenerConfig as HTTPListenerConfiguration;
             AddListener(new HttpStatsListener(httpConfig.Port, systemMetrics));
             systemMetrics.LogCount("startup.listener.http." + httpConfig.Port);
         }
         else if (listenerConfig is StatsdnetListenerConfiguration)
         {
             var statsdnetConfig = listenerConfig as StatsdnetListenerConfiguration;
             AddListener(new StatsdnetTcpListener(statsdnetConfig.Port, systemMetrics));
             systemMetrics.LogCount("startup.listener.statsdnet." + statsdnetConfig.Port);
         }
         else if (listenerConfig is MSSQLRelayListenerConfiguration)
         {
             var mssqlRelayConfig = listenerConfig as MSSQLRelayListenerConfiguration;
             AddListener(new MSSQLRelayListener(mssqlRelayConfig.ConnectionString,
                 mssqlRelayConfig.PollInterval,
                 cancellationToken,
                 mssqlRelayConfig.BatchSize,
                 mssqlRelayConfig.DeleteAfterSend,
                 systemMetrics));
         }
     }
 }
Example #4
0
        public Statsd(StatsdnetConfiguration config)
            : this(config.Name)
        {
            _log.Info("statsd.net loading config.");
            var systemMetrics = SuperCheapIOC.Resolve<ISystemMetricsService>();
            systemMetrics.HideSystemStats = config.HideSystemStats;

            LoadBackends(config, systemMetrics);

            // Load Aggregators
            var intervalServices = new List<IIntervalService>();
            var intervalService = new IntervalService(config.FlushInterval,
              _tokenSource.Token);
            intervalServices.Add(intervalService);
            LoadAggregators(config,
              intervalService,
              _messageBroadcaster,
              systemMetrics);
            // Load Listeners
            LoadListeners(config, _tokenSource.Token, systemMetrics);

            // Now start the interval service
            intervalServices.ForEach(p => p.Start());

            // Announce that we've started
            systemMetrics.LogCount("started");
        }
Example #5
0
 private void LoadBackends(StatsdnetConfiguration config, ISystemMetricsService systemMetrics)
 {
     foreach (var backend in config.GetConfiguredBackends(systemMetrics))
     {
         AddBackend(backend, systemMetrics, backend.Name);
     }
 }
Example #6
0
        private void LoadAggregators(StatsdnetConfiguration config,
          IntervalService intervalService,
          BroadcastBlock<Bucket> messageBroadcaster,
          ISystemMetricsService systemMetrics)
        {
            foreach (var aggregator in config.Aggregators)
            {
                switch (aggregator.Key)
                {
                    case "counters":
                        var counter = aggregator.Value as CounterAggregationConfig;
                        AddAggregator(MessageType.Counter,
                          TimedCounterAggregatorBlockFactory.CreateBlock(messageBroadcaster,
                            counter.Namespace,
                            intervalService,
                            _log),
                          systemMetrics);
                        break;
                    case "gauges":
                        var gauge = aggregator.Value as GaugeAggregatorConfig;
                        AddAggregator(MessageType.Gauge,
                          TimedGaugeAggregatorBlockFactory.CreateBlock(messageBroadcaster,
                            gauge.Namespace,
                            gauge.RemoveZeroGauges,
                            intervalService,
                            _log),
                          systemMetrics);
                        break;
                    case "calendargrams":
                        var calendargram = aggregator.Value as CalendargramAggregationConfig;
                        AddAggregator(MessageType.Calendargram,
                            TimedCalendargramAggregatorBlockFactory.CreateBlock(messageBroadcaster,
                                calendargram.Namespace,
                                intervalService,
                                new TimeWindowService(),
                                _log),
                                systemMetrics);
                        break;
                    case "timers":
                        var timer = aggregator.Value as TimersAggregationConfig;
                        AddAggregator(MessageType.Timing,
                          TimedLatencyAggregatorBlockFactory.CreateBlock(messageBroadcaster,
                            timer.Namespace,
                            intervalService,
                            timer.CalculateSumSquares,
                            _log),
                          systemMetrics);
                        // Add Percentiles
                        foreach (var percentile in timer.Percentiles)
                        {
                            AddAggregator(MessageType.Timing,
                              TimedLatencyPercentileAggregatorBlockFactory.CreateBlock(messageBroadcaster,
                                timer.Namespace,
                                intervalService,
                                percentile.Threshold,
                                percentile.Name,
                                _log),
                              systemMetrics);
                        }
                        break;

                }
            }
            // Add the Raw (pass-through) aggregator
            AddAggregator(MessageType.Raw,
              PassThroughBlockFactory.CreateBlock(messageBroadcaster, intervalService),
              systemMetrics);
        }
Example #7
0
        public static StatsdnetConfiguration Parse(string configFile)
        {
            var config    = new StatsdnetConfiguration();
            var xml       = XDocument.Parse(File.ReadAllText(configFile));
            var statsdnet = xml.Element("statsdnet");

            config.Name = statsdnet.Attribute("name").Value;
            if (statsdnet.Attributes().Any(p => p.Name == "hideSystemStats"))
            {
                config.HideSystemStats = statsdnet.ToBoolean("hideSystemStats");
            }

            // Add listeners
            foreach (var item in statsdnet.Element("listeners").Elements())
            {
                ListenerConfiguration listener = null;
                switch (item.Name.LocalName)
                {
                case "udp":
                    listener = new UDPListenerConfiguration(item.ToInt("port"));
                    break;

                case "tcp":
                    listener = new TCPListenerConfiguration(item.ToInt("port"));
                    break;

                case "http":
                    listener = new HTTPListenerConfiguration(item.ToInt("port"));
                    if (item.Attribute("headerKey") != null)
                    {
                        ((HTTPListenerConfiguration)listener).HeaderKey = item.Attribute("headerKey").Value;
                    }
                    break;

                case "statsdnet":
                    listener = new StatsdnetListenerConfiguration(item.ToInt("port"));
                    break;

                case "mssql-relay":
                    listener = new MSSQLRelayListenerConfiguration(item.Attribute("connectionString").Value,
                                                                   item.ToInt("batchSize"),
                                                                   item.ToBoolean("deleteAfterSend"),
                                                                   item.ToTimeSpan("pollInterval"));
                    break;

                default:
                    throw new ArgumentOutOfRangeException("Not sure what this listener is: " + item.Name);
                }
                config.Listeners.Add(listener);
            }

            // Add Backends
            foreach (var item in statsdnet.Element("backends").Elements())
            {
                string name = item.Name.LocalName;
                config.BackendConfigurations[name] = item;
            }

            // Add aggregators
            var flushInterval = statsdnet.Element("aggregation").ToTimeSpan("flushInterval");

            config.FlushInterval = flushInterval;
            var aggregatorGroup = new AggregatorConfiguration();

            foreach (var item in statsdnet.Element("aggregation").Elements())
            {
                switch (item.Name.LocalName)
                {
                case "gauges":
                    config.Aggregators.Add("gauges", new GaugeAggregatorConfig(ns: item.Attribute("namespace").Value,
                                                                               removeZeroGauges: item.ToBoolean("removeZeroGauges")));
                    break;

                case "counters":
                    config.Aggregators.Add("counters", new CounterAggregationConfig(ns: item.Attribute("namespace").Value));
                    break;

                case "sets":
                    config.Aggregators.Add("sets", new SetAggregationConfig(ns: item.Attribute("namespace").Value));
                    break;

                case "calendargrams":
                    config.Aggregators.Add("calendargrams", new CalendargramAggregationConfig(ns: item.Attribute("namespace").Value));
                    break;

                case "timers":
                    var timerConfig = new TimersAggregationConfig(ns: item.Attribute("namespace").Value, calculateSumSquares: item.ToBoolean("calculateSumSquares"));
                    config.Aggregators.Add("timers", timerConfig);
                    // Now add the percentiles
                    foreach (var subItem in item.Elements())
                    {
                        if (!timerConfig.AddPercentile(new PercentileConfig(
                                                           name: subItem.Attribute("name").Value,
                                                           threshold: subItem.ToInt("threshold"),
                                                           flushInterval: subItem.ToTimeSpan("flushInterval")
                                                           )))
                        {
                            // TODO: log that a duplicate percentile was ignored
                        }
                    }
                    break;
                }
            }
            return(config);
        }
Example #8
0
 private void LoadListeners(StatsdnetConfiguration config, ISystemMetricsService systemMetrics)
 {
   // Load listeners - done last and once the rest of the chain is in place
   foreach (var listenerConfig in config.Listeners)
   {
     if (listenerConfig is UDPListenerConfiguration)
     {
       var udpConfig = listenerConfig as UDPListenerConfiguration;
       AddListener(new UdpStatsListener(udpConfig.Port, systemMetrics));
       systemMetrics.LogCount("startup.listener.udp." + udpConfig.Port);
     }
     else if (listenerConfig is TCPListenerConfiguration)
     {
       var tcpConfig = listenerConfig as TCPListenerConfiguration;
       AddListener(new TcpStatsListener(tcpConfig.Port, systemMetrics));
       systemMetrics.LogCount("startup.listener.tcp." + tcpConfig.Port);
     }
     else if (listenerConfig is HTTPListenerConfiguration)
     {
       var httpConfig = listenerConfig as HTTPListenerConfiguration;
       AddListener(new HttpStatsListener(httpConfig.Port, systemMetrics));
       systemMetrics.LogCount("startup.listener.http." + httpConfig.Port);
     }
     else if (listenerConfig is StatsdnetListenerConfiguration)
     {
       var statsdnetConfig = listenerConfig as StatsdnetListenerConfiguration;
       AddListener(new StatsdnetTcpListener(statsdnetConfig.Port, systemMetrics));
       systemMetrics.LogCount("startup.listener.statsdnet." + statsdnetConfig.Port);
     }
   }
 }
        public static StatsdnetConfiguration Parse(string configFile)
        {
            var config = new StatsdnetConfiguration();
            var xml = XDocument.Parse(File.ReadAllText(configFile));
            var statsdnet = xml.Element("statsdnet");
            config.Name = statsdnet.Attribute("name").Value;
            if (statsdnet.Attributes().Any(p => p.Name == "hideSystemStats"))
            {
                config.HideSystemStats = statsdnet.ToBoolean("hideSystemStats");
            }

            // Add listeners
            foreach (var item in statsdnet.Element("listeners").Elements())
            {
                ListenerConfiguration listener = null;
                switch (item.Name.LocalName)
                {
                    case "udp":
                        listener = new UDPListenerConfiguration(item.ToInt("port"));
                        break;
                    case "tcp":
                        listener = new TCPListenerConfiguration(item.ToInt("port"));
                        break;
                    case "http":
                        listener = new HTTPListenerConfiguration(item.ToInt("port"));
                        if (item.Attribute("headerKey") != null)
                        {
                            ((HTTPListenerConfiguration)listener).HeaderKey = item.Attribute("headerKey").Value;
                        }
                        break;
                    case "statsdnet":
                        listener = new StatsdnetListenerConfiguration(item.ToInt("port"));
                        break;
                    case "mssql-relay":
                        listener = new MSSQLRelayListenerConfiguration(item.Attribute("connectionString").Value,
                            item.ToInt("batchSize"),
                            item.ToBoolean("deleteAfterSend"),
                            item.ToTimeSpan("pollInterval"));
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("Not sure what this listener is: " + item.Name);
                }
                config.Listeners.Add(listener);
            }

            // Add Backends
            foreach (var item in statsdnet.Element("backends").Elements())
            {
                string name = item.Name.LocalName;
                config.BackendConfigurations[name] = item;
            }

            // Add aggregators
            var flushInterval = statsdnet.Element("aggregation").ToTimeSpan("flushInterval");
            config.FlushInterval = flushInterval;
            var aggregatorGroup = new AggregatorConfiguration();
            foreach (var item in statsdnet.Element("aggregation").Elements())
            {
                switch (item.Name.LocalName)
                {
                    case "gauges":
                        config.Aggregators.Add("gauges", new GaugeAggregatorConfig(ns: item.Attribute("namespace").Value,
                            removeZeroGauges: item.ToBoolean("removeZeroGauges")));
                        break;
                    case "counters":
                        config.Aggregators.Add("counters", new CounterAggregationConfig(ns: item.Attribute("namespace").Value));
                        break;
                    case "sets":
                        config.Aggregators.Add("sets", new SetAggregationConfig(ns: item.Attribute("namespace").Value));
                        break;
                    case "calendargrams":
                        config.Aggregators.Add("calendargrams", new CalendargramAggregationConfig(ns: item.Attribute("namespace").Value));
                        break;
                    case "timers":
                        var timerConfig = new TimersAggregationConfig(ns: item.Attribute("namespace").Value, calculateSumSquares: item.ToBoolean("calculateSumSquares"));
                        config.Aggregators.Add("timers", timerConfig);
                        // Now add the percentiles
                        foreach (var subItem in item.Elements())
                        {
                            if (!timerConfig.AddPercentile(new PercentileConfig(
                              name: subItem.Attribute("name").Value,
                              threshold: subItem.ToInt("threshold"),
                              flushInterval: subItem.ToTimeSpan("flushInterval")
                              )))
                            {
                                // TODO: log that a duplicate percentile was ignored
                            }
                        }
                        break;
                }
            }
            return config;
        }