public void BuildCountMetric()
        {
            var timestamp = DateTime.UtcNow;
            var interval  = 33L;
            var value     = 22;

            var attributes = new Dictionary <string, object>
            {
                { "attrKey", "attrValue" }
            };

            var metricBuilder = MetricBuilder.CreateCountMetric("metricname")
                                .WithValue(value)
                                .WithTimestamp(timestamp)
                                .WithIntervalMs(interval)
                                .WithAttribute("adsfasdf", 12)
                                .WithAttributes(attributes);

            var metric = metricBuilder.Build();

            Assert.AreEqual("metricname", metric.Name);
            Assert.AreEqual("count", metric.Type);
            Assert.AreEqual(value, metric.MetricValue);
            Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(timestamp), metric.Timestamp);
            Assert.AreEqual(interval, metric.IntervalMs);
            Assert.AreEqual("attrValue", metric.Attributes["attrKey"]);
        }
Example #2
0
        public void MetricBatchAllowsCommonAndSpecificSameNamedFields()
        {
            var commonTimestamp = DateTime.UtcNow;
            var commonInterval  = 125L;

            var metricTimestamp = DateTime.UtcNow + TimeSpan.FromSeconds(60);
            var metricInterval  = 312L;

            var countValue = 88d;

            var metricBatch = MetricBatchBuilder.Create()
                              .WithTimestamp(commonTimestamp)
                              .WithIntervalMs(commonInterval)
                              .WithAttribute("Attr1Key", "comAttr1Value")
                              .WithMetric(MetricBuilder.CreateCountMetric("CountMetric")
                                          .WithTimestamp(metricTimestamp)
                                          .WithIntervalMs(metricInterval)
                                          .WithAttribute("Attr1Key", "metAttr1Value")
                                          .WithValue(countValue)
                                          .Build())
                              .Build();

            Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(commonTimestamp), metricBatch.CommonProperties.Timestamp, $"MetricBatch CommonProperties [Timestamp] - expected: {commonTimestamp}, actual: {metricBatch.CommonProperties.Timestamp}");
            Assert.AreEqual(commonInterval, metricBatch.CommonProperties.IntervalMs, $"MetricBatch CommonProperties [IntervalMs] - expected: {commonInterval}, actual: {metricBatch.CommonProperties.IntervalMs}");
            Assert.AreEqual("comAttr1Value", metricBatch.CommonProperties.Attributes["Attr1Key"], $"MetricBatch CommonProperties Attributes value - expected: comAttr1Value, actual: {metricBatch.CommonProperties.Attributes["Attr1Key"]}");

            Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(metricTimestamp), metricBatch.Metrics[0].Timestamp, $"MetricBatch Metric [Timestamp] - expected: {DateTimeExtensions.ToUnixTimeMilliseconds(metricTimestamp)}, actual: {metricBatch.Metrics[0].Timestamp}");
            Assert.AreEqual(metricInterval, metricBatch.Metrics[0].IntervalMs, $"MetricBatch Metric [IntervalMs] - expected: {metricInterval}, actual: {metricBatch.Metrics[0].IntervalMs}");
            Assert.AreEqual("metAttr1Value", metricBatch.Metrics[0].Attributes["Attr1Key"], $"MetricBatch Metric Attributes value - expected: metAttr1Value, actual: {metricBatch.Metrics[0].Attributes["Attr1Key"]}");
        }
