Beispiel #1
0
        public StatsdData BuildStatsData(StatsdConfig config)
        {
            var endPoint       = BuildEndPoint(config);
            var transportData  = CreateTransportData(endPoint, config);
            var transport      = transportData.Transport;
            var globalTags     = GetGlobalTags(config);
            var telemetry      = CreateTelemetry(config, globalTags, endPoint, transportData.Transport);
            var serializers    = CreateSerializers(config.Prefix, globalTags, config.Advanced.MaxMetricsInAsyncQueue);
            var statsBufferize = CreateStatsBufferize(
                telemetry,
                transportData.Transport,
                transportData.BufferCapacity,
                config.Advanced,
                serializers,
                config.ClientSideAggregation);

            var metricsSender = new MetricsSender(
                statsBufferize,
                new RandomGenerator(),
                new StopWatchFactory(),
                telemetry,
                config.StatsdTruncateIfTooLong,
                config.Advanced.MaxMetricsInAsyncQueue * 2);

            return(new StatsdData(metricsSender, statsBufferize, transport, telemetry));
        }
        public void Configure(StatsdConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (string.IsNullOrEmpty(config.StatsdServerName))
            {
                throw new ArgumentNullException("config.StatsdServername");
            }

            if (_config != null)
            {
                throw new InvalidOperationException("Configuration for DogStatsdService already performed");
            }

            _config = config;
            _prefix = config.Prefix;

            if (!string.IsNullOrEmpty(config.StatsdServerName))
            {
                var statsdUdp = new StatsdUDP(config.StatsdServerName, config.StatsdPort, config.StatsdMaxUDPPacketSize);
                _statsD = new Statsd(statsdUdp);
                _statsD.TruncateIfTooLong = config.StatsdTruncateIfTooLong;
                _disposable = statsdUdp;
            }
        }
        public void Configure(StatsdConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_config != null)
            {
                throw new InvalidOperationException("Configuration for DogStatsdService already performed");
            }

            _config = config;
            _prefix = config.Prefix;

            if (!string.IsNullOrEmpty(config.StatsdServerName) || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(StatsdConfig.DD_AGENT_HOST_ENV_VAR)))
            {
                var statsdUdp = new StatsdUDP(config.StatsdServerName, config.StatsdPort, config.StatsdMaxUDPPacketSize);
                _statsD = new Statsd(statsdUdp, new RandomGenerator(), new StopWatchFactory(), "", config.ConstantTags);
                _statsD.TruncateIfTooLong = config.StatsdTruncateIfTooLong;
                _disposable = statsdUdp;
            }
            else
            {
                throw new ArgumentNullException("config.StatsdServername and DD_AGENT_HOST environment variable not set");
            }
        }
Beispiel #4
0
        private Telemetry CreateTelemetry(
            StatsdConfig config,
            string[] globalTags,
            DogStatsdEndPoint dogStatsdEndPoint,
            ITransport transport)
        {
            var telemetryFlush = config.Advanced.TelemetryFlushInterval;

            if (telemetryFlush.HasValue)
            {
                var        assembly = typeof(StatsdBuilder).GetTypeInfo().Assembly;
                var        version  = assembly.GetName().Version.ToString();
                var        optionalTelemetryEndPoint = config.Advanced.OptionalTelemetryEndPoint;
                ITransport telemetryTransport        = transport;
                if (optionalTelemetryEndPoint != null && !dogStatsdEndPoint.AreEquals(optionalTelemetryEndPoint))
                {
                    telemetryTransport = CreateTransport(optionalTelemetryEndPoint, config);
                }

                return(_factory.CreateTelemetry(version, telemetryFlush.Value, telemetryTransport, globalTags));
            }

            // Telemetry is not enabled
            return(new Telemetry());
        }
Beispiel #5
0
        private ITransport CreateTransport(DogStatsdEndPoint endPoint, StatsdConfig config)
        {
            var serverName = endPoint.ServerName;

            if (!string.IsNullOrEmpty(serverName))
            {
                if (serverName.StartsWith(UnixDomainSocketPrefix))
                {
                    serverName = serverName.Substring(UnixDomainSocketPrefix.Length);
                    var unixEndPoint = new UnixEndPoint(serverName);
                    return(_factory.CreateUnixDomainSocketTransport(
                               unixEndPoint,
                               config.Advanced.UDSBufferFullBlockDuration));
                }

                return(CreateUDPTransport(endPoint));
            }

            var pipeName = endPoint.PipeName;

            if (string.IsNullOrEmpty(pipeName))
            {
                throw new ArgumentException($"Error: empty {nameof(DogStatsdEndPoint)}");
            }

            return(_factory.CreateNamedPipeTransport(pipeName));
        }
