public static void ConfigureMetrics(IMetricsBuilder builder, MetricsInfluxConfig config, string appName)
        {
            builder.Report.ToInfluxDb(options =>
            {
                options.InfluxDb.BaseUri                 = new Uri(config.Url);
                options.InfluxDb.Database                = config.Database;
                options.InfluxDb.UserName                = config.Username;
                options.InfluxDb.Password                = config.Password;
                options.HttpPolicy.BackoffPeriod         = TimeSpan.FromSeconds(30);
                options.HttpPolicy.FailuresBeforeBackoff = 5;
                options.HttpPolicy.Timeout               = TimeSpan.FromSeconds(10);
                options.MetricsOutputFormatter           = new MetricsInfluxDbLineProtocolOutputFormatter();
                options.Filter        = null;
                options.FlushInterval = TimeSpan.FromSeconds(20);
            });

            builder.Configuration.Configure(options =>
            {
                options.GlobalTags["app"] = appName;
                options.Enabled           = true;
                options.ReportingEnabled  = true;
            });

            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(3),
                async() => { await Task.WhenAll(new MetricsBuilder().Build().ReportRunner.RunAllAsync()); });

            scheduler.Start();
        }
        /*
         * Initializing metrics
         */

        #region Methods
        private static void InitializeMetrics()
        {
            //Building the metrics object and setting host and port
            Metrics = AppMetrics.CreateDefaultBuilder()
                      .Report.ToInfluxDb(options =>
            {
                options.InfluxDb.BaseUri  = new Uri("http://127.0.0.1:8086/");
                options.InfluxDb.Database = "sample-db";
                options.InfluxDb.CreateDataBaseIfNotExists = true;
            })
                      .Configuration.Configure(options =>
            {
                options.AddServerTag();
            })
                      .Build();

            //Start logging metrics
            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(5),
                async() =>
            {
                await Task.WhenAll(Metrics.ReportRunner.RunAllAsync());
            });

            scheduler.Start();

            machineMetricsThread.Start();
        }
Example #3
0
        public static void ScheduleSomeSampleMetrics(IMetrics metrics)
        {
            var processSample            = new ProcessSample(metrics);
            var userValueSample          = new UserValueTimerSample(metrics);
            var simpleMetrics            = new SampleMetrics(metrics);
            var setCounterSample         = new SetCounterSample(metrics);
            var setMeterSample           = new SetMeterSample(metrics);
            var userValueHistogramSample = new UserValueHistogramSample(metrics);

            var samplesScheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromMilliseconds(300),
                () =>
            {
                using (metrics.Measure.Apdex.Track(AppMetricsRegistry.ApdexScores.AppApdex))
                {
                    processSample.Run();
                    setCounterSample.Run();
                    setMeterSample.Run();
                    userValueHistogramSample.Run();
                    userValueSample.Run();
                    simpleMetrics.Run();
                }

                SetGaugeValues(metrics);

                return(Task.CompletedTask);
            });

            samplesScheduler.Start();
        }
        public ProcessInfoStatsCollector(IMetrics metrics)
        {
            var cpuUsage = new ProcessTotalCpuTimer();

            _scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromMilliseconds(500),
                () =>
                Task.Run(
                    () =>
            {
                var process = Process.GetCurrentProcess();
                cpuUsage.Calculate();
                metrics.Provider.Timer.Instance(ProcessMetricsRegistry.Timers.CpuUsedMilliseconds).Record(
                    cpuUsage.ProcessTimeUsed.Ticks * 100, TimeUnit.Nanoseconds);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.CpuUsageRatio, () =>
                {
                    return(cpuUsage.ProcessCpuUsedRatio);
                });
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessPagedMemorySize, () => process.PagedMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessPeekPagedMemorySize, () => process.PeakPagedMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessPeekVirtualMemorySize, () => process.PeakVirtualMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessWorkingSetSize, () => process.WorkingSet64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessPeekWorkingSetSize, () => process.PeakWorkingSet64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessPrivateMemorySize, () => process.PrivateMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ProcessVirtualMemorySize, () => process.VirtualMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.SystemNonPagedMemory, () => process.NonpagedSystemMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.SystemPagedMemorySize, () => process.PagedSystemMemorySize64);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.ThreadCount, () => process.Threads.Count);
                metrics.Measure.Gauge.SetValue(ProcessMetricsRegistry.Gauges.HandlesCount, () => process.HandleCount);
            }));
            _scheduler.Start();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Feed"/> class
        /// </summary>
        /// <param name="config">A <see cref="IOddsFeedConfiguration"/> instance representing feed configuration</param>
        /// <param name="isReplay">Value indicating whether the constructed instance will be used to connect to replay server</param>
        /// <param name="loggerFactory">A <see cref="ILoggerFactory"/> used to create <see cref="ILogger"/> used within sdk</param>
        /// <param name="metricsRoot">A <see cref="IMetricsRoot"/> used to provide metrics within sdk</param>
        protected Feed(IOddsFeedConfiguration config, bool isReplay, ILoggerFactory loggerFactory = null, IMetricsRoot metricsRoot = null)
        {
            Guard.Argument(config, nameof(config)).NotNull();

            FeedInitialized = false;

            UnityContainer = new UnityContainer();
            //UnityContainer = new UnityContainer().EnableDiagnostic();
            UnityContainer.RegisterBaseTypes(config, loggerFactory, metricsRoot);
            InternalConfig = UnityContainer.Resolve <IOddsFeedConfigurationInternal>();
            if (isReplay || InternalConfig.Environment == SdkEnvironment.Replay)
            {
                InternalConfig.EnableReplayServer();
            }

            _log = SdkLoggerFactory.GetLoggerForExecution(typeof(Feed));

            LogInit();

            _metricsRoot          = UnityContainer.Resolve <IMetricsRoot>();
            _metricsLogger        = SdkLoggerFactory.GetLoggerForStats(typeof(Feed));
            _metricsTaskScheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(InternalConfig.StatisticsTimeout),
                async() => { await LogMetricsAsync(); });
        }
