additional metrics

This commit is contained in:
Leath Cooper 2024-10-26 08:17:02 -04:00
parent 56ea91b890
commit 6e49f60fbc
6 changed files with 85 additions and 17 deletions

View file

@ -23,6 +23,7 @@ using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.IO;
using System.Net;
using System.Net.Sockets;
@ -37,6 +38,8 @@ public delegate int EncodePacket(ReadOnlySpan<byte> inputBuffer, Span<byte> outp
public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetState>
{
private static readonly Counter<long> packetCounter =
Telemetry.NetworkMeter.CreateCounter<long>("net_state_packet_counter");
private static readonly ILogger logger = LogFactory.GetLogger(typeof(NetState));
private static readonly TimeSpan ConnectingSocketIdleLimit = TimeSpan.FromMilliseconds(5000); // 5 seconds
@ -476,6 +479,12 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
LogPacket(span, false);
}
packetCounter.Add(1, new KeyValuePair<string, object>[]
{
new("PacketID", span[0]),
new("Kind", "SERVER"),
});
SendPipe.Writer.Advance((uint)length);
if (!_flushQueued)
@ -564,6 +573,11 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
var packetId = packetReader.ReadByte();
int packetLength = length;
packetCounter.Add(1, new KeyValuePair<string, object>[]
{
new("PacketID", packetId),
new("Kind", "CLIENT"),
});
// These can arrive at any time and are only informational
if (_protocolState != ProtocolState.AwaitingSeed && IncomingPackets.IsInfoPacket(packetId))
{

View file

@ -14,6 +14,7 @@
*************************************************************************/
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
using System.Net;
@ -27,6 +28,18 @@ namespace Server.Network;
public static class TcpServer
{
private static readonly Counter<long> attemptedConnectionsCounter =
Telemetry.NetworkMeter.CreateCounter<long>("tcp_server_attempted_connections_count");
private static readonly Counter<long> ipLimitedConnectionsCounter =
Telemetry.NetworkMeter.CreateCounter<long>("tcp_server_ip_limited_connections_count");
private static readonly Counter<long> firewalledConnectionsCounter =
Telemetry.NetworkMeter.CreateCounter<long>("tcp_server_firewalled_connections_count");
private static readonly Counter<long> allowedConnectionsCounter =
Telemetry.NetworkMeter.CreateCounter<long>("tcp_server_allowed_connections_count");
private static readonly ILogger logger = LogFactory.GetLogger(typeof(TcpServer));
// AccountLoginReject BadComm
@ -78,11 +91,14 @@ public static class TcpServer
}
public static IEnumerable<IPEndPoint> GetListeningAddresses(IPEndPoint ipep) =>
NetworkInterface.GetAllNetworkInterfaces().SelectMany(adapter =>
adapter.GetIPProperties().UnicastAddresses
.Where(uip => ipep.AddressFamily == uip.Address.AddressFamily)
.Select(uip => new IPEndPoint(uip.Address, ipep.Port))
);
NetworkInterface.GetAllNetworkInterfaces()
.SelectMany(
adapter =>
adapter.GetIPProperties()
.UnicastAddresses
.Where(uip => ipep.AddressFamily == uip.Address.AddressFamily)
.Select(uip => new IPEndPoint(uip.Address, ipep.Port))
);
public static Socket CreateListener(IPEndPoint ipep)
{
@ -133,13 +149,17 @@ public static class TcpServer
socket = await listener.AcceptAsync();
var remoteIP = ((IPEndPoint)socket.RemoteEndPoint)!.Address;
attemptedConnectionsCounter.Add(1);
if (!IPLimiter.Verify(remoteIP))
{
ipLimitedConnectionsCounter.Add(1);
TraceDisconnect("Past IP limit threshold", remoteIP);
logger.Debug("{Address} Past IP limit threshold", remoteIP);
}
else if (Firewall.IsBlocked(remoteIP))
{
firewalledConnectionsCounter.Add(1);
TraceDisconnect("Firewalled", remoteIP);
logger.Debug("{Address} Firewalled", remoteIP);
}
@ -150,6 +170,7 @@ public static class TcpServer
if (args.AllowConnection)
{
allowedConnectionsCounter.Add(1);
_ = new NetState(socket);
continue;
}