public MetricsCollectorBuilder(IServiceCollection services)
        {
            _metricEndpoints = ImmutableArray.CreateBuilder <MetricEndpoint>();
            _metricEndpoints.Add(new MetricEndpoint("Local", new LocalMetricHandler()));
            _defaultTags = ImmutableDictionary.CreateBuilder <string, string>();
            _defaultTags.Add("host", NameTransformers.Sanitize(Environment.MachineName));

            Options  = new MetricsCollectorOptions();
            Services = services;
        }
Esempio n. 2
0
        public void SetUp()
        {
            _cancellationTokenSource = new CancellationTokenSource();

            var emptyOptions = new MetricsCollectorOptions
            {
                ExceptionHandler = ex => Console.WriteLine(ex),
                Endpoints        = Array.Empty <MetricEndpoint>(),
                DefaultTags      = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
                }.ToImmutableDictionary(),
                MetricsNamePrefix = "benchmark1",
                SnapshotInterval  = TimeSpan.FromSeconds(1),
                ThrowOnQueueFull  = false,
            };

            var httpOptions = new MetricsCollectorOptions
            {
                ExceptionHandler = ex => Console.WriteLine(ex),
                Endpoints        = new[]
                {
                    new MetricEndpoint("Benchmark", new SignalFxMetricHandler(new Uri("http://127.0.0.1/")))
                },
                DefaultTags = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
                }.ToImmutableDictionary(),
                MetricsNamePrefix = "benchmark1",
                SnapshotInterval  = TimeSpan.FromSeconds(1),
                ThrowOnQueueFull  = false,
            };

            var udpOptions = new MetricsCollectorOptions
            {
                ExceptionHandler = ex => Console.WriteLine(ex),
                Endpoints        = new[]
                {
                    new MetricEndpoint("Benchmark", new TestUdpMetricHandler(_cancellationTokenSource.Token)
                    {
                        MaxPayloadSize = 100
                    })
                },
                DefaultTags = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
                }.ToImmutableDictionary(),
                MetricsNamePrefix = "benchmark1",
                SnapshotInterval  = TimeSpan.FromSeconds(1),
                ThrowOnQueueFull  = false,
            };

            _emptyCollector = new MetricsCollector(emptyOptions);
            _httpCollector  = new MetricsCollector(httpOptions);
            _udpCollector   = new MetricsCollector(udpOptions);
        }
Esempio n. 3
0
        private static MetricsCollector SetupCollector()
        {
            var options = new BosunOptions()
            {
                MetricsNamePrefix = "present.",
                GetBosunUrl       = () => new Uri("http://192.168.59.103:8070/"),
                ThrowOnPostFail   = true,
                ReportingInterval = 5,
                PropertyToTagName = tag => tag.ToLowerInvariant(),
                DefaultTags       = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
                }
            };

            return(new MetricsCollector(options));
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            _collector = new MetricsCollector(new BosunOptions(ex => Handle(ex))
            {
                MetricsNamePrefix = "TestApp.",
                BosunUrl          = new System.Uri("http://192.168.1.5:8070"),
                PropertyToTagName = NameTransformers.CamelToLowerSnakeCase,
                ThrowOnPostFail   = true,
                DefaultTags       = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) },
                    { "client", "home" },
                }
            });

            _timer = _collector.CreateMetric <EventGauge>("GetWeather", "time taken", "measures time taken to get weather from api");

            var dispatcherTimer = new Timer();

            dispatcherTimer.Interval = 5000;
            dispatcherTimer.Start();
            dispatcherTimer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

            Console.ReadLine();
        }