Example #6
0
        public static async Task Main()
        {
            long threshold      = 1;
            var  metricsBuilder = new MetricsBuilder();
            var  healthBuilder  = new HealthBuilder();

            var metrics = metricsBuilder.Report.ToInfluxDb(AppSetting.InfluxDB.Url,
                                                           AppSetting.InfluxDB.DatabaseName,
                                                           TimeSpan.FromSeconds(5))
                          .Report.ToConsole(TimeSpan.FromSeconds(5))
                          .Build()
            ;
            var health = healthBuilder.Configuration
                         .Configure(p =>
            {
                p.Enabled          = true;
                p.ReportingEnabled = true;
            })
                         .Report
                         .ToMetrics(metrics)
                         .HealthChecks.AddProcessPrivateMemorySizeCheck("Private Memory Size", threshold)
                         .HealthChecks.AddProcessVirtualMemorySizeCheck("Virtual Memory Size", threshold)
                         .HealthChecks.AddProcessPhysicalMemoryCheck("Working Set", threshold)
                         .HealthChecks.AddPingCheck("google ping", "google.com", TimeSpan.FromSeconds(10))
                         .Build();

            var counter = new CounterOptions {
                Name = "my_counter"
            };

            metrics.Measure.Counter.Increment(counter);

            var scheduler =
                new AppMetricsTaskScheduler(TimeSpan.FromSeconds(5),
                                            async() =>
            {
                await Task.WhenAll(metrics.ReportRunner.RunAllAsync());
                var healthStatus =
                    await health.HealthCheckRunner.ReadAsync();

                using (var stream = new MemoryStream())
                {
                    await health.DefaultOutputHealthFormatter
                    .WriteAsync(stream, healthStatus);
                    var result = Encoding.UTF8.GetString(stream.ToArray());
                    Console.WriteLine(result);
                }

                foreach (var reporter in health.Reporters)
                {
                    await reporter.ReportAsync(health.Options,
                                               healthStatus);
                }
            });

            scheduler.Start();

            Console.ReadKey();
        }
Example #7
0
 public static void StartReporter(TimeSpan delay)
 {
     _scheduler = new AppMetricsTaskScheduler(
         delay,
         () => Task.WhenAll(_metrics.ReportRunner.RunAllAsync())
         );
     _scheduler.Start();
 }
