RSG.Metrics.Flush C# (CSharp) Method

Flush() public method

Flushes all queued metrics.
public Flush ( ) : void
return void
        public void Flush()
        {
            if (metricQueueIndex == 0)
            {
                return;
            }

            try
            {
                if (metricQueueIndex < batchSize)
                {
                    var metricsToEmit = new Metric[metricQueueIndex];

                    for (var i = 0; i < metricQueueIndex; i++)
                    {
                        metricsToEmit[i] = metricQueue[i];
                    }

                    emitter.Emit(properties, metricsToEmit);
                }
                else
                {
                    emitter.Emit(properties, metricQueue);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Exception occurred while attempting to flush metrics", ex);
            }
            finally
            {
                metricQueueIndex = 0;
            }
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            var httpService = new SimpleHttpService();
            var postUrl = "http://localhost:3000/emit";

            // Create the metrics with batching.
            var metrics = new Metrics(new HttpJsonPostEmitter(postUrl, httpService), 5);

            // Create some dummy metrics data
            metrics.SetProperty("Metrics entry", "test");
            for (var i = 0; i < 10; i++)
            {
                metrics.Entry("Test int entry", i);
            }
            metrics.SetProperty("Second property", "true");
            metrics.Event("An event occurred");
            metrics.Entry("Test string entry", "foo");

            // Flush the metrics before in case there are still queued metrics.
            metrics.Flush();

            Console.WriteLine("Sent metrics data to " + postUrl);
        }