Exemplo n.º 1
0
 public void LinkTo(ITargetBlock <string> target, CancellationToken cancellationToken)
 {
     _cancellationToken = cancellationToken;
     Task.Factory.StartNew(() =>
     {
         try
         {
             var endpoint  = new IPEndPoint(IPAddress.Any, _port);
             var udpClient = new UdpClient(endpoint);
             while (true)
             {
                 if (_cancellationToken.IsCancellationRequested)
                 {
                     return;
                 }
                 byte[] data = udpClient.Receive(ref endpoint);
                 _systemMetrics.LogCount("listeners.udp.bytes", data.Length);
                 string rawPacket = Encoding.UTF8.GetString(data);
                 string[] lines   = rawPacket.Replace("\r", "").Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                 for (int index = 0; index < lines.Length; index++)
                 {
                     target.Post(lines[index]);
                 }
                 _systemMetrics.LogCount("listeners.udp.lines", lines.Length);
             }
         }
         catch (ObjectDisposedException) { /* Eat it, socket was closed */ }
         finally { IsListening = false; }
     },
                           cancellationToken);
     IsListening = true;
 }
Exemplo n.º 2
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);
         }
     }
 }
Exemplo n.º 3
0
 public void Send(StatsdMessage[] lines)
 {
     string[] rawLines = lines.Select(p => p.ToString()).ToArray();
     byte[]   payload  = Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, rawLines));
     _client.Send(payload, payload.Length);
     _systemMetrics.LogCount("sent.bytes", payload.Length);
     _systemMetrics.LogCount("sent.lines", payload.Length);
 }
Exemplo n.º 4
0
 private void InitialiseRetryHandling()
 {
     _retryPolicy = Policy.Handle <SqlException>()
                    .WaitAndRetry(_retries, retryAttempt => TimeSpan.FromSeconds(1), (exception, timeSpan) =>
     {
         _log.WarnException(
             string.Format("Retry failed. Trying again. Delay {1}, Error: {2}", timeSpan, exception.Message), exception);
         _systemMetrics.LogCount("backends.sqlserver.retry");
     });
 }
