Skip to content

Commit

Permalink
Fix duration.sum.us value in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Nov 9, 2023
1 parent a352170 commit 4287823
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/Elastic.Apm/Model/DroppedSpanStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -49,7 +50,6 @@ double durationSumUs
/// </summary>
public Outcome Outcome { get; }


/// <summary>
/// Duration holds duration aggregations about the dropped span.
/// </summary>
Expand All @@ -63,13 +63,17 @@ internal class DroppedSpanDuration
public int Count { get; set; }

/// <summary>
/// Sum holds dimensions about the dropped span's duration.
/// Sum holds dimensions about the dropped span's duration.
/// </summary>
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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Apm/Model/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 6 additions & 6 deletions test/Elastic.Apm.Tests/DroppedSpansStatsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]
Expand Down Expand Up @@ -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");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion test/Elastic.Apm.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public void DroppedSpanStatsTest()
transaction.End();

var json = _payloadItemSerializer.Serialize(transaction);
json.Should().Contain("\"duration\":{\"count\":1,\"sum\":{\"us\":100.0}}");
json.Should().Contain("\"duration\":{\"count\":1,\"sum\":{\"us\":100}}");
}

/// <summary>
Expand Down

0 comments on commit 4287823

Please sign in to comment.