private void RunHttpClientExceptionScenario(Exception throwOnHttpPost)
        {
            Exception lastError     = null;
            var       onErrorCalled = new ManualResetEventSlim();

            void OnError(Exception ex)
            {
                lastError = ex;
                onErrorCalled.Set();
            }

            var pusher = new MetricPusher(new MetricPusherOptions
            {
                Job = "Test",
                // Small interval to ensure that we exit fast.
                IntervalMilliseconds = 100,
                Endpoint             = "https://any_valid.url/the_push_fails_with_fake_httpclient_throwing_exceptions",
                OnError            = OnError,
                HttpClientProvider = () => new ThrowingHttpClient(throwOnHttpPost)
            });

            pusher.Start();

            var onErrorWasCalled = onErrorCalled.Wait(TimeSpan.FromSeconds(5));

            Assert.IsTrue(onErrorWasCalled, "OnError was not called even though the push failed.");
            Assert.IsNotNull(lastError);

            pusher.Stop();
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IHostApplicationLifetime applicationLifetime, IWebHostEnvironment env)
        {
            applicationLifetime.ApplicationStopping.Register(OnShutdown);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            loggerFactory.AddLog4Net();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseOpenApiUi(Configuration);

            Metrics.SuppressDefaultMetrics();
            var prometheusEndpoint = Configuration.GetSection("EpMon:PrometheusPushGateway").Value;

            if (!string.IsNullOrEmpty(prometheusEndpoint))
            {
                _metricPusher = new MetricPusher(prometheusEndpoint, $"EpMon", Environment.MachineName);
                _metricPusher.Start();
            }
        }
        internal static void Main(string[] args)
        {
            var defaultPusher = new MetricPusher("http://localhost:9091", "pushgateway-testworker", "default");
            var registry      = new CollectorRegistry();
            var customPusher  = new MetricPusher(registry, "http://localhost:9091", "pushgateway-testworker", "custom", null, null);

            var counter         = Metrics.CreateCounter("example_counter1", "help");
            var counterInCustom = Metrics.WithCustomRegistry(registry).CreateCounter("example_counter2", "help1");

            IMetricPushServer server = new MetricPushServer(new IMetricPusher[]
            {
                defaultPusher, customPusher
            });

            server.Start();

            for (int i = 0; i < 10; i++)
            {
                counter.Inc();
                counterInCustom.Inc(2);

                Console.WriteLine("count: " + i);
                Thread.Sleep(2000);
            }

            server.Stop();
        }
Exemple #4
0
        public static void AddMetricsCapturing(this IApplicationBuilder app, MetricsConfiguration configuration)
        {
            app.UseHttpMetrics();
            var pusher = new MetricPusher(configuration.Url, "FakeApi");

            pusher.Start();
        }
Exemple #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CurrencyConversionService v1"));
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseHttpMetrics();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });


            if (_metricsPusher == default)
            {
                _metricsPusher = new MetricPusher(
                    endpoint: "http://localhost:9091/metrics",
                    job: nameof(CurrencyConversionService),
                    additionalLabels: new[] { new Tuple <string, string>("domain", "currency") }
                    );
                _metricsPusher.Start();
            }
        }