Exemplo n.º 5
0
 private void InitialiseRetryHandling()
 {
     _retryStrategy         = new Incremental(_retries, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
     _retryPolicy           = new RetryPolicy <SqlServerErrorDetectionStrategy>(_retries);
     _retryPolicy.Retrying += (sender, args) =>
     {
         _log.Warn(String.Format("Retry {0} failed. Trying again. Delay {1}, Error: {2}", args.CurrentRetryCount, args.Delay, args.LastException.Message), args.LastException);
         _systemMetrics.LogCount("backends.sqlserver.retry");
     };
 }
Exemplo n.º 6
0
 private void PostToLibrato(LibratoMetric[] lines)
 {
     try
     {
         PostToLibratoInternal(lines);
     }
     catch (Exception ex)
     {
         _log.ErrorException("Failed to post metrics to Librato.com", ex);
         _systemMetrics.LogCount("backends.librato.post.error." + ex.GetType().Name);
     }
 }
Exemplo n.º 7
0
        private void ProcessIncomingConnection(TcpClient tcpClient)
        {
            try
            {
                Interlocked.Increment(ref _activeConnections);
                _systemMetrics.LogGauge("listeners.statsdnet.activeConnections", _activeConnections);
                _systemMetrics.LogCount("listeners.statsdnet.connection.open");
                using (BinaryReader reader = new BinaryReader(tcpClient.GetStream()))
                {
                    while (true)
                    {
                        if (reader.PeekChar() == 0)
                        {
                            // close the socket
                            return;
                        }
                        // Get the length
                        var packetLength = reader.ReadInt32();
                        // Is it compressed?
                        var isCompressed = reader.ReadBoolean();
                        // Now get the packet
                        var packet = reader.ReadBytes(packetLength);
                        // Decode
                        _decoderBlock.Post(new DecoderBlockPacket(packet, isCompressed));
                    }
                }
            }
            catch (SocketException se)
            {
                // oops, we're done
                _systemMetrics.LogCount("listeners.statsdnet.error.SocketException." + se.SocketErrorCode.ToString());
                _log.Error(String.Format("Socket Error occurred while listening. Code: {0}", se.SocketErrorCode), se);
            }
            catch (Exception ex)
            {
                _systemMetrics.LogCount("listeners.statsdnet.error." + ex.GetType().Name);
                _log.Error(String.Format("{0} Error occurred while listening: ", ex.GetType().Name, ex.Message),
                           ex);
            }
            finally
            {
                try
                {
                    tcpClient.Close();
                }
                catch
                {
                    // Do nothing but log that this happened
                    _systemMetrics.LogCount("listeners.statsdnet.error.closeThrewException");
                }

                _systemMetrics.LogCount("listeners.statsdnet.connection.closed");
                Interlocked.Decrement(ref _activeConnections);
                _systemMetrics.LogGauge("listeners.statsdnet.activeConnections", _activeConnections);
            }
        }
Exemplo n.º 8
0
 private void SendLine(GraphiteLine line)
 {
     byte[] data = Encoding.ASCII.GetBytes(line.ToString());
     try
     {
         _client.Send(data, data.Length);
         _systemMetrics.LogCount("backends.graphite.lines");
         _systemMetrics.LogCount("backends.graphite.bytes", data.Length);
     }
     catch (SocketException ex)
     {
         _log.Error("Failed to send packet to Graphite: " + ex.SocketErrorCode);
     }
 }
Exemplo n.º 9
0
        private void ProcessIncomingConnection(TcpClient tcpClient)
        {
            try
            {
                Interlocked.Increment(ref _activeConnections);
                _systemMetrics.LogGauge("tcp.activeConnections", _activeConnections);
                _systemMetrics.LogCount("tcp.connection.open");
                using (var networkStream = tcpClient.GetStream())
                {
                    // Set an aggressive read timeout
                    networkStream.ReadTimeout = 1000; /* one second */
                    var buffer = new byte[4096];
                    while (!_token.IsCancellationRequested)
                    {
                        var byteCount = networkStream.Read(buffer, 0, buffer.Length);
                        if (byteCount == 0)
                        {
                            return;
                        }
                        _systemMetrics.LogCount("tcp.reads");
                        _systemMetrics.LogCount("tcp.bytes", byteCount);
                        var lines = Encoding.UTF8.GetString(buffer, 0, byteCount).Replace("\r", "").Split('\n');
                        // Post what we have
                        _systemMetrics.LogCount("tcp.lines", lines.Length);
                        lines.Where(p => !String.IsNullOrEmpty(p)).PostManyTo(_target);
                        // Two blank lines means end the connection
                        if (lines.Length >= 2 && lines[lines.Length - 2] == "" && lines[lines.Length - 1] == "")
                        {
                            return;
                        }
                    }
                }
            }
            catch (SocketException se)
            {
                // oops, we're done
                _systemMetrics.LogCount("tcp.error.SocketException." + se.SocketErrorCode.ToString());
            }
            catch (IOException)
            {
                // Not much we can do here.
                _systemMetrics.LogCount("tcp.error.IOException");
            }
            finally
            {
                try
                {
                    tcpClient.Close();
                }
                catch
                {
                    // Do nothing but log that this happened
                    _systemMetrics.LogCount("tcp.error.closeThrewException");
                }

                _systemMetrics.LogCount("tcp.connection.closed");
                Interlocked.Decrement(ref _activeConnections);
                _systemMetrics.LogGauge("tcp.activeConnections", _activeConnections);
            }
        }
Exemplo n.º 10
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, GraphiteLine messageValue, ISourceBlock <GraphiteLine> source, bool consumeToAccept)
 {
     byte[] data = Encoding.ASCII.GetBytes(messageValue.ToString());
     try
     {
         _client.Send(data, data.Length);
         _systemMetrics.LogCount("backends.graphite.lines");
         _systemMetrics.LogCount("backends.graphite.bytes", data.Length);
     }
     catch (SocketException ex)
     {
         _log.Error("Failed to send packet to Graphite: " + ex.SocketErrorCode.ToString());
     }
     return(DataflowMessageStatus.Accepted);
 }