Example #3
0
        public void MetricBatchWithCommonPropertiesAndMetrics()
        {
            var timestamp   = DateTime.UtcNow;
            var interval    = 125L;
            var commonAttrs = new Dictionary <string, object>()
            {
                { "attr1Key", "attr1Value" }
            };
            var countValue = 88d;
            var gaugeValue = 213d;

            var metricBatch = MetricBatchBuilder.Create()
                              .WithTimestamp(timestamp)
                              .WithIntervalMs(interval)
                              .WithAttributes(commonAttrs)
                              .WithMetric(MetricBuilder.CreateCountMetric("CountMetric")
                                          .WithValue(countValue)
                                          .Build())
                              .WithMetric(MetricBuilder.CreateGaugeMetric("GaugeMetric")
                                          .WithValue(gaugeValue)
                                          .Build())
                              .Build();

            Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(timestamp), metricBatch.CommonProperties.Timestamp, $"MetricBatch CommonProperties [Timestamp] - expected: {timestamp}, actual: {metricBatch.CommonProperties.Timestamp}");
            Assert.AreEqual(interval, metricBatch.CommonProperties.IntervalMs, $"MetricBatch CommonProperties [IntervalMs] - expected: {interval}, actual: {metricBatch.CommonProperties.IntervalMs}");

            Assert.AreEqual(2, metricBatch.Metrics.Count, $"MetricBatch Metrics count - expected: 2, actual: {metricBatch.Metrics.Count}");
            Assert.AreEqual("count", metricBatch.Metrics[0].Type, $"MetricBatch Metrics[0].Type - expected: count, actual: {metricBatch.Metrics[0].Type}");
            Assert.AreEqual("gauge", metricBatch.Metrics[1].Type, $"MetricBatch Metrics[1].Type - expected: gauge, actual: {metricBatch.Metrics[1].Type}");
        }
        public void ToJson_NonEmptyMetricBatch()
        {
            // Arrange
            var metricBatch = MetricBatchBuilder.Create()
                              .WithIntervalMs(interval)
                              .WithTimestamp(timestamp)
                              .WithMetric(MetricBuilder.CreateCountMetric("metric1")
                                          .WithValue(countValue)
                                          .WithAttributes(CustomAttributes)
                                          .Build())
                              .WithMetric(MetricBuilder.CreateSummaryMetric("metric2")
                                          .WithValue(summaryValue)
                                          .Build())

                              .Build();

            // Act
            var jsonString = metricBatch.ToJson();

            // Assert
            var resultMetricBatches = TestHelpers.DeserializeArray(jsonString);

            TestHelpers.AssertForCollectionLength(resultMetricBatches, 1);

            // CountMetric
            var resultMetricBatch = resultMetricBatches.First();
            var resultCommonProps = TestHelpers.DeserializeObject(resultMetricBatch["common"]);

            TestHelpers.AssertForAttribValue(resultCommonProps, "timestamp", timestampL);
            TestHelpers.AssertForAttribValue(resultCommonProps, "interval.ms", interval);

            var resultMetrics = TestHelpers.DeserializeArray(resultMetricBatch["metrics"]);

            TestHelpers.AssertForCollectionLength(resultMetrics, 2);

            var countMetric = resultMetrics.FirstOrDefault();

            TestHelpers.AssertForAttribCount(countMetric, 4);

            TestHelpers.AssertForAttribValue(countMetric, "name", "metric1");
            TestHelpers.AssertForAttribValue(countMetric, "type", "count");
            TestHelpers.AssertForAttribValue(countMetric, "value", countValue);

            var countMetricAttribs = TestHelpers.DeserializeObject(countMetric["attributes"]);

            TestHelpers.AssertForAttribCount(countMetricAttribs, 1);
            TestHelpers.AssertForAttribValue(countMetricAttribs, "attr1Key", "attr1Value");

            // SummaryMetric
            var summaryMetric = resultMetrics[1];

            TestHelpers.AssertForAttribCount(summaryMetric, 3);

            TestHelpers.AssertForAttribValue(summaryMetric, "name", "metric2");
            TestHelpers.AssertForAttribValue(summaryMetric, "type", "summary");
            TestHelpers.AssertForAttribValue(summaryMetric, "value", summaryValue);
        }
 public void ThrowExceptionIfNullName()
 {
     Assert.Throws <ArgumentNullException>(new TestDelegate(() => MetricBuilder.CreateCountMetric(null)));
 }