Skip to content

Commit

Permalink
Fix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Dec 2, 2024
1 parent 5129f12 commit 07b8343
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ internal PayloadItemSerializer() =>

public JsonTypeInfo GetTypeInfo(Type type) => Settings.TypeInfoResolver!.GetTypeInfo(type, Settings);

public void Serialize(object item, StreamWriter writer) =>
public void Serialize(object item, StreamWriter writer)
{
writer.Flush(); // ensure the base stream is "up-to-date" before we attempt to serialize into it
JsonSerializer.Serialize(writer.BaseStream, item, item.GetType(), Settings);
}

/// <summary>
/// Deserializes an instance of <typeparamref name="T"/> from JSON
Expand Down
12 changes: 5 additions & 7 deletions test/Elastic.Apm.Tests/ActivityIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ public void MultipleTransactionInOneActivity()
}

#if NET
/// <summary>
/// Makes sure that transactions on the same Activity are part of the same trace.
/// </summary>
[Fact]
public async Task ActivityRespectsSampling()
{
Expand All @@ -179,27 +176,28 @@ public async Task ActivityRespectsSampling()

Activity.Current = null;
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
var source = new ActivitySource(GetType().FullName, "1.0.0");

using var source = new ActivitySource(GetType().FullName, "1.0.0");

var payloadSender = new MockPayloadSender();
var config = new MockConfiguration(
transactionSampleRate: rate.ToString("N2")

);

using var components = new TestAgentComponents(
apmServerInfo: MockApmServerInfo.Version716,
configuration: config,
payloadSender: payloadSender

);

using var agent = new ApmAgent(components);
for (var i = 0; i < count; i++)
{
using var transaction = source.StartActivity($"Trace {i}");
using var span = new Activity("UnitTestActivity").Start();
await Task.Delay(1);
}
//Activity.Current.Should().BeNull();

payloadSender.WaitForTransactions(count: count);

var sampled = payloadSender.Transactions.Where(t => t.IsSampled).ToArray();
Expand Down
41 changes: 36 additions & 5 deletions test/startuphook/Elastic.Apm.StartupHook.Tests/StartupHookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Transaction(st
apmServer.RunInBackground(port);
var waitHandle = new ManualResetEvent(false);

var error = string.Empty;

apmServer.OnReceive += o =>
{
if (o is TransactionDto)
waitHandle.Set();

else if (o is string s) // may occur if there is an error
{
waitHandle.Set();
error = s;
}
};

using (var sampleApp = new SampleApplication())
Expand All @@ -86,8 +94,9 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Transaction(st
var response = await client.GetAsync(uri);

response.IsSuccessStatusCode.Should().BeTrue();
waitHandle.WaitOne(TimeSpan.FromMinutes(1));

waitHandle.WaitOne(TimeSpan.FromMinutes(2));
error.Should().BeEmpty();
apmServer.ReceivedData.Transactions.Should().HaveCount(1);

var transaction = apmServer.ReceivedData.Transactions.First();
Expand All @@ -108,12 +117,22 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Error(string t
var transactionWaitHandle = new ManualResetEvent(false);
var errorWaitHandle = new ManualResetEvent(false);

var serverError = string.Empty;

apmServer.OnReceive += o =>
{
if (o is TransactionDto)
transactionWaitHandle.Set();
if (o is ErrorDto)

else if (o is ErrorDto)
errorWaitHandle.Set();

else if (o is string s) // may occur if there is an error
{
transactionWaitHandle.Set();
errorWaitHandle.Set();
serverError = s;
}
};

using (var sampleApp = new SampleApplication())
Expand All @@ -131,13 +150,15 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Error(string t

response.IsSuccessStatusCode.Should().BeFalse();

transactionWaitHandle.WaitOne(TimeSpan.FromMinutes(2));
transactionWaitHandle.WaitOne(TimeSpan.FromMinutes(1));
serverError.Should().BeEmpty();
apmServer.ReceivedData.Transactions.Should().HaveCount(1);

var transaction = apmServer.ReceivedData.Transactions.First();
transaction.Name.Should().Be("GET Home/Exception");

errorWaitHandle.WaitOne(TimeSpan.FromMinutes(2));
errorWaitHandle.WaitOne(TimeSpan.FromMinutes(1));
serverError.Should().BeEmpty();
apmServer.ReceivedData.Errors.Should().HaveCount(1);

var error = apmServer.ReceivedData.Errors.First();
Expand All @@ -159,11 +180,19 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Metadata(
var port = apmServer.FindAvailablePortToListen();
apmServer.RunInBackground(port);

var error = string.Empty;
var waitHandle = new ManualResetEvent(false);

apmServer.OnReceive += o =>
{
if (o is MetadataDto)
waitHandle.Set();

else if (o is string s) // may occur if there is an error
{
waitHandle.Set();
error = s;
}
};

using (var sampleApp = new SampleApplication())
Expand All @@ -180,7 +209,9 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Metadata(

response.IsSuccessStatusCode.Should().BeTrue();

waitHandle.WaitOne(TimeSpan.FromMinutes(2));
waitHandle.WaitOne(TimeSpan.FromMinutes(1));

error.Should().BeEmpty();
apmServer.ReceivedData.Metadata.Should().HaveCountGreaterOrEqualTo(1);

var metadata = apmServer.ReceivedData.Metadata.First();
Expand Down

0 comments on commit 07b8343

Please sign in to comment.