Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncMessagePump"/> class.
        /// </summary>
        /// <param name="queueName">Name of the queue.</param>
        /// <param name="storageAccount">The cloud storage account.</param>
        /// <param name="concurrentTasks">The number of concurrent tasks.</param>
        /// <param name="visibilityTimeout">The visibility timeout.</param>
        /// <param name="maxDequeueCount">The maximum dequeue count.</param>
        /// <param name="metrics">The system where metrics are published</param>
        public AsyncMessagePump(string queueName, CloudStorageAccount storageAccount, int concurrentTasks = 25, TimeSpan?visibilityTimeout = null, int maxDequeueCount = 3, IMetrics metrics = null)
        {
            if (concurrentTasks < 1)
            {
                throw new ArgumentException("Number of concurrent tasks must be greather than zero", nameof(concurrentTasks));
            }
            if (maxDequeueCount < 1)
            {
                throw new ArgumentException("Number of retries must be greather than zero", nameof(maxDequeueCount));
            }

            _queueManager      = new QueueManager(queueName, storageAccount);
            _concurrentTasks   = concurrentTasks;
            _visibilityTimeout = visibilityTimeout;
            _maxDequeueCount   = maxDequeueCount;

            if (metrics == null)
            {
                var noop = new MetricsBuilder();
                noop.Configuration.Configure(new MetricsOptions()
                {
                    Enabled          = false,
                    ReportingEnabled = false
                });
                _metrics = noop.Build();
            }
            else
            {
                _metrics = metrics;
            }

            OnQueueEmpty = cancellationToken => Task.Delay(1500, cancellationToken).Wait();
            OnError      = (message, exception, isPoison) => _logger.ErrorException("An error occured when processing a message", exception);
        }
Esempio n. 2
0
        public static IMetricsRoot Create(MetricsOptions opts, AppInfo appInfo)
        {
            var builder = new MetricsBuilder()
                          .Configuration
                          .Configure(options =>
            {
                options.Enabled          = opts.Enabled;
                options.ReportingEnabled = opts.ReportingEnabled;
                options.GlobalTags       = new GlobalMetricTags
                {
                    { "app", appInfo.Name },
                    { "env", appInfo.Env },
                    { "host", appInfo.Host },
                };
            });

            if (opts.Enabled && opts.ReportingEnabled)
            {
                builder.Report
                .ToInfluxDb(options =>
                {
                    options.InfluxDb.BaseUri  = opts.BaseUri;
                    options.InfluxDb.Database = opts.Database;
                    options.InfluxDb.CreateDataBaseIfNotExists = opts.CreateDataBaseIfNotExists;
                    options.HttpPolicy.BackoffPeriod           = opts.BackoffInterval;
                    options.HttpPolicy.FailuresBeforeBackoff   = opts.FailuresBeforeBackoff;
                    options.HttpPolicy.Timeout = opts.Timeout;
                    options.FlushInterval      = opts.FlushInterval;
                });
            }

            return(builder.Build());
        }
        public static ISolariBuilder AddCeres(this ISolariBuilder builder)
        {
            IConfigurationSection section = builder.Configuration.GetSection(CeresConstants.AppSettingsSection);

            if (!section.Exists())
            {
                throw new CeresException("Ceres AppSettings section not found!");
            }

            var options = section.GetOptions <CeresOptions>();

            ApplicationOptions appOptions = builder.GetAppOptions();
            var metricsBuilder            = new MetricsBuilder();

            if (!options.Enabled)
            {
                return(builder);
            }

            builder.Services.Configure <CeresOptions>(builder.Configuration.GetSection(CeresConstants.AppSettingsSection));

            ConfigureMetricsBuilder(metricsBuilder, options, appOptions);
            builder.Services.AddMetrics(metricsBuilder.Build());
            builder.Services.AddMetricsEndpoints();
            ConfigureReporters.ConfigurePrometheus(options, metricsBuilder);
            ConfigureInfluxDb(builder, options, metricsBuilder);
            ConfigureEndpoints(builder, options);
            ConfigureMiddleware(builder, options.Middlewares);
            ConfigureCpuUsageMetric(builder, options.CollectCpuMetrics);
            ConfigureMemoryUsageMetric(builder, options.CollectMemoryMetrics);
            return(builder);
        }
        public void Can_use_uds_socket_reporter_with_setup_actions()
        {
            // Arrange
            var filter        = new MetricsFilter().WhereType(MetricType.Apdex);
            var flushInterval = TimeSpan.FromDays(1);
            var settings      = new SocketSettings();

            settings.Address = defaultUnixAddress;
            var builder = new MetricsBuilder().Report.OverUds(
                options =>
            {
                options.Filter                 = filter;
                options.FlushInterval          = flushInterval;
                options.MetricsOutputFormatter = new TestMetricsFormatter();
                options.SocketSettings         = settings;
            });

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Reporters.Should().Contain(reportMetrics => reportMetrics is SocketMetricsReporter);
            metrics.Reporters.First().FlushInterval.Should().Be(flushInterval);
            metrics.Reporters.First().Filter.Should().BeSameAs(filter);
            metrics.Reporters.First().Formatter.Should().BeOfType <TestMetricsFormatter>();
        }