Esempio n. 5
0
        static async Task Main(string[] args)
        {
            //BenchmarkRunner.Run<Benchmark>();
            //var x = 1;
            //if (x == 1)
            //{
            //    return;
            //}

            s_cancellationTokenSource = new CancellationTokenSource();

            Debug.AutoFlush = true;

            const string LocalEndpointKey = "Local";

            var localHandler = new LocalMetricHandler();
            var options      = new MetricsCollectorOptions(exception =>
            {
                Console.WriteLine("Hey, there was an exception.");
                Console.WriteLine(exception);
                foreach (var key in exception.Data.Keys)
                {
                    Console.WriteLine($"{key}={exception.Data[key]}");
                }
            })
            {
                Endpoints = new MetricEndpoint[] {
                    //new MetricEndpoint(LocalEndpointKey, localHandler),
                    //new MetricEndpoint("Test UDP", new TestUdpMetricHandler(s_cancellationTokenSource.Token) { MaxPayloadSize = 320 }),
                    //new MetricEndpoint("Bosun (no URL)", new BosunMetricHandler(null)),
                    //new MetricEndpoint("Bosun", new BosunMetricHandler(new Uri("http://devbosun.ds.stackexchange.com/"))),
                    //new MetricEndpoint("SignalFx Agent", new SignalFxMetricHandler(new Uri("http://sfxgateway.signalfx.svc.ny-intkube.k8s.ds.stackexchange.com:18080"))),
                    new MetricEndpoint("SignalFx StatsD Agent", new SignalFxMetricHandler(new Uri("udp://[::1]:8125"))
                    {
                    }),
                    //new MetricEndpoint("SignalFx Cloud", new SignalFxMetricHandler(new Uri("https://ingest.us1.signalfx.com/"), "{API_KEY}")),
                    //new MetricEndpoint("SignalFx Cloud (no URL)", new SignalFxMetricHandler(null, "{API_KEY}")),
                },
                MetricsNamePrefix = "bosun.reporter.",
                ThrowOnPostFail   = true,
                ThrowOnQueueFull  = false,
                SnapshotInterval  = TimeSpan.FromSeconds(10),
                PropertyToTagName = NameTransformers.CamelToLowerSnakeCase,
                TagValueConverter = (name, value) => name == "converted" ? value.ToLowerInvariant() : value,
                DefaultTags       = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
                }
            };

            var collector = new MetricsCollector(options);

            foreach (var endpoint in collector.Endpoints)
            {
                switch (endpoint.Handler)
                {
                case BosunMetricHandler bosunHandler:
                    Console.WriteLine($"{endpoint.Name}: {bosunHandler.BaseUri?.AbsoluteUri ?? "null"}");
                    break;

                case SignalFxMetricHandler signalFxHandler:
                    Console.WriteLine($"{endpoint.Name}: {signalFxHandler.BaseUri?.AbsoluteUri ?? "null"}");
                    break;
                }
            }

            collector.BeforeSerialization += () => Console.WriteLine("BosunReporter: Running metrics snapshot.");
            collector.AfterSerialization  += info => Console.WriteLine($"BosunReporter: Metric Snapshot wrote {info.Count} metrics ({info.BytesWritten} bytes) to {info.Endpoint} in {info.Duration.TotalMilliseconds.ToString("0.##")}ms");
            collector.AfterSend           += info =>
            {
                if (info.Endpoint == LocalEndpointKey)
                {
                    foreach (var reading in localHandler.GetReadings())
                    {
                        Console.WriteLine($"{reading.Name}{reading.Suffix}@{reading.Timestamp:s} {reading.Value}");
                    }
                }
                Console.WriteLine($"BosunReporter: Payload {info.PayloadType} - {info.BytesWritten} bytes posted to endpoint {info.Endpoint} in {info.Duration.TotalMilliseconds.ToString("0.##")}ms ({(info.Successful ? "SUCCESS" : "FAILED")})");
            };

            collector.BindMetric("my_counter", "increments", typeof(TestCounter));
            var counter = collector.GetMetric <TestCounter>("my_counter", "increments", "This is meaningless.");

            counter.Increment();
            counter.Increment();

            var gauge = collector.CreateMetric("gauge", "watts", "Some description of a gauge.", new TestAggregateGauge("1"));

            if (gauge != collector.GetMetric("gauge", "watts", null, new TestAggregateGauge("1")))
            {
                throw new Exception("WAT?");
            }

            try
            {
                collector.CreateMetric("gauge", "watts", "Some description of a gauge.", new TestAggregateGauge("1"));
            }
            catch (Exception)
            {
                goto SKIP_EXCEPTION;
            }

            throw new Exception("CreateMetric should have failed for duplicate metric.");

SKIP_EXCEPTION:

            var gauge2 = collector.GetMetric <CountGauge>("gauge2", "newtons", "Number of newtons currently applied.");

            for (var i = 0; i < 6; i++)
            {
                new Thread(Run).Start(new Tuple <AggregateGauge, AggregateGauge, int>(gauge, gauge2, i));
            }

            var enumCounter = collector.GetMetricGroup <SomeEnum, EnumCounter>("some_enum", "things", "Some of something");

            enumCounter.PopulateFromEnum();

            Type   t;
            string u;

            if (collector.TryGetMetricInfo("gauge2", out t, out u))
            {
                Console.WriteLine(t);
                Console.WriteLine(u);
            }
            else
            {
                Console.WriteLine("NOOOOO!!!!!");
            }

            var si       = 0;
            var snapshot = collector.GetMetric("my_snapshot", "snappys", "Snap snap snap.", new SnapshotGauge(() => ++ si % 5));

            var group = collector.GetMetricGroup <string, TestGroupGauge>("test_group", "tests", "These gauges are for testing.");

            group.Add("low").Description    = "Low testing.";
            group.Add("medium").Description = "Medium testing.";
            group.Add("high").Description   = "High testing.";
            var sampler    = collector.GetMetric("sampler", "french fries", "Collect them all.", new SamplingGauge());
            var eventGauge = collector.GetMetric("event", "count", "How many last time.", new EventGauge());
            var converted  = collector.CreateMetric("convert_test", "units", "Checking to see if the tag value converter works.", new ConvertedTagsTestCounter("ThingsAndStuff"));
            var noHost     = collector.CreateMetric <ExcludeHostCounter>("no_host", "units", "Shouldn't have a host tag.");

            var externalCounter = collector.GetMetricGroup <SomeEnum, TestExternalCounter>("external.test", "units", "Should aggregate externally.");

            externalCounter.PopulateFromEnum();
            //            var externalNoTags = collector.CreateMetric<ExternalNoTagsCounter>("external.no_tags", "units", "Shouldn't have any tags except relay.");

            var lotsOfCounters = new List <Counter>();

            for (var i = 0; i < 400; i++)
            {
                lotsOfCounters.Add(collector.GetMetric("counter_" + i, "counts", "Testing lots of counters", new Counter()));
            }

            var sai    = 0;
            var random = new Random();

            s_samplerTask = Task.Run(async() =>
            {
                while (true)
                {
                    await Task.Delay(100);

                    sampler.Record(++sai % 35);
                    eventGauge.Record(sai % 35);
                    group["low"].Record(random.Next(0, 10));
                    group["medium"].Record(random.Next(10, 20));
                    group["high"].Record(random.Next(20, 30));

                    enumCounter[SomeEnum.One].Increment();
                    enumCounter[SomeEnum.Two].Increment(2);
                    enumCounter[SomeEnum.Three].Increment(3);
                    enumCounter[SomeEnum.Four].Increment(4);

                    externalCounter[SomeEnum.One].Increment();
                    if (sai % 2 == 0)
                    {
                        externalCounter[SomeEnum.Two].Increment();
                    }
                    if (sai % 3 == 0)
                    {
                        externalCounter[SomeEnum.Three].Increment();
                    }
                    if (sai % 4 == 0)
                    {
                        externalCounter[SomeEnum.Four].Increment();
                    }

                    foreach (var lotsOfCounter in lotsOfCounters)
                    {
                        lotsOfCounter.Increment(random.Next(0, 5));
                    }

                    //                    externalNoTags.Increment();

                    converted.Increment();
                    noHost.Increment();

                    if (sai == 1000 || s_cancellationTokenSource.IsCancellationRequested)
                    {
                        collector.Shutdown();
                        break;
                    }
                }
            });

            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
            {
                s_cancellationTokenSource.Cancel();
            };

            try
            {
                await s_samplerTask;
            }
            catch (TaskCanceledException)
            {
                // meh, ignore
            }
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
            Debug.AutoFlush = true;

            Func <Uri> getUrl = () =>
            {
                return(new Uri("http://192.168.99.100:8070/"));
//                return new Uri("http://127.0.0.1:1337/");
            };

            // for testing minimum event threshold
//            AggregateGauge.GetDefaultMinimumEvents = () => 1306000;

            var options = new BosunOptions()
            {
                MetricsNamePrefix = "bret.",
                GetBosunUrl       = getUrl,
                ThrowOnPostFail   = true,
                ReportingInterval = 5,
                PropertyToTagName = NameTransformers.CamelToLowerSnakeCase,
                TagValueConverter = (name, value) => name == "converted" ? value.ToLowerInvariant() : value,
                DefaultTags       = new Dictionary <string, string> {
                    { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
                }
            };
            var collector = new MetricsCollector(options);

            collector.OnBackgroundException += exception =>
            {
                Console.WriteLine("Hey, there was an exception.");
                Console.WriteLine(exception);
            };

            collector.BeforeSerialization += () => Console.WriteLine("BosunReporter: Running metrics snapshot.");
            collector.AfterSerialization  += info => Console.WriteLine($"BosunReporter: Metric Snapshot took {info.MillisecondsDuration.ToString("0.##")}ms");
            collector.AfterPost           += info => Console.WriteLine($"BosunReporter: {info.Count} metrics posted to Bosun in {info.MillisecondsDuration.ToString("0.##")}ms ({(info.Successful ? "SUCCESS" : "FAILED")})");

            collector.BindMetric("my_counter", "increments", typeof(TestCounter));
            var counter = collector.GetMetric <TestCounter>("my_counter", "increments", "This is meaningless.");

            counter.Increment();
            counter.Increment();

            var gauge = collector.CreateMetric("gauge", "watts", "Some description of a gauge.", new TestAggregateGauge("1"));

            if (gauge != collector.GetMetric("gauge", "watts", null, new TestAggregateGauge("1")))
            {
                throw new Exception("WAT?");
            }

            try
            {
                collector.CreateMetric("gauge", "watts", "Some description of a gauge.", new TestAggregateGauge("1"));
            }
            catch (Exception)
            {
                goto SKIP_EXCEPTION;
            }

            throw new Exception("CreateMetric should have failed for duplicate metric.");

SKIP_EXCEPTION:

            var gauge2 = collector.GetMetric <AggregateGauge>("gauge2", "newtons", "Number of newtons currently applied.");

            for (var i = 0; i < 6; i++)
            {
                new Thread(Run).Start(new Tuple <AggregateGauge, AggregateGauge, int>(gauge, gauge2, i));
            }

            var enumCounter = collector.GetMetricGroup <SomeEnum, EnumCounter>("some_enum", "things", "Some of something");

            enumCounter.PopulateFromEnum();

            Type   t;
            string u;

            if (collector.TryGetMetricInfo("gauge2", out t, out u))
            {
                Console.WriteLine(t);
                Console.WriteLine(u);
            }
            else
            {
                Console.WriteLine("NOOOOO!!!!!");
            }

            var si       = 0;
            var snapshot = collector.GetMetric("my_snapshot", "snappys", "Snap snap snap.", new SnapshotGauge(() => ++ si % 5));

            var group = collector.GetMetricGroup <string, TestGroupGauge>("test_group", "tests", "These gauges are for testing.");

            group.Add("low").Description    = "Low testing.";
            group.Add("medium").Description = "Medium testing.";
            group.Add("high").Description   = "High testing.";
            var sampler    = collector.GetMetric("sampler", "french fries", "Collect them all.", new SamplingGauge());
            var eventGauge = collector.GetMetric("event", "count", "How many last time.", new EventGauge());
            var converted  = collector.CreateMetric("convert_test", "units", "Checking to see if the tag value converter works.", new ConvertedTagsTestCounter("ThingsAndStuff"));
            var noHost     = collector.CreateMetric <ExcludeHostCounter>("no_host", "units", "Shouldn't have a host tag.");

            var externalCounter = collector.GetMetricGroup <SomeEnum, TestExternalCounter>("external.test", "units", "Should aggregate externally.");

            externalCounter.PopulateFromEnum();
//            var externalNoTags = collector.CreateMetric<ExternalNoTagsCounter>("external.no_tags", "units", "Shouldn't have any tags except relay.");

            var sai    = 0;
            var random = new Random();

            _samplerTimer = new Timer(o =>
            {
                sampler.Record(++sai % 35);
                eventGauge.Record(sai % 35);
                group["low"].Record(random.Next(0, 10));
                group["medium"].Record(random.Next(10, 20));
                group["high"].Record(random.Next(20, 30));

                enumCounter[SomeEnum.One].Increment();
                enumCounter[SomeEnum.Two].Increment(2);
                enumCounter[SomeEnum.Three].Increment(3);
                enumCounter[SomeEnum.Four].Increment(4);

                externalCounter[SomeEnum.One].Increment();
                if (sai % 2 == 0)
                {
                    externalCounter[SomeEnum.Two].Increment();
                }
                if (sai % 3 == 0)
                {
                    externalCounter[SomeEnum.Three].Increment();
                }
                if (sai % 4 == 0)
                {
                    externalCounter[SomeEnum.Four].Increment();
                }

//                    externalNoTags.Increment();

                converted.Increment();
                noHost.Increment();

                if (sai == 40)
                {
                    collector.Shutdown();
                    Environment.Exit(0);
                }
            }, null, 1000, 1000);

            Thread.Sleep(4000);
            collector.UpdateDefaultTags(new Dictionary <string, string> {
                { "host", NameTransformers.Sanitize(Environment.MachineName.ToLower()) }
            });
//            Thread.Sleep(4000);
//            collector.UpdateDefaultTags(new Dictionary<string, string>() { { "host", "test_env" } });
        }