public void timing_exception_fails_silently() { _udp.Stub(x => x.Send(Arg<string>.Is.Anything)).Throw(new Exception()); var s = new Statsd(_udp); s.Send<Statsd.Timing>("timer", 5); Assert.Pass(); }
public ListenerService() { InitializeComponent(); // ReSharper disable once DoNotCallOverridableMethodsInConstructor EventLog.Log = "HubCollectorLog"; StatsdClient = new Statsd(Config.Hostname, Config.Port); KlondikeConnections = new List<HubConnection>(); Config.Hubs.ForEach(hub => { var connection = new HubConnection(hub); IHubProxy statusHubProxy = connection.CreateHubProxy("status"); statusHubProxy.On("updateStatus", status => { string name = Config.NameFromUrl(connection.Url); var message = String.Format("From {2}: Status: {0}, Total: {1}", status.synchronizationState, status.totalPackages, name); EventLog.WriteEntry(message); //Console.WriteLine(message); StatsdClient.LogGauge("nuget."+ name +".packageCount", (int) status.totalPackages); }); KlondikeConnections.Add(connection); }); }
public void counting_exception_fails_silently() { var s = new Statsd(_udp, _randomGenerator, _stopwatch); _udp.Stub(x => x.Send(Arg<string>.Is.Anything)).Throw(new Exception()); s.Send<Statsd.Counting>("counter", 5); Assert.Pass(); }
public void add_gauge_with_sample_rate_and_tags_double() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add<Statsd.Gauge,int>("gauge", 5, sampleRate: 0.5, tags: new[] {"tag1:true", "tag2"}); Assert.That(s.Commands.Count, Is.EqualTo(1)); Assert.That(s.Commands[0], Is.EqualTo("gauge:5|g|@0.5|#tag1:true,tag2")); }
public void add_gauge_with_sample_rate() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add<Statsd.Gauge,int>("gauge", 5, sampleRate: 0.5); Assert.That(s.Commands.Count, Is.EqualTo(1)); Assert.That(s.Commands[0], Is.EqualTo("gauge:5|g|@0.5")); }
public void add_gauge_double() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add<Statsd.Gauge,double>("gauge", 5.3); Assert.That(s.Commands.Count, Is.EqualTo(1)); Assert.That(s.Commands[0], Is.EqualTo("gauge:5.3|g")); }
public void SetUpUdpListenerAndStatsd() { udpListener = new UdpListener(serverName, serverPort); var metricsConfig = new MetricsConfig { StatsdServerName = serverName }; StatsdClient.Metrics.Configure(metricsConfig); udp = new StatsdUDP(serverName, serverPort); statsd = new Statsd(udp); }
public void add_one_counter_and_one_timer_sends_in_one_go() { var s = new Statsd(_udp, _randomGenerator, _stopwatch); s.Add<Statsd.Counting>("counter", 1, 0.1); s.Add<Statsd.Timing>("timer", 1); s.Send(); _udp.AssertWasCalled(x => x.Send("counter:1|c|@0.1\ntimer:1|ms")); }
public void add_one_counter_and_one_gauge_sends_and_removes_commands() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add("counter", 1, 0.1); s.Add<Statsd.Timing>("timer", 1); s.Send(); Assert.That(s.Commands.Count,Is.EqualTo(0)); }
public void add_one_counter_and_one_gauge_sends_in_one_go() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add("counter", 1, 0.1); s.Add<Statsd.Timing>("timer", 1); s.Send(); udp.AssertWasCalled(x => x.Send("counter:1|c|@0.1" + Environment.NewLine + "timer:1|ms" + Environment.NewLine)); }
public void add_one_counter_and_one_gauge_with_no_sample_rate_shows_in_commands() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add<Statsd.Counting>("counter", 1); s.Add<Statsd.Timing>("timer", 1); Assert.That(s.Commands.Count, Is.EqualTo(2)); Assert.That(s.Commands[0], Is.EqualTo("counter:1|c")); Assert.That(s.Commands[1], Is.EqualTo("timer:1|ms")); }
public void add_one_counter_and_one_gauge_shows_in_commands() { var s = new Statsd(_udp, _randomGenerator, _stopwatch); s.Add<Statsd.Counting>("counter", 1, 0.1); s.Add<Statsd.Timing>("timer", 1); Assert.That(s.Commands.Count, Is.EqualTo(2)); Assert.That(s.Commands[0], Is.EqualTo("counter:1|c|@0.1")); Assert.That(s.Commands[1], Is.EqualTo("timer:1|ms")); }
public void add_counter_sets_prefix_on_name() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch, "another.prefix."); s.Add<Statsd.Counting,int>("counter", 1, sampleRate: 0.1); s.Add<Statsd.Timing,int>("timer", 1); s.Send(); udp.AssertWasCalled(x => x.Send("another.prefix.counter:1|c|@0.1\nanother.prefix.timer:1|ms")); }
/// <summary> /// Construct a sink that uses datadog with the specified details. /// </summary> /// <param name="datadogConfiguration">Connection information used to construct the Datadog client.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public DatadogSink(DatadogConfiguration datadogConfiguration, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (datadogConfiguration == null) throw new ArgumentNullException("datadogConfiguration"); _datadogConfiguration = datadogConfiguration; _textFormatter = textFormatter; _statsdUdp = new StatsdUDP(datadogConfiguration.StatsdServer, datadogConfiguration.StatsdPort); _statsd = new Statsd(_statsdUdp); }
public void add_counter_sets_prefix_on_name() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch, "another.prefix."); s.Add("counter", 1, 0.1); s.Add<Statsd.Timing>("timer", 1); s.Send(); udp.AssertWasCalled(x => x.Send("another.prefix.counter:1|c|@0.1" + Environment.NewLine + "another.prefix.timer:1|ms")); }
public void send_one_counter_and_one_timer_with_no_sample_rate_shows_in_commands() { var s = new Statsd(new Statsd.Configuration() { Udp = _udp, RandomGenerator = _randomGenerator, StopwatchFactory = _stopwatch, Sender = _sender }); s.Send<Counting>("counter", 1); s.Send<Timing>("timer", 1); IList<object[]> argsPerCall = _sender.GetArgumentsForCallsMadeOn(x => x.Send(Arg<Metric>.Is.Anything)); Assert.That(argsPerCall.Count, Is.EqualTo(2)); Assert.That(((Metric)argsPerCall[0][0]).Command, Is.EqualTo("counter:1|c")); Assert.That(((Metric)argsPerCall[1][0]).Command, Is.EqualTo("timer:1|ms")); }
static void Main(string[] args) { var options = new Options(); if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options)) { var client = new Statsd(options.Host, options.Port, prefix : options.Namespace, connectionType : options.UseTCP ? ConnectionType.Tcp : ConnectionType.Udp); var tokenSource = new System.Threading.CancellationTokenSource(); var stopwatch = Stopwatch.StartNew(); var totalMetricsSent = 0; var tasks = new List<Task>(); int numThreads = options.Threads == 0 ? 1 : options.Threads; for ( int count = 0; count < numThreads; count++ ) { int myTaskNumber = count; var task = Task.Factory.StartNew( () => { var rnd = new Random(); int taskNumber = myTaskNumber; if ( taskNumber == 0 ) { Console.WriteLine( "Feeding stats to {0}:{1}, ctrl+c to exit.", options.Host, options.Port ); } while ( true ) { client.LogCount( "test.count.one." + rnd.Next( 5 ) ); client.LogCount( "test.count.bigValue", rnd.Next( 50 ) ); client.LogTiming( "test.timing." + rnd.Next( 5 ), rnd.Next( 100, 2000 ) ); client.LogGauge( "test.gauge." + rnd.Next( 5 ), rnd.Next( 100 ) ); Thread.Sleep( options.Delay ); Interlocked.Add( ref totalMetricsSent, 4 ); if ( taskNumber == 0 && stopwatch.ElapsedMilliseconds >= 5000 ) { Console.WriteLine( "Total sent: {0}", totalMetricsSent ); stopwatch.Restart(); } } }, tokenSource.Token ); tasks.Add( task ); } Console.CancelKeyPress += (sender, e) => { tokenSource.Cancel(); }; Task.WaitAll( tasks.ToArray() ); } }
public void send_oversized_udp_packets_are_split_if_possible() { var msg = new String('f', MetricsConfig.DefaultStatsdMaxUDPPacketSize - 15); listenThread.Start(3); // Listen for 3 messages var sender = new BatchSender(); var statsd = new Statsd(new Statsd.Configuration() { Udp = udp, Sender = sender }); statsd.Send<Statsd.Counting>(msg, 1); statsd.Send<Statsd.Timing>(msg, 2); sender.Flush(); // These two metrics should be split as their combined lengths exceed the maximum packet size AssertWasReceived(String.Format("{0}:1|c", msg), 0); AssertWasReceived(String.Format("{0}:2|ms", msg), 1); // No extra metric should be sent at the end AssertWasReceived(null, 2); }
public void send_oversized_udp_packets_are_split_if_possible_with_multiple_messages_in_one_packet() { var msg = new String('f', MetricsConfig.DefaultStatsdMaxUDPPacketSize / 2); listenThread.Start(3); var sender = new BatchSender(); var statsd = new Statsd(new Statsd.Configuration() { Udp = udp, Sender = sender }); statsd.Send<Statsd.Counting>("counter", 1); statsd.Send<Statsd.Counting>(msg, 2); statsd.Send<Statsd.Counting>(msg, 3); sender.Flush(); // Make sure that a split packet can contain mulitple metrics AssertWasReceived(String.Format("counter:1|c\n{0}:2|c", msg), 0); AssertWasReceived(String.Format("{0}:3|c", msg), 1); AssertWasReceived(null, 2); }
public void single_send_is_thread_safe() { var counts = new CountingUDP(); var test = new Statsd(counts); // send some commands in parallel, `command' just being a number in sequence int sends = 1024, threads = 2; // appears sufficient to surface error most of the time but may vary by machine var sent = new ManualResetEvent[threads]; for (int i = 0; i < threads; i++) { var done = sent[i] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(CreateSender(sends, threads, i, test, done)); } // allow threads to complete, cleanup WaitHandle.WaitAll(sent); foreach (IDisposable d in sent) d.Dispose(); counts.ExpectSequence(sends); }
public void Configure(dynamic configuration) { try { DisposeDependencies(); //IResolveCounterType resolver, Uri endpoint, int maxUdpPacket = 512 _connection = new StatsdUDP(configuration.Host.ToString(), (int)configuration.Port, (int)configuration.MaxUdpPacket); _sender = new Statsd(_connection); _resolver = new RegexResolver(); foreach (var map in configuration.CounterTypeMaps) { _resolver.Add(map.Regex.ToString(), Enum.Parse(typeof(StatsDTypes),map.Type.ToString())); } } catch (Exception ex) { Logger.ErrorFormat("Error configuring StatsDSink, check your configuration file! Exception:{@exception}",ex); throw; } }
public void add_one_counter_and_send_one_gauge_sends_only_sends_the_last() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add<Statsd.Counting>("counter", 1); s.Send<Statsd.Timing>("timer", 1); udp.AssertWasCalled(x => x.Send("timer:1|ms" + Environment.NewLine)); }
public void add_timer_with_lamba() { const string statName = "name"; const double sampleRate = 0.1; IStopwatch stopwatch = MockRepository.GenerateMock<IStopwatch>(); stopwatch.Stub(x => x.ElapsedMilliseconds()).Return(500); _stopwatch.Stub(x => x.Get()).Return(stopwatch); Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Add(() => testMethod(), statName); Assert.That(s.Commands.Count,Is.EqualTo(1)); Assert.That(s.Commands[0], Is.EqualTo("name:500|ms")); }
public void gauge_exception_fails_silently() { udp.Stub(x=>x.Send(Arg<string>.Is.Anything)).Throw(new Exception()); Statsd s = new Statsd(udp); s.Send<Statsd.Gauge>("gauge", 5); Assert.Pass(); }
public void set_max_udp_packet_size() { // Make sure that we can set the max UDP packet size udp = new StatsdUDP(serverName, serverPort, 10); statsd = new Statsd(udp); var msg = new String('f', 5); listenThread.Start(2); statsd.Add<Statsd.Counting>(msg, 1); statsd.Add<Statsd.Timing>(msg, 2); statsd.Send(); // Since our packet size limit is now 10, this (short) message should still be split AssertWasReceived(String.Format("{0}:1|c", msg), 0); AssertWasReceived(String.Format("{0}:2|ms", msg), 1); }
public void Increases_counter_with_value_of_X() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Send<Statsd.Counting>("counter", 5); udp.AssertWasCalled(x => x.Send("counter:5|c" + Environment.NewLine)); }
public void Adds_timing() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Send<Statsd.Timing>("timer", 5); udp.AssertWasCalled(x => x.Send("timer:5|ms" + Environment.NewLine)); }
public void Adds_gauge() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Send<Statsd.Gauge>("gauge", 5); udp.AssertWasCalled(x => x.Send("gauge:5|g" + Environment.NewLine)); }
public void set_return_value_with_send_timer_with_lambda() { const string statName = "name"; IStopwatch stopwatch = MockRepository.GenerateMock<IStopwatch>(); stopwatch.Stub(x => x.ElapsedMilliseconds()).Return(500); _stopwatch.Stub(x => x.Get()).Return(stopwatch); Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); int returnValue = 0; s.Send(() => returnValue = testMethod(), statName); udp.AssertWasCalled(x => x.Send("name:500|ms" + Environment.NewLine)); Assert.That(returnValue,Is.EqualTo(5)); }
public void Increases_counter_with_value_of_X_and_sample_rate() { Statsd s = new Statsd(udp, _randomGenerator, _stopwatch); s.Send("counter", 5,0.1); udp.AssertWasCalled(x => x.Send("counter:5|c|@0.1" + Environment.NewLine)); }