Example #1
0
 /// <summary>
 /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
 /// and is stopped and published when the delegate invocation completes.
 /// </summary>
 /// <typeparam name="T">The type of the result of the delegate to invoke.</typeparam>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 /// <param name="func">A delegate to a method whose invocation should be timed and result returned.</param>
 /// <returns>
 /// The value from invoking <paramref name="func"/>.
 /// </returns>
 public static T Time <T>(this IStatsDPublisher publisher, string bucket, Func <T> func)
 {
     using (StartTimer(publisher, bucket))
     {
         return(func());
     }
 }
Example #2
0
 /// <summary>
 /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
 /// and awaited, and is stopped and published when the asynchronous delegate invocation completes.
 /// </summary>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 /// <param name="action">A delegate to a method whose invocation should be timed.</param>
 /// <returns>
 /// A <see cref="Task"/> representing the asynchronous operation to time.
 /// </returns>
 public static async Task Time(this IStatsDPublisher publisher, string bucket, Func <Task> action)
 {
     using (StartTimer(publisher, bucket))
     {
         await action().ConfigureAwait(false);
     }
 }
Example #3
0
 /// <summary>
 /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
 /// and is stopped and published when the delegate invocation completes.
 /// </summary>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 /// <param name="action">A delegate to a method whose invocation should be timed.</param>
 public static void Time(this IStatsDPublisher publisher, string bucket, Action action)
 {
     using (StartTimer(publisher, bucket))
     {
         action();
     }
 }
Example #4
0
 /// <summary>
 /// functional style for timing an delegate with no return value, is not async
 /// </summary>
 /// <param name="publisher">the stats publisher</param>
 /// <param name="bucket">the stat name</param>
 /// <param name="action">the delegate to time</param>
 public static void Time(this IStatsDPublisher publisher, string bucket, Action <IDisposableTimer> action)
 {
     using (var timer = StartTimer(publisher, bucket))
     {
         action(timer);
     }
 }
Example #5
0
 public WorkerTask(IOptions <LoaderSettings> settings, IStatsDPublisher stats)
 {
     _stats    = stats;
     _settings = settings.Value;
     ThreadPool.GetMaxThreads(out var maxParallelization, out _);
     _semaphoreSlim = new SemaphoreSlim(_settings.Parallelization == 0 ? maxParallelization : _settings.Parallelization);
 }
Example #6
0
 /// <summary>
 /// functional style for timing an delegate with no return value, is async
 /// </summary>
 /// <param name="publisher">the stats publisher</param>
 /// <param name="bucket">the stat name</param>
 /// <param name="action">the delegate to time</param>
 public static async Task Time(this IStatsDPublisher publisher, string bucket, Func <IDisposableTimer, Task> action)
 {
     using (var timer = StartTimer(publisher, bucket))
     {
         await action(timer);
     }
 }
Example #7
0
 /// <summary>
 /// functional style for timing a function with a return value, is not async
 /// </summary>
 /// <param name="publisher">the stats publisher</param>
 /// <param name="bucket">the stat name</param>
 /// <param name="func">the function to time</param>
 public static T Time <T>(this IStatsDPublisher publisher, string bucket, Func <IDisposableTimer, T> func)
 {
     using (var timer = StartTimer(publisher, bucket))
     {
         return(func(timer));
     }
 }
Example #8
0
 /// <summary>
 /// functional style for timing a function with a return value, is async
 /// </summary>
 /// <param name="publisher">the stats publisher</param>
 /// <param name="bucket">the stat name</param>
 /// <param name="func">the function to time</param>
 public static async Task <T> Time <T>(this IStatsDPublisher publisher, string bucket, Func <IDisposableTimer, Task <T> > func)
 {
     using (var timer = StartTimer(publisher, bucket))
     {
         return(await func(timer));
     }
 }
Example #9
0
 /// <summary>
 /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
 /// and awaited, and is stopped and published when the asynchronous delegate invocation completes.
 /// </summary>
 /// <typeparam name="T">The type of the result of the delegate to invoke.</typeparam>
 /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
 /// <param name="bucket">The bucket to publish the timer for.</param>
 /// <param name="func">A delegate to a method whose invocation should be timed and result returned.</param>
 /// <returns>
 /// A <see cref="Task"/> representing the asynchronous operation to time.
 /// </returns>
 public static async Task <T> Time <T>(this IStatsDPublisher publisher, string bucket, Func <Task <T> > func)
 {
     using (StartTimer(publisher, bucket))
     {
         return(await func().ConfigureAwait(false));
     }
 }
        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());
        }
Example #11
0
 public WorkerService(ILogger <WorkerService> logger, IOptions <WorkerSettings> settings, IStatsDPublisher stats)
 {
     _logger   = logger;
     _stats    = stats;
     _settings = settings.Value;
     ThreadPool.GetMaxThreads(out var maxParallelization, out _);
     _semaphoreSlim = new SemaphoreSlim(_settings.Parallelization == 0 ? maxParallelization : _settings.Parallelization);
 }