Example #8
0
        /// <summary>
        /// Constructs a new instance of the <see cref="MtsSdk"/> class
        /// </summary>
        /// <param name="config">A <see cref="ISdkConfiguration"/> instance representing feed configuration</param>
        /// <param name="loggerFactory">A <see cref="ILoggerFactory"/> used to create <see cref="ILogger"/> used within sdk</param>
        /// <param name="metricsRoot">A <see cref="IMetricsRoot"/> used to provide metrics within sdk</param>
        public MtsSdk(ISdkConfiguration config, ILoggerFactory loggerFactory = null, IMetricsRoot metricsRoot = null)
        {
            Guard.Argument(config, nameof(config)).NotNull();

            LogInit();

            _isDisposed = false;
            _isOpened   = 0;

            _unityContainer = new UnityContainer();
            _unityContainer.RegisterTypes(config, loggerFactory, metricsRoot);
            _config = _unityContainer.Resolve <ISdkConfigurationInternal>();

            _executionLog   = SdkLoggerFactory.GetLoggerForExecution(typeof(MtsSdk));
            _interactionLog = SdkLoggerFactory.GetLoggerForExecution(typeof(MtsSdk));

            LogInit();

            _metricsRoot          = _unityContainer.Resolve <IMetricsRoot>();
            _metricsLogger        = SdkLoggerFactory.GetLoggerForStats(typeof(MtsSdk));
            _metricsTaskScheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(_config.StatisticsTimeout),
                async() => { await LogMetricsAsync(); });


            _connectionValidator = _unityContainer.Resolve <ConnectionValidator>();

            BuilderFactory          = _unityContainer.Resolve <IBuilderFactory>();
            _ticketPublisherFactory = _unityContainer.Resolve <ITicketSenderFactory>();

            _entitiesMapper = _unityContainer.Resolve <EntitiesMapper>();

            _rabbitMqMessageReceiverForTickets           = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketCancels     = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketCancelResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketCashouts    = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketCashoutResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketNonSrSettle = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketNonSrSettleResponseMessageReceiver");

            ClientApi        = _unityContainer.Resolve <IMtsClientApi>();
            CustomBetManager = _unityContainer.Resolve <ICustomBetManager>();
            ConnectionStatus = _unityContainer.Resolve <IConnectionStatus>();
            ReportManager    = _unityContainer.Resolve <IReportManager>();

            _autoResetEventsForBlockingRequests        = new ConcurrentDictionary <string, AutoResetEvent>();
            _responsesFromBlockingRequests             = new ConcurrentDictionary <string, ISdkTicket>();
            _ticketsForNonBlockingRequests             = new MemoryCache("TicketsForNonBlockingRequests");
            _lockForTicketsForNonBlockingRequestsCache = new object();

            foreach (var t in Enum.GetValues(typeof(SdkTicketType)))
            {
                var publisher = _ticketPublisherFactory.GetTicketSender((SdkTicketType)t);
                if (publisher != null)
                {
                    publisher.TicketSendFailed += PublisherOnTicketSendFailed;
                }
            }
        }
Example #9
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            var requestSamplesScheduler = new AppMetricsTaskScheduler(TimeSpan.FromMilliseconds(_options.Value.ReportInterval), async() =>
            {
                await Task.WhenAll(_metrics.ReportRunner.RunAllAsync());  // send metrics to reporter
            });

            requestSamplesScheduler.Start();

            return(Task.CompletedTask);
        }
Example #10
0
        static void StartReporting(IMetricsRoot metricsCollector)
        {
            // Start reporting metrics every 20s
            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(20),
                async() =>
            {
                await Task.WhenAll(metricsCollector.ReportRunner.RunAllAsync());
            });

            scheduler.Start();
        }
 private async static void Program1_StartReporting1(object sender, IMetricsRoot metrics)
 {
     await Task.Run(() =>
     {
         var scheduler = new AppMetricsTaskScheduler(
             TimeSpan.FromSeconds(3),
             async() =>
         {
             await Task.WhenAll(metrics.ReportRunner.RunAllAsync());
         });
         scheduler.Start();
     });
 }
Example #12
0
        public void Start()
        {
            var processSample    = new HangfireMetricsRunner(metrics);
            var samplesScheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromMilliseconds(1000),
                () =>
            {
                processSample.Run();
                return(Task.CompletedTask);
            });

            samplesScheduler.Start();
        }
