public void BuildSummaryMetric()
        {
            var timestamp = DateTime.UtcNow;
            var interval  = 33L;
            var value     = MetricSummaryValue.Create(10d, 64, 3, 15);

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

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

            var metric = metricBuilder.Build();

            Assert.AreEqual("metricname", metric.Name);
            Assert.AreEqual("summary", metric.Type);
            Assert.AreEqual(value, metric.Value);
            Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(timestamp), metric.Timestamp);
            Assert.AreEqual(interval, metric.IntervalMs);
            Assert.AreEqual(12, metric.Attributes["adsfasdf"]);
            Assert.AreEqual("attrValue", metric.Attributes["attrKey"]);
        }
        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);
        }
Exemple #3
0
        public void MetricBatchWithNoCommonElement()
        {
            var timestamp    = DateTime.UtcNow;
            var interval     = 125L;
            var gaugeValue   = 213d;
            var summaryValue = MetricSummaryValue.Create(10d, 64, 3, 15);

            var metricBatch = MetricBatchBuilder.Create()
                              .WithMetric(MetricBuilder.CreateSummaryMetric("SummaryMetric")
                                          .WithTimestamp(timestamp)
                                          .WithIntervalMs(interval)
                                          .WithValue(summaryValue)
                                          .Build())
                              .WithMetric(MetricBuilder.CreateGaugeMetric("GaugeMetric")
                                          .WithTimestamp(timestamp)
                                          .WithIntervalMs(interval)
                                          .WithValue(gaugeValue)
                                          .Build())
                              .Build();

            Assert.AreEqual(2, metricBatch.Metrics.Count, $"MetricBatch Metrics count - expected: 2, actual: {metricBatch.Metrics.Count}");
            Assert.IsNull(metricBatch.CommonProperties, $"MetricBatch CommonProperties - expected: null, actual: not null");
        }