Exemplo n.º 11
0
        public MSSQLRelayListener(string connectionString,
                                  TimeSpan pollInterval,
                                  CancellationToken cancellationToken,
                                  int batchSize,
                                  bool deleteAfterSend,
                                  ISystemMetricsService metrics)
        {
            _connectionString  = connectionString;
            _intervalService   = new IntervalService(pollInterval, cancellationToken);
            _cancellationToken = cancellationToken;
            _batchSize         = batchSize;
            _deleteAfterSend   = deleteAfterSend;
            _metrics           = metrics;

            var stopwatch = new Stopwatch();

            _intervalService.Elapsed += (sender, e) =>
            {
                if (IsListening)
                {
                    _intervalService.Cancel(true);
                    stopwatch.Restart();
                    ReadAndFeed();
                    stopwatch.Stop();
                    metrics.LogCount("listeners.mssql-relay.feedTimeSeconds", Convert.ToInt32(stopwatch.Elapsed.TotalSeconds));

                    // Only continue the interval service if cancellation
                    // isn't in progress
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        _intervalService.Start();
                    }
                }
            };
        }
Exemplo n.º 12
0
 private void AddAggregator(MessageType targetType,
                            ActionBlock <StatsdMessage> aggregator,
                            ISystemMetricsService systemMetrics)
 {
     _router.AddTarget(targetType, aggregator);
     systemMetrics.LogCount("startup.aggregator." + targetType.ToString());
 }
Exemplo n.º 13
0
        public MSSQLRelayListener(string connectionString, 
            TimeSpan pollInterval,
            CancellationToken cancellationToken,
            int batchSize,
            bool deleteAfterSend,
            ISystemMetricsService metrics)
        {
            _connectionString = connectionString;
            _intervalService = new IntervalService(pollInterval, cancellationToken);
            _cancellationToken = cancellationToken;
            _batchSize = batchSize;
            _deleteAfterSend = deleteAfterSend;
            _metrics = metrics;

            var stopwatch = new Stopwatch();

            _intervalService.Elapsed += (sender, e) =>
                {
                    if (IsListening)
                    {
                        _intervalService.Cancel(true);
                        stopwatch.Restart();
                        ReadAndFeed();
                        stopwatch.Stop();
                        metrics.LogCount("listeners.mssql-relay.feedTimeSeconds", Convert.ToInt32(stopwatch.Elapsed.TotalSeconds));

                        // Only continue the interval service if cancellation
                        // isn't in progress
                        if (!cancellationToken.IsCancellationRequested)
                        {
                            _intervalService.Start();
                        }
                    }
                };
        }
Exemplo n.º 14
0
    private void ProcessRequest(HttpListenerContext context)
    {
      context.Response.Headers.Add("Server", "statsd.net");
      System.Threading.Thread.Sleep(10000);

      if (context.Request.Url.PathAndQuery.Equals("/crossdomain.xml", StringComparison.OrdinalIgnoreCase))
      {
        SendCrossdomainFile(context.Response);
        return;
      }
      else if (context.Request.Url.PathAndQuery.Equals("/favicon.ico", StringComparison.OrdinalIgnoreCase))
      {
        context.Response.StatusCode = (int)HttpStatusCode.OK;
        context.Response.Close();
        return;
      }
      else if (context.Request.HttpMethod.ToUpper() != "POST")
      {
        context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
        context.Response.StatusDescription = "Please use POST.";
        context.Response.Close();
        return;
      }

      context.Response.StatusCode = (int)HttpStatusCode.OK;
      context.Response.StatusDescription = "OK";

      // Get the body of this request
      using (var body = context.Request.InputStream)
      {
        using (var reader = new System.IO.StreamReader(body, context.Request.ContentEncoding))
        {
          var rawPacket = reader.ReadToEnd();
          _systemMetrics.LogCount("listeners.http.bytes", (int)context.Request.ContentLength64);
          string[] lines = rawPacket.Replace("\r", "").Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
          for (int index = 0; index < lines.Length; index++)
          {
            _target.Post(lines[index]);
          }
          _systemMetrics.LogCount("listeners.http.lines", lines.Length);
        }
      }
      context.Response.Close();
    }
Exemplo n.º 15
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));
         }
     }
 }