Esempio n. 5
0
        public void Should_merge_global_tags_when_key_values_provided_that_match_an_existing_tag()
        {
            // Arrange
            var builder       = new MetricsBuilder();
            var keyValuePairs = new Dictionary <string, string>
            {
                { "MetricsOptions:GlobalTags:tag1", "replaced" },
                { "MetricsOptions:GlobalTags:tag2", "added" }
            };
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(keyValuePairs).Build();
            var options       = new MetricsOptions();

            options.GlobalTags.Add("tag1", "value1");

            // Act
            builder.Configuration.Configure(o => o.GlobalTags.Add("tag1", "value1"));
            builder.Configuration.ReadFrom(configuration);
            var metrics = builder.Build();

            // Assert
            metrics.Options.GlobalTags.Count.Should().Be(2);

            var tag1 = metrics.Options.GlobalTags.FirstOrDefault(t => t.Key == "tag1");

            tag1.Should().NotBeNull();
            tag1.Value.Should().Be("replaced");

            var tag2 = metrics.Options.GlobalTags.FirstOrDefault(t => t.Key == "tag2");

            tag2.Should().NotBeNull();
            tag2.Value.Should().Be("added");
        }
        public void Should_merge_global_tags_when_key_values_provided_that_match_an_existing_tag()
        {
            // Arrange
            var builder       = new MetricsBuilder();
            var configuration = new ConfigurationBuilder().AddJsonFile("JsonFIles/MetricsOptions.json").Build();
            var options       = new MetricsOptions();

            options.GlobalTags.Add("tag1", "value1");

            // Act
            builder.Configuration.Configure(options);
            builder.Configuration.ReadFrom(configuration);
            var metrics = builder.Build();

            // Assert
            metrics.Options.GlobalTags.Count.Should().Be(3);

            var tag1 = metrics.Options.GlobalTags.FirstOrDefault(t => t.Key == "tag1");

            tag1.Should().NotBeNull();
            tag1.Value.Should().Be("value1");

            var tag2 = metrics.Options.GlobalTags.FirstOrDefault(t => t.Key == "env");

            tag2.Should().NotBeNull();
            tag2.Value.Should().Be("stage");

            var tag3 = metrics.Options.GlobalTags.FirstOrDefault(t => t.Key == "tagkey");

            tag3.Should().NotBeNull();
            tag3.Value.Should().Be("tagValue");
        }
Esempio n. 7
0
        public static IServiceCollection AddAppMetrics(this IServiceCollection services, string sectionName = SectionName)
        {
            if (_initialized)
            {
                return(services);
            }
            _initialized = true;

            var options = GetAppMetricOptions(services, sectionName);

            services.AddSingleton(options);

            var metricsBuilder = new MetricsBuilder().Configuration.Configure(cfg =>
            {
                var tags = options.Tags;
                if (tags == null)
                {
                    return;
                }
                tags.TryGetValue("app", out var app);
                tags.TryGetValue("env", out var env);
                tags.Add("server", Environment.MachineName);

                foreach (var tag in tags)
                {
                    if (!cfg.GlobalTags.ContainsKey(tag.Key))
                    {
                        cfg.GlobalTags.Add(tag.Key, tag.Value);
                    }
                }
            });

            if (options.InfluxEnabled)
            {
                metricsBuilder.Report.ToInfluxDb(o =>
                {
                    o.InfluxDb.Database = options.Database;
                    o.InfluxDb.BaseUri  = new Uri(options.InfluxUrl);
                    o.InfluxDb.CreateDataBaseIfNotExists = true;
                    o.FlushInterval          = TimeSpan.FromSeconds(options.Interval);
                    o.MetricsOutputFormatter = new MetricsInfluxDbLineProtocolOutputFormatter();
                });
            }

            var metrics = metricsBuilder.Build();
            var metricsWebHostOptions = GetMetricsWebHostOptions(options);

            using var seriveProvider = services.BuildServiceProvider();
            var configuration = seriveProvider.GetService <IConfiguration>();

            services.AddMetricsTrackingMiddleware(configuration);
            services.AddMetricsEndpoints(configuration);
            services.AddMetricsReportingHostedService(metricsWebHostOptions.UnobservedTaskExceptionHandler);
            services.AddMetricsTrackingMiddleware(configuration);
            services.AddMetricsEndpoints(configuration);
            services.AddMetrics(metrics);

            return(services);
        }
