Skip to content

Commit

Permalink
Allow batch size to be configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
features-not-bugs committed Mar 2, 2021
1 parent b40460f commit 84a34c2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/RustServerMetrics/Config/ConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ class ConfigData

[JsonProperty(PropertyName = "Debug Logging")]
public bool debugLogging = false;

[JsonProperty(PropertyName = "Amount of metrics to submit in each request")]
public ushort batchSize = 400;
}
}
54 changes: 27 additions & 27 deletions src/RustServerMetrics/MetricsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ public class MetricsLogger : SingletonComponent<MetricsLogger>
readonly Dictionary<Message.Type, int> _networkUpdates = new Dictionary<Message.Type, int>();

public bool Ready { get; private set; }
ConfigData _configuration;
internal ConfigData Configuration { get; private set; }
ReportUploader _reportUploader;
Message.Type _lastMessageType;
Uri _baseUri;

public bool DebugLogging => _configuration?.debugLogging == true;
public bool DebugLogging => Configuration?.debugLogging == true;
public Uri BaseUri
{
get
{
if (_baseUri == null)
{
var uri = new Uri(_configuration.databaseUrl);
_baseUri = new Uri(uri, $"/write?db={_configuration.databaseName}&precision=ms&u={_configuration.databaseUser}&p={_configuration.databasePassword}");
var uri = new Uri(Configuration.databaseUrl);
_baseUri = new Uri(uri, $"/write?db={Configuration.databaseName}&precision=ms&u={Configuration.databaseUser}&p={Configuration.databasePassword}");
}
return _baseUri;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ override protected void Awake()
LoadConfiguration();
if (ValidateConfiguration())
{
if (!_configuration.enabled)
if (!Configuration.enabled)
{
Debug.LogWarning("[ServerMetrics]: Metrics gathering has been disabled in the configuration");
return;
Expand Down Expand Up @@ -110,7 +110,7 @@ void StatusCommand(ConsoleSystem.Arg arg)
_stringBuilder.AppendLine("[ServerMetrics]: Status");
_stringBuilder.AppendLine("Overview");
_stringBuilder.Append("\tReady: "); _stringBuilder.Append(Ready); _stringBuilder.AppendLine();
_stringBuilder.AppendLine("Report Uploader:");
_stringBuilder.AppendLine("Report Uploader:");
_stringBuilder.Append("\tRunning: "); _stringBuilder.Append(_reportUploader.IsRunning); _stringBuilder.AppendLine();
_stringBuilder.Append("\tIn Buffer: "); _stringBuilder.Append(_reportUploader.BufferSize); _stringBuilder.AppendLine();

Expand All @@ -120,7 +120,7 @@ void StatusCommand(ConsoleSystem.Arg arg)
void ReloadCfgCommand(ConsoleSystem.Arg arg)
{
LoadConfiguration();
if (!ValidateConfiguration() || _configuration.enabled == false)
if (!ValidateConfiguration() || Configuration.enabled == false)
{
Ready = false;
CancelInvoke(LogNetworkUpdates);
Expand All @@ -132,7 +132,7 @@ void ReloadCfgCommand(ConsoleSystem.Arg arg)
}
_reportUploader.Stop();

if (!_configuration.enabled)
if (!Configuration.enabled)
{
arg.ReplyWith("[ServerMetrics]: Metrics gathering has been disabled in the configuration");
return;
Expand Down Expand Up @@ -189,7 +189,7 @@ void LogNetworkUpdates()
var epochNow = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
_stringBuilder.Clear();
_stringBuilder.Append("network_updates,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" ");

var enumerator = _networkUpdates.GetEnumerator();
Expand Down Expand Up @@ -225,7 +225,7 @@ void GatherPlayerSecondStats(BasePlayer player)
var epochNow = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
_stringBuilder.Clear();
_stringBuilder.Append("connection_latency,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(",steamid=");
_stringBuilder.Append(player.UserIDString);
_stringBuilder.Append(",ip=");
Expand All @@ -249,7 +249,7 @@ internal void OnPerformanceReportGenerated()

_stringBuilder.Clear();
_stringBuilder.Append("framerate,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" instant=");
_stringBuilder.Append(current.frameRate);
_stringBuilder.Append(",average=");
Expand All @@ -259,7 +259,7 @@ internal void OnPerformanceReportGenerated()
_stringBuilder.Append("\n");

_stringBuilder.Append("frametime,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" instant=");
_stringBuilder.Append(current.frameTime);
_stringBuilder.Append(",average=");
Expand All @@ -269,7 +269,7 @@ internal void OnPerformanceReportGenerated()
_stringBuilder.Append("\n");

_stringBuilder.Append("memory,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" used=");
_stringBuilder.Append(current.memoryUsageSystem);
_stringBuilder.Append("i,collections=");
Expand All @@ -283,7 +283,7 @@ internal void OnPerformanceReportGenerated()
_stringBuilder.Append("\n");

_stringBuilder.Append("tasks,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" load_balancer=");
_stringBuilder.Append(current.loadBalancerTasks);
_stringBuilder.Append("i,invoke_handler=");
Expand All @@ -299,7 +299,7 @@ internal void OnPerformanceReportGenerated()
var packetLossLastSecond = Net.sv.GetStat(null, BaseNetwork.StatTypeLong.PacketLossLastSecond);

_stringBuilder.Append("network,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" bytes_received=");
_stringBuilder.Append(bytesReceivedLastSecond);
_stringBuilder.Append("i,bytes_sent=");
Expand All @@ -311,7 +311,7 @@ internal void OnPerformanceReportGenerated()
_stringBuilder.Append("\n");

_stringBuilder.Append("players,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" count=");
_stringBuilder.Append(BasePlayer.activePlayerList.Count);
_stringBuilder.Append("i,joining=");
Expand All @@ -323,7 +323,7 @@ internal void OnPerformanceReportGenerated()
_stringBuilder.Append("\n");

_stringBuilder.Append("entities,server=");
_stringBuilder.Append(_configuration.serverTag);
_stringBuilder.Append(Configuration.serverTag);
_stringBuilder.Append(" count=");
_stringBuilder.Append(BaseNetworkable.serverEntities.Count);
_stringBuilder.Append("i ");
Expand All @@ -333,22 +333,22 @@ internal void OnPerformanceReportGenerated()

bool ValidateConfiguration()
{
if (_configuration == null) return false;
if (Configuration == null) return false;

bool valid = true;
if (_configuration.databaseUrl == ConfigData.DEFAULT_INFLUX_DB_URL)
if (Configuration.databaseUrl == ConfigData.DEFAULT_INFLUX_DB_URL)
{
Debug.LogError("[ServerMetrics]: Default database url detected in configuration, loading aborted");
valid = false;
}

if (_configuration.databaseName == ConfigData.DEFAULT_INFLUX_DB_NAME)
if (Configuration.databaseName == ConfigData.DEFAULT_INFLUX_DB_NAME)
{
Debug.LogError("[ServerMetrics]: Default database name detected in configuration, loading aborted");
valid = false;
}

if (_configuration.serverTag == ConfigData.DEFAULT_SERVER_TAG)
if (Configuration.serverTag == ConfigData.DEFAULT_SERVER_TAG)
{
Debug.LogError("[ServerMetrics]: Default server tag detected in configuration, loading aborted");
valid = false;
Expand All @@ -362,15 +362,15 @@ void LoadConfiguration()
try
{
var configStr = File.ReadAllText(CONFIGURATION_PATH);
_configuration = JsonConvert.DeserializeObject<ConfigData>(configStr);
if (_configuration == null) _configuration = new ConfigData();
var uri = new Uri(_configuration.databaseUrl);
_baseUri = new Uri(uri, $"/write?db={_configuration.databaseName}&precision=ms&u={_configuration.databaseUser}&p={_configuration.databasePassword}");
Configuration = JsonConvert.DeserializeObject<ConfigData>(configStr);
if (Configuration == null) Configuration = new ConfigData();
var uri = new Uri(Configuration.databaseUrl);
_baseUri = new Uri(uri, $"/write?db={Configuration.databaseName}&precision=ms&u={Configuration.databaseUser}&p={Configuration.databasePassword}");
}
catch
{
Debug.LogError("[ServerMetrics]: The configuration seems to be missing or malformed. Defaults will be loaded.");
_configuration = new ConfigData();
Configuration = new ConfigData();

if (File.Exists(CONFIGURATION_PATH))
{
Expand All @@ -386,7 +386,7 @@ void SaveConfiguration()
{
var configFileInfo = new FileInfo(CONFIGURATION_PATH);
if (!configFileInfo.Directory.Exists) configFileInfo.Directory.Create();
var serializedConfiguration = JsonConvert.SerializeObject(_configuration, Formatting.Indented);
var serializedConfiguration = JsonConvert.SerializeObject(Configuration, Formatting.Indented);
File.WriteAllText(CONFIGURATION_PATH, serializedConfiguration);
}
catch (Exception ex)
Expand Down
1 change: 0 additions & 1 deletion src/RustServerMetrics/MetricsTimeWarning.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Facepunch;
using Facepunch.Math;
using System;
using System.Collections;
using System.Text;
Expand Down
4 changes: 2 additions & 2 deletions src/RustServerMetrics/ReportUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace RustServerMetrics
{
class ReportUploader : MonoBehaviour
{
const int _batchAmount = 200;
const int _sendBufferCapacity = 10000;

readonly List<string> _sendBuffer = new List<string>(_sendBufferCapacity);
Expand All @@ -21,6 +20,7 @@ class ReportUploader : MonoBehaviour
Uri _uri = null;
MetricsLogger _metricsLogger;

public ushort BatchSize => _metricsLogger.Configuration?.batchSize ?? 300;
public bool IsRunning => _isRunning;
public int BufferSize => _sendBuffer.Count;

Expand Down Expand Up @@ -52,7 +52,7 @@ IEnumerator SendBufferLoop()

while (_sendBuffer.Count > 0 && _isRunning)
{
int amountToTake = Mathf.Min(_sendBuffer.Count, _batchAmount);
int amountToTake = Mathf.Min(_sendBuffer.Count, BatchSize);
for (int i = 0; i < amountToTake; i++)
{
_payloadBuilder.Append(_sendBuffer[i]);
Expand Down

0 comments on commit 84a34c2

Please sign in to comment.