Ejemplo n.º 1
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();
        }
        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();
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
 public static void StartReporter(TimeSpan delay)
 {
     _scheduler = new AppMetricsTaskScheduler(
         delay,
         () => Task.WhenAll(_metrics.ReportRunner.RunAllAsync())
         );
     _scheduler.Start();
 }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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();
     });
 }
Ejemplo n.º 10
0
        public void Start()
        {
            var processSample    = new HangfireMetricsRunner(metrics);
            var samplesScheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromMilliseconds(1000),
                () =>
            {
                processSample.Run();
                return(Task.CompletedTask);
            });

            samplesScheduler.Start();
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 15
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();
        }
Ejemplo n.º 16
0
        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;
        }
Ejemplo n.º 17
0
        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();
        }
Ejemplo n.º 18
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();
        }
Ejemplo n.º 19
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();
            });
        }
Ejemplo n.º 21
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();
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Opens the current feed by opening all created sessions
        /// </summary>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="InvalidOperationException">
        /// The feed is already opened
        /// or
        /// The configuration is not valid
        /// </exception>
        /// <exception cref="CommunicationException">
        /// Connection to the REST-ful API failed, Probable Reason={Invalid or expired token}
        /// or
        /// Connection to the message broker failed, Probable Reason={Invalid or expired token}
        /// or
        /// </exception>
        public void Open()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(ToString());
            }

            try
            {
                InitFeed();

                if (Interlocked.CompareExchange(ref _opened, 1, 0) != 0)
                {
                    throw new InvalidOperationException("The feed is already opened");
                }

                _log.LogInformation($"Feed configuration: [{InternalConfig}]");

                _connection = UnityContainer.Resolve <IConnectionFactory>().CreateConnection();
                _connection.ConnectionShutdown              += OnConnectionShutdown;
                _connection.CallbackException               += OnCallbackException;
                _feedRecoveryManager.ProducerUp             += MarkProducerAsUp;
                _feedRecoveryManager.ProducerDown           += MarkProducerAsDown;
                _feedRecoveryManager.CloseFeed              += OnCloseFeed;
                _feedRecoveryManager.EventRecoveryCompleted += OnEventRecoveryCompleted;

                ((ProducerManager)ProducerManager).Lock();

                foreach (var session in Sessions)
                {
                    session.Open();
                }

                var interests = Sessions.Select(s => ((OddsFeedSession)s).MessageInterest).ToList();
                _feedRecoveryManager.Open(interests);

                if (InternalConfig.StatisticsEnabled)
                {
                    _metricsTaskScheduler.Start();
                }
            }
            catch (CommunicationException ex)
            {
                Interlocked.CompareExchange(ref _opened, 0, 1);

                // this should really almost never happen
                var result = _connectionValidator.ValidateConnection();
                if (result == ConnectionValidationResult.Success)
                {
                    throw new CommunicationException("Connection to the RESTful API failed, Probable Reason={Invalid or expired token}",
                                                     $"{InternalConfig.ApiBaseUri}:443",
                                                     ex.InnerException);
                }

                var publicIp = _connectionValidator.GetPublicIp();
                throw new CommunicationException($"Connection to the RESTful API failed. Probable Reason={result.Message}, Public IP={publicIp}",
                                                 $"{InternalConfig.ApiBaseUri}:443",
                                                 ex);
            }
            catch (BrokerUnreachableException ex)
            {
                Interlocked.CompareExchange(ref _opened, 0, 1);

                // this should really almost never happen
                var result = _connectionValidator.ValidateConnection();
                if (result == ConnectionValidationResult.Success)
                {
                    throw new CommunicationException("Connection to the message broker failed, Probable Reason={Invalid or expired token}",
                                                     $"{InternalConfig.Host}:{InternalConfig.Port}",
                                                     ex.InnerException);
                }

                var publicIp = _connectionValidator.GetPublicIp();
                throw new CommunicationException($"Connection to the message broker failed. Probable Reason={result.Message}, Public IP={publicIp}",
                                                 $"{InternalConfig.Host}:{InternalConfig.Port}",
                                                 ex);
            }
            catch (Exception)
            {
                Interlocked.CompareExchange(ref _opened, 0, 1);
                throw;
            }
        }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
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();
        }
 public void Start()
 {
     _scheduler.Start();
 }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Opens the current feed
        /// </summary>
        /// <exception cref="ObjectDisposedException">The feed is already disposed</exception>
        /// <exception cref="InvalidOperationException">The feed is already opened</exception>
        /// <exception cref="CommunicationException"> Connection to the message broker failed, Probable Reason={Invalid or expired token}</exception>
        public void Open()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(ToString());
            }

            if (Interlocked.CompareExchange(ref _isOpened, 1, 0) != 0)
            {
                throw new InvalidOperationException("The feed is already opened");
            }

            try
            {
                _rabbitMqMessageReceiverForTickets.MqMessageReceived += OnMqMessageReceived;
                //_rabbitMqMessageReceiverForTickets.MqMessageDeserializationFailed += OnMqMessageDeserializationFailed;
                _rabbitMqMessageReceiverForTickets.Open();

                _rabbitMqMessageReceiverForTicketCancels.MqMessageReceived += OnMqMessageReceived;
                //_rabbitMqMessageReceiverForTicketCancels.MqMessageDeserializationFailed += OnMqMessageDeserializationFailed;
                _rabbitMqMessageReceiverForTicketCancels.Open();

                _rabbitMqMessageReceiverForTicketCashouts.MqMessageReceived += OnMqMessageReceived;
                //_rabbitMqMessageReceiverForTicketCashouts.MqMessageDeserializationFailed += OnMqMessageDeserializationFailed;
                _rabbitMqMessageReceiverForTicketCashouts.Open();

                _rabbitMqMessageReceiverForTicketNonSrSettle.MqMessageReceived += OnMqMessageReceived;
                //_rabbitMqMessageReceiverForTicketNonSrSettle.MqMessageDeserializationFailed += OnMqMessageDeserializationFailed;
                _rabbitMqMessageReceiverForTicketNonSrSettle.Open();

                _ticketPublisherFactory.Open();

                if (_config.StatisticsEnabled)
                {
                    _metricsTaskScheduler.Start();
                }
            }
            catch (CommunicationException ex)
            {
                // this should really almost never happen
                var result = _connectionValidator.ValidateConnection();
                if (result == ConnectionValidationResult.Success)
                {
                    throw new CommunicationException(
                              "Connection to the API failed, Probable Reason={Invalid or expired token}",
                              $"{_config.Host}:{_config.Port}",
                              ex.InnerException);
                }

                var publicIp = _connectionValidator.GetPublicIp();
                throw new CommunicationException(
                          $"Connection to the API failed. Probable Reason={result.Message}, Public IP={publicIp}",
                          $"{_config.Host}:{_config.Port}",
                          ex);
            }
            catch (BrokerUnreachableException ex)
            {
                // this should really almost never happen
                var result = _connectionValidator.ValidateConnection();
                if (result == ConnectionValidationResult.Success)
                {
                    throw new CommunicationException(
                              "Connection to the message broker failed, Probable Reason={Invalid or expired token}",
                              $"{_config.Host}:{_config.Port}",
                              ex.InnerException);
                }

                var publicIp = _connectionValidator.GetPublicIp();
                throw new CommunicationException(
                          $"Connection to the message broker failed. Probable Reason={result.Message}, Public IP={publicIp}",
                          $"{_config.Host}:{_config.Port}",
                          ex);
            }
            _executionLog.LogInformation("MtsSdk instance opened.");
        }
Ejemplo n.º 28
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);
        }