Skip to content

Commit

Permalink
feat: Add voice server allocation metric
Browse files Browse the repository at this point in the history
  • Loading branch information
angelobreuer committed Mar 17, 2024
1 parent 19b73e4 commit c642e78
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Lavalink4NET/Players/LavalinkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class LavalinkPlayer : ILavalinkPlayer, ILavalinkPlayerListener
private volatile ITrackQueueItem? _replacedItem;
private volatile ITrackQueueItem? _nextItem;
private Counter<int>? _previousStateCounter;
private string? _previousVoiceServer;

public LavalinkPlayer(IPlayerProperties<LavalinkPlayer, LavalinkPlayerOptions> properties)
{
Expand Down Expand Up @@ -488,6 +490,12 @@ protected virtual async ValueTask DisposeAsyncCore()
return;
}

if (_previousVoiceServer is not null)
{
Diagnostics.VoiceServer.Add(-1, KeyValuePair.Create<string, object?>("server", _previousVoiceServer));
_previousVoiceServer = null;
}

await UpdateStateAsync(PlayerState.Destroyed).ConfigureAwait(false);

// Dispose the lifecycle to notify the player is being destroyed
Expand Down Expand Up @@ -641,7 +649,7 @@ private ValueTask UpdateVoiceCredentialsAsync(CancellationToken cancellationToke
{
cancellationToken.ThrowIfCancellationRequested();

if (_disposed is 1 || VoiceServer is null || VoiceState.SessionId is null)
if (_disposed is 1 || VoiceServer is null || VoiceServer is { Endpoint: null, } or { Token: null, } || VoiceState.SessionId is null)
{
return ValueTask.CompletedTask;
}
Expand All @@ -654,6 +662,23 @@ private ValueTask UpdateVoiceCredentialsAsync(CancellationToken cancellationToke
SessionId: VoiceState.SessionId),
};

var voiceServerName = GetVoiceServerName(VoiceServer.Value);

if (!StringComparer.Ordinal.Equals(voiceServerName, _previousVoiceServer))
{
if (_previousVoiceServer is not null)
{
Diagnostics.VoiceServer.Add(-1, KeyValuePair.Create<string, object?>("server", _previousVoiceServer));
}

if (voiceServerName is not null)
{
Diagnostics.VoiceServer.Add(1, KeyValuePair.Create<string, object?>("server", voiceServerName));
}

_previousVoiceServer = voiceServerName;
}

return PerformUpdateAsync(properties, cancellationToken);
}

Expand All @@ -675,6 +700,11 @@ private ValueTask UpdateVoiceCredentialsAsync(CancellationToken cancellationToke

return new TrackQueueItem(new TrackReference(track));
}

private static string? GetVoiceServerName(VoiceServer voiceServer)
{
return voiceServer.Endpoint.Split('.', StringSplitOptions.TrimEntries).FirstOrDefault();
}
}

internal static partial class Logging
Expand Down Expand Up @@ -719,11 +749,14 @@ static Diagnostics()
PausedPlayers = meter.CreateCounter<int>("paused-players");
NotPlayingPlayers = meter.CreateCounter<int>("not-playing-players");
PlayingPlayers = meter.CreateCounter<int>("playing-players");
VoiceServer = meter.CreateCounter<int>("voice-server");
}

public static Counter<int> PausedPlayers { get; }

public static Counter<int> NotPlayingPlayers { get; }

public static Counter<int> PlayingPlayers { get; }

public static Counter<int> VoiceServer { get; }
}

0 comments on commit c642e78

Please sign in to comment.