Exemplo n.º 16
0
 private void ProcessIncomingConnection(TcpClient tcpClient)
 {
     try
     {
         Interlocked.Increment(ref _activeConnections);
         _systemMetrics.LogGauge("tcp.activeConnections", _activeConnections);
         _systemMetrics.LogCount("tcp.connection.open");
         using (var networkStream = tcpClient.GetStream())
         {
             var buffer = new byte[4096];
             while (!_token.IsCancellationRequested)
             {
                 var byteCount = networkStream.Read(buffer, 0, buffer.Length);
                 _systemMetrics.LogCount("tcp.bytes", byteCount);
                 var lines = Encoding.UTF8.GetString(buffer, 0, byteCount).Replace("\r", "").Split('\n');
                 // Post what we have
                 _systemMetrics.LogCount("tcp.lines", lines.Length);
                 lines.Where(p => !String.IsNullOrEmpty(p)).PostManyTo(_target);
                 // Two blank lines means end the connection
                 if (lines.Length >= 2 && lines[lines.Length - 2] == "" && lines[lines.Length - 1] == "")
                 {
                     return;
                 }
             }
         }
     }
     catch (SocketException)
     {
         // oops, we're done
     }
     catch (IOException)
     {
         // Not much we can do here.
     }
     finally
     {
         tcpClient.Close();
         _systemMetrics.LogCount("tcp.connection.closed");
         Interlocked.Increment(ref _activeConnections);
         _systemMetrics.LogGauge("tcp.activeConnections", _activeConnections);
     }
 }
Exemplo n.º 17
0
    public UdpStatsListener(int port, ISystemMetricsService systemMetrics)
    {
      _port = port;
      _systemMetrics = systemMetrics;
      _preprocessorBlock = new ActionBlock<byte []>( (data) =>
        {
          // Log a metric to see what the difference between the read buffer and the max buffer size is
          _systemMetrics.LogGauge("listeners.udp.buffer.max", MAX_BUFFER_SIZE);
          _systemMetrics.LogGauge("listeners.udp.buffer.current", data.Length);
          _systemMetrics.LogCount( "listeners.udp.bytes", data.Length );
          string rawPacket = Encoding.UTF8.GetString( data );

          string [] lines = rawPacket.Replace( "\r", "" ).Split( new char [] { '\n' }, StringSplitOptions.RemoveEmptyEntries );
          for ( int index = 0; index < lines.Length; index++ )
          {
            _targetBlock.Post( lines [ index ] );
          }
          _systemMetrics.LogCount( "listeners.udp.lines", lines.Length );
        }, Utility.UnboundedExecution() );
    }
Exemplo n.º 18
0
        public UdpStatsListener(int port, ISystemMetricsService systemMetrics)
        {
            _port              = port;
            _systemMetrics     = systemMetrics;
            _preprocessorBlock = new ActionBlock <byte []>((data) =>
            {
                // Log a metric to see what the difference between the read buffer and the max buffer size is
                _systemMetrics.LogGauge("listeners.udp.buffer.max", MAX_BUFFER_SIZE);
                _systemMetrics.LogGauge("listeners.udp.buffer.current", data.Length);
                _systemMetrics.LogCount("listeners.udp.bytes", data.Length);
                string rawPacket = Encoding.UTF8.GetString(data);

                string [] lines = rawPacket.Replace("\r", "").Split(new char [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < lines.Length; index++)
                {
                    _targetBlock.Post(lines [index]);
                }
                _systemMetrics.LogCount("listeners.udp.lines", lines.Length);
            }, Utility.UnboundedExecution());
        }
Exemplo n.º 19
0
 public void AddBackend(IBackend backend, ISystemMetricsService systemMetrics, string name)
 {
     _log.InfoFormat("Adding backend {0} named '{1}'", backend.GetType().Name, name);
     _backends.Add(backend);
     _messageBroadcaster.LinkTo(backend);
     backend.Completion.LogAndContinueWith(name, () =>
     {
         if (_backends.All(q => !q.IsActive))
         {
             _shutdownComplete.Set();
         }
     });
     systemMetrics.LogCount("startup.backend." + name);
 }
 public static TransformBlock<String, StatsdMessage> CreateMessageParserBlock(CancellationToken cancellationToken,
   ISystemMetricsService systemMetrics,
   ILog log)
 {
   var block = new TransformBlock<String, StatsdMessage>(
     (line) =>
     {
       systemMetrics.LogCount("parser.linesSeen");
       StatsdMessage message = StatsdMessageFactory.ParseMessage(line);
       if (message is InvalidMessage)
       {
         systemMetrics.LogCount("parser.badLinesSeen");
         log.Info("Bad message: " + ((InvalidMessage)message).Reason);
       }
       return message;
     },
     new ExecutionDataflowBlockOptions()
     {
       MaxDegreeOfParallelism = ExecutionDataflowBlockOptions.Unbounded,
       CancellationToken = cancellationToken
     });
   return block;
 }
Exemplo n.º 21
0
        public static TransformBlock <String, StatsdMessage> CreateMessageParserBlock(CancellationToken cancellationToken,
                                                                                      ISystemMetricsService systemMetrics)
        {
            var block = new TransformBlock <String, StatsdMessage>(
                (line) =>
            {
                systemMetrics.LogCount("parser.linesSeen");
                StatsdMessage message = StatsdMessageFactory.ParseMessage(line);
                if (message is InvalidMessage)
                {
                    systemMetrics.LogCount("parser.badLinesSeen");
                    log.Info("Bad message: " + ((InvalidMessage)message).Reason + Environment.NewLine + line);
                }
                return(message);
            },
                new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = ExecutionDataflowBlockOptions.Unbounded,
                CancellationToken      = cancellationToken
            });

            return(block);
        }