Example #13
0
        public static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.LiterateConsole(LogEventLevel.Information)
                         .WriteTo.Seq("http://localhost:5341", LogEventLevel.Verbose)
                         .CreateLogger();

            var host = new HostBuilder()
                       .ConfigureAppConfiguration(
                (hostContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddEnvironmentVariables();
                config.AddJsonFile("appsettings.json", optional: true);
                config.AddCommandLine(args);
            })
                       .ConfigureMetrics(
                (context, builder) =>
            {
                builder.Report.Using <SimpleConsoleMetricsReporter>(TimeSpan.FromSeconds(5));
            })
                       .Build();

            var metrics = host.Services.GetRequiredService <IMetricsRoot>();

            var cancellationTokenSource = new CancellationTokenSource();

            await host.WriteEnvAsync(cancellationTokenSource, metrics);

            host.PressAnyKeyToContinue();

            await host.WriteMetricsAsync(metrics, cancellationTokenSource);

            host.PressAnyKeyToContinue();

            var recordMetricsTask = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(2),
                () =>
            {
                Clear();
                host.RecordMetrics(metrics);
                return(Task.CompletedTask);
            });

            recordMetricsTask.Start();

            await host.RunAsync(token : cancellationTokenSource.Token);
        }
Example #14
0
        public static void SetupHealth(IMetricsRoot metricsRoot)
        {
            long threshold = 1;

            //var healthBuilder = serviceProvider.GetService<IHealthBuilder>();
            var healthBuilder = new HealthBuilder();

            var health = healthBuilder.Configuration
                         .Configure(p =>
            {
                p.Enabled          = true;
                p.ReportingEnabled = true;
            })
                         .Report
                         .ToMetrics(metricsRoot)
                         .HealthChecks.AddProcessPrivateMemorySizeCheck("Private Memory Size", threshold)
                         .HealthChecks.AddProcessVirtualMemorySizeCheck("Virtual Memory Size", threshold)
                         .HealthChecks.AddProcessPhysicalMemoryCheck("Working Set", threshold)
                         .HealthChecks.AddPingCheck("google ping", "google.com", TimeSpan.FromSeconds(10))
                         .HealthChecks.AddHttpGetCheck("github", new Uri("https://github.com/"), TimeSpan.FromSeconds(10))
                         .Build();


            var scheduler =
                new AppMetricsTaskScheduler(TimeSpan.FromSeconds(5),
                                            async() =>
            {
                //await Task.WhenAll(metricsRoot.ReportRunner.RunAllAsync());
                var healthStatus =
                    await health.HealthCheckRunner.ReadAsync();

                //using (var stream = new MemoryStream())
                //{
                //    await health.DefaultOutputHealthFormatter
                //                .WriteAsync(stream, healthStatus);
                //    var result = Encoding.UTF8.GetString(stream.ToArray());
                //    Console.WriteLine(result);
                //}

                foreach (var reporter in health.Reporters)
                {
                    await reporter.ReportAsync(health.Options,
                                               healthStatus);
                }
            });

            scheduler.Start();
        }
Example #15
0
        static void StartReporting(IMetricsRoot metricsCollector)
        {
            // Start reporting metrics every 5s
            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(5),
                async() =>
            {
                foreach (var callback in Gauges)
                {
                    callback();
                }
                await Task.WhenAll(metricsCollector.ReportRunner.RunAllAsync());
            });

            scheduler.Start();
        }
        private void InitMetricsHistogramsReporting(
            WavefrontSpanReporter wfSpanReporter, ApplicationTags applicationTags,
            TimeSpan reportFrequency, ILoggerFactory loggerFactory, out IMetricsRoot metricsRoot,
            out AppMetricsTaskScheduler metricsScheduler, out HeartbeaterService heartbeaterService,
            out WavefrontSdkMetricsRegistry sdkMetricsRegistry)
        {
            var tempMetricsRoot = new MetricsBuilder()
                                  .Configuration.Configure(
                options =>
            {
                options.DefaultContextLabel = DerivedMetricPrefix;
            })
                                  .Report.ToWavefront(
                options =>
            {
                options.WavefrontSender = wfSpanReporter.WavefrontSender;
                options.Source          = wfSpanReporter.Source;
                options.ApplicationTags = applicationTags;
                options.WavefrontHistogram.ReportMinuteDistribution = true;
                options.LoggerFactory = loggerFactory;
            })
                                  .Build();

            metricsRoot = tempMetricsRoot;

            metricsScheduler = new AppMetricsTaskScheduler(
                reportFrequency,
                async() =>
            {
                await Task.WhenAll(tempMetricsRoot.ReportRunner.RunAllAsync());
            });
            metricsScheduler.Start();

            heartbeaterService = new HeartbeaterService(
                wfSpanReporter.WavefrontSender, applicationTags, HeartbeaterComponents,
                wfSpanReporter.Source, loggerFactory);
            heartbeaterService.Start();

            sdkMetricsRegistry = new WavefrontSdkMetricsRegistry
                                 .Builder(wfSpanReporter.WavefrontSender)
                                 .Prefix(Constants.SdkMetricPrefix + ".opentracing")
                                 .Source(wfSpanReporter.Source)
                                 .Tags(applicationTags.ToPointTags())
                                 .LoggerFactory(loggerFactory)
                                 .Build();
            wfSpanReporter.SetSdkMetricsRegistry(sdkMetricsRegistry);
        }
