예제 #1
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" } });
        }
예제 #2
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/");
            };

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

            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 += () => Debug.WriteLine("BosunReporter: Running metrics snapshot.");
            collector.AfterSerialization += info => Debug.WriteLine($"BosunReporter: Metric Snapshot took {info.MillisecondsDuration.ToString("0.##")}ms");
            collector.AfterPost += info => Debug.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 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);

                    converted.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" } });
        }