Exemplo n.º 22
0
        private void PostMetrics(GraphiteLine[][] lineArrays)
        {
            var lines = new List <GraphiteLine>();

            foreach (var graphiteLineArray in lineArrays)
            {
                lines.AddRange(graphiteLineArray);
            }
            var rawText = string.Join(Environment.NewLine,
                                      lines.Select(line => line.ToString()).ToArray());
            var bytes = Encoding.UTF8.GetBytes(rawText);

            if (_client.Send(bytes))
            {
                _systemMetrics.LogCount("backends.statsdnet.lines", lines.Count);
                _systemMetrics.LogGauge("backends.statsdnet.bytes", bytes.Length);
            }
        }
Exemplo n.º 23
0
        public void Configure(string collectorName, XElement configElement, ISystemMetricsService systemMetrics)
        {
            _completionTask = new Task(() => IsActive = false);
            _systemMetrics  = systemMetrics;

            var config = new LibratoBackendConfiguration(
                email: configElement.Attribute("email").Value,
                token: configElement.Attribute("token").Value,
                numRetries: configElement.ToInt("numRetries"),
                retryDelay: Utility.ConvertToTimespan(configElement.Attribute("retryDelay").Value),
                postTimeout: Utility.ConvertToTimespan(configElement.Attribute("postTimeout").Value),
                maxBatchSize: configElement.ToInt("maxBatchSize"),
                countersAsGauges: configElement.ToBoolean("countersAsGauges")
                );

            _config         = config;
            _source         = collectorName;
            _serviceVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

            _preprocessorBlock = new ActionBlock <Bucket>(bucket => ProcessBucket(bucket), Utility.UnboundedExecution());
            _batchBlock        = new BatchBlock <LibratoMetric>(_config.MaxBatchSize);
            _outputBlock       = new ActionBlock <LibratoMetric[]>(lines => PostToLibrato(lines), Utility.OneAtATimeExecution());
            _batchBlock.LinkTo(_outputBlock);

            _client = new HttpClient()
            {
                BaseAddress = new Uri(LIBRATO_API_URL)
            };

            var authByteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", _config.Email, _config.Token));

            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authByteArray));
            _client.Timeout = TimeSpan.FromMilliseconds(_config.PostTimeout.TotalMilliseconds);


            _retryPolicy = Policy.Handle <TimeoutException>().WaitAndRetry(_config.NumRetries, retryAttempt => _config.RetryDelay, (exception, timeSpan) =>
            {
                _log.WarnException(String.Format("Retry failed. Trying again. Delay {1}, Error: {2}", timeSpan, exception.Message), exception);
                _systemMetrics.LogCount("backends.librato.retry");
            });

            IsActive = true;
        }
