private static ReportFactory SetupReportFactory()
        {
            var metricsMock = new Mock <IMetrics>();
            var options     = new AppMetricsOptions();

            return(new ReportFactory(options, metricsMock.Object, new LoggerFactory()));
        }
Exemplo n.º 2
0
        public MetricsReportingFixture()
        {
            var healthFactoryLogger = _loggerFactory.CreateLogger <HealthCheckFactory>();
            var options             = new AppMetricsOptions();
            var clock = new TestClock();

            IMetricContextRegistry NewContextRegistry(string name) => new DefaultMetricContextRegistry(name);

            var registry             = new DefaultMetricsRegistry(_loggerFactory, options, clock, new EnvironmentInfoProvider(), NewContextRegistry);
            var healthCheckFactory   = new HealthCheckFactory(healthFactoryLogger);
            var metricBuilderFactory = new DefaultMetricsBuilderFactory();
            var filter        = new DefaultMetricsFilter();
            var healthManager = new DefaultHealthProvider(new Lazy <IMetrics>(() => Metrics), _loggerFactory.CreateLogger <DefaultHealthProvider>(), healthCheckFactory);
            var dataManager   = new DefaultMetricValuesProvider(
                filter,
                registry);

            var metricsManagerFactory         = new DefaultMeasureMetricsProvider(registry, metricBuilderFactory, clock);
            var metricsManagerAdvancedFactory = new DefaultMetricsProvider(registry, metricBuilderFactory, clock);
            var metricsManager = new DefaultMetricsManager(registry, _loggerFactory.CreateLogger <DefaultMetricsManager>());

            Metrics = new DefaultMetrics(
                clock,
                filter,
                metricsManagerFactory,
                metricBuilderFactory,
                metricsManagerAdvancedFactory,
                dataManager,
                metricsManager,
                healthManager);

            RecordSomeMetrics();
        }
Exemplo n.º 3
0
        public MetricsFixture()
        {
            var healthFactoryLogger = _loggerFactory.CreateLogger <HealthCheckFactory>();
            var clock   = new TestClock();
            var options = new AppMetricsOptions();
            Func <string, IMetricContextRegistry> newContextRegistry = name => new DefaultMetricContextRegistry(name);
            var registry             = new DefaultMetricsRegistry(_loggerFactory, options, clock, new EnvironmentInfoProvider(), newContextRegistry);
            var healthCheckFactory   = new HealthCheckFactory(healthFactoryLogger);
            var metricBuilderFactory = new DefaultMetricsBuilderFactory();
            var filter                        = new DefaultMetricsFilter();
            var dataManager                   = new DefaultMetricValuesProvider(filter, registry);
            var healthStatusProvider          = new DefaultHealthProvider(_loggerFactory.CreateLogger <DefaultHealthProvider>(), healthCheckFactory);
            var metricsManagerFactory         = new DefaultMeasureMetricsProvider(registry, metricBuilderFactory, clock);
            var metricsManagerAdvancedFactory = new DefaultMetricsProvider(registry, metricBuilderFactory, clock);
            var metricsManager                = new DefaultMetricsManager(registry, _loggerFactory.CreateLogger <DefaultMetricsManager>());

            Metrics = new DefaultMetrics(
                clock,
                filter,
                metricsManagerFactory,
                metricBuilderFactory,
                metricsManagerAdvancedFactory,
                dataManager,
                metricsManager,
                healthStatusProvider);
        }
        public HealthCheckRegistryTests()
        {
            _metircsSetup = healthCheckFactory =>
            {
                var clock   = new TestClock();
                var options = new AppMetricsOptions();
                Func <string, IMetricContextRegistry> newContextRegistry = name => new DefaultMetricContextRegistry(name);
                var registry = new DefaultMetricsRegistry(
                    LoggerFactory,
                    options,
                    clock,
                    new EnvironmentInfoProvider(),
                    newContextRegistry);
                var metricBuilderFactory = new DefaultMetricsBuilderFactory();
                var filter        = new DefaultMetricsFilter();
                var healthManager = new DefaultHealthProvider(LoggerFactory.CreateLogger <DefaultHealthProvider>(), healthCheckFactory);
                var dataManager   = new DefaultMetricValuesProvider(
                    filter,
                    registry);

                var metricsManagerFactory         = new DefaultMeasureMetricsProvider(registry, metricBuilderFactory, clock);
                var metricsManagerAdvancedFactory = new DefaultMetricsProvider(registry, metricBuilderFactory, clock);
                var metricsManager = new DefaultMetricsManager(registry, LoggerFactory.CreateLogger <DefaultMetricsManager>());

                return(new DefaultMetrics(
                           clock,
                           filter,
                           metricsManagerFactory,
                           metricBuilderFactory,
                           metricsManagerAdvancedFactory,
                           dataManager,
                           metricsManager,
                           healthManager));
            };
        }