Example #12
0
        public MessageLogBehavior(DiscordSocketClient discordClient, ILogger <MessageLogBehavior> logger, IServiceProvider serviceProvider, IStatsDPublisher stats)
            : base(serviceProvider)
        {
            _discordClient = discordClient;

            Log    = logger;
            _stats = stats;
        }
Example #13
0
        public StatsdMetricSink(IStatsDPublisher statsDPublisher, ILogger <StatsdMetricSink> logger)
        {
            Guard.NotNull(statsDPublisher, nameof(statsDPublisher));
            Guard.NotNull(logger, nameof(logger));

            _statsDPublisher = statsDPublisher;
            _logger          = logger;
        }
        public static void StartTimerThrowsIfPublisherIsNull()
        {
            // Arrange
            IStatsDPublisher publisher = null;
            string           bucket    = "bucket";

            // Act and Assert
            Assert.Throws <ArgumentNullException>("publisher", () => publisher.StartTimer(bucket));
        }
Example #15
0
        public StatisticsReporter(StatisticsReporterConfiguration setting)
        {
            _mandatoryTags = $"Env={setting.Environment},Tenant={setting.Tenant}";

            _statsDPublisher = new StatsDPublisher(new StatsDConfiguration
            {
                Host = string.IsNullOrEmpty(setting.StatsDHost) ? DefaultHost : setting.StatsDHost,
                Port = setting.StatsDPort ?? DefaultPort
            });
        }
 public TechController(IStatsDPublisher statsPublisher)
 {
     this.statsPublisher = statsPublisher;
     this.technologies   = new List <string>()
     {
         "Visual Studio Team Services",
         "Gitlab",
         "Jenkins"
     };
 }
Example #17
0
        public ValuesController(CLOUD_CSYEContext context, ILogger <ValuesController> log)
        {
            _log     = log;
            _context = context;
            s3Client = new AmazonS3Client(bucketRegion);

            statsDConfig = new  StatsDConfiguration {
                Host = "localhost", Port = 8125
            };
            statsDPublisher = new StatsDPublisher(statsDConfig);
        }
Example #18
0
        public DisposableTimer(IStatsDPublisher publisher, string bucket)
        {
            _publisher = publisher ?? throw new ArgumentNullException(nameof(publisher));

            if (string.IsNullOrEmpty(bucket))
            {
                throw new ArgumentNullException(nameof(bucket));
            }

            Bucket     = bucket;
            _stopwatch = Stopwatch.StartNew();
        }
Example #19
0
        public DisposableTimer(IStatsDPublisher publisher, string statName)
        {
            _publisher = publisher ?? throw new ArgumentNullException(nameof(publisher));

            if (string.IsNullOrEmpty(statName))
            {
                throw new ArgumentNullException(nameof(statName));
            }

            StatName   = statName;
            _stopwatch = Stopwatch.StartNew();
        }
Example #20
0
        /// <summary>
        /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
        /// and awaited, and is stopped and published when the asynchronous delegate invocation completes.
        /// </summary>
        /// <typeparam name="T">The type of the result of the delegate to invoke.</typeparam>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="bucket">The bucket to publish the timer for.</param>
        /// <param name="func">A delegate to a method whose invocation should be timed and result returned.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation to time.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="func"/> is <see langword="null"/>.
        /// </exception>
        public static async Task <T> Time <T>(this IStatsDPublisher publisher, string bucket, Func <IDisposableTimer, Task <T> > func)
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            using (var timer = StartTimer(publisher, bucket))
            {
                return(await func(timer).ConfigureAwait(false));
            }
        }
Example #21
0
        /// <summary>
        /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
        /// and is stopped and published when the delegate invocation completes.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="bucket">The bucket to publish the timer for.</param>
        /// <param name="action">A delegate to a method whose invocation should be timed.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
        /// </exception>
        public static void Time(this IStatsDPublisher publisher, string bucket, Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using (StartTimer(publisher, bucket))
            {
                action();
            }
        }
Example #22
0
        /// <summary>
        /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
        /// and is stopped and published when the delegate invocation completes.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="bucket">The bucket to publish the timer for.</param>
        /// <param name="action">A delegate to a method whose invocation should be timed.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
        /// </exception>
        public static void Time(this IStatsDPublisher publisher, string bucket, Action <IDisposableTimer> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using (var timer = StartTimer(publisher, bucket))
            {
                action(timer);
            }
        }
Example #23
0
        /// <summary>
        /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
        /// and awaited, and is stopped and published when the asynchronous delegate invocation completes.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="bucket">The bucket to publish the timer for.</param>
        /// <param name="action">A delegate to a method whose invocation should be timed.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation to time.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
        /// </exception>
        public static async Task Time(this IStatsDPublisher publisher, string bucket, Func <Task> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using (StartTimer(publisher, bucket))
            {
                await action().ConfigureAwait(false);
            }
        }