Beispiel #6
0
        private static DogStatsdEndPoint BuildEndPoint(StatsdConfig config)
        {
            var statsdServerName = !string.IsNullOrEmpty(config.StatsdServerName)
                            ? config.StatsdServerName
                            : Environment.GetEnvironmentVariable(StatsdConfig.DD_AGENT_HOST_ENV_VAR);

            var pipeName = !string.IsNullOrEmpty(config.PipeName)
                            ? config.PipeName
                            : Environment.GetEnvironmentVariable(StatsdConfig.AgentPipeNameEnvVar);

            if (string.IsNullOrEmpty(statsdServerName) && string.IsNullOrEmpty(pipeName))
            {
                // Ignore pipe name in the error message as its usage is internal only.
                throw new ArgumentNullException(
                    $"{nameof(config)}.{nameof(config.StatsdServerName)} and"
                    + $" {StatsdConfig.DD_AGENT_HOST_ENV_VAR} environment variable not set");
            }

            return new DogStatsdEndPoint
            {
                ServerName = statsdServerName,
                PipeName = pipeName,
                Port = GetPort(config.StatsdPort),
            };
        }
        private StatsSender CreateUDPStatsSender(StatsdConfig config, string statsdServerName)
        {
            var address = StatsdUDP.GetIpv4Address(statsdServerName);
            var port    = GetPort(config);

            var endPoint = new IPEndPoint(address, port);

            return(_factory.CreateUDPStatsSender(endPoint));
        }
 public void default_port_is_8125()
 {
     var metricsConfig = new StatsdConfig
     {
         StatsdServerName = "127.0.0.1"
     };
     StatsdClient.DogStatsd.Configure(metricsConfig);
     testReceive("127.0.0.1", 8125, "test", "test:1|c");
 }
 public void setting_port()
 {
     var metricsConfig = new StatsdConfig
     {
         StatsdServerName = "127.0.0.1",
         StatsdPort = 8126
     };
     StatsdClient.DogStatsd.Configure(metricsConfig);
     testReceive("127.0.0.1", 8126, "test", "test:1|c");
 }
        private UdpTransport(string prefix, string statsdHost, int port, string[] globalTags, double sampleRate) {

            var dogStatsdConfig = new StatsdConfig
            {
                Prefix = prefix,
                StatsdServerName = statsdHost,
                StatsdPort = port
            };

            DogStatsd.Configure(dogStatsdConfig);
            this._sampleRate = sampleRate;
        }
Beispiel #11
0
        public void Configure(StatsdConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (string.IsNullOrEmpty(config.StatsdServerName))
            {
                throw new ArgumentNullException("config.StatsdServername");
            }

            _prefix = config.Prefix;
            _statsD = new Statsd(new StatsdUDP(config.StatsdServerName, config.StatsdPort, config.StatsdMaxUDPPacketSize));
        }
Beispiel #12
0
        private TransportData CreateTransportData(DogStatsdEndPoint endPoint, StatsdConfig config)
        {
            var transportData = new TransportData();

            transportData.Transport = CreateTransport(endPoint, config);
            switch (transportData.Transport.TransportType)
            {
                case TransportType.UDP: transportData.BufferCapacity = config.StatsdMaxUDPPacketSize; break;
                case TransportType.UDS: transportData.BufferCapacity = config.StatsdMaxUnixDomainSocketPacketSize; break;
                // use StatsdMaxUDPPacketSize for named pipe
                case TransportType.NamedPipe: transportData.BufferCapacity = config.StatsdMaxUDPPacketSize; break;
                default: throw new NotSupportedException();
            }

            return transportData;
        }
        private string[] GetGlobalTags(StatsdConfig config)
        {
            var globalTags = new List <string>();

            if (config.ConstantTags != null)
            {
                globalTags.AddRange(config.ConstantTags);
            }

            AddTag(globalTags, _entityIdInternalTagKey, StatsdConfig.EntityIdEnvVar);
            AddTag(globalTags, StatsdConfig.ServiceTagKey, StatsdConfig.ServiceEnvVar, config.ServiceName);
            AddTag(globalTags, StatsdConfig.EnvironmentTagKey, StatsdConfig.EnvironmentEnvVar, config.Environment);
            AddTag(globalTags, StatsdConfig.VersionTagKey, StatsdConfig.VersionEnvVar, config.ServiceVersion);

            return(globalTags.ToArray());
        }
Beispiel #14
0
        private string[] GetGlobalTags(StatsdConfig config)
        {
            var globalTags = new List<string>();

            if (config.ConstantTags != null)
            {
                globalTags.AddRange(config.ConstantTags);
            }

            string entityId = Environment.GetEnvironmentVariable(StatsdConfig.EntityIdEnvVar);
            if (!string.IsNullOrEmpty(entityId))
            {
                globalTags.Add($"{_entityIdInternalTagKey}:{entityId}");
            }

            return globalTags.ToArray();
        }
        private Telemetry CreateTelemetry(
            StatsdConfig config,
            string[] globalTags,
            IStatsSender statsSender)
        {
            var telemetryFlush = config.Advanced.TelemetryFlushInterval;

            if (telemetryFlush.HasValue)
            {
                var assembly = typeof(StatsdBuilder).GetTypeInfo().Assembly;
                var version  = assembly.GetName().Version.ToString();

                return(_factory.CreateTelemetry(version, telemetryFlush.Value, statsSender, globalTags));
            }

            // Telemetry is not enabled
            return(new Telemetry());
        }
