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); } }
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); } }
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); } }
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() ); }
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()); }
private void PostToLibratoInternal(LibratoMetric[] lines) { var pendingLines = 0; foreach (var epochGroup in lines.GroupBy(p => p.Epoch)) { var payload = GetPayload(epochGroup); pendingLines = payload.gauges.Length + payload.counters.Length; _systemMetrics.LogGauge("backends.librato.lines", pendingLines); Interlocked.Add(ref _pendingOutputCount, pendingLines); var request = new HttpRequestMessage(); request.Headers.Add("User-Agent", "statsd.net-librato-backend/" + _serviceVersion); var content = new StringContent(SimpleJson.SerializeObject(payload), Encoding.UTF8, "application/json"); _retryPolicy.Execute(() => { bool succeeded = false; try { _systemMetrics.LogCount("backends.librato.post.attempt"); var task = _client.PostAsync("/v1/metrics", content); var response = task.Result; if (response.StatusCode == HttpStatusCode.Unauthorized) { _systemMetrics.LogCount("backends.librato.error.unauthorised"); throw new UnauthorizedAccessException("Librato.com reports that your access is not authorised. Is your API key and email address correct?"); } else if (response.StatusCode != HttpStatusCode.OK) { _systemMetrics.LogCount("backends.librato.error." + response.StatusCode); throw new Exception(String.Format("Request could not be processed. Server said {0}", response.StatusCode)); } else { succeeded = true; _log.Info(String.Format("Wrote {0} lines to Librato.", pendingLines)); } } finally { Interlocked.Add(ref _pendingOutputCount, -pendingLines); _systemMetrics.LogCount("backends.librato.post." + (succeeded ? "success" : "failure")); } }); } }
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); } }