Exemplo n.º 5
0
        protected void SetupServices(
            IServiceCollection services,
            AppMetricsOptions appMetricsOptions,
            AspNetMetricsOptions aspNetMetricsOptions,
            IFilterMetrics filter = null,
            IEnumerable <HealthCheckResult> healthChecks = null)
        {
            services
            .AddLogging()
            .AddRouting(options => { options.LowercaseUrls = true; });

            services.AddMvc(options => options.AddMetricsResourceFilter());

            var builder = services
                          .AddMetrics(
                options =>
            {
                options.DefaultContextLabel = appMetricsOptions.DefaultContextLabel;
                options.MetricsEnabled      = appMetricsOptions.MetricsEnabled;
            })
                          .AddJsonSerialization()
                          .AddDefaultReservoir(() => new DefaultAlgorithmRReservoir(1028))
                          .AddClockType <TestClock>()
                          .AddHealthChecks(
                factory =>
            {
                var checks = healthChecks != null
                            ? healthChecks.ToList()
                            : new List <HealthCheckResult>();

                for (var i = 0; i < checks.Count; i++)
                {
                    var check = checks[i];
                    factory.Register("Check" + i, () => Task.FromResult(check));
                }
            })
                          .AddMetricsMiddleware(
                options =>
            {
                options.MetricsTextEndpointEnabled = aspNetMetricsOptions.MetricsTextEndpointEnabled;
                options.HealthEndpointEnabled      = aspNetMetricsOptions.HealthEndpointEnabled;
                options.MetricsEndpointEnabled     = aspNetMetricsOptions.MetricsEndpointEnabled;
                options.PingEndpointEnabled        = aspNetMetricsOptions.PingEndpointEnabled;
                options.OAuth2TrackingEnabled      = aspNetMetricsOptions.OAuth2TrackingEnabled;

                options.HealthEndpoint      = aspNetMetricsOptions.HealthEndpoint;
                options.MetricsEndpoint     = aspNetMetricsOptions.MetricsEndpoint;
                options.MetricsTextEndpoint = aspNetMetricsOptions.MetricsTextEndpoint;
                options.PingEndpoint        = aspNetMetricsOptions.PingEndpoint;

                options.IgnoredRoutesRegexPatterns = aspNetMetricsOptions.IgnoredRoutesRegexPatterns;
                options.IgnoredHttpStatusCodes     = aspNetMetricsOptions.IgnoredHttpStatusCodes;
            });

            if (filter != null)
            {
                builder.AddGlobalFilter(filter);
            }
        }
Exemplo n.º 6
0
 public MetricsEndpointTextEndpointMiddleware(
     AppMetricsOptions options,
     OwinMetricsOptions owinOptions,
     ILoggerFactory loggerFactory,
     IMetrics metrics)
     : base(owinOptions, loggerFactory, metrics)
 {
     _reportGenerator = new DefaultReportGenerator(options, loggerFactory);
 }
Exemplo n.º 7
0
 public MetricsEndpointTextEndpointMiddleware(
     RequestDelegate next,
     AppMetricsOptions appMetricsOptions,
     AspNetMetricsOptions aspNetOptions,
     ILoggerFactory loggerFactory,
     IMetrics metrics)
     : base(next, aspNetOptions, loggerFactory, metrics)
 {
     _reportGenerator = new DefaultReportGenerator(appMetricsOptions, loggerFactory);
 }
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions();

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = false
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions();

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsEndpoint = new PathString("/metrics-json")
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
Exemplo n.º 10
0
        public void logger_factory_is_required()
        {
            var metrics = new Mock <IMetrics>();
            var options = new AppMetricsOptions();

            Action action = () =>
            {
                var reportFactory = new ReportFactory(options, metrics.Object, null);
            };

            action.ShouldThrow <ArgumentNullException>();
        }
Exemplo n.º 11
0
        public Reporter(
            AppMetricsOptions options,
            ReportFactory reportFactory,
            IMetrics metrics,
            IScheduler scheduler,
            ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _metrics   = metrics ?? throw new ArgumentNullException(nameof(metrics));
            _scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
            _providers = reportFactory?.GetProviders() ?? throw new ArgumentNullException(nameof(reportFactory));

            _reportGenerator = new DefaultReportGenerator(options, loggerFactory);
            _loggerFactory   = loggerFactory;
            _logger          = loggerFactory.CreateLogger <Reporter>();

            if (_providers.Count <= 0)
            {
                return;
            }

            _metricReporters = new Dictionary <Type, IMetricReporter>(_providers.Count);

            foreach (var provider in _providers)
            {
                _metricReporters.Add(provider.Key, provider.Value.CreateMetricReporter(provider.Key.Name, _loggerFactory));
            }

            _successCounter = new CounterOptions
            {
                Context          = Constants.InternalMetricsContext,
                MeasurementUnit  = Unit.Items,
                ResetOnReporting = true,
                Name             = "report_success"
            };

            _failedCounter = new CounterOptions
            {
                Context          = Constants.InternalMetricsContext,
                MeasurementUnit  = Unit.Items,
                ResetOnReporting = true,
                Name             = "report_failed"
            };
        }
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultSamplingType = SamplingType.LongTerm,
                MetricsEnabled      = true,
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions();

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions,
                          new DefaultMetricsFilter().WhereType(MetricType.Counter));
        }
