Skip to content

Commit a0bdbc2

Browse files
authored
Fix TCP stats failure on Azure (#94)
1 parent 97211b1 commit a0bdbc2

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/Elastic.Transport/Diagnostics/TcpStats.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,40 @@ namespace Elastic.Transport.Diagnostics;
1515
internal static class TcpStats
1616
{
1717
private static readonly int StateLength = Enum.GetNames(typeof(TcpState)).Length;
18+
private static readonly ReadOnlyDictionary<TcpState, int> Empty = new(new Dictionary<TcpState, int>());
1819

1920
/// <summary>
2021
/// Gets the active TCP connections
2122
/// </summary>
22-
/// <returns></returns>
23-
public static TcpConnectionInformation[] GetActiveTcpConnections() =>
24-
IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
23+
/// <returns>TcpConnectionInformation[]</returns>
24+
/// <remarks>Can return `null` when there is a permissions issue retrieving TCP connections.</remarks>
25+
public static TcpConnectionInformation[]? GetActiveTcpConnections()
26+
{
27+
try
28+
{
29+
return IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
30+
}
31+
catch (NetworkInformationException) // host might not allow this information to be fetched.
32+
{
33+
// ignored
34+
}
35+
36+
return null;
37+
}
2538

2639
/// <summary>
2740
/// Gets the sum for each state of the active TCP connections
2841
/// </summary>
2942
public static ReadOnlyDictionary<TcpState, int> GetStates()
3043
{
31-
var states = new Dictionary<TcpState, int>(StateLength);
3244
var connections = GetActiveTcpConnections();
45+
if (connections is null)
46+
{
47+
return Empty;
48+
}
49+
50+
var states = new Dictionary<TcpState, int>(StateLength);
51+
3352
for (var index = 0; index < connections.Length; index++)
3453
{
3554
var connection = connections[index];

0 commit comments

Comments
 (0)