Exemplo n.º 1
0
        public void start_timer_runs_the_code_in_the_using_block_plus_logs_a_timer_metric()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            int counter = 0;
            var metrics = new DogMetrics(contextMock.Object);

            using (metrics.StartTimer("mystat"))
            {
                counter++;
                Thread.Sleep(3000);
            }

            Assert.True(counter == 1,
                        "Expected the code in the using block to be executed, but it wasn\'t");
            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|histogram|"),
                        $@"Expected the stat type: ""|histogram|"" but couldn't find it in <{logs.First()}>");
            Assert.True(logs.First().Contains("|mystat|"),
                        $@"Expected the stat name: ""|mystat|"" but couldn't find it in <{logs.First()}>");
            Assert.InRange(GetTimeFromLog(logs.First()), 3000, 3005);
        }
Exemplo n.º 2
0
        public async Task RaiseMetricShouldBeSuccessful()
        {
            IMetricDestination    target = new DogMetrics(_testApiKey);
            HoundMetricCollection data   = GetTestMetrics();

            HoundResult eventResponse = await target.RaiseMetric(data);

            eventResponse.IsSuccess.ShouldBeTrue();
        }
Exemplo n.º 3
0
        public void if_the_given_func_throws_an_exception_then_so_does_time()
        {
            var metrics = new DogMetrics(Mock.Of <ILambdaLogger>());

            Assert.Throws <Exception>(() =>
            {
                metrics.Time <string>(() => throw new Exception("pow!"), "mystat");
            });
        }
Exemplo n.º 4
0
        public void it_contains_the_stat_name()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Gauge("mystat", 1);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|mystat|"),
                        $@"Expected the given stat name: ""|mystat|"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 5
0
        public void it_contains_the_monitoring_command_string()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Counter("mystat", 1);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("MONITORING"),
                        $@"Expected the command string: ""MONITORING"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 6
0
        public void it_contains_the_given_value(ServiceCheck checkValue, int expectedValue)
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Check("mystat", checkValue);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains($"|{expectedValue}|"),
                        $@"Expected the given stat value: ""|{expectedValue}|"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 7
0
        public void sample_rate_of_1_always_sends_it()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            Repeat(100, () =>
            {
                var metrics = new DogMetrics(contextMock.Object);
                metrics.Gauge("mystat", 1, 1D); //don't usually need to specify 1D
            });

            Assert.True(logs.Count == 100,
                        $"Expected all lines to come through but only had {logs.Count}");
        }
Exemplo n.º 8
0
        public void sample_rate_of_0_never_sends_it()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            Repeat(100, () =>
            {
                var metrics = new DogMetrics(contextMock.Object);
                metrics.Gauge("mystat", 1, 0D);
            });

            Assert.True(logs.Count == 0,
                        $"Expected no lines to come through but had {logs.Count}");
        }
Exemplo n.º 9
0
        public void adding_a_tag_appends_it_with_a_hash()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);
            var colour  = new StatTag("colour", "blue");

            metrics.Gauge("mystat", 1, 1D, colour);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|#colour:blue"),
                        $@"Expected the tag string: ""|#colour:blue"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 10
0
        public void a_tag_need_not_have_value()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics  = new DogMetrics(contextMock.Object);
            var isBlue   = new StatTag("isBlue");
            var isCircle = new StatTag("isCircle");

            metrics.Gauge("mystat", 1, 1D, isBlue, isCircle);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|#isBlue,isCircle"),
                        $@"Expected the tag string: ""|#isBlue,isCircle"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 11
0
        public void multiple_tags_are_separated_by_comma()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);
            var colour  = new StatTag("colour", "blue");
            var shape   = new StatTag("shape", "circle");

            metrics.Gauge("mystat", 1, 1D, colour, shape);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|#colour:blue,shape:circle"),
                        $@"Expected the tag string: ""|#colour:blue,shape:circle"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 12
0
        public void a_sample_rate_of_0_point_5_sends_it_roughly_half_the_time()
        {
            // You probably shouldn't use this for .Counter or .Check though
            // since there is no way to tell datadog it is sampled...

            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            Repeat(100, () =>
            {
                var metrics = new DogMetrics(contextMock.Object);
                metrics.Gauge("mystat", 1, 0.5D);
            });

            Assert.InRange(logs.Count, 40, 60);
        }
Exemplo n.º 13
0
        public void with_no_tags_it_still_includes_the_hash()
        {
            // I need to check if this is 'ok' by the integration
            // but for now just documenting the behaviour

            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Gauge("mystat", 1);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|#"),
                        $@"Expected the tag string: ""|#"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 14
0
        public void it_contains_the_current_unix_timestamp()
        {
            // Could this test fail when run on exactly the border of a second?

            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Gauge("mystat", 1);

            var currentUnixTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains($"|{currentUnixTime}|"),
                        $@"Expected the current unix timestamp: ""|{currentUnixTime}|"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 15
0
        public void timer_is_just_a_histogram_with_the_value_nothing_fancy()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Timer("mystat", 34D);

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|histogram|"),
                        $@"Expected the stat type: ""|histogram|"" but couldn't find it in <{logs.First()}>");
            Assert.True(logs.First().Contains("|mystat|"),
                        $@"Expected the stat name: ""|mystat|"" but couldn't find it in <{logs.First()}>");
            Assert.True(logs.First().Contains("|34|"),
                        $@"Expected the stat value: ""|34|"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 16
0
        public void decrement_is_a_counter_with_value_negative_1()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);

            metrics.Decrement("mystat");

            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|count|"),
                        $@"Expected the stat type: ""|count|"" but couldn't find it in <{logs.First()}>");
            Assert.True(logs.First().Contains("|mystat|"),
                        $@"Expected the stat name: ""|mystat|"" but couldn't find it in <{logs.First()}>");
            Assert.True(logs.First().Contains("|-1|"),
                        $@"Expected the stat value: ""|-1|"" but couldn't find it in <{logs.First()}>");
        }
Exemplo n.º 17
0
        public void time_runs_the_function_and_returns_its_result_plus_logs_a_timer_metric()
        {
            var logs        = new List <string>();
            var contextMock = new Mock <ILambdaLogger>(MockBehavior.Strict);

            contextMock.Setup(logger => logger.LogLine(It.IsAny <string>()))
            .Callback((string s) => logs.Add(s));

            var metrics = new DogMetrics(contextMock.Object);
            var result  = metrics.Time(() =>
            {
                Thread.Sleep(3000);
                return("zap");
            }, "mystat");

            Assert.True(result == "zap",
                        $@"Expected the result of the function ""zap"" but had <{result}>");
            Assert.True(logs.Count == 1);
            Assert.True(logs.First().Contains("|histogram|"),
                        $@"Expected the stat type: ""|histogram|"" but couldn't find it in <{logs.First()}>");
            Assert.True(logs.First().Contains("|mystat|"),
                        $@"Expected the stat name: ""|mystat|"" but couldn't find it in <{logs.First()}>");
            Assert.InRange(GetTimeFromLog(logs.First()), 3000, 3005);
        }