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"]}"); }
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 MetricBatchWithCommonTimestampAndNoMetrics() { var timestamp = DateTime.UtcNow; var metricBatch = MetricBatchBuilder.Create() .WithTimestamp(timestamp) .Build(); Assert.AreEqual(DateTimeExtensions.ToUnixTimeMilliseconds(timestamp), metricBatch.CommonProperties.Timestamp, $"MetricBatch CommonProperties [Timestamp] - expected: {timestamp}, actual: {metricBatch.CommonProperties.Timestamp}"); Assert.IsNull(metricBatch.Metrics, $"MetricBatch Metrics - expected: null, actual: not null"); }
public void ToJson_EmptyMetricBatch() { // Arrange var metricBatch = MetricBatchBuilder.Create().WithTimestamp(timestamp) .Build(); // Act var jsonString = metricBatch.ToJson(); //Assert var resultMetricBatch = TestHelpers.DeserializeArrayFirstOrDefault(jsonString); var resultCommonProps = TestHelpers.DeserializeObject(resultMetricBatch["common"]); TestHelpers.AssertForAttribValue(resultCommonProps, "timestamp", timestampL); }
public void SendAnEmptyMetricBatch() { var spanBatch = MetricBatchBuilder.Create() .Build(); var dataSender = new MetricDataSender(new TelemetryConfiguration().WithApiKey("123456")); dataSender.WithHttpHandlerImpl((serializedJson) => { var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); return(Task.FromResult(response)); }); var response = dataSender.SendDataAsync(spanBatch).Result; Assert.AreEqual(NewRelicResponseStatus.DidNotSend_NoData, response.ResponseStatus); }
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"); }