Example #17
0
        private static void RunRequestsToSample()
        {
            var httpClient = new HttpClient
            {
                BaseAddress = ApiBaseAddress
            };

            var requestSamplesScheduler = new AppMetricsTaskScheduler(TimeSpan.FromMilliseconds(100), async() =>
            {
                var uniform = httpClient.GetStringAsync("api/reservoirs/uniform");
                var exponentiallyDecaying = httpClient.GetStringAsync("api/reservoirs/exponentially-decaying");
                var slidingWindow         = httpClient.GetStringAsync("api/reservoirs/sliding-window");

                await Task.WhenAll(uniform, exponentiallyDecaying, slidingWindow);
            });

            requestSamplesScheduler.Start();
        }
        private static void AddMetrics(IContainer container, string appName)
        {
            if (_metrics != null)
            {
                container.RegisterNonScopedSingleton <IMetrics>(_metrics);
                return;
            }

            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("metricsettings.json")
                                .Build();

            var influxOptions = new MetricsReportingInfluxDbOptions();

            configuration.GetSection(nameof(MetricsReportingInfluxDbOptions)).Bind(influxOptions);

            var metricsRoot = new MetricsBuilder()
                              .Configuration.ReadFrom(configuration)
                              .Configuration.Configure(
                options =>
            {
                options.AddServerTag();
                options.AddAppTag(appName);
            })
                              .Report.ToInfluxDb(influxOptions)
                              .Build();


            var metrics = new DotNetWorkQueue.AppMetrics.Metrics(metricsRoot);

            container.RegisterNonScopedSingleton <IMetrics>(metrics);

            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(3),
                async() =>
            {
                await Task.WhenAll(metricsRoot.ReportRunner.RunAllAsync());
            });

            scheduler.Start();
            _metricScheduler = scheduler;
            _metrics         = metrics;
        }
        private static async Task Main()
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .WriteTo.Console()
                         .WriteTo.Seq("http://localhost:5341/")
                         .CreateLogger();

            Log.Information("Starting AppMetrics Serilog Reporter sample");

            var metrics = AppMetrics.CreateDefaultBuilder()
                          .Configuration.Configure(options =>
                                                   options.GlobalTags.Add("InstanceId", Guid.NewGuid().ToString("N")))
                          .Report.ToConsole()
                          .Report.ToSerilog(LogEventLevel.Information)
                          .Build();

            var cts = new CancellationTokenSource();

            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(3),
                () => Task.WhenAll(metrics.ReportRunner.RunAllAsync()));

            scheduler.Start();

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                eventArgs.Cancel = true;
                scheduler.Dispose();
                cts.Cancel();
            };

            var counterTask   = RunCounter(metrics, cts.Token);
            var timerTask     = RunTimer(metrics, cts.Token);
            var meterTask     = RunMeter(metrics, cts.Token);
            var histogramTask = RunHistogram(metrics, cts.Token);
            var gaugeTask     = RunGauge(metrics, cts.Token);

            await Task.WhenAll(counterTask, timerTask, meterTask, histogramTask, gaugeTask);

            Log.Information("Sample application shutdown");
            Log.CloseAndFlush();
        }
Example #20
0
        public Metrics(ILogger <Metrics> logger)
        {
            _logger  = logger;
            _metrics = new MetricsBuilder()
                       .Report.ToConsole()
                       .Build();
            _counter = new CounterOptions {
                Name = "my_counter"
            };
            _logger.LogDebug("Metrics built!");
            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(3),
                async() =>
            {
                await Task.WhenAll(_metrics.ReportRunner.RunAllAsync());
            });

            scheduler.Start();
        }