Exemplo n.º 24
0
        public void Configure(string collectorName, XElement configElement, ISystemMetricsService systemMetrics)
        {
            _completionTask = new Task(() => IsActive = false);
            _log            = SuperCheapIOC.Resolve <ILog>();
            _systemMetrics  = systemMetrics;

            var config = new LibratoBackendConfiguration(
                email: configElement.Attribute("email").Value,
                token: configElement.Attribute("token").Value,
                numRetries: configElement.ToInt("numRetries"),
                retryDelay: Utility.ConvertToTimespan(configElement.Attribute("retryDelay").Value),
                postTimeout: Utility.ConvertToTimespan(configElement.Attribute("postTimeout").Value),
                maxBatchSize: configElement.ToInt("maxBatchSize"),
                countersAsGauges: configElement.ToBoolean("countersAsGauges")
                );

            _config         = config;
            _source         = collectorName;
            _serviceVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

            _preprocessorBlock = new ActionBlock <Bucket>(bucket => ProcessBucket(bucket), Utility.UnboundedExecution());
            _batchBlock        = new BatchBlock <LibratoMetric>(_config.MaxBatchSize);
            _outputBlock       = new ActionBlock <LibratoMetric[]>(lines => PostToLibrato(lines), Utility.OneAtATimeExecution());
            _batchBlock.LinkTo(_outputBlock);

            _client = new RestClient(LIBRATO_API_URL);
            _client.Authenticator = new HttpBasicAuthenticator(_config.Email, _config.Token);
            _client.Timeout       = (int)_config.PostTimeout.TotalMilliseconds;

            _retryPolicy           = new RetryPolicy <LibratoErrorDetectionStrategy>(_config.NumRetries);
            _retryPolicy.Retrying += (sender, args) =>
            {
                _log.Warn(String.Format("Retry {0} failed. Trying again. Delay {1}, Error: {2}", args.CurrentRetryCount, args.Delay, args.LastException.Message), args.LastException);
                _systemMetrics.LogCount("backends.librato.retry");
            };
            _retryStrategy = new Incremental(_config.NumRetries, _config.RetryDelay, TimeSpan.FromSeconds(2));
            IsActive       = true;
        }
Exemplo n.º 25
0
        private void SendBatch(GraphiteLine[] batch)
        {
            try
            {
                _systemMetrics.LogCount("backends.graphite-tcp.write.attempt");
                EnsureConnectedClient(ref _client);
                var lines   = batch.Select(p => p.ToString()).ToArray();
                var payload = string.Join(Environment.NewLine, lines) + Environment.NewLine;
                var bytes   = Encoding.UTF8.GetBytes(payload);
                _systemMetrics.LogCount("backends.graphite-tcp.lines", lines.Length);
                _systemMetrics.LogCount("backends.graphite-tcp.bytes", bytes.Length);

                _client.GetStream().Write(bytes, 0, bytes.Length);
                _systemMetrics.LogCount("backends.graphite-tcp.write.success");
            }
            catch (Exception ex)
            {
                _log.ErrorException(string.Format("Could not write batch to graphite host at {0}:{1}", _host, _port), ex);
                _systemMetrics.LogCount("backends.graphite-tcp.write.failure");
                _systemMetrics.LogCount("backends.graphite-tcp.write.exception." + ex.GetType().Name);
            }
        }