Exemple #6
0
        public async Task Worker_10Step()
        {
            var counter = Metrics.CreateCounter("worker_counter1", "help");
            var pusher  = new MetricPusher("http://localhost:9091", "pushgateway-testworker");

            var worker = new MetricPushServer(pusher);

            worker.Start();

            for (int i = 0; i < 10; i++)
            {
                counter.Inc();
                _output.WriteLine($"Step: {i}, IsRunning: {worker.IsRunning}");

                switch (i)
                {
                case 5:
                    worker.Stop();
                    break;

                case 8:
                    worker.Start();
                    break;
                }

                await Task.Delay(2000);
            }
            worker.Stop();
        }
        public Measurer(IOptions <MeasurerConfiguration> measurerConfigurationOptions,
                        IOptions <CommonConfiguration> commonConfigurationOptions)
        {
            var measurerConfiguration = measurerConfigurationOptions.Value;

            _collector = DotNetRuntimeStatsBuilder.Default().StartCollecting();

            _heartbeatCounter = Prometheus.Metrics.CreateCounter("heartbeat_count", "heartbeat");

            _httpRequestCounter = Prometheus.Metrics.CreateCounter("request_total", "HTTP Requests Total",
                                                                   new CounterConfiguration
            {
                LabelNames = new[] { "path", "method", "status", "correlation-id" }
            });

            if (measurerConfiguration.UsePushMetricsServer)
            {
                //push model
                _pusher = new MetricPusher(new MetricPusherOptions
                {
                    Endpoint             = measurerConfiguration.PushMetricsServerEndpoint,
                    Job                  = "push-metrics-job",
                    Instance             = commonConfigurationOptions.Value.InstanceId,
                    IntervalMilliseconds = (long)measurerConfiguration.FlushPeriod.TotalMilliseconds
                });

                _pusher.Start();
            }
        }
        public void Dispose()
        {
            Push();

            var collector = _collector;

            if (collector != null)
            {
                _collector = null;
                collector.Dispose();
            }

            var server = _server;

            if (server != null)
            {
                _server = null;
                server.Stop();
            }

            var pusher = _pusher;

            if (pusher != null)
            {
                _pusher = null;
                pusher.Stop();
            }
        }
        public void OnError_CallsErrorCallback()
        {
            Exception lastError     = null;
            var       onErrorCalled = new ManualResetEventSlim();

            void OnError(Exception ex)
            {
                lastError = ex;
                onErrorCalled.Set();
            }

            var pusher = new MetricPusher(new MetricPusherOptions
            {
                Job = "Test",
                // Small interval to ensure that we exit fast.
                IntervalMilliseconds = 100,
                // Nothing listening there, should throw error right away.
                Endpoint = "https://127.0.0.1:1",
                OnError  = OnError
            });

            pusher.Start();

            var onErrorWasCalled = onErrorCalled.Wait(TimeSpan.FromSeconds(5));

            Assert.IsTrue(onErrorWasCalled, "OnError was not called even though at least one failed push should have happened already.");
            Assert.IsNotNull(lastError);

            pusher.Stop();
        }
Exemple #10
0
        public static void Main(string[] args)
        {
            var          env    = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            const string file   = "nlog.config";
            var          logger = NLogBuilder.ConfigureNLog(file).GetCurrentClassLogger();

            try
            {
                logger.Debug("init main");
                if (env != "Development")
                {
                    var metricServer = new MetricPusher(
                        endpoint: "https://push.qaybe.de/metrics",
                        job: "Stack.Air");
                    metricServer.Start();
                }
                CreateHostBuilder(args)
                .Build()
                .Run();
            }
            catch (Exception ex)
            {
                //NLog: catch setup errors
                logger.Error(ex, "Stopped program because of exception");
                throw;
            }
            finally
            {
                NLog.LogManager.Shutdown();
            }
        }
 public Monitoring(DbConfig config, int shardId)
 {
     if (this.Prometheus == null)
     {
         this.Prometheus = new MetricPusher(config.PrometheusEndpoint, config.PrometheusJob, shardId.ToString(), config.PrometheusInterval);
     }
     this.Prometheus.Start();
 }
Exemple #12
0
        public async Task Simple_Push()
        {
            var counter = Metrics.CreateCounter("test_c12", "help");

            counter.Inc();

            var pusher = new MetricPusher("http://localhost:9091", "pushgateway-test", "instance");
            await pusher.PushAsync();
        }
 public HostedService()
 {
     metricPusher = new MetricPusher(new MetricPusherOptions
     {
         Endpoint             = "http://localhost:5000/metrics",
         Job                  = "dicomstore",
         IntervalMilliseconds = 5000
     });
 }
