forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetricsTest.cs
1384 lines (1183 loc) · 64.7 KB
/
MetricsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Net.Http.Metrics;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Http.Functional.Tests
{
public abstract class DiagnosticsTestBase : HttpClientHandlerTestBase
{
protected DiagnosticsTestBase(ITestOutputHelper output) : base(output)
{
}
protected static void VerifyTag<T>(IEnumerable<KeyValuePair<string, object?>> tags, string name, T value)
{
if (value is null)
{
Assert.DoesNotContain(tags, t => t.Key == name);
}
else
{
object? actualValue = tags.Single(t => t.Key == name).Value;
Assert.Equal(value, (T)actualValue);
}
}
protected static void VerifySchemeHostPortTags(IEnumerable<KeyValuePair<string, object?>> tags, Uri uri)
{
VerifyTag(tags, "url.scheme", uri.Scheme);
VerifyTag(tags, "server.address", uri.Host);
VerifyTag(tags, "server.port", uri.Port);
}
protected static string? GetVersionString(Version? version) => version == null ? null : version.Major switch
{
1 => "1.1",
2 => "2",
_ => "3"
};
}
public abstract class HttpMetricsTestBase : DiagnosticsTestBase
{
protected static class InstrumentNames
{
public const string RequestDuration = "http.client.request.duration";
public const string ActiveRequests = "http.client.active_requests";
public const string OpenConnections = "http.client.open_connections";
public const string IdleConnections = "http-client-current-idle-connections";
public const string ConnectionDuration = "http.client.connection.duration";
public const string TimeInQueue = "http.client.request.time_in_queue";
}
protected HttpMetricsTestBase(ITestOutputHelper output) : base(output)
{
}
private static void VerifyPeerAddress(KeyValuePair<string, object?>[] tags, IPAddress[] validPeerAddresses = null)
{
string ipString = (string)tags.Single(t => t.Key == "network.peer.address").Value;
validPeerAddresses ??= [IPAddress.Loopback.MapToIPv6(), IPAddress.Loopback, IPAddress.IPv6Loopback];
IPAddress ip = IPAddress.Parse(ipString);
Assert.Contains(ip, validPeerAddresses);
}
protected static void VerifyRequestDuration(Measurement<double> measurement,
Uri uri,
Version? protocolVersion = null,
int? statusCode = null,
string method = "GET",
string[] acceptedErrorTypes = null) =>
VerifyRequestDuration(InstrumentNames.RequestDuration, measurement.Value, measurement.Tags.ToArray(), uri, protocolVersion, statusCode, method, acceptedErrorTypes);
protected static void VerifyRequestDuration(string instrumentName,
double measurement,
KeyValuePair<string, object?>[] tags,
Uri uri,
Version? protocolVersion,
int? statusCode,
string method = "GET",
string[] acceptedErrorTypes = null)
{
Assert.Equal(InstrumentNames.RequestDuration, instrumentName);
Assert.InRange(measurement, double.Epsilon, 60);
VerifySchemeHostPortTags(tags, uri);
VerifyTag(tags, "http.request.method", method);
VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion));
VerifyTag(tags, "http.response.status_code", statusCode);
if (acceptedErrorTypes == null)
{
Assert.DoesNotContain(tags, t => t.Key == "error.type");
}
else
{
string errorReason = (string)tags.Single(t => t.Key == "error.type").Value;
Assert.Contains(errorReason, acceptedErrorTypes);
}
}
protected static void VerifyActiveRequests(Measurement<long> measurement, long expectedValue, Uri uri, string method = "GET") =>
VerifyActiveRequests(InstrumentNames.ActiveRequests, measurement.Value, measurement.Tags.ToArray(), expectedValue, uri, method);
protected static void VerifyActiveRequests(string instrumentName, long measurement, KeyValuePair<string, object?>[] tags, long expectedValue, Uri uri, string method = "GET")
{
Assert.Equal(InstrumentNames.ActiveRequests, instrumentName);
Assert.Equal(expectedValue, measurement);
VerifySchemeHostPortTags(tags, uri);
Assert.Equal(method, tags.Single(t => t.Key == "http.request.method").Value);
}
protected static void VerifyOpenConnections(string actualName, object measurement, KeyValuePair<string, object?>[] tags, long expectedValue, Uri uri, Version? protocolVersion, string state, IPAddress[] validPeerAddresses = null)
{
Assert.Equal(InstrumentNames.OpenConnections, actualName);
Assert.Equal(expectedValue, Assert.IsType<long>(measurement));
VerifySchemeHostPortTags(tags, uri);
VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion));
VerifyTag(tags, "http.connection.state", state);
VerifyPeerAddress(tags, validPeerAddresses);
}
protected static void VerifyConnectionDuration(string instrumentName, object measurement, KeyValuePair<string, object?>[] tags, Uri uri, Version? protocolVersion, IPAddress[] validPeerAddresses = null)
{
Assert.Equal(InstrumentNames.ConnectionDuration, instrumentName);
double value = Assert.IsType<double>(measurement);
// This flakes for remote requests on CI.
if (validPeerAddresses is null)
{
Assert.InRange(value, double.Epsilon, 60);
}
VerifySchemeHostPortTags(tags, uri);
VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion));
VerifyPeerAddress(tags, validPeerAddresses);
}
protected static void VerifyTimeInQueue(string instrumentName, object measurement, KeyValuePair<string, object?>[] tags, Uri uri, Version? protocolVersion, string method = "GET")
{
Assert.Equal(InstrumentNames.TimeInQueue, instrumentName);
double value = Assert.IsType<double>(measurement);
Assert.InRange(value, double.Epsilon, 60);
VerifySchemeHostPortTags(tags, uri);
VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion));
VerifyTag(tags, "http.request.method", method);
}
protected static async Task WaitForEnvironmentTicksToAdvance()
{
long start = Environment.TickCount64;
while (Environment.TickCount64 == start)
{
await Task.Delay(1);
}
}
protected sealed class InstrumentRecorder<T> : IDisposable where T : struct
{
private readonly MeterListener _meterListener = new();
private readonly ConcurrentQueue<Measurement<T>> _values = new();
private Meter? _meter;
public Action? MeasurementRecorded;
public Action<IReadOnlyList<T>> VerifyHistogramBucketBoundaries;
public int MeasurementCount => _values.Count;
public InstrumentRecorder(string instrumentName)
{
_meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name == "System.Net.Http" && instrument.Name == instrumentName)
{
listener.EnableMeasurementEvents(instrument);
}
};
_meterListener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
_meterListener.Start();
}
public InstrumentRecorder(IMeterFactory meterFactory, string instrumentName)
{
_meter = meterFactory.Create("System.Net.Http");
_meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter == _meter && instrument.Name == instrumentName)
{
listener.EnableMeasurementEvents(instrument);
}
};
_meterListener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
_meterListener.Start();
}
private void OnMeasurementRecorded(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state)
{
_values.Enqueue(new Measurement<T>(measurement, tags));
MeasurementRecorded?.Invoke();
if (VerifyHistogramBucketBoundaries is not null)
{
Histogram<T> histogram = (Histogram<T>)instrument;
IReadOnlyList<T> boundaries = histogram.Advice.HistogramBucketBoundaries;
Assert.NotNull(boundaries);
VerifyHistogramBucketBoundaries(boundaries);
}
}
public IReadOnlyList<Measurement<T>> GetMeasurements() => _values.ToArray();
public void Dispose() => _meterListener.Dispose();
}
protected record RecordedCounter(string InstrumentName, object Value, KeyValuePair<string, object?>[] Tags)
{
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append($"{InstrumentName}={Value} [");
for (int i = 0; i < Tags.Length - 1; i++)
{
sb.Append($"{Tags[i].Key}={Tags[i].Value}, ");
}
sb.Append($"{Tags.Last().Key}={Tags.Last().Value}]");
return sb.ToString();
}
}
protected sealed class MultiInstrumentRecorder : IDisposable
{
private readonly MeterListener _meterListener = new();
private readonly ConcurrentQueue<RecordedCounter> _values = new();
public MultiInstrumentRecorder()
: this(meter: null)
{ }
public MultiInstrumentRecorder(IMeterFactory meterFactory)
: this(meterFactory.Create("System.Net.Http"))
{ }
private MultiInstrumentRecorder(Meter? meter)
{
_meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter == meter || (meter is null && instrument.Meter.Name == "System.Net.Http"))
{
listener.EnableMeasurementEvents(instrument);
}
};
_meterListener.SetMeasurementEventCallback<long>((instrument, measurement, tags, _) =>
_values.Enqueue(new RecordedCounter(instrument.Name, measurement, tags.ToArray())));
_meterListener.SetMeasurementEventCallback<double>((instrument, measurement, tags, _) =>
_values.Enqueue(new RecordedCounter(instrument.Name, measurement, tags.ToArray())));
_meterListener.Start();
}
public IReadOnlyList<RecordedCounter> GetMeasurements() => _values.ToArray();
public void Dispose() => _meterListener.Dispose();
}
}
public abstract class HttpMetricsTest : HttpMetricsTestBase
{
public static readonly bool SupportsSeparateHttpSpansForRedirects = PlatformDetection.IsNotMobile && PlatformDetection.IsNotBrowser;
private IMeterFactory _meterFactory = new TestMeterFactory();
protected HttpClientHandler Handler { get; }
protected virtual bool TestHttpMessageInvoker => false;
public HttpMetricsTest(ITestOutputHelper output) : base(output)
{
Handler = CreateHttpClientHandler();
}
[Fact]
public Task ActiveRequests_Success_Recorded()
{
return LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<long> recorder = SetupInstrumentRecorder<long>(InstrumentNames.ActiveRequests);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
HttpResponseMessage response = await SendAsync(client, request);
response.Dispose(); // Make sure disposal doesn't interfere with recording by enforcing early disposal.
Assert.Collection(recorder.GetMeasurements(),
m => VerifyActiveRequests(m, 1, uri),
m => VerifyActiveRequests(m, -1, uri));
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync();
});
}
[ConditionalFact(typeof(SocketsHttpHandler), nameof(SocketsHttpHandler.IsSupported))]
public async Task ActiveRequests_InstrumentEnabledAfterSending_NotRecorded()
{
if (UseVersion == HttpVersion.Version30)
{
return; // This test depends on ConnectCallback.
}
TaskCompletionSource connectionStarted = new TaskCompletionSource();
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
GetUnderlyingSocketsHttpHandler(Handler).ConnectCallback = async (ctx, cancellationToken) =>
{
connectionStarted.SetResult();
return await DefaultConnectCallback(ctx.DnsEndPoint, cancellationToken);
};
// Enable recording request-duration to test the path with metrics enabled.
using InstrumentRecorder<double> unrelatedRecorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
Task<HttpResponseMessage> clientTask = Task.Run(() => SendAsync(client, request));
await connectionStarted.Task;
using InstrumentRecorder<long> recorder = new(Handler.MeterFactory, InstrumentNames.ActiveRequests);
using HttpResponseMessage response = await clientTask;
Assert.Empty(recorder.GetMeasurements());
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync();
});
}
[Theory]
[InlineData("GET", HttpStatusCode.OK)]
[InlineData("PUT", HttpStatusCode.Created)]
public Task RequestDuration_Success_Recorded(string method, HttpStatusCode statusCode)
{
return LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Parse(method), uri) { Version = UseVersion };
using HttpResponseMessage response = await SendAsync(client, request);
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, UseVersion, (int)statusCode, method);
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync(statusCode);
});
}
[OuterLoop("Uses external server.")]
[ConditionalFact]
public async Task ExternalServer_DurationMetrics_Recorded()
{
if (UseVersion == HttpVersion.Version30)
{
throw new SkipTestException("No remote HTTP/3 server available for testing.");
}
using InstrumentRecorder<double> requestDurationRecorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using InstrumentRecorder<double> connectionDurationRecorder = SetupInstrumentRecorder<double>(InstrumentNames.ConnectionDuration);
using InstrumentRecorder<long> openConnectionsRecorder = SetupInstrumentRecorder<long>(InstrumentNames.OpenConnections);
Uri uri = UseVersion == HttpVersion.Version11
? Test.Common.Configuration.Http.RemoteHttp11Server.EchoUri
: Test.Common.Configuration.Http.RemoteHttp2Server.EchoUri;
IPAddress[] addresses = await Dns.GetHostAddressesAsync(uri.Host);
addresses = addresses.Union(addresses.Select(a => a.MapToIPv6())).ToArray();
using (HttpMessageInvoker client = CreateHttpMessageInvoker())
{
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
request.Headers.ConnectionClose = true;
using HttpResponseMessage response = await SendAsync(client, request);
await response.Content.LoadIntoBufferAsync();
await WaitForEnvironmentTicksToAdvance();
}
VerifyRequestDuration(Assert.Single(requestDurationRecorder.GetMeasurements()), uri, UseVersion, 200, "GET");
Measurement<double> cd = Assert.Single(connectionDurationRecorder.GetMeasurements());
VerifyConnectionDuration(InstrumentNames.ConnectionDuration, cd.Value, cd.Tags.ToArray(), uri, UseVersion, addresses);
Measurement<long> oc = openConnectionsRecorder.GetMeasurements().First();
VerifyOpenConnections(InstrumentNames.OpenConnections, oc.Value, oc.Tags.ToArray(), 1, uri, UseVersion, "idle", addresses);
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public async Task RequestDuration_HttpTracingEnabled_RecordedWhileRequestActivityRunning()
{
await RemoteExecutor.Invoke(static testClass =>
{
HttpMetricsTest test = (HttpMetricsTest)Activator.CreateInstance(Type.GetType(testClass), (ITestOutputHelper)null);
return test.LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = test.CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = test.SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
Activity? activity = null;
bool stopped = false;
ActivitySource.AddActivityListener(new ActivityListener
{
ShouldListenTo = s => s.Name is "System.Net.Http",
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
ActivityStarted = created => activity = created,
ActivityStopped = _ => stopped = true
});
recorder.MeasurementRecorded = () =>
{
Assert.NotNull(activity);
Assert.False(stopped);
Assert.Same(activity, Activity.Current);
};
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = test.UseVersion };
using HttpResponseMessage response = await test.SendAsync(client, request);
Assert.NotNull(activity);
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, test.UseVersion, 200, "GET");
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync();
});
}, GetType().FullName).DisposeAsync();
}
[Fact]
public Task RequestDuration_CustomTags_Recorded()
{
return LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
HttpMetricsEnrichmentContext.AddCallback(request, static ctx =>
{
ctx.AddCustomTag("route", "/test");
});
using HttpResponseMessage response = await SendAsync(client, request);
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, UseVersion, 200);
Assert.Equal("/test", m.Tags.ToArray().Single(t => t.Key == "route").Value);
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync();
});
}
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData("System.Net.Http.HttpRequestOut.Start")]
[InlineData("System.Net.Http.Request")]
public async Task RequestDuration_CustomTags_DiagnosticListener_Recorded(string eventName)
{
await RemoteExecutor.Invoke(static async (testClassName, eventNameInner) =>
{
using HttpMetricsTest test = (HttpMetricsTest)Activator.CreateInstance(Type.GetType(testClassName), (ITestOutputHelper)null);
await test.RequestDuration_CustomTags_DiagnosticListener_Recorded_Core(eventNameInner);
}, GetType().FullName, eventName).DisposeAsync();
}
private async Task RequestDuration_CustomTags_DiagnosticListener_Recorded_Core(string eventName)
{
FakeDiagnosticListenerObserver diagnosticListenerObserver = new(kv =>
{
if (kv.Key == eventName)
{
HttpRequestMessage request = GetProperty<HttpRequestMessage>(kv.Value, "Request");
HttpMetricsEnrichmentContext.AddCallback(request, static ctx =>
{
ctx.AddCustomTag("observed?", "observed!");
Assert.NotNull(ctx.Response);
});
}
});
using IDisposable subscription = DiagnosticListener.AllListeners.Subscribe(diagnosticListenerObserver);
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
diagnosticListenerObserver.Enable();
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
HttpMetricsEnrichmentContext.AddCallback(request, static ctx =>
{
ctx.AddCustomTag("route", "/test");
});
using HttpResponseMessage response = await SendAsync(client, request);
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, UseVersion, 200);
Assert.Equal("/test", m.Tags.ToArray().Single(t => t.Key == "route").Value);
Assert.Equal("observed!", m.Tags.ToArray().Single(t => t.Key == "observed?").Value);
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync();
});
static T GetProperty<T>(object obj, string propertyName)
{
Type t = obj.GetType();
PropertyInfo p = t.GetRuntimeProperty(propertyName);
object propertyValue = p.GetValue(obj);
Assert.NotNull(propertyValue);
Assert.IsAssignableFrom<T>(propertyValue);
return (T)propertyValue;
}
}
public enum ResponseContentType
{
Empty,
ContentLength,
TransferEncodingChunked
}
[Theory]
[InlineData(HttpCompletionOption.ResponseContentRead, ResponseContentType.Empty)]
[InlineData(HttpCompletionOption.ResponseContentRead, ResponseContentType.ContentLength)]
[InlineData(HttpCompletionOption.ResponseContentRead, ResponseContentType.TransferEncodingChunked)]
[InlineData(HttpCompletionOption.ResponseHeadersRead, ResponseContentType.Empty)]
[InlineData(HttpCompletionOption.ResponseHeadersRead, ResponseContentType.ContentLength)]
[InlineData(HttpCompletionOption.ResponseHeadersRead, ResponseContentType.TransferEncodingChunked)]
public async Task RequestDuration_EnrichmentHandler_Success_Recorded(HttpCompletionOption completionOption, ResponseContentType responseContentType)
{
if (TestHttpMessageInvoker)
{
// HttpCompletionOption not supported for HttpMessageInvoker, skipping.
return;
}
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpClient client = CreateHttpClient(new EnrichmentHandler(Handler));
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
using HttpResponseMessage response = await client.SendAsync(TestAsync, request, completionOption);
string responseContent = await response.Content.ReadAsStringAsync();
if (responseContentType == ResponseContentType.ContentLength)
{
Assert.NotNull(response.Content.Headers.ContentLength);
}
else if (responseContentType == ResponseContentType.TransferEncodingChunked)
{
Assert.NotNull(response.Headers.TransferEncodingChunked);
}
else
{
// Empty
Assert.Empty(responseContent);
}
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, UseVersion, 200); ;
Assert.Equal("before!", m.Tags.ToArray().Single(t => t.Key == "before").Value);
}, async server =>
{
if (responseContentType == ResponseContentType.ContentLength)
{
string content = string.Join(' ', Enumerable.Range(0, 100));
int contentLength = Encoding.ASCII.GetByteCount(content);
await server.AcceptConnectionSendResponseAndCloseAsync(content: content, additionalHeaders: new[] { new HttpHeaderData("Content-Length", $"{contentLength}") });
}
else if (responseContentType == ResponseContentType.TransferEncodingChunked)
{
string content = "3\r\nfoo\r\n3\r\nbar\r\n0\r\n\r\n";
await server.AcceptConnectionSendResponseAndCloseAsync(content: content, additionalHeaders: new[] { new HttpHeaderData("Transfer-Encoding", "chunked") });
}
else
{
// Empty
await server.AcceptConnectionSendResponseAndCloseAsync();
}
});
}
[ConditionalFact(nameof(SupportsSeparateHttpSpansForRedirects))]
public Task ActiveRequests_Redirect_RecordedForEachHttpSpan()
{
return LoopbackServerFactory.CreateServerAsync((originalServer, originalUri) =>
{
return LoopbackServerFactory.CreateServerAsync(async (redirectServer, redirectUri) =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<long> recorder = SetupInstrumentRecorder<long>(InstrumentNames.ActiveRequests);
using HttpRequestMessage request = new(HttpMethod.Get, originalUri) { Version = UseVersion };
Task clientTask = SendAsync(client, request);
Task serverTask = originalServer.HandleRequestAsync(HttpStatusCode.Redirect, new[] { new HttpHeaderData("Location", redirectUri.AbsoluteUri) });
await Task.WhenAny(clientTask, serverTask);
Assert.False(clientTask.IsCompleted, $"{clientTask.Status}: {clientTask.Exception}");
await serverTask;
serverTask = redirectServer.HandleRequestAsync();
await TestHelper.WhenAllCompletedOrAnyFailed(clientTask, serverTask);
await clientTask;
Assert.Collection(recorder.GetMeasurements(),
m => VerifyActiveRequests(m, 1, originalUri),
m => VerifyActiveRequests(m, -1, originalUri),
m => VerifyActiveRequests(m, 1, redirectUri),
m => VerifyActiveRequests(m, -1, redirectUri));
});
});
}
public static TheoryData<string, string> MethodData = new TheoryData<string, string>()
{
{ "GET", "GET" },
{ "get", "GET" },
{ "PUT", "PUT" },
{ "Put", "PUT" },
{ "POST", "POST" },
{ "pOst", "POST" },
{ "delete", "DELETE" },
{ "head", "HEAD" },
{ "options", "OPTIONS" },
{ "trace", "TRACE" },
{ "patch", "PATCH" },
{ "connect", "CONNECT" },
{ "g3t", "_OTHER" },
};
[Theory]
[PlatformSpecific(~TestPlatforms.Browser)] // BrowserHttpHandler supports only a limited set of methods.
[MemberData(nameof(MethodData))]
public async Task RequestMetrics_EmitNormalizedMethodTags(string method, string expectedMethodTag)
{
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> requestDuration = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using InstrumentRecorder<long> activeRequests = SetupInstrumentRecorder<long>(InstrumentNames.ActiveRequests);
using InstrumentRecorder<double> timeInQueue = SetupInstrumentRecorder<double>(InstrumentNames.TimeInQueue);
using HttpRequestMessage request = new(new HttpMethod(method), uri) { Version = UseVersion };
if (expectedMethodTag == "CONNECT")
{
request.Headers.Host = "localhost";
}
using HttpResponseMessage response = await client.SendAsync(TestAsync, request);
Assert.All(requestDuration.GetMeasurements(), m => VerifyTag(m.Tags.ToArray(), "http.request.method", expectedMethodTag));
Assert.All(activeRequests.GetMeasurements(), m => VerifyTag(m.Tags.ToArray(), "http.request.method", expectedMethodTag));
Assert.All(timeInQueue.GetMeasurements(), m => VerifyTag(m.Tags.ToArray(), "http.request.method", expectedMethodTag));
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync();
});
}
[ConditionalFact(typeof(SocketsHttpHandler), nameof(SocketsHttpHandler.IsSupported))]
public async Task AllSocketsHttpHandlerCounters_Success_Recorded()
{
TaskCompletionSource clientWaitingTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
TaskCompletionSource clientDisposedTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using MultiInstrumentRecorder recorder = new(_meterFactory);
using (HttpMessageInvoker invoker = CreateHttpMessageInvoker())
{
Handler.MeterFactory = _meterFactory;
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
Task<HttpResponseMessage> sendAsyncTask = SendAsync(invoker, request);
clientWaitingTcs.SetResult();
using HttpResponseMessage response = await sendAsyncTask;
await WaitForEnvironmentTicksToAdvance();
}
clientDisposedTcs.SetResult();
Action<RecordedCounter> requestsQueueDuration = m =>
VerifyTimeInQueue(m.InstrumentName, m.Value, m.Tags, uri, UseVersion);
Action<RecordedCounter> connectionNoLongerIdle = m =>
VerifyOpenConnections(m.InstrumentName, m.Value, m.Tags, -1, uri, UseVersion, "idle");
Action<RecordedCounter> connectionIsActive = m =>
VerifyOpenConnections(m.InstrumentName, m.Value, m.Tags, 1, uri, UseVersion, "active");
Action<RecordedCounter> check1 = requestsQueueDuration;
Action<RecordedCounter> check2 = connectionNoLongerIdle;
Action<RecordedCounter> check3 = connectionIsActive;
if (UseVersion.Major > 1)
{
// With HTTP/2 and HTTP/3, the idle state change is emitted before RequestsQueueDuration.
check1 = connectionNoLongerIdle;
check2 = connectionIsActive;
check3 = requestsQueueDuration;
}
IReadOnlyList<RecordedCounter> measurements = recorder.GetMeasurements();
foreach (RecordedCounter m in measurements)
{
_output.WriteLine(m.ToString());
}
Assert.Collection(measurements,
m => VerifyActiveRequests(m.InstrumentName, (long)m.Value, m.Tags, 1, uri),
m => VerifyOpenConnections(m.InstrumentName, m.Value, m.Tags, 1, uri, UseVersion, "idle"),
check1, // requestsQueueDuration, connectionNoLongerIdle, connectionIsActive in the appropriate order.
check2,
check3,
m => VerifyOpenConnections(m.InstrumentName, m.Value, m.Tags, -1, uri, UseVersion, "active"),
m => VerifyOpenConnections(m.InstrumentName, m.Value, m.Tags, 1, uri, UseVersion, "idle"),
m => VerifyActiveRequests(m.InstrumentName, (long)m.Value, m.Tags, -1, uri),
m => VerifyRequestDuration(m.InstrumentName, (double)m.Value, m.Tags, uri, UseVersion, 200),
m => VerifyConnectionDuration(m.InstrumentName, m.Value, m.Tags, uri, UseVersion),
m => VerifyOpenConnections(m.InstrumentName, m.Value, m.Tags, -1, uri, UseVersion, "idle"));
},
async server =>
{
await clientWaitingTcs.Task.WaitAsync(TestHelper.PassingTestTimeout);
await server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestDataAsync();
await connection.SendResponseAsync();
await clientDisposedTcs.Task.WaitAsync(TestHelper.PassingTestTimeout);
});
});
}
[Fact]
public async Task RequestDuration_RequestCancelled_ErrorReasonIsExceptionType()
{
TaskCompletionSource clientCompleted = new(TaskCreationOptions.RunContinuationsAsynchronously);
TaskCompletionSource requestReceived = new(TaskCreationOptions.RunContinuationsAsynchronously);
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
using CancellationTokenSource requestCts = new();
Task clientTask = SendAsync(client, request, requestCts.Token);
await requestReceived.Task.WaitAsync(TestHelper.PassingTestTimeout);
requestCts.Cancel();
Exception clientException = await Assert.ThrowsAnyAsync<Exception>(() => clientTask);
_output.WriteLine($"Client exception: {clientException}");
string[] expectedExceptionTypes = TestAsync
? [typeof(TaskCanceledException).FullName]
: [typeof(TaskCanceledException).FullName, typeof(OperationCanceledException).FullName];
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, acceptedErrorTypes: expectedExceptionTypes);
clientCompleted.SetResult();
},
async server =>
{
await IgnoreExceptions(async () =>
{
await server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestDataAsync();
requestReceived.SetResult();
await clientCompleted.Task.WaitAsync(TestHelper.PassingTestTimeout);
});
});
});
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public async Task RequestDuration_ConnectionError_LogsExpectedErrorReason()
{
if (UseVersion.Major == 3)
{
// HTTP/3 doesn't use the ConnectCallback that this test is relying on.
return;
}
Uri uri = new("https://dummy:8080");
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
using CancellationTokenSource requestCts = new();
GetUnderlyingSocketsHttpHandler(Handler).ConnectCallback = (_, _) => throw new Exception();
Exception ex = await Assert.ThrowsAsync<HttpRequestException>(() => SendAsync(client, request));
_output.WriteLine($"Client exception: {ex}");
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, acceptedErrorTypes: ["connection_error"]);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Handler.Dispose();
_meterFactory.Dispose();
}
base.Dispose(disposing);
}
protected Task<HttpResponseMessage> SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request, CancellationToken cancellationToken = default)
{
if (TestHttpMessageInvoker)
{
return TestAsync
? invoker.SendAsync(request, cancellationToken)
: Task.Run(() => invoker.Send(request, cancellationToken));
}
return ((HttpClient)invoker).SendAsync(TestAsync, request, cancellationToken);
}
protected HttpMessageInvoker CreateHttpMessageInvoker(HttpMessageHandler? handler = null) =>
TestHttpMessageInvoker ?
new HttpMessageInvoker(handler ?? Handler) :
CreateHttpClient(handler ?? Handler);
protected InstrumentRecorder<T> SetupInstrumentRecorder<T>(string instrumentName)
where T : struct
{
Handler.MeterFactory = _meterFactory;
return new InstrumentRecorder<T>(_meterFactory, instrumentName);
}
protected sealed class EnrichmentHandler : DelegatingHandler
{
public EnrichmentHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpMetricsEnrichmentContext.AddCallback(request, Enrich);
return base.Send(request, cancellationToken);
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpMetricsEnrichmentContext.AddCallback(request, Enrich);
return base.SendAsync(request, cancellationToken);
}
private static void Enrich(HttpMetricsEnrichmentContext context) => context.AddCustomTag("before", "before!");
}
}
public abstract class HttpMetricsTest_Http11 : HttpMetricsTest
{
protected override Version UseVersion => HttpVersion.Version11;
public HttpMetricsTest_Http11(ITestOutputHelper output) : base(output)
{
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNodeJS))]
public async Task RequestDuration_EnrichmentHandler_ContentLengthError_Recorded()
{
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker(new EnrichmentHandler(Handler));
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
if (TestHttpMessageInvoker)
{
using HttpResponseMessage response = await SendAsync(client, request);
}
else
{
await Assert.ThrowsAsync<HttpRequestException>(async () =>
{
using HttpResponseMessage response = await SendAsync(client, request);
});
}
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, UseVersion, 200);
Assert.Equal("before!", m.Tags.ToArray().Single(t => t.Key == "before").Value);
}, server => server.HandleRequestAsync(headers: new[] {
new HttpHeaderData("Content-Length", "1000")
}, content: "x"));
}
[Theory]
[InlineData(400)]
[InlineData(404)]
[InlineData(599)]
public Task RequestDuration_ErrorStatus_ErrorTypeRecorded(int statusCode)
{
return LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Get, uri) { Version = UseVersion };
using HttpResponseMessage response = await SendAsync(client, request);
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, UseVersion, statusCode, "GET", acceptedErrorTypes: new[] { $"{statusCode}" });
}, async server =>
{
await server.AcceptConnectionSendResponseAndCloseAsync(statusCode: (HttpStatusCode)statusCode);
});
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "Browser is relaxed about validating HTTP headers")]
public async Task RequestDuration_ConnectionClosedWhileReceivingHeaders_Recorded()
{
using CancellationTokenSource cancelServerCts = new CancellationTokenSource();
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> recorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using HttpRequestMessage request = new(HttpMethod.Post, uri) { Version = UseVersion };
request.Content = new StringContent("{}");
Exception ex = await Assert.ThrowsAnyAsync<Exception>(async () =>
{
// To avoid unlimited blocking, lets bound it to 20 seconds.
using CancellationTokenSource cts = new CancellationTokenSource(20_000);
using HttpResponseMessage response = await SendAsync(client, request, cts.Token);
});
cancelServerCts.Cancel();
Assert.True(ex is HttpRequestException or TaskCanceledException);
Measurement<double> m = Assert.Single(recorder.GetMeasurements());
VerifyRequestDuration(m, uri, acceptedErrorTypes: [typeof(TaskCanceledException).FullName, "response_ended"], method: "POST");
}, async server =>
{
await IgnoreExceptions(async () =>
{
LoopbackServer.Connection connection = await server.EstablishConnectionAsync().WaitAsync(cancelServerCts.Token);
connection.Socket.Shutdown(SocketShutdown.Send);
});
});
}
[Fact]
public Task DurationHistograms_HaveBucketSizeHints()
{
return LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpMessageInvoker client = CreateHttpMessageInvoker();
using InstrumentRecorder<double> requestDurationRecorder = SetupInstrumentRecorder<double>(InstrumentNames.RequestDuration);
using InstrumentRecorder<double> timeInQueueRecorder = SetupInstrumentRecorder<double>(InstrumentNames.TimeInQueue);
using InstrumentRecorder<double> connectionDurationRecorder = SetupInstrumentRecorder<double>(InstrumentNames.ConnectionDuration);
requestDurationRecorder.VerifyHistogramBucketBoundaries = b =>
{
// Verify first and last value of the boundaries defined in
// https://github.com/open-telemetry/semantic-conventions/blob/release/v1.23.x/docs/http/http-metrics.md#metric-httpserverrequestduration
Assert.Equal(0.005, b.First());
Assert.Equal(10, b.Last());
};
timeInQueueRecorder.VerifyHistogramBucketBoundaries = requestDurationRecorder.VerifyHistogramBucketBoundaries;
connectionDurationRecorder.VerifyHistogramBucketBoundaries =