Esempio n. 8
0
        public void Should_set_env_output_formatters_when_selected_via_type()
        {
            // Arrange
            var builder = new MetricsBuilder().OutputEnvInfo.Using <EnvInfoTextOutputFormatter>().OutputEnvInfo.Using <EnvInfoJsonOutputFormatter>();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.OutputEnvFormatters.Count.Should().Be(2);
        }
Esempio n. 9
0
        public void Default_env_output_formatter_should_be_first_formatter_selected_via_type()
        {
            // Arrange
            var builder = new MetricsBuilder().OutputEnvInfo.Using <EnvInfoTextOutputFormatter>().OutputEnvInfo.Using <EnvInfoJsonOutputFormatter>();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.DefaultOutputEnvFormatter.Should().BeOfType <EnvInfoTextOutputFormatter>();
        }
Esempio n. 10
0
 public AppMetricsProvider(MetricsBuilder builder = null)
 {
     if (builder == null)
     {
         _metrics = AppMetrics.CreateDefaultBuilder().Build();
     }
     else
     {
         _metrics = builder.Build();
     }
 }
Esempio n. 11
0
        public void Can_use_reporter_of_type()
        {
            // Arrange
            var builder = new MetricsBuilder().Report.Using <TestReporter>();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Reporters.Should().Contain(reportMetrics => reportMetrics is TestReporter);
        }
        public void Can_set_clock_to_system_clock()
        {
            // Arrange
            var builder = new MetricsBuilder().TimeWith.SystemClock();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Clock.Should().BeOfType <SystemClock>();
        }
        public void Can_set_clock_using_instantiation()
        {
            // Arrange
            var builder = new MetricsBuilder().TimeWith.Clock(new TestClock());

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Clock.Should().BeOfType <TestClock>();
        }
        public void Can_set_clock_to_custom_implementation()
        {
            // Arrange
            var builder = new MetricsBuilder().TimeWith.Clock <TestClock>();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Clock.Should().BeOfType <TestClock>();
        }
        public void Can_set_clock__to_stopwatch_clock_should_the_last_selected_clock()
        {
            // Arrange
            var builder = new MetricsBuilder().TimeWith.Clock <SystemClock>();

            // Act
            builder.TimeWith.StopwatchClock();
            var metrics = builder.Build();

            // Assert
            metrics.Clock.Should().BeOfType <StopwatchClock>();
        }
Esempio n. 16
0
        public void Can_use_reporter_of_type_and_override_flushinterval()
        {
            // Arrange
            var builder = new MetricsBuilder().Report.Using <TestReporter>(flushInterval: TimeSpan.FromDays(1));

            // Act
            var metrics  = builder.Build();
            var reporter = (metrics.Reporters as MetricsReporterCollection)?.GetType <TestReporter>();

            // Assert
            reporter?.FlushInterval.Should().Be(TimeSpan.FromDays(1));
        }
Esempio n. 17
0
        public void Can_use_reporter_of_type_and_use_specific_output_formatter()
        {
            // Arrange
            var builder = new MetricsBuilder().Report.Using <TestReporter>(formatter: new MetricsJsonOutputFormatter());

            // Act
            var metrics  = builder.Build();
            var reporter = (metrics.Reporters as MetricsReporterCollection)?.GetType <TestReporter>();

            // Assert
            reporter?.Formatter.Should().BeOfType(typeof(MetricsJsonOutputFormatter));
        }
        public void Can_use_console_reporter_overriding_flush_interval()
        {
            // Arrange
            var builder = new MetricsBuilder().Report.ToConsole(TimeSpan.FromDays(1));

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Reporters.Should().Contain(reportMetrics => reportMetrics is ConsoleMetricsReporter);
            metrics.Reporters.First().FlushInterval.Should().Be(TimeSpan.FromDays(1));
        }
Esempio n. 19
0
        public void Formatter_set_to_user_specified_when_specified()
        {
            // Arrange
            var builder = new MetricsBuilder().OutputMetrics.AsJson().Report.Using <TestReporter>();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.Reporters.Should().Contain(reportMetrics => reportMetrics is TestReporter);
            metrics.Reporters.First(r => r.GetType() == typeof(TestReporter)).Formatter.Should().BeOfType <MetricsJsonOutputFormatter>();
        }