Example #21
0
        private static void RunRequestsToWebApplication()
        {
            var httpClient = new HttpClient
            {
                BaseAddress = ApiBaseAddress
            };

            var requestSamplesScheduler = new AppMetricsTaskScheduler(TimeSpan.FromMilliseconds(10), async() =>
            {
                Sample data = new Sample()
                {
                    CreatedOn = DateTime.UtcNow, Id = Guid.NewGuid(), Name = "Test"
                };

                var samplesGet  = httpClient.GetStringAsync("samples");
                var samplesPost = httpClient.PostAsync("samples", new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));
                await Task.WhenAll(samplesGet, samplesPost);
            });

            requestSamplesScheduler.Start();
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services
            .AddMvc(options => { options.Filters.Add(typeof(GlobalExceptionFilter)); })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddApiVersioning()
            .AddHttpClient();

            var metrics = AppMetrics.CreateDefaultBuilder().Report.ToConsole(options =>
            {
                options.FlushInterval          = TimeSpan.FromSeconds(5);
                options.Filter                 = new MetricsFilter().WhereType(MetricType.Timer);
                options.MetricsOutputFormatter = new MetricsJsonOutputFormatter();
            }).Build();

            services.AddMetrics(metrics)
            .AddMetricsTrackingMiddleware()
            .AddMetricsEndpoints()
            .AddHealth()
            .AddHealthEndpoints();

            var scheduler = new AppMetricsTaskScheduler(TimeSpan.FromSeconds(5),
                                                        async() => await Task.WhenAll(metrics.ReportRunner.RunAllAsync()));

            scheduler.Start();

            services.AddRssFeedsConfigurations(_env, options =>
            {
                options.AddDefault()
                .AddDataProtection()
                .AddDbContext()
                .AddCors()
                .AddSwagger()
                .AddTraceId()
                .AddRegistComponents()
                .AddRegistDomainEventHandlers();
            });
        }
Example #23
0
        private static void startWavefrontReportingViaProxy()
        {
            IWavefrontSender wavefrontProxyClient = new WavefrontProxyClient.Builder(proxyHost)
                                                    .MetricsPort(metricsPort)
                                                    .Build();

            IMetricsRoot metrics = new MetricsBuilder()
                                   .Configuration.Configure(options =>
            {
                options.DefaultContextLabel = "service";
                options.GlobalTags          = new GlobalMetricTags(new Dictionary <string, string>
                {
                    { "dc", "us-west-2" },
                    { "env", "staging" }
                });
            })
                                   .Report.ToWavefront(options =>
            {
                options.WavefrontSender = wavefrontProxyClient;
                options.Source          = "app-1.company.com";
            })
                                   .Build();

            CounterOptions evictions = new CounterOptions
            {
                Name = "cache-evictions"
            };

            metrics.Measure.Counter.Increment(evictions);

            var scheduler = new AppMetricsTaskScheduler(TimeSpan.FromSeconds(5), async() =>
            {
                await Task.WhenAll(metrics.ReportRunner.RunAllAsync());
            });

            scheduler.Start();
        }
Example #24
0
        public static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.LiterateConsole(LogEventLevel.Information)
                         .WriteTo.Seq("http://localhost:5341", LogEventLevel.Verbose)
                         .CreateLogger();

            var host = new HostBuilder()
                       .ConfigureAppConfiguration(
                (hostContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddEnvironmentVariables();
                config.AddJsonFile("appsettings.json", optional: true);
                config.AddCommandLine(args);
            })
                       .ConfigureMetrics(
                (context, builder) =>
            {
                builder.Report.Using <SimpleConsoleMetricsReporter>(TimeSpan.FromSeconds(5));
            })
                       .ConfigureServices(services =>
            {
                services.AddHealthChecks()
                .AddCheck(
                    "example_health_check_healthy",
                    new StaticHealthCheck(HealthCheckResult.Healthy()),
                    failureStatus: HealthStatus.Degraded,
                    tags: new[] { "ready" });
                services.AddHealthChecks()
                .AddCheck(
                    "example_health_check_degraded",
                    new StaticHealthCheck(HealthCheckResult.Degraded()),
                    failureStatus: HealthStatus.Degraded,
                    tags: new[] { "ready" });

                // Won't be required for ASP.NET Core 3.0 (https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-2.2#health-check-publisher)
                services.TryAddEnumerable(
                    ServiceDescriptor.Singleton(
                        typeof(IHostedService),
                        typeof(HealthCheckPublisherOptions).Assembly
                        .GetType(HealthCheckServiceAssembly)));

                services.Configure <HealthCheckPublisherOptions>(options =>
                {
                    options.Delay     = TimeSpan.FromSeconds(2);
                    options.Predicate = (check) => check.Tags.Contains("ready");
                });

                services.AddAppMetricsHealthPublishing();
            })
                       .Build();

            var metrics = host.Services.GetRequiredService <IMetricsRoot>();

            var cancellationTokenSource = new CancellationTokenSource();

            await host.WriteEnvAsync(cancellationTokenSource, metrics);

            host.PressAnyKeyToContinue();

            await host.WriteMetricsAsync(metrics, cancellationTokenSource);

            host.PressAnyKeyToContinue();

            await host.WriteHealthChecksAsync(cancellationTokenSource);

            host.PressAnyKeyToContinue();

            var recordMetricsTask = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(2),
                () =>
            {
                Clear();
                host.RecordMetrics(metrics);
                return(Task.CompletedTask);
            });

            recordMetricsTask.Start();

            await host.RunAsync(token : cancellationTokenSource.Token);
        }
