From d78b3e16323b609682f63d3347fa2258afaf93c7 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Thu, 9 Nov 2023 13:09:53 +0000 Subject: [PATCH] Fix duration.sum.us value in JSON --- src/Elastic.Apm/Model/DroppedSpanStats.cs | 12 ++++++++---- src/Elastic.Apm/Model/Transaction.cs | 2 +- test/Elastic.Apm.Tests/DroppedSpansStatsTests.cs | 12 ++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Elastic.Apm/Model/DroppedSpanStats.cs b/src/Elastic.Apm/Model/DroppedSpanStats.cs index 2254a6f78d..63121a0bef 100644 --- a/src/Elastic.Apm/Model/DroppedSpanStats.cs +++ b/src/Elastic.Apm/Model/DroppedSpanStats.cs @@ -3,6 +3,7 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information +using System; using Elastic.Apm.Api; using Elastic.Apm.Libraries.Newtonsoft.Json; @@ -18,7 +19,7 @@ public DroppedSpanStats(string serviceTargetType, string serviceTargetName, stri double durationSumUs ) { - Duration = new DroppedSpanDuration { Count = 1, Sum = new DroppedSpanDuration.DroppedSpanDurationSum { Us = durationSumUs } }; + Duration = new DroppedSpanDuration { Count = 1, Sum = new DroppedSpanDuration.DroppedSpanDurationSum { UsRaw = durationSumUs } }; ServiceTargetType = serviceTargetType; ServiceTargetName = serviceTargetName; DestinationServiceResource = destinationServiceResource; @@ -49,7 +50,6 @@ double durationSumUs /// public Outcome Outcome { get; } - /// /// Duration holds duration aggregations about the dropped span. /// @@ -63,13 +63,17 @@ internal class DroppedSpanDuration public int Count { get; set; } /// - /// Sum holds dimensions about the dropped span's duration. + /// Sum holds dimensions about the dropped span's duration. /// public DroppedSpanDurationSum Sum { get; set; } internal class DroppedSpanDurationSum { - public double Us { get; set; } + [JsonIgnore] + public double UsRaw { get; set; } + + // As `duration.sum.us` is an integer in the intake API we round during serialization. + public int Us => Convert.ToInt32(UsRaw); } } } diff --git a/src/Elastic.Apm/Model/Transaction.cs b/src/Elastic.Apm/Model/Transaction.cs index c1f401d59f..c80f962e21 100644 --- a/src/Elastic.Apm/Model/Transaction.cs +++ b/src/Elastic.Apm/Model/Transaction.cs @@ -575,7 +575,7 @@ double duration new DroppedSpanStats.DroppedSpanDuration { Sum = new DroppedSpanStats.DroppedSpanDuration.DroppedSpanDurationSum() }; item.Duration.Count++; - item.Duration.Sum.Us += duration; + item.Duration.Sum.UsRaw += duration; } else { diff --git a/test/Elastic.Apm.Tests/DroppedSpansStatsTests.cs b/test/Elastic.Apm.Tests/DroppedSpansStatsTests.cs index e01434844d..e0cd2453e3 100644 --- a/test/Elastic.Apm.Tests/DroppedSpansStatsTests.cs +++ b/test/Elastic.Apm.Tests/DroppedSpansStatsTests.cs @@ -60,7 +60,7 @@ public void SingleDroppedSpanTest() //This span will be dropped var span1 = transaction.StartSpan("foo", "bar", isExitSpan: true); span1.Context.Http = new Http { Method = "GET", StatusCode = 200, Url = "https://foo.bar" }; - span1.Duration = 100; + span1.Duration = 100.001; span1.End(); transaction.End(); @@ -75,7 +75,7 @@ public void SingleDroppedSpanTest() payloadSender.FirstTransaction.DroppedSpanStats.First().ServiceTargetType.Should().Be("bar"); payloadSender.FirstTransaction.DroppedSpanStats.First().Outcome.Should().Be(Outcome.Success); payloadSender.FirstTransaction.DroppedSpanStats.First().Duration.Count.Should().Be(1); - payloadSender.FirstTransaction.DroppedSpanStats.First().Duration.Sum.Us.Should().Be(100); + payloadSender.FirstTransaction.DroppedSpanStats.First().Duration.Sum.Us.Should().Be(100); // This should be rounded and will be sent in the JSON } [Fact] @@ -130,22 +130,22 @@ public void MultipleDroppedSpanTest() payloadSender.FirstTransaction.DroppedSpanStats.Should() .Contain(n => n.Outcome == Outcome.Success - && n.Duration.Count == 2 && Math.Abs(n.Duration.Sum.Us - 250) < 1 && n.DestinationServiceResource == "foo.bar:443" + && n.Duration.Count == 2 && Math.Abs(n.Duration.Sum.UsRaw - 250) < 1 && n.DestinationServiceResource == "foo.bar:443" && n.ServiceTargetName == "foo.bar:443" && n.ServiceTargetType == "bar"); payloadSender.FirstTransaction.DroppedSpanStats.Should() .Contain(n => n.Outcome == Outcome.Failure - && n.Duration.Count == 1 && Math.Abs(n.Duration.Sum.Us - 50) < 1 && n.DestinationServiceResource == "foo.bar:443" + && n.Duration.Count == 1 && Math.Abs(n.Duration.Sum.UsRaw - 50) < 1 && n.DestinationServiceResource == "foo.bar:443" && n.ServiceTargetName == "foo.bar:443" && n.ServiceTargetType == "bar"); payloadSender.FirstTransaction.DroppedSpanStats.Should() .Contain(n => n.Outcome == Outcome.Success - && n.Duration.Count == 1 && Math.Abs(n.Duration.Sum.Us - 15) < 1 && n.DestinationServiceResource == "foo2.bar:443" + && n.Duration.Count == 1 && Math.Abs(n.Duration.Sum.UsRaw - 15) < 1 && n.DestinationServiceResource == "foo2.bar:443" && n.ServiceTargetName == "foo2.bar:443" && n.ServiceTargetType == "bar"); payloadSender.FirstTransaction.DroppedSpanStats.Should() .Contain(n => n.Outcome == Outcome.Success - && n.Duration.Count == 50 && Math.Abs(n.Duration.Sum.Us - 50 * 50) < 1 && n.DestinationServiceResource == "mysql"); + && n.Duration.Count == 50 && Math.Abs(n.Duration.Sum.UsRaw - 50 * 50) < 1 && n.DestinationServiceResource == "mysql"); } ///