Example #24
0
        /// <summary>
        /// Starts a new timer for the specified bucket which is started when the specified delegate is invoked
        /// and is stopped and published when the delegate invocation completes.
        /// </summary>
        /// <typeparam name="T">The type of the result of the delegate to invoke.</typeparam>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="bucket">The bucket to publish the timer for.</param>
        /// <param name="func">A delegate to a method whose invocation should be timed and result returned.</param>
        /// <returns>
        /// The value from invoking <paramref name="func"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="func"/> is <see langword="null"/>.
        /// </exception>
        public static T Time <T>(this IStatsDPublisher publisher, string bucket, Func <IDisposableTimer, T> func)
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            using (var timer = StartTimer(publisher, bucket))
            {
                return(func(timer));
            }
        }
        /// <summary>
        /// Publishes counter(s) for the specified bucket(s) and value.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="value">The value to increment the counter(s) by.</param>
        /// <param name="sampleRate">The sample rate for the counter(s).</param>
        /// <param name="buckets">The bucket(s) to increment the counter(s) for.</param>
        public static void Increment(this IStatsDPublisher publisher, long value, double sampleRate, params string[] buckets)
        {
            if (buckets == null || buckets.Length == 0)
            {
                return;
            }

            foreach (string bucket in buckets)
            {
                publisher.Increment(value, sampleRate, bucket);
            }
        }
        /// <summary>
        /// Publishes counter(s) for the specified bucket(s) and value.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="value">The value to increment the counter(s) by.</param>
        /// <param name="sampleRate">The sample rate for the counter(s).</param>
        /// <param name="buckets">The bucket(s) to increment the counter(s) for.</param>
        public static void Increment(this IStatsDPublisher publisher, long value, double sampleRate, IEnumerable <string> buckets)
        {
            if (buckets == null)
            {
                return;
            }

            foreach (string bucket in buckets)
            {
                publisher.Increment(value, sampleRate, bucket);
            }
        }
        public Runner(IHttpClientFactory clientFactory, Settings settings, IStatsDPublisher statsDPublisher)
        {
            _clientFactory   = clientFactory;
            _settings        = settings;
            _statsDPublisher = statsDPublisher;

//
//            Metrics.Configure(new MetricsConfig
//            {
//                StatsdServerName = _settings.StatsDServer,
//                Prefix = _settings.AppName
//            });
        }
        /// <summary>
        /// Publishes counter decrement(s) for the specified bucket(s) and value.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="value">The value to decrement the counter(s) by.</param>
        /// <param name="sampleRate">The sample rate for the counter(s).</param>
        /// <param name="buckets">The bucket(s) to decrement the counter(s) for.</param>
        public static void Decrement(this IStatsDPublisher publisher, long value, double sampleRate, params string[] buckets)
        {
            if (buckets == null || buckets.Length == 0)
            {
                return;
            }

            long adjusted = value > 0 ? -value : value;

            foreach (string bucket in buckets)
            {
                publisher.Increment(adjusted, sampleRate, bucket);
            }
        }
        /// <summary>
        /// Publishes counter decrement(s) for the specified bucket(s) and value.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="value">The value to decrement the counter(s) by.</param>
        /// <param name="sampleRate">The sample rate for the counter(s).</param>
        /// <param name="buckets">The bucket(s) to decrement the counter(s) for.</param>
        public static void Decrement(this IStatsDPublisher publisher, long value, double sampleRate, IEnumerable <string> buckets)
        {
            if (buckets == null)
            {
                return;
            }

            long adjusted = value > 0 ? -value : value;

            foreach (string bucket in buckets)
            {
                publisher.Increment(adjusted, sampleRate, bucket);
            }
        }
        /// <summary>
        /// Publishes counter(s) for the specified bucket(s) and value.
        /// </summary>
        /// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
        /// <param name="value">The value to increment the counter(s) by.</param>
        /// <param name="sampleRate">The sample rate for the counter(s).</param>
        /// <param name="buckets">The bucket(s) to increment the counter(s) for.</param>
        public static void Increment(this IStatsDPublisher publisher, long value, double sampleRate, params string[] buckets)
        {
#pragma warning disable CA1508
            if (buckets == null || buckets.Length == 0)
#pragma warning restore CA1508
            {
                return;
            }

            foreach (string bucket in buckets)
            {
                publisher.Increment(value, sampleRate, bucket);
            }
        }
        public DisposableTimer(IStatsDPublisher publisher, string statName)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException("publisher");
            }

            if (string.IsNullOrEmpty(statName))
            {
                throw new ArgumentNullException("statName");
            }

            _publisher = publisher;
            StatName = statName;
            _stopwatch = Stopwatch.StartNew();
        }
        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 PublishToStatsD(IStatsDPublisher statsd)
 {
     _statsd = statsd;
 }
 public StatsDExceptionLogger(IStatsDPublisher stats)
 {
     _stats = stats;
 }