Exemple #14
0
        static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .Build();

            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration)
                         .CreateLogger();

            var serviceCollection = new ServiceCollection();

            ConfigureServices(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var logger          = serviceProvider.GetService <ILogger <Program> >();

            var monitoringConfig = new MonitoringConfig();

            configuration.GetSection("MonitoringConfig").Bind(monitoringConfig);

            var prometheus = new PrometheusConfig();

            configuration.GetSection(Constants.DefaultMetricsServerName).Bind(prometheus);

            MetricPusher metricServer = null;

            if (!string.IsNullOrEmpty(prometheus.Instance) && !string.IsNullOrEmpty(prometheus.Endpoint))
            {
                metricServer = new MetricPusher(endpoint: prometheus.Endpoint,
                                                job: "FileMonitoring", instance: prometheus.Instance);
                metricServer.Start();
            }

            logger.LogInformation("Приложение запущено");
            if (FailedMigrateDb.CountExceptions(() => MigrateDb(configuration, logger)))
            {
                //запуск планировщика задач
                var mainSheduler = new MainSheduler(monitoringConfig, logger, configuration, JobsGaugeException, metricServer);
                logger.LogInformation("Для завершения работы нажмите клавишу Q");
                var stopKey = new ConsoleKeyInfo();
                while (stopKey.Key != ConsoleKey.Q)
                {
                    stopKey = Console.ReadKey();
                }
                mainSheduler.Dispose();
                metricServer?.Stop();
            }
            logger.LogInformation("Приложение завершило работу");
        }
Exemple #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("Running");

            Console.WriteLine("running Test");
            //var server = new MetricServer(hostname: "localhost", port: 1245);
            //server.Start();
            var pusher = new MetricPusher(new MetricPusherOptions
            {
                Endpoint = "http://grafanaserver.westus2.cloudapp.azure.com:9091/metrics",
                Job      = "123"
            });

            pusher.Start();


            Stopwatch clockStop;

            Console.WriteLine("starting clock");

            while (true)
            {
                clockStop = Stopwatch.StartNew(); // time how long our compariosn takes.
                Console.WriteLine("random data loop");
                Random random1       = new Random();
                int    randomNumber1 = random1.Next(0, 100);
                randomProm1.Set(randomNumber1);

                Random random2       = new Random();
                int    randomNumber2 = random2.Next(0, 100);
                randomProm2.Set(randomNumber2);

                Random random3       = new Random();
                int    randomNumber3 = random3.Next(0, 100);
                randomProm3.Set(randomNumber3);

                Random random4       = new Random();
                int    randomNumber4 = random4.Next(0, 100);
                randomProm3.Set(randomNumber4);

                Random random5       = new Random();
                int    randomNumber5 = random5.Next(0, 100);
                randomProm3.Set(randomNumber5);

                while (clockStop.ElapsedMilliseconds / 1000 != 5)
                {
                }
                clockStop.Stop();
            }
        }
 // Start is called before the first frame update
 void Start()
 {
     LogProxy.WriteLine("start 1");
     pusher = new MetricPusher(endpoint: "http://192.168.2.5:9091/metrics", job: "deviced1", intervalMilliseconds: 5000);
     pusher.Start();
     LogProxy.WriteLine("start 2");
     ProcessedJobCount = Metrics
                         .CreateCounter("myapp_jobs_processed_total", "Number of processed jobs.",
                                        new CounterConfiguration
     {
         // Here you specify only the names of the labels.
         LabelNames = new[] { "DeviceId" }
     });
     ProcessedJobCount.Labels("android1");
 }
        public async Task StartAsync()
        {
            MetricPusher metricServer = new MetricPusher(_pushGatewayUrl, _options.Job, _options.Instance,
                                                         _intervalSeconds * 1000, new[]
            {
                new Tuple <string, string>("nethermind_group", _options.Group),
            });

            metricServer.Start();
            await Task.Factory.StartNew(() => _metricsUpdater.StartUpdating(), TaskCreationOptions.LongRunning);

            if (_logger.IsInfo)
            {
                _logger.Info($"Started monitoring for the group: {_options.Group}, instance: {_options.Instance}");
            }
        }