Esempio n. 20
0
        public void Should_only_add_a_single_formatter_of_the_same_type()
        {
            // Arrange
            var builder = new MetricsBuilder().OutputMetrics.Using <MetricsTextOutputFormatter>().OutputMetrics.Using <MetricsTextOutputFormatter>();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.OutputEnvFormatters.Count.Should().Be(1);
            metrics.DefaultOutputEnvFormatter.Should().NotBeNull();
        }
        public void At_least_one_formatter_is_required()
        {
            // Arrange
            var builder = new MetricsBuilder();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.OutputMetricsFormatters.Count.Should().Be(1);
            metrics.DefaultOutputMetricsFormatter.Should().NotBeNull();
        }
Esempio n. 22
0
        public void Can_use_reporter_of_type_and_use_specific_metrics_filter()
        {
            // Arrange
            var filter  = new MetricsFilter().WhereType(MetricType.Apdex);
            var builder = new MetricsBuilder().Report.Using <TestReporter>(filter: filter);

            // Act
            var metrics  = builder.Build();
            var reporter = (metrics.Reporters as MetricsReporterCollection)?.GetType <TestReporter>();

            // Assert
            reporter?.Filter.Should().BeSameAs(filter);
        }
        public void Can_set_env_plain_text_output_formatting()
        {
            // Arrange
            var builder = new MetricsBuilder().OutputEnvInfo.AsJson();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.DefaultOutputEnvFormatter.Should().BeOfType <EnvInfoJsonOutputFormatter>();
            metrics.OutputEnvFormatters.Count.Should().Be(1);
            metrics.OutputEnvFormatters.First().Should().BeOfType <EnvInfoJsonOutputFormatter>();
        }
        public void Textfile_reporter_should_throw_when_output_file_is_not_specified(string outputFile)
        {
            // Arrange
            Action action = () =>
            {
                // Act
                var builder = new MetricsBuilder().Report.ToTextFile(outputFile);
                var unused  = builder.Build();
            };

            // Assert
            action.Should().Throw <InvalidOperationException>();
        }
        public void Should_set_metrics_output_formatters_when_selected_via_instantiation()
        {
            // Arrange
            var builder = new MetricsBuilder()
                          .OutputMetrics.Using(new MetricsTextOutputFormatter())
                          .OutputMetrics.Using(new MetricsJsonOutputFormatter());

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.OutputMetricsFormatters.Count.Should().Be(2);
        }
Esempio n. 26
0
        public void Http_reporter_should_throw_when_uri_is_null()
        {
            // Arrange
            Action action = () =>
            {
                // Act
                var builder = new MetricsBuilder().Report.OverHttp(endpoint: null);
                var unused  = builder.Build();
            };

            // Assert
            action.Should().Throw <ArgumentNullException>();
        }
Esempio n. 27
0
        public void Http_reporter_should_throw_when_uri_is_empty_or_white_space(string uri)
        {
            // Arrange
            Action action = () =>
            {
                // Act
                var builder = new MetricsBuilder().Report.OverHttp(uri);
                var unused  = builder.Build();
            };

            // Assert
            action.Should().Throw <InvalidOperationException>();
        }
        public void Can_set_metrics_plain_text_output_formatting()
        {
            // Arrange
            var builder = new MetricsBuilder().OutputMetrics.AsPlainText();

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.DefaultOutputMetricsFormatter.Should().BeOfType <MetricsTextOutputFormatter>();
            metrics.OutputMetricsFormatters.Count.Should().Be(1);
            metrics.OutputMetricsFormatters.First().Should().BeOfType <MetricsTextOutputFormatter>();
        }
        public void Default_metrics_output_formatter_should_be_first_formatter_selected_via_instantiation()
        {
            // Arrange
            var builder = new MetricsBuilder()
                          .OutputMetrics.Using(new MetricsTextOutputFormatter())
                          .OutputMetrics.Using(new MetricsJsonOutputFormatter());

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.DefaultOutputMetricsFormatter.Should().BeOfType <MetricsTextOutputFormatter>();
        }
Esempio n. 30
0
        public void Can_use_reporter_of_type_and_use_specific_metrics_formatter_and_flushinterval()
        {
            // Arrange
            var formatter = new MetricsJsonOutputFormatter();
            var builder   = new MetricsBuilder().Report.Using <TestReporter>(formatter: formatter, flushInterval: TimeSpan.FromDays(1));

            // Act
            var metrics  = builder.Build();
            var reporter = (metrics.Reporters as MetricsReporterCollection)?.GetType <TestReporter>();

            // Assert
            reporter?.Formatter.Should().BeOfType(typeof(MetricsJsonOutputFormatter));
            reporter?.FlushInterval.Should().Be(TimeSpan.FromDays(1));
        }