Example #25
0
        public static IApplicationBuilder UseTestStuff(
            this IApplicationBuilder app,
            IApplicationLifetime lifetime,
            bool runSampleRequestsFromApp)
        {
            app.Use(
                (context, next) =>
            {
                RandomClientIdForTesting.SetTheFakeClaimsPrincipal(context);
                return(next());
            });

            var token = lifetime.ApplicationStopping;

            if (runSampleRequestsFromApp)
            {
                var apdexSamples = new AppMetricsTaskScheduler(
                    TimeSpan.FromSeconds(ApdexSamplesInterval),
                    () =>
                {
                    var satisfied   = HttpClient.GetAsync("api/satisfying", token);
                    var tolerating  = HttpClient.GetAsync("api/tolerating", token);
                    var frustrating = HttpClient.GetAsync("api/frustrating", token);

                    return(Task.WhenAll(satisfied, tolerating, frustrating));
                });

                apdexSamples.Start();

                var randomErrorSamples = new AppMetricsTaskScheduler(
                    TimeSpan.FromSeconds(RandomSamplesInterval),
                    () =>
                {
                    var randomStatusCode = HttpClient.GetAsync("api/randomstatuscode", token);
                    var randomException  = HttpClient.GetAsync("api/randomexception", token);

                    return(Task.WhenAll(randomStatusCode, randomException));
                });

                randomErrorSamples.Start();

                var testSamples = new AppMetricsTaskScheduler(
                    TimeSpan.FromSeconds(GetEndpointSuccessInterval),
                    () => HttpClient.GetAsync("api/test", token));

                testSamples.Start();

                var slaSamples = new AppMetricsTaskScheduler(
                    TimeSpan.FromSeconds(SlaEndpointsInterval),
                    () => HttpClient.GetAsync("api/slatest/timer", token));

                slaSamples.Start();

                var randomBufferGenerator = new RandomBufferGenerator(50000);
                var postPutSamples        = new AppMetricsTaskScheduler(
                    TimeSpan.FromSeconds(PutAndPostRequestsInterval),
                    () =>
                {
                    var putBytes    = new ByteArrayContent(randomBufferGenerator.GenerateBufferFromSeed());
                    var putFormData = new MultipartFormDataContent {
                        { putBytes, "put-file", "rnd-put" }
                    };
                    var putRequest = HttpClient.PutAsync("api/file", putFormData, token);

                    var postBytes    = new ByteArrayContent(randomBufferGenerator.GenerateBufferFromSeed());
                    var postFormData = new MultipartFormDataContent {
                        { postBytes, "post-file", "rnd-post" }
                    };
                    var postRequest = HttpClient.PostAsync("api/file", postFormData, token);

                    return(Task.WhenAll(putRequest, postRequest));
                });

                postPutSamples.Start();
            }

            return(app);
        }