Exemplo n.º 13
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultSamplingType = SamplingType.LongTerm
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = false
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
Exemplo n.º 14
0
 public DefaultMetricsRegistry(
     ILoggerFactory loggerFactory,
     AppMetricsOptions options,
     IClock clock,
     EnvironmentInfoProvider environmentInfoProvider,
     Func <string, IMetricContextRegistry> newContextRegistry)
 {
     _logger = loggerFactory.CreateLogger <DefaultMetricContextRegistry>();
     _environmentInfoProvider = environmentInfoProvider;
     _clock = clock;
     _newContextRegistry  = newContextRegistry;
     _defaultContextLabel = options.DefaultContextLabel;
     _contexts.TryAdd(_defaultContextLabel, newContextRegistry(_defaultContextLabel));
 }
Exemplo n.º 15
0
        public void can_create_reporter_with_default_scheduler()
        {
            var metrics       = new Mock <IMetrics>();
            var options       = new AppMetricsOptions();
            var loggerFactory = new LoggerFactory();
            var reportFactory = new ReportFactory(options, metrics.Object, loggerFactory);

            Action action = () =>
            {
                var reporter = reportFactory.CreateReporter();
            };

            action.ShouldNotThrow <Exception>();
        }
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultSamplingType = SamplingType.LongTerm
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsEndpoint = new PathString("/metrics-json")
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
 public HealthCheckRegistryTests()
 {
     _metircsSetup = healthCheckFactory =>
     {
         var metricsLogger = LoggerFactory.CreateLogger <DefaultAdvancedMetrics>();
         var clock         = new TestClock();
         var options       = new AppMetricsOptions();
         Func <string, IMetricContextRegistry> newContextRegistry = name => new DefaultMetricContextRegistry(name);
         var registry = new DefaultMetricsRegistry(LoggerFactory, options, clock, new EnvironmentInfoProvider(LoggerFactory),
                                                   newContextRegistry);
         var advancedContext = new DefaultAdvancedMetrics(metricsLogger, options, clock, new DefaultMetricsFilter(), registry,
                                                          healthCheckFactory);
         return(new DefaultMetrics(options, registry, advancedContext));
     };
 }
Exemplo n.º 18
0
        public MetricsFixture()
        {
            var metricsLogger       = _loggerFactory.CreateLogger <DefaultAdvancedMetrics>();
            var healthFactoryLogger = _loggerFactory.CreateLogger <HealthCheckFactory>();
            var clock   = new TestClock();
            var options = new AppMetricsOptions {
                DefaultSamplingType = SamplingType.LongTerm
            };
            Func <string, IMetricContextRegistry> newContextRegistry = name => new DefaultMetricContextRegistry(name);
            var registry           = new DefaultMetricsRegistry(_loggerFactory, options, clock, new EnvironmentInfoProvider(), newContextRegistry);
            var healthCheckFactory = new HealthCheckFactory(healthFactoryLogger);
            var advancedContext    = new DefaultAdvancedMetrics(metricsLogger, options, clock, new DefaultMetricsFilter(), registry, healthCheckFactory);

            Metrics = new DefaultMetrics(options, registry, advancedContext);
        }
Exemplo n.º 19
0
        public DefaultReportGenerator(AppMetricsOptions options, ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _options = options;
            _logger  = loggerFactory.CreateLogger <DefaultReportGenerator>();
        }
        public MetricCoreTestFixture()
        {
            var loggerFactory = new LoggerFactory();
            var options       = new AppMetricsOptions();

            Clock   = new TestClock();
            Builder = new DefaultMetricsBuilderFactory();

            Func <string, IMetricContextRegistry> contextRegistrySetup = context => new DefaultMetricContextRegistry(context);
            var registry = new DefaultMetricsRegistry(loggerFactory, options, Clock, new EnvironmentInfoProvider(), contextRegistrySetup);

            Registry  = registry;
            Providers = new DefaultMetricsProvider(Registry, Builder, Clock);
            Snapshot  = new DefaultMetricValuesProvider(new NoOpMetricsFilter(), Registry);
            Managers  = new DefaultMeasureMetricsProvider(Registry, Builder, Clock);
            Context   = options.DefaultContextLabel;
        }