Exemple #18
0
        public override IMetricServer InitializeMetricServer()
        {
            // We add the username/password (even though it is not used) just to verify the extensibility logic works.
            var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"));
            var httpClient  = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue);

            var pusher = new MetricPusher(new MetricPusherOptions
            {
                Endpoint           = $"http://localhost:{TesterConstants.TesterPort}/metrics",
                Job                = "some_job",
                HttpClientProvider = () => httpClient
            });

            return(pusher);
        }
        public async Task PushTest()
        {
            var registry = Metrics.NewCustomRegistry();
            var factory  = Metrics.WithCustomRegistry(registry);

            var pusher = new MetricPusher(new MetricPusherOptions
            {
                Endpoint             = "http://127.0.0.1:9091/metrics",
                Registry             = registry,
                IntervalMilliseconds = 30,
                Job = "job"
            });

            pusher.Start();

            var counters = new List <Counter>();

            for (int i = 0; i < 1000; i++)
            {
                var counter = factory.CreateCounter($"Counter{i}", String.Empty);
                counters.Add(counter);
            }

            var ct      = new CancellationTokenSource();
            var incTask = Task.Run(async() =>
            {
                while (!ct.IsCancellationRequested)
                {
                    foreach (var counter in counters)
                    {
                        counter.Inc();
                    }

                    await Task.Delay(30);
                }
            });

            await Task.Delay(5000);

            ct.Cancel();
            await incTask;

            pusher.Stop();
        }
Exemple #20
0
        public void On(Event <LogEventData> evt)
        {
            var applicationName          = FormatTemplate(evt);
            var pushGatewayUrl           = PushgatewayUrl;
            var seqPushgatewayWorkerName = "pushgateway-testworker";
            var instanceName             = "default";

            var defaultPusher        = new MetricPusher(pushGatewayUrl, seqPushgatewayWorkerName, instanceName);
            IMetricPushServer server = new MetricPushServer(new IMetricPusher[]
            {
                defaultPusher
            });

            server.Start();
            var counter = Metrics.CreateCounter("webjobcounter", "helptext", new[] { "ApplicationName" });

            counter.Labels(applicationName).Inc();
            server.Stop();
        }
Exemple #21
0
        public void On(Event <LogEventData> evt)
        {
            var applicationName          = FormatTemplate(evt);
            var pushGatewayUrl           = "https://kmd-shareddev-monitoring.westeurope.cloudapp.azure.com/pushgateway";
            var seqPushgatewayWorkerName = "pushgateway-testworker";
            var instanceName             = "default";

            var defaultPusher        = new MetricPusher(pushGatewayUrl, seqPushgatewayWorkerName, instanceName);
            IMetricPushServer server = new MetricPushServer(new IMetricPusher[]
            {
                defaultPusher
            });

            server.Start();
            var counter = Metrics.CreateCounter("webjobcounter", "helptext", new[] { "ApplicationName" });

            counter.Labels(applicationName).Inc();
            server.Stop();
        }
Exemple #22
0
        public static void Main(string[] args)
        {
            var    stage = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT");
            string file;

            if (stage == "Development")
            {
                file = "nlog-dev.config";
            }
            else
            {
                file = "nlog.config";
            }

            var logger = NLogBuilder.ConfigureNLog(file).GetCurrentClassLogger();

            try
            {
                if (stage != "Development")
                {
                    var metricServer = new MetricPusher(
                        endpoint: "https://push.qaybe.de/metrics",
                        job: "stack_graphql");
                    metricServer.Start();
                }

                logger.Debug("init main");
                CreateHostBuilder(args)
                .Build()
                .Run();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Stopped program because of exception");
                throw;
            }
            finally
            {
                NLog.LogManager.Shutdown();
            }
        }