Example #26
0
        private static async Task MainAsync(string[] args)
        {
            //// App Metrics configuration

            // Build metrics root
            var metrics =
                new MetricsBuilder()
                .Report.ToGraphite($"net.tcp://{Program.GraphiteEndpoint}")
                .Build();

            // Build and run scheduler
            var scheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromMilliseconds(Program.GraphiteUpdateIntervalMilliseconds),
                async() => { await Task.WhenAll(metrics.ReportRunner.RunAllAsync()); });

            scheduler.Start();


            //// DataStax C# Driver configuration

            Cassandra.Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Warning;
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            var cluster = Cluster.Builder()
                          .AddContactPoint(Program.ContactPoint)
                          .WithSessionName(Program.SessionName)
                          .WithMetrics(
                metrics.CreateDriverMetricsProvider(),
                new DriverMetricsOptions()
                .SetEnabledNodeMetrics(NodeMetric.AllNodeMetrics)
                .SetEnabledSessionMetrics(SessionMetric.AllSessionMetrics))
                          .Build();

            var session = await cluster.ConnectAsync().ConfigureAwait(false);


            //// Run some queries to have metrics data

            var cts  = new CancellationTokenSource();
            var task = Task.Run(async() =>
            {
                while (!cts.IsCancellationRequested)
                {
                    try
                    {
                        await session.ExecuteAsync(new SimpleStatement("SELECT * FROM system.local"));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"ERROR: {ex}");
                    }
                }
            });

            Console.WriteLine("Press enter to shutdown the session and exit.");
            Console.ReadLine();

            cts.Cancel();

            await task.ConfigureAwait(false);

            await cluster.ShutdownAsync().ConfigureAwait(false);
        }
Example #27
0
        static void Main()
        {
            // Ensure the storage emulator is running
            AzureEmulatorManager.EnsureStorageEmulatorIsStarted();

            // If you want to see tracing from the Picton libary, change the LogLevel to 'Trace'
            var minLogLevel = Logging.LogLevel.Debug;

            // Configure logging to the console
            var logProvider = new ColoredConsoleLogProvider(minLogLevel);
            var logger      = logProvider.GetLogger("Main");

            LogProvider.SetCurrentLogProvider(logProvider);

            // Ensure the Console is tall enough
            Console.WindowHeight = Math.Min(60, Console.LargestWindowHeight);

            // Configure where metrics are published to
            var datadogApiKey = Environment.GetEnvironmentVariable("DATADOG_APIKEY");
            var metrics       = new MetricsBuilder()
                                .Report.OverHttp(o =>
            {
                o.HttpSettings.RequestUri = new Uri($"https://app.datadoghq.com/api/v1/series?api_key={datadogApiKey}");
                o.MetricsOutputFormatter  = new DatadogFormatter(new DatadogFormatterOptions {
                    Hostname = Environment.MachineName
                });
                o.FlushInterval = TimeSpan.FromSeconds(2);
            })
                                .Build();

            // Send metrics to Datadog
            var sendMetricsJob = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(2),
                async() =>
            {
                await Task.WhenAll(metrics.ReportRunner.RunAllAsync());
            });

            sendMetricsJob.Start();

            // Setup the message queue in Azure storage emulator
            var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            var queueName      = "myqueue";

            logger(Logging.LogLevel.Info, () => "Begin integration tests...");

            var numberOfMessages = 25;

            logger(Logging.LogLevel.Info, () => $"Adding {numberOfMessages} string messages to the queue...");
            AddStringMessagesToQueue(numberOfMessages, queueName, storageAccount, logProvider).Wait();
            logger(Logging.LogLevel.Info, () => "Processing the messages in the queue...");
            ProcessSimpleMessages(queueName, storageAccount, logProvider, metrics);

            logger(Logging.LogLevel.Info, () => $"Adding {numberOfMessages} simple messages to the queue...");
            AddSimpleMessagesToQueue(numberOfMessages, queueName, storageAccount, logProvider).Wait();
            logger(Logging.LogLevel.Info, () => "Processing the messages in the queue...");
            ProcessSimpleMessages(queueName, storageAccount, logProvider, metrics);

            logger(Logging.LogLevel.Info, () => $"Adding {numberOfMessages} messages with handlers to the queue...");
            AddMessagesWithHandlerToQueue(numberOfMessages, queueName, storageAccount, logProvider).Wait();
            logger(Logging.LogLevel.Info, () => "Processing the messages in the queue...");
            ProcessMessagesWithHandlers(queueName, storageAccount, logProvider, metrics);

            // Flush the console key buffer
            while (Console.KeyAvailable)
            {
                Console.ReadKey(true);
            }

            // Wait for user to press a key
            logger(Logging.LogLevel.Info, () => "Press any key to exit...");
            Console.ReadKey();
        }