Skip to content

Commit 07b8343

Browse files
committed
Fix serialization
1 parent 5129f12 commit 07b8343

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

src/Elastic.Apm/Report/Serialization/PayloadItemSerializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ internal PayloadItemSerializer() =>
8383

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

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

8992
/// <summary>
9093
/// Deserializes an instance of <typeparamref name="T"/> from JSON

test/Elastic.Apm.Tests/ActivityIntegrationTests.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ public void MultipleTransactionInOneActivity()
168168
}
169169

170170
#if NET
171-
/// <summary>
172-
/// Makes sure that transactions on the same Activity are part of the same trace.
173-
/// </summary>
174171
[Fact]
175172
public async Task ActivityRespectsSampling()
176173
{
@@ -179,27 +176,28 @@ public async Task ActivityRespectsSampling()
179176

180177
Activity.Current = null;
181178
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
182-
var source = new ActivitySource(GetType().FullName, "1.0.0");
179+
180+
using var source = new ActivitySource(GetType().FullName, "1.0.0");
183181

184182
var payloadSender = new MockPayloadSender();
185183
var config = new MockConfiguration(
186184
transactionSampleRate: rate.ToString("N2")
187-
188185
);
186+
189187
using var components = new TestAgentComponents(
190188
apmServerInfo: MockApmServerInfo.Version716,
191189
configuration: config,
192190
payloadSender: payloadSender
193-
194191
);
192+
195193
using var agent = new ApmAgent(components);
196194
for (var i = 0; i < count; i++)
197195
{
198196
using var transaction = source.StartActivity($"Trace {i}");
199197
using var span = new Activity("UnitTestActivity").Start();
200198
await Task.Delay(1);
201199
}
202-
//Activity.Current.Should().BeNull();
200+
203201
payloadSender.WaitForTransactions(count: count);
204202

205203
var sampled = payloadSender.Transactions.Where(t => t.IsSampled).ToArray();

test/startuphook/Elastic.Apm.StartupHook.Tests/StartupHookTests.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ public async Task Auto_Instrument_With_StartupHook_Should_Capture_Transaction(st
6767
apmServer.RunInBackground(port);
6868
var waitHandle = new ManualResetEvent(false);
6969

70+
var error = string.Empty;
71+
7072
apmServer.OnReceive += o =>
7173
{
7274
if (o is TransactionDto)
7375
waitHandle.Set();
76+
77+
else if (o is string s) // may occur if there is an error
78+
{
79+
waitHandle.Set();
80+
error = s;
81+
}
7482
};
7583

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

8896
response.IsSuccessStatusCode.Should().BeTrue();
97+
waitHandle.WaitOne(TimeSpan.FromMinutes(1));
8998

90-
waitHandle.WaitOne(TimeSpan.FromMinutes(2));
99+
error.Should().BeEmpty();
91100
apmServer.ReceivedData.Transactions.Should().HaveCount(1);
92101

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

120+
var serverError = string.Empty;
121+
111122
apmServer.OnReceive += o =>
112123
{
113124
if (o is TransactionDto)
114125
transactionWaitHandle.Set();
115-
if (o is ErrorDto)
126+
127+
else if (o is ErrorDto)
116128
errorWaitHandle.Set();
129+
130+
else if (o is string s) // may occur if there is an error
131+
{
132+
transactionWaitHandle.Set();
133+
errorWaitHandle.Set();
134+
serverError = s;
135+
}
117136
};
118137

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

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

134-
transactionWaitHandle.WaitOne(TimeSpan.FromMinutes(2));
153+
transactionWaitHandle.WaitOne(TimeSpan.FromMinutes(1));
154+
serverError.Should().BeEmpty();
135155
apmServer.ReceivedData.Transactions.Should().HaveCount(1);
136156

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

140-
errorWaitHandle.WaitOne(TimeSpan.FromMinutes(2));
160+
errorWaitHandle.WaitOne(TimeSpan.FromMinutes(1));
161+
serverError.Should().BeEmpty();
141162
apmServer.ReceivedData.Errors.Should().HaveCount(1);
142163

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

183+
var error = string.Empty;
162184
var waitHandle = new ManualResetEvent(false);
185+
163186
apmServer.OnReceive += o =>
164187
{
165188
if (o is MetadataDto)
166189
waitHandle.Set();
190+
191+
else if (o is string s) // may occur if there is an error
192+
{
193+
waitHandle.Set();
194+
error = s;
195+
}
167196
};
168197

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

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

183-
waitHandle.WaitOne(TimeSpan.FromMinutes(2));
212+
waitHandle.WaitOne(TimeSpan.FromMinutes(1));
213+
214+
error.Should().BeEmpty();
184215
apmServer.ReceivedData.Metadata.Should().HaveCountGreaterOrEqualTo(1);
185216

186217
var metadata = apmServer.ReceivedData.Metadata.First();

0 commit comments

Comments
 (0)