Exemple #23
0
 //starting server
 public void StartServer()
 {
     try
     {
         //if ServerMetricsIsON=1 in config file metric server starts
         if (ServerMetricsIsON)
         {
             mSrv = new MetricServer(port: ServerMetricsPort);
             logger.Debug("Start exporter");
             mSrv.Start();
         }
         //if PushGatewayIsON=1 in config file metric pusher starts
         if (PushGatewayIsON)
         {
             pusher = new MetricPusher(endpoint: PushGatewayURL, job: JobNameOfGatewayPusher);
             pusher.Start();
         }
     }
     catch (Exception e)
     {
         logger.Error(e.Message);
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Starting our Database Writing/Tests");

            //If we wants to run our tests locally we use this code

            //var server = new MetricServer(hostname: "localhost", port: 1235);
            //server.Start();

            // push to this address that is running the pushgateway.exe from the Prom website.  We need the job name and the endpoint here to be the values present in the prometheus.YML file.
            var pusher = new MetricPusher(new MetricPusherOptions
            {
                Endpoint = "http://grafanaserver.westus2.cloudapp.azure.com:9091/metrics", // replace this with your domain + port 9091; The pushgateway runes on port 9091
                Job      = "123"                                                           // this job name needs to equal the job name you put in the YML file to scrap localhost9091;
            });

            pusher.Start();

            // basic setting of values in prometheus.
            number1.Set(5.5);
            Console.WriteLine("Set a value to 5.5");
            setNum(number2, 10);
            Console.WriteLine("Set a value to 10");
            setNum(number2, 20);
            Console.WriteLine("Set a value to 20");


            // Ping a website every 10 seconds Example.
            checkGoogleUp();


            // Example of a More advanced pattern that should be used in conjution with Grafana.
            //If we have a scrap time of 30 seconds it makes sense to make our results get updated every 30 seconsd, so we graph the correct latest values.
            // This also ensures that we dont skip over any values. for example if we have:
            for (int i = 0; i < 100; i++)
            {
                setNum(number2, i);
            }
            // this for loop will run a hundred times but grafana/prometheus will only print the single final value of 100. This occurs as this code will run 100 times before being scrapped and read.
            //EG Grafana will wait 30 seconds due to user setting of a 30 second scrap time. Grafana now scraps after 30 sconds, sees the value of 100, add thats to the database and graphs it.
            // what needs to happen in this scenario is after each time setNum function completes the values need to be added to the database.

            // To solve this problem we create ab additional thread that runs every 30 seconds independant of the main thread. This way the scrapping time and the results set time are the same.
            // This means we end up with something that effectively functions like this but doesnt look like this.

            for (int i = 0; i < 100; i++)
            {
                setNum(number2, i);    // every 30 seconds we run this value and add it to the to database.
            }

            // is it also worth noting that you can use timers - code execution time to get an almost accurate synch between prometheus and your code. But after X amount of executions it will be slightly off.

            // To implement the above we do this this we do this :
            countUp(null, null);

            aTimer          = new System.Timers.Timer(10000);
            aTimer.Elapsed += new ElapsedEventHandler(countUp);

            // Set the Interval to 30 seconds (30000 milliseconds).
            aTimer.Interval = 30000;
            aTimer.Enabled  = true;

            Console.WriteLine("Every 30 seconds we are now running a function that increases the number added to our database");

            bool runTestforever = true;

            // by not ending out test we have a monitoring solution. As every 30 seconds we are running code and adding vlaues to our database with the seperate thread we made that runs on a timer.
            // we could also just add code inside this loop that isnt sensative to exact values and timing out with prometheus like getting the resposne code from google to see if its up
            while (runTestforever == true)
            {
                Thread.Sleep(5000);
                checkGoogleUp();
            }
        }