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();
        }
        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();
        }
示例#3
0
 //stopping server
 public void StopServer()
 {
     if (ServerMetricsIsON)
     {
         mSrv.Stop();
     }
     if (PushGatewayIsON)
     {
         pusher.Stop();
     }
 }
        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();
        }
示例#5
0
 private void OnShutdown()
 {
     _metricPusher?.Stop();
 }