Beispiel #1
0
        public void SendMetrics(IEnumerable <string> metrics)
        {
            var data = _eventArgsPool.Pop();

            //firehose alert! -- keep it moving!
            if (null == data)
            {
                return;
            }

            try
            {
                data.RemoteEndPoint = _ipBasedEndpoint ??
                                      //only DNS resolve if we were given a hostname
                                      new IPEndPoint(Dns.GetHostAddresses(_hostNameOrAddress)[0], _port);
                data.SendPacketsElements = metrics.ToMaximumBytePackets()
                                           .Select(bytes => new SendPacketsElement(bytes, 0, bytes.Length, true))
                                           .ToArray();

                if (!_client.Client.Connected)
                {
                    _client.Client.Connect(data.RemoteEndPoint);
                }
                //_client.Client.NoDelay = true;
                _client.Client.SendPacketsAsync(data);

                //Write-Debug "Wrote $(byteBlock.length) bytes to $server:$port"
            }
            //fire and forget, so just eat intermittent failures / exceptions
            catch
            { }
        }
Beispiel #2
0
        private static void SendToServer(string server, int port, IEnumerable <string> metrics, bool stream)
        {
            UdpMessenger messenger = null;
            SimpleObjectPool <UdpMessenger> serverPool = null;

            try
            {
                serverPool = _messengerPool.GetOrAdd(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", server, port),
                                                     new SimpleObjectPool <UdpMessenger>(3, pool => new UdpMessenger(server, port)));
                messenger = serverPool.Pop();

                //all used up, sorry!
                if (null == messenger)
                {
                    return;
                }

                if (stream)
                {
                    messenger.StreamMetrics(metrics);
                }
                else
                {
                    messenger.SendMetrics(metrics);
                }
            }
            finally
            {
                if (null != serverPool && null != messenger)
                {
                    serverPool.Push(messenger);
                }
            }
        }
Beispiel #3
0
        public static string Sanitize(this string key)
        {
            if (IsValidKey(key))
            {
                return(key);
            }

            StringBuilder sanitized = null;

            try
            {
                sanitized = _builderPool.Pop();
                if (null != sanitized)
                {
                    sanitized.Clear();
                    sanitized.Append(key);
                }
                //autogrow pool as necessary
                else
                {
                    sanitized = new StringBuilder(key);
                }

                for (int i = 0; i < key.Length; ++i)
                {
                    if (_badChars.Contains(key[i]))
                    {
                        sanitized[i] = '_';
                    }
                }

                return(sanitized.ToString());
            }
            finally
            {
                if (null != sanitized)
                {
                    _builderPool.Push(sanitized);
                }
            }
        }