private static async Task AssertMetrics(StatsDConfiguration config, IStatsDPublisher publisher)
        {
            // Act - Create a counter
            publisher.Increment("apple");

            // Act - Create and change a counter
            publisher.Increment("bear");        // 1
            publisher.Increment(10, "bear");    // 11
            publisher.Increment(10, 0, "bear"); // 11
            publisher.Decrement("bear");        // 10
            publisher.Decrement(5, "bear");     // 5
            publisher.Decrement(5, 0, "bear");  // 5

            // Act - Mark an event (which is a counter)
            publisher.Increment("fish");

            // Act - Create a gauge
            publisher.Gauge(3.141, "circle");

            // Act - Create and change a gauge
            publisher.Gauge(10, "dog");
            publisher.Gauge(42, "dog");

            // Act - Create a timer
            publisher.Timing(123, "elephant");
            publisher.Timing(TimeSpan.FromSeconds(2), "fox");
            publisher.Timing(456, 1, "goose");
            publisher.Timing(TimeSpan.FromSeconds(3.5), 1, "hen");

            // Act - Increment multiple counters
            publisher.Increment(7, 1, "green", "red"); // 7
            publisher.Increment(2, 0, "green", "red"); // 7
            publisher.Decrement(1, 0, "red", "green"); // 7
            publisher.Decrement(4, 1, "red", "green"); // 3

            // Allow enough time for metrics to be registered
            await Task.Delay(TimeSpan.FromSeconds(1.0));

            // Assert
            var result = await SendCommandAsync("counters");

            result.Value <int>(config.Prefix + ".apple").ShouldBe(1, result.ToString());
            result.Value <int>(config.Prefix + ".bear").ShouldBe(5, result.ToString());
            result.Value <int>(config.Prefix + ".fish").ShouldBe(1, result.ToString());
            result.Value <int>(config.Prefix + ".green").ShouldBe(3, result.ToString());
            result.Value <int>(config.Prefix + ".red").ShouldBe(3, result.ToString());

            result = await SendCommandAsync("gauges");

            result.Value <double>(config.Prefix + ".circle").ShouldBe(3.141, result.ToString());
            result.Value <int>(config.Prefix + ".dog").ShouldBe(42, result.ToString());

            result = await SendCommandAsync("timers");

            result[config.Prefix + ".elephant"].Values <int>().ShouldBe(new[] { 123 }, result.ToString());
            result[config.Prefix + ".fox"].Values <int>().ShouldBe(new[] { 2000 }, result.ToString());
            result[config.Prefix + ".goose"].Values <int>().ShouldBe(new[] { 456 }, result.ToString());
            result[config.Prefix + ".hen"].Values <int>().ShouldBe(new[] { 3500 }, result.ToString());
        }
示例#2
0
        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;
                _stopwatch.Stop();

                if (string.IsNullOrEmpty(Bucket))
                {
                    throw new InvalidOperationException($"The {nameof(Bucket)} property must have a value.");
                }

                _publisher.Timing(_stopwatch.Elapsed, Bucket);
            }
        }
示例#3
0
        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;
                _stopwatch.Stop();

                if (string.IsNullOrEmpty(StatName))
                {
                    throw new InvalidOperationException("StatName must be set");
                }

                _publisher.Timing(_stopwatch.Elapsed, StatName);

                _stopwatch = null;
                _publisher = null;
            }
        }
 public Task Timing(long duration, string bucket)
 {
     _stats.Timing(duration, bucket);
     return(Task.CompletedTask);
 }
 /// <summary>
 /// Publishes a timer for the specified bucket and value.
 /// </summary>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="duration">The value to publish for the timer.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 public static void Timing(this IStatsDPublisher publisher, long duration, string bucket)
 {
     publisher.Timing(duration, DefaultSampleRate, bucket);
 }
 /// <summary>
 /// Publishes a timer for the specified bucket and value.
 /// </summary>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="duration">The value to publish for the timer.</param>
 /// <param name="sampleRate">The sample rate for the timer.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 public static void Timing(this IStatsDPublisher publisher, TimeSpan duration, double sampleRate, string bucket)
 {
     publisher.Timing((long)duration.TotalMilliseconds, sampleRate, bucket);
 }
 public void RunIp()
 {
     _ipSender.Increment("increment.ip");
     _ipSender.Timing(Timed, "timer.ip");
 }
 public void RunUdp()
 {
     _udpSender.Increment("increment.ud");
     _udpSender.Timing(Timed, "timer.ud");
 }