Beispiel #16
0
        public static void Main(string[] args)
        {
            StatsdClient.StatsdConfig config = new StatsdClient.StatsdConfig()
            {
                Prefix           = "Playground",
                StatsdPort       = 8125,
                StatsdServerName = "127.0.0.1"
            };
            StatsdClient.DogStatsd.Configure(config);

            //StackifyLib.Logger.GlobalApiKey = "3Gz8Sh1Do9Yl3Uo0Mx6Zq4Wa2Oo9Qa3Wl6Ud0Ly";
            for (int i = 0; i < 1000; i++)
            {
                DogStatsd.Counter <int>("Run Count", 1);
                using (DogStatsd.StartTimer("Run Time"))
                {
                    System.Threading.Thread.Sleep(GetRandomTime());
                }
                Console.WriteLine(string.Format("Metric Count {0}", i));
            }
        }
        private StatsSenderData CreateStatsSender(StatsdConfig config, string statsdServerName)
        {
            var statsSenderData = new StatsSenderData();

            if (statsdServerName.StartsWith(UnixDomainSocketPrefix))
            {
                statsdServerName = statsdServerName.Substring(UnixDomainSocketPrefix.Length);
                var endPoint = new UnixEndPoint(statsdServerName);
                statsSenderData.Sender = _factory.CreateUnixDomainSocketStatsSender(
                    endPoint,
                    config.Advanced.UDSBufferFullBlockDuration);
                statsSenderData.BufferCapacity = config.StatsdMaxUnixDomainSocketPacketSize;
            }
            else
            {
                statsSenderData.Sender         = CreateUDPStatsSender(config, statsdServerName);
                statsSenderData.BufferCapacity = config.StatsdMaxUDPPacketSize;
            }

            return(statsSenderData);
        }
Beispiel #18
0
        public void Configure(StatsdConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (string.IsNullOrEmpty(config.StatsdServerName))
            {
                throw new ArgumentNullException("config.StatsdServername");
            }

            _prefix = config.Prefix;
            Dispose();
            if (!string.IsNullOrEmpty(config.StatsdServerName))
            {
                var statsdUdp = new StatsdUDP(config.StatsdServerName, config.StatsdPort, config.StatsdMaxUDPPacketSize);
                _statsD     = new Statsd(statsdUdp);
                _disposable = statsdUdp;
            }
        }
Beispiel #19
0
        /// <summary>
        /// Configures the instance.
        /// Must be called before any other methods.
        /// </summary>
        /// <param name="config">The value of the config.</param>
        public void Configure(StatsdConfig config)
        {
            if (_statsdBuilder == null)
            {
                throw new ObjectDisposedException(nameof(DogStatsdService));
            }

            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_config != null)
            {
                throw new InvalidOperationException("Configuration for DogStatsdService already performed");
            }

            _config        = config;
            _statsdData    = _statsdBuilder.BuildStatsData(config);
            _metricsSender = _statsdData.MetricsSender;
        }
        private static int GetPort(StatsdConfig config)
        {
            if (config.StatsdPort != 0)
            {
                return(config.StatsdPort);
            }

            var portString = Environment.GetEnvironmentVariable(StatsdConfig.DD_DOGSTATSD_PORT_ENV_VAR);

            if (!string.IsNullOrEmpty(portString))
            {
                if (int.TryParse(portString, out var port))
                {
                    return(port);
                }

                throw new ArgumentException($"Environment Variable '{StatsdConfig.DD_DOGSTATSD_PORT_ENV_VAR}' bad format: {portString}");
            }

            return(StatsdConfig.DefaultStatsdPort);
        }
Beispiel #21
0
 public static void Configure(StatsdConfig config) => _dogStatsdService.Configure(config);
 public void throw_exception_when_no_hostname_provided()
 {
     var metricsConfig = new StatsdConfig { };
     Assert.Throws<ArgumentNullException>(() => StatsdClient.DogStatsd.Configure(metricsConfig));
 }
Beispiel #23
0
 public DogStatsd(StatsdConfig config) : this()
 {
     _dogStatsdService.Configure(config);
 }
 public void SetUpUdpListener()
 {
     udpListener = new UdpListener(serverName, serverPort);
     var metricsConfig = new StatsdConfig { StatsdServerName = serverName, StatsdPort = serverPort};
     StatsdClient.DogStatsd.Configure(metricsConfig);
 }
 public void SetUpUdpListenerAndStatsd()
 {
     udpListener = new UdpListener(serverName, serverPort);
     var metricsConfig = new StatsdConfig { StatsdServerName = serverName };
     StatsdClient.DogStatsd.Configure(metricsConfig);
     udp = new StatsdUDP(serverName, serverPort);
     statsd = new Statsd(udp);
 }
Beispiel #26
0
 /// <summary>
 /// Disposes an instance of DogStatsdService.
 /// Flushes all metrics.
 /// </summary>
 public void Dispose()
 {
     _statsdData?.Dispose();
     _statsdData = null;
     _config     = null;
 }