Exemplo n.º 26
0
        private bool Send(byte[] data, int retryAttemptsLeft)
        {
            /** Statsd.net packet format consists of
             * byte 0: 32-bit integer length
             * byte 33: boolean for whether the data is compressed or not
             * byte 34-: the packet
             */

            if (data.Length == 0)
            {
                return(true);
            }

            Func <bool> handleRetry = () =>
            {
                _systemMetrics.LogCount("backends.statsdnet.sendFailed");
                if (retryAttemptsLeft > 0)
                {
                    _systemMetrics.LogCount("backends.statsdnet.retrySend");
                    return(Send(data, --retryAttemptsLeft));
                }
                else
                {
                    _systemMetrics.LogCount("backends.statsdnet.retrySendFailed");
                    return(false);
                }
            };

            try
            {
                if (!_client.Connected)
                {
                    try
                    {
                        _client.Close();
                    }
                    catch (Exception)
                    {
                        // eat it
                    }
                    _client = new TcpClient();
                    _client.Connect(_host, _port);
                    _writer = new BinaryWriter(_client.GetStream());
                }
                if (_enableCompression && data.Length < COMPRESSION_SIZE_THRESHOLD)
                {
                    _writer.Write(data.Length);
                    _writer.Write(false);
                    _writer.Write(data);
                    _systemMetrics.LogCount("backends.statsdnet.bytes.raw", data.Length);
                    _systemMetrics.LogCount("backends.statsdnet.bytes.compressed", data.Length);
                }
                else
                {
                    var compressedData = data.Compress();
                    _writer.Write(compressedData.Length);
                    _writer.Write(true);
                    _writer.Write(compressedData);
                    _systemMetrics.LogCount("backends.statsdnet.bytes.raw", data.Length);
                    _systemMetrics.LogCount("backends.statsdnet.bytes.compressed", compressedData.Length);
                }
                return(true);
            }
            catch (SocketException se)
            {
                _systemMetrics.LogCount("backends.statsdnet.error.SocketException." + se.SocketErrorCode.ToString());
                _log.Error(String.Format("Socket Error occurred while listening. Code: {0}", se.SocketErrorCode), se);
                return(handleRetry());
            }
            catch (Exception ex)
            {
                _systemMetrics.LogCount("backends.statsdnet.error." + ex.GetType().Name);
                _log.Error(String.Format("{0} Error occurred while listening: ", ex.GetType().Name, ex.Message),
                           ex);
                if (ex is IOException)
                {
                    return(handleRetry());
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 27
0
 private void ReadAndFeed()
 {
     try
     {
         _metrics.LogCount("listeners.mssql-relay.feed.attempt");
         var lines = GetNewLinesFromDB();
         foreach (String line in lines)
         {
             var parts = line.Split(SPACE_SPLITTER, StringSplitOptions.RemoveEmptyEntries);
             _target.Post(parts[0] + ":" + parts[1] + "|r|" + parts[2]);
         }
         _metrics.LogCount("listeners.mssql-relay.lines.posted" + lines.Count);
         _metrics.LogCount("listeners.mssql-relay.feed.success");
     }
     catch (Exception ex)
     {
         _metrics.LogCount("listeners.mssql-relay.error." + ex.GetType().Name);
         _metrics.LogCount("listeners.mssql-relay.feed.failure");
     }
 }
Exemplo n.º 28
0
    public void Configure(string collectorName, XElement configElement, ISystemMetricsService systemMetrics)
    {
      _completionTask = new Task(() => IsActive = false);
      _log = SuperCheapIOC.Resolve<ILog>();
      _systemMetrics = systemMetrics;

      var config = new LibratoBackendConfiguration(
          email: configElement.Attribute("email").Value,
          token: configElement.Attribute("token").Value,
          numRetries: configElement.ToInt("numRetries"),
          retryDelay: Utility.ConvertToTimespan(configElement.Attribute("retryDelay").Value),
          postTimeout: Utility.ConvertToTimespan(configElement.Attribute("postTimeout").Value),
          maxBatchSize: configElement.ToInt("maxBatchSize"),
          countersAsGauges: configElement.ToBoolean("countersAsGauges")
        );
      
      _config = config;
      _source = collectorName;
      _serviceVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

      _preprocessorBlock = new ActionBlock<Bucket>(bucket => ProcessBucket(bucket), Utility.UnboundedExecution());
      _batchBlock = new BatchBlock<LibratoMetric>(_config.MaxBatchSize);
      _outputBlock = new ActionBlock<LibratoMetric[]>(lines => PostToLibrato(lines), Utility.OneAtATimeExecution());
      _batchBlock.LinkTo(_outputBlock);

      _client = new RestClient(LIBRATO_API_URL);
      _client.Authenticator = new HttpBasicAuthenticator(_config.Email, _config.Token);
      _client.Timeout = (int)_config.PostTimeout.TotalMilliseconds;

      _retryPolicy = new RetryPolicy<LibratoErrorDetectionStrategy>(_config.NumRetries);
      _retryPolicy.Retrying += (sender, args) =>
      {
        _log.Warn(String.Format("Retry {0} failed. Trying again. Delay {1}, Error: {2}", args.CurrentRetryCount, args.Delay, args.LastException.Message), args.LastException);
        _systemMetrics.LogCount("backends.librato.retry");
      };
      _retryStrategy = new Incremental(_config.NumRetries, _config.RetryDelay, TimeSpan.FromSeconds(2));
      IsActive = true;
    }
Exemplo n.º 29
0
 public void AddBackend(IBackend backend, ISystemMetricsService systemMetrics, string name)
 {
     _log.InfoFormat("Adding backend {0} named '{1}'", backend.GetType().Name, name);
     _backends.Add(backend);
     _messageBroadcaster.LinkTo(backend);
     backend.Completion.LogAndContinueWith(_log, name, () =>
       {
           if (_backends.All(q => !q.IsActive))
           {
               _shutdownComplete.Set();
           }
       });
     systemMetrics.LogCount("startup.backend." + name);
 }
Exemplo n.º 30
0
 private void AddAggregator(MessageType targetType,
   ActionBlock<StatsdMessage> aggregator,
   ISystemMetricsService systemMetrics)
 {
     _router.AddTarget(targetType, aggregator);
     systemMetrics.LogCount("startup.aggregator." + targetType.ToString());
 }
Exemplo n.º 31
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));
         }
     }
 }
Exemplo n.º 32
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);
     }
   }
 }