示例#9
0
 /// <inheritdoc />
 public void Timing(long duration, double sampleRate, string bucket, IDictionary <string, string>?tags = null)
 {
     _inner.Timing(duration, sampleRate, bucket, tags);
 }
示例#10
0
        private static async Task AssertMetrics(StatsDConfiguration config, IStatsDPublisher publisher)
        {
            // Act - Create a counter
            publisher.Increment("apple");

            // Act - Create and change a counter
            publisher.Increment("bear");              // 1
            publisher.Increment(10, "bear");          // 11
            publisher.Increment(10, 0, "bear", null); // 11
            publisher.Decrement("bear");              // 10
            publisher.Decrement(5, "bear");           // 5
            publisher.Decrement(5, 0, "bear");        // 5

            // Act - Create and change a counter with tags
            Dictionary<string, string> tags = new Dictionary<string, string>();
            tags.Add("key", "value");
            tags.Add("key2", "value2");
            publisher.Increment("ant", tags);              // 1
            publisher.Increment(15, "ant", tags);          // 16
            publisher.Decrement(5,"ant", tags);            // 11

            // Act - Create multiple counters with tags and change them
            tags = new Dictionary<string, string>();
            tags.Add("key", "value");
            tags.Add("key2", "value2");
            publisher.Increment(100, 1, tags, new string[] { "gmc", "ford" });        // 100
            publisher.Decrement(5, 1, tags, new string[] { "gmc", "ford" });          // 95

            // Act - Create multiple counters without tags and change them
            publisher.Increment(150, 1, null, new string[] { "jaguar", "kia" });      // 100
            publisher.Decrement(20, 1, null, new string[] { "jaguar", "kia" });       // 130

            // Act - Create a counter with tags
            tags = new Dictionary<string, string>();
            tags.Add("key", "value");
            tags.Add("key2", "value2");
            publisher.Increment("orange", tags);      // 1
            publisher.Increment(50, "orange", tags);  // 51

            // Act - Create multiple counters with tags
            tags = new Dictionary<string, string>();
            tags.Add("type", "vehicle");
            publisher.Increment(60, 1, tags, new string[] { "mazda", "fiat" });

            // Act - Create multiple counters without tags
            publisher.Increment(25, 1, null, new string[] { "ferrari", "jeep" });

            // Act - Mark an event (which is a counter)
            publisher.Increment("fish");

            // Act - Create a gauge
            publisher.Gauge(3.141, "circle", Operation.Set, null);

            // Act - Create and change a gauge
            publisher.Gauge(10, "dog", Operation.Set, null);
            publisher.Gauge(42, "dog", Operation.Set, null);

            // Act - Create a gauge with tags
            tags = new Dictionary<string, string>();
            tags.Add("foo", "bar");
            tags.Add("lorem", "ipsum");
            publisher.Gauge(5.5, "square", Operation.Set, tags);

            // Act - Create a gauge and decrement it
            publisher.Gauge(2020, "year", Operation.Set, null);
            publisher.Gauge(10, "year", Operation.Decrement, null);

            // Act - Create a gauge and increment it
            publisher.Gauge(15, "score", Operation.Set, null);
            publisher.Gauge(2, "score", Operation.Increment, null);

            // Act - Create a timer
            publisher.Timing(123, "elephant");
            publisher.Timing(TimeSpan.FromSeconds(2), "fox");
            publisher.Timing(456, 1, "goose", null);
            publisher.Timing(TimeSpan.FromSeconds(3.5), 1, "hen");

            // Act - Create a timer with tags
            tags = new Dictionary<string, string>();
            tags.Add("class", "mammal");
            tags.Add("genus", "panthera");
            publisher.Timing(128, "leopard", tags);

            // Act - Increment multiple counters
            publisher.Increment(7, 1, new string[] { "green", "red" });       // 7
            publisher.Increment(2, 0, new List<string>() { "green", "red" }); // 7
            publisher.Decrement(1, 0, new string[] { "red", "green" });       // 7
            publisher.Decrement(4, 1, new List<string>() { "red", "green" }); // 3

            // Allow enough time for metrics to be registered
            await Task.Delay(TimeSpan.FromSeconds(1.0));

            // Assert
            var result = await SendCommandAsync("counters");
            result.Value<int>(config.Prefix + ".apple").ShouldBe(1, result.ToString());
            result.Value<int>(config.Prefix + ".bear").ShouldBe(5, result.ToString());
            result.Value<int>(config.Prefix + ".ant;key=value;key2=value2").ShouldBe(11, result.ToString());
            result.Value<int>(config.Prefix + ".gmc;key=value;key2=value2").ShouldBe(95, result.ToString());
            result.Value<int>(config.Prefix + ".ford;key=value;key2=value2").ShouldBe(95, result.ToString());
            result.Value<int>(config.Prefix + ".jaguar").ShouldBe(130, result.ToString());
            result.Value<int>(config.Prefix + ".kia").ShouldBe(130, result.ToString());
            result.Value<int>(config.Prefix + ".orange;key=value;key2=value2").ShouldBe(51, result.ToString());
            result.Value<int>(config.Prefix + ".mazda;type=vehicle").ShouldBe(60, result.ToString());
            result.Value<int>(config.Prefix + ".fiat;type=vehicle").ShouldBe(60, result.ToString());
            result.Value<int>(config.Prefix + ".ferrari").ShouldBe(25, result.ToString());
            result.Value<int>(config.Prefix + ".jeep").ShouldBe(25, result.ToString());
            result.Value<int>(config.Prefix + ".fish").ShouldBe(1, result.ToString());
            result.Value<int>(config.Prefix + ".green").ShouldBe(3, result.ToString());
            result.Value<int>(config.Prefix + ".red").ShouldBe(3, result.ToString());

            result = await SendCommandAsync("gauges");
            result.Value<double>(config.Prefix + ".circle").ShouldBe(3.141, result.ToString());
            result.Value<int>(config.Prefix + ".dog").ShouldBe(42, result.ToString());
            result.Value<double>(config.Prefix + ".square;foo=bar;lorem=ipsum").ShouldBe(5.5, result.ToString());
            result.Value<int>(config.Prefix + ".year").ShouldBe(2010, result.ToString());
            result.Value<int>(config.Prefix + ".score").ShouldBe(17, result.ToString());

            result = await SendCommandAsync("timers");
            result[config.Prefix + ".elephant"].Values<int>().ShouldBe(new[] { 123 }, result.ToString());
            result[config.Prefix + ".leopard;class=mammal;genus=panthera"].Values<int>().ShouldBe(new[] { 128 }, result.ToString());
            result[config.Prefix + ".fox"].Values<int>().ShouldBe(new[] { 2000 }, result.ToString());
            result[config.Prefix + ".goose"].Values<int>().ShouldBe(new[] { 456 }, result.ToString());
            result[config.Prefix + ".hen"].Values<int>().ShouldBe(new[] { 3500 }, result.ToString());
        }
