10
10
using System . Linq ;
11
11
using System . Threading ;
12
12
using Elastic . Apm . Api ;
13
+ using Elastic . Apm . Ingest ;
13
14
using Elastic . Apm . Libraries . Newtonsoft . Json . Linq ;
14
15
using Elastic . Apm . Logging ;
15
16
using Elastic . Apm . Metrics ;
16
17
using Elastic . Apm . Model ;
17
18
using Elastic . Apm . Report ;
19
+ using Elastic . Transport ;
18
20
using FluentAssertions ;
19
21
22
+ #nullable enable
20
23
namespace Elastic . Apm . Tests . Utilities
21
24
{
22
- internal class MockPayloadSender : IPayloadSender
25
+ internal class MockPayloadSender : ApmChannel
23
26
{
24
27
private static readonly JObject JsonSpanTypesData =
25
28
JObject . Parse ( File . ReadAllText ( "./TestResources/json-specs/span_types.json" ) ) ;
26
29
27
- private readonly List < IError > _errors = new List < IError > ( ) ;
28
- private readonly List < Func < IError , IError > > _errorFilters = new List < Func < IError , IError > > ( ) ;
29
- private readonly object _spanLock = new object ( ) ;
30
- private readonly object _transactionLock = new object ( ) ;
31
- private readonly object _metricsLock = new object ( ) ;
32
- private readonly object _errorLock = new object ( ) ;
33
- private readonly List < IMetricSet > _metrics = new List < IMetricSet > ( ) ;
34
- private readonly List < Func < ISpan , ISpan > > _spanFilters = new List < Func < ISpan , ISpan > > ( ) ;
35
- private readonly List < ISpan > _spans = new List < ISpan > ( ) ;
36
- private readonly List < Func < ITransaction , ITransaction > > _transactionFilters = new List < Func < ITransaction , ITransaction > > ( ) ;
37
- private readonly List < ITransaction > _transactions = new List < ITransaction > ( ) ;
38
-
39
- public MockPayloadSender ( IApmLogger logger = null )
30
+ private readonly object _spanLock = new ( ) ;
31
+ private readonly object _transactionLock = new ( ) ;
32
+ private readonly object _metricsLock = new ( ) ;
33
+ private readonly object _errorLock = new ( ) ;
34
+ private readonly List < IMetricSet > _metrics = new ( ) ;
35
+ private readonly List < IError > _errors = new ( ) ;
36
+ private readonly List < ISpan > _spans = new ( ) ;
37
+ private readonly List < ITransaction > _transactions = new ( ) ;
38
+
39
+ public MockPayloadSender ( IApmLogger ? logger = null )
40
+ : base ( new ApmChannelOptions ( new Uri ( "http://localhost:8080" ) , transportClient : new InMemoryConnection ( ) ) , logger )
40
41
{
41
42
_waitHandles = new [ ] { new AutoResetEvent ( false ) , new AutoResetEvent ( false ) , new AutoResetEvent ( false ) , new AutoResetEvent ( false ) } ;
42
43
@@ -45,7 +46,6 @@ public MockPayloadSender(IApmLogger logger = null)
45
46
_errorWaitHandle = _waitHandles [ 2 ] ;
46
47
_metricSetWaitHandle = _waitHandles [ 3 ] ;
47
48
48
- PayloadSenderV2 . SetUpFilters ( _transactionFilters , _spanFilters , _errorFilters , MockApmServerInfo . Version710 , logger ?? new NoopLogger ( ) ) ;
49
49
}
50
50
51
51
/// <summary>
@@ -61,6 +61,54 @@ public MockPayloadSender(IApmLogger logger = null)
61
61
private readonly AutoResetEvent [ ] _waitHandles ;
62
62
private static readonly TimeSpan DefaultTimeout = TimeSpan . FromMinutes ( 1 ) ;
63
63
64
+ public override bool TryWrite ( IIntakeRoot item )
65
+ {
66
+ var written = base . TryWrite ( item ) ;
67
+ switch ( item )
68
+ {
69
+ case IError error :
70
+ _errors . Add ( error ) ;
71
+ _errorWaitHandle . Set ( ) ;
72
+ break ;
73
+ case ITransaction transaction :
74
+ _transactions . Add ( transaction ) ;
75
+ _transactionWaitHandle . Set ( ) ;
76
+ break ;
77
+ case ISpan span :
78
+ _spans . Add ( span ) ;
79
+ _spanWaitHandle . Set ( ) ;
80
+ break ;
81
+ case IMetricSet metricSet :
82
+ _metrics . Add ( metricSet ) ;
83
+ _metricSetWaitHandle . Set ( ) ;
84
+ break ;
85
+ }
86
+ return written ;
87
+ }
88
+
89
+ public override void QueueError ( IError error )
90
+ {
91
+ lock ( _errorLock ) base . QueueError ( error ) ;
92
+ }
93
+
94
+ public override void QueueTransaction ( ITransaction transaction )
95
+ {
96
+ lock ( _transactionLock ) base . QueueTransaction ( transaction ) ;
97
+ }
98
+
99
+ public override void QueueSpan ( ISpan span )
100
+ {
101
+ VerifySpan ( span ) ;
102
+ lock ( _spanLock ) base . QueueSpan ( span ) ;
103
+ }
104
+
105
+ public override void QueueMetrics ( IMetricSet metricSet )
106
+ {
107
+ lock ( _metricsLock ) base . QueueMetrics ( metricSet ) ;
108
+ }
109
+
110
+
111
+
64
112
/// <summary>
65
113
/// Waits for any events to be queued
66
114
/// </summary>
@@ -191,27 +239,27 @@ public IReadOnlyList<IError> Errors
191
239
get
192
240
{
193
241
lock ( _errorLock )
194
- return CreateImmutableSnapshot < IError > ( _errors ) ;
242
+ return CreateImmutableSnapshot ( _errors ) ;
195
243
}
196
244
}
197
245
198
- public Error FirstError => Errors . FirstOrDefault ( ) as Error ;
199
- public MetricSet FirstMetric => Metrics . FirstOrDefault ( ) as MetricSet ;
246
+ public Error ? FirstError => Errors . FirstOrDefault ( ) as Error ;
247
+ public MetricSet ? FirstMetric => Metrics . FirstOrDefault ( ) as MetricSet ;
200
248
201
249
/// <summary>
202
250
/// The 1. Span on the 1. Transaction
203
251
/// </summary>
204
- public Span FirstSpan => Spans . FirstOrDefault ( ) as Span ;
252
+ public Span ? FirstSpan => Spans . FirstOrDefault ( ) as Span ;
205
253
206
- public Transaction FirstTransaction =>
254
+ public Transaction ? FirstTransaction =>
207
255
Transactions . FirstOrDefault ( ) as Transaction ;
208
256
209
257
public IReadOnlyList < IMetricSet > Metrics
210
258
{
211
259
get
212
260
{
213
261
lock ( _metricsLock )
214
- return CreateImmutableSnapshot < IMetricSet > ( _metrics ) ;
262
+ return CreateImmutableSnapshot ( _metrics ) ;
215
263
}
216
264
}
217
265
@@ -220,7 +268,7 @@ public IReadOnlyList<ISpan> Spans
220
268
get
221
269
{
222
270
lock ( _spanLock )
223
- return CreateImmutableSnapshot < ISpan > ( _spans ) ;
271
+ return CreateImmutableSnapshot ( _spans ) ;
224
272
}
225
273
}
226
274
@@ -229,45 +277,15 @@ public IReadOnlyList<ITransaction> Transactions
229
277
get
230
278
{
231
279
lock ( _transactionLock )
232
- return CreateImmutableSnapshot < ITransaction > ( _transactions ) ;
280
+ return CreateImmutableSnapshot ( _transactions ) ;
233
281
}
234
282
}
235
283
236
284
public Span [ ] SpansOnFirstTransaction =>
237
- Spans . Where ( n => n . TransactionId == Transactions . First ( ) . Id ) . Select ( n => n as Span ) . ToArray ( ) ;
238
-
239
- public void QueueError ( IError error )
240
- {
241
- lock ( _errorLock )
242
- {
243
- error = _errorFilters . Aggregate ( error ,
244
- ( current , filter ) => filter ( current ) ) ;
245
- _errors . Add ( error ) ;
246
- _errorWaitHandle . Set ( ) ;
247
- }
248
- }
249
-
250
- public virtual void QueueTransaction ( ITransaction transaction )
251
- {
252
- lock ( _transactionLock )
253
- {
254
- transaction = _transactionFilters . Aggregate ( transaction ,
255
- ( current , filter ) => filter ( current ) ) ;
256
- _transactions . Add ( transaction ) ;
257
- _transactionWaitHandle . Set ( ) ;
258
- }
259
- }
260
-
261
- public void QueueSpan ( ISpan span )
262
- {
263
- VerifySpan ( span ) ;
264
- lock ( _spanLock )
265
- {
266
- span = _spanFilters . Aggregate ( span , ( current , filter ) => filter ( current ) ) ;
267
- _spans . Add ( span ) ;
268
- _spanWaitHandle . Set ( ) ;
269
- }
270
- }
285
+ Spans
286
+ . Where ( n => n . TransactionId == Transactions . First ( ) . Id )
287
+ . Select ( n => ( Span ) n )
288
+ . ToArray ( ) ;
271
289
272
290
private void VerifySpan ( ISpan span )
273
291
{
@@ -279,7 +297,7 @@ private void VerifySpan(ISpan span)
279
297
var spanTypeInfo = JsonSpanTypesData [ type ] as JObject ;
280
298
spanTypeInfo . Should ( ) . NotBeNull ( $ "span type '{ type } ' is not allowed by the spec") ;
281
299
282
- var allowNullSubtype = spanTypeInfo [ "allow_null_subtype" ] ? . Value < bool > ( ) ;
300
+ var allowNullSubtype = spanTypeInfo ! [ "allow_null_subtype" ] ? . Value < bool > ( ) ;
283
301
var allowUnlistedSubtype = spanTypeInfo [ "allow_unlisted_subtype" ] ? . Value < bool > ( ) ;
284
302
var subTypes = spanTypeInfo [ "subtypes" ] ;
285
303
var hasSubtypes = subTypes != null && subTypes . Any ( ) ;
@@ -289,7 +307,7 @@ private void VerifySpan(ISpan span)
289
307
{
290
308
if ( ! allowUnlistedSubtype . GetValueOrDefault ( ) && hasSubtypes )
291
309
{
292
- var subTypeInfo = subTypes [ subType ] ;
310
+ var subTypeInfo = subTypes ! [ subType ] ;
293
311
subTypeInfo . Should ( )
294
312
. NotBeNull ( $ "span subtype '{ subType } ' is not allowed by the spec for type '{ type } '") ;
295
313
}
@@ -305,15 +323,6 @@ private void VerifySpan(ISpan span)
305
323
}
306
324
}
307
325
308
- public void QueueMetrics ( IMetricSet metricSet )
309
- {
310
- lock ( _metricsLock )
311
- {
312
- _metrics . Add ( metricSet ) ;
313
- _metricSetWaitHandle . Set ( ) ;
314
- }
315
- }
316
-
317
326
public void Clear ( )
318
327
{
319
328
lock ( _spanLock )
0 commit comments