Exemplo n.º 21
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                MetricsEnabled = true
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = true,
                HealthEndpointEnabled      = true,
                MetricsEndpointEnabled     = true,
                PingEndpointEnabled        = true,
                OAuth2TrackingEnabled      = true
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
Exemplo n.º 22
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultContextLabel = "testing",
                MetricsEnabled      = true
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = true,
                HealthEndpointEnabled      = true,
                MetricsEndpointEnabled     = true,
                PingEndpointEnabled        = true
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
Exemplo n.º 23
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultContextLabel = "testing",
                MetricsEnabled      = true
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = true,
                HealthEndpointEnabled      = true,
                MetricsEndpointEnabled     = true,
                PingEndpointEnabled        = true
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions,
                          healthChecks: new[] { HealthCheckResult.Healthy(), HealthCheckResult.Degraded(), HealthCheckResult.Unhealthy() });
        }
        public DefaultAdvancedMetrics(
            ILogger <DefaultAdvancedMetrics> logger,
            AppMetricsOptions options,
            IClock clock,
            IMetricsFilter globalFilter,
            IMetricsRegistry registry,
            IHealthCheckFactory healthCheckFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            GlobalFilter = globalFilter ?? new DefaultMetricsFilter();
            Clock        = clock;

            _logger             = logger;
            _registry           = registry;
            _healthCheckFactory = healthCheckFactory;
        }
Exemplo n.º 25
0
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultContextLabel = "testing",
                MetricsEnabled      = true
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = true,
                HealthEndpointEnabled      = true,
                MetricsEndpointEnabled     = true,
                PingEndpointEnabled        = true
            };

            aspNetMetricsOptions.IgnoredRoutesRegexPatterns.Add("(?i)^api/test/ignore");

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
        public void ConfigureServices(IServiceCollection services)
        {
            var appMetricsOptions = new AppMetricsOptions
            {
                DefaultContextLabel = "testing",
                MetricsEnabled      = true,
                DefaultSamplingType = SamplingType.LongTerm
            };

            var aspNetMetricsOptions = new AspNetMetricsOptions
            {
                MetricsTextEndpointEnabled = true,
                HealthEndpointEnabled      = true,
                MetricsEndpointEnabled     = true,
                PingEndpointEnabled        = true,
                HealthEndpoint             = new PathString("/health-status")
            };

            SetupServices(services, appMetricsOptions, aspNetMetricsOptions);
        }
Exemplo n.º 27
0
        public ReportFactory(AppMetricsOptions options, IMetrics metrics, ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (metrics == null)
            {
                throw new ArgumentNullException(nameof(metrics));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _options       = options;
            _metrics       = metrics;
            _loggerFactory = loggerFactory;
        }
Exemplo n.º 28
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="DefaultMetrics" /> class.
        /// </summary>
        /// <param name="options">The global metrics options configure on startup.</param>
        /// <param name="registry">The registry storing all metric data.</param>
        /// <param name="advanced">The implementation providing access to more advanced metric options</param>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        public DefaultMetrics(
            AppMetricsOptions options,
            IMetricsRegistry registry,
            IAdvancedMetrics advanced)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }
            if (advanced == null)
            {
                throw new ArgumentNullException(nameof(advanced));
            }

            _registry = registry;
            Advanced  = advanced;
        }
Exemplo n.º 29
0
        public void can_set_global_tags_on_metric_options()
        {
            var environmentBuilder = new EnvironmentInfoProvider();
            var environmentInfo    = environmentBuilder.Build();
            var expected           = new GlobalMetricTags(new Dictionary <string, string>
            {
                { "host", environmentInfo.HostName },
                { "machine_name", environmentInfo.MachineName },
                { "app_name", environmentInfo.EntryAssemblyName },
                { "app_version", environmentInfo.EntryAssemblyVersion }
            });
            var options = new AppMetricsOptions();

            options.WithGlobalTags((globalTags, envInfo) =>
            {
                globalTags.Add("host", envInfo.HostName);
                globalTags.Add("machine_name", envInfo.MachineName);
                globalTags.Add("app_name", envInfo.EntryAssemblyName);
                globalTags.Add("app_version", envInfo.EntryAssemblyVersion);
            });

            options.GlobalTags.Should().Equal(expected);
        }
Exemplo n.º 30
0
 public DefaultReportGenerator(AppMetricsOptions options, ILoggerFactory loggerFactory)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
     _logger  = loggerFactory?.CreateLogger <DefaultReportGenerator>() ?? throw new ArgumentNullException(nameof(loggerFactory));
 }