示例#11
0
 /// <inheritdoc />
 public void Timing(long duration, double sampleRate, string bucket)
 {
     _inner.Timing(duration, sampleRate, bucket);
 }
示例#12
0
 /// <inheritdoc />
 public void Timer(string @event, string tags, TimeSpan duration)
 {
     _statsDPublisher.Timing(duration, $"{@event},{tags},{_mandatoryTags}");
 }
 /// <summary>
 /// Publishes a timer for the specified bucket and value.
 /// </summary>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="duration">The value to publish for the timer.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 /// <param name="tags">An optional dictionary of tags.</param>
 public static void Timing(this IStatsDPublisher publisher, long duration, string bucket, IDictionary <string, string>?tags = null)
 {
     publisher.Timing(duration, DefaultSampleRate, bucket, tags);
 }
 /// <summary>
 /// Publishes a timer for the specified bucket and value.
 /// </summary>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="duration">The value to publish for the timer.</param>
 /// <param name="sampleRate">The sample rate for the timer.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 /// <param name="tags">An optional dictionary of tags.</param>
 public static void Timing(this IStatsDPublisher publisher, TimeSpan duration, double sampleRate, string bucket, IDictionary <string, string>?tags = null)
 {
     publisher.Timing((long)duration.TotalMilliseconds, sampleRate, bucket, tags);
 }