Skip to content

Commit

Permalink
Misc monitoring plugin improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Jan 7, 2025
1 parent 289010b commit 1302fd8
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions ArchiSteamFarm.OfficialPlugins.Monitoring/MonitoringPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -47,30 +46,13 @@ namespace ArchiSteamFarm.OfficialPlugins.Monitoring;

[Export(typeof(IPlugin))]
[SuppressMessage("ReSharper", "MemberCanBeFileLocal")]
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialGitHubPluginUpdates, IWebInterface, IWebServiceProvider, IBotTradeOfferResults {
internal sealed class MonitoringPlugin : OfficialPlugin, IBot, IBotTradeOfferResults, IDisposable, IOfficialGitHubPluginUpdates, IWebInterface, IWebServiceProvider {
private const string MeterName = SharedInfo.AssemblyName;

private const string MetricNamePrefix = "asf";

private const string UnknownLabelValueFallback = "unknown";

private static readonly Measurement<byte> BuildInfo = new(
1,
new KeyValuePair<string, object?>(TagNames.Version, SharedInfo.Version.ToString()),
new KeyValuePair<string, object?>(TagNames.Variant, Core.BuildInfo.Variant)
);

private static readonly Measurement<byte> RuntimeInfo = new(
1,
new KeyValuePair<string, object?>(TagNames.Framework, OS.Framework ?? UnknownLabelValueFallback),
new KeyValuePair<string, object?>(TagNames.Runtime, OS.Runtime ?? UnknownLabelValueFallback),
new KeyValuePair<string, object?>(TagNames.OS, OS.Description ?? UnknownLabelValueFallback)
);

private static bool Enabled => ASF.GlobalConfig?.IPC ?? GlobalConfig.DefaultIPC;

private static FrozenSet<Measurement<int>>? PluginMeasurements;

[JsonInclude]
public override string Name => nameof(MonitoringPlugin);

Expand All @@ -85,6 +67,20 @@ internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialG

public void Dispose() => Meter?.Dispose();

public Task OnBotDestroy(Bot bot) {
ArgumentNullException.ThrowIfNull(bot);

TradeStatistics.TryRemove(bot, out _);

return Task.CompletedTask;
}

public Task OnBotInit(Bot bot) {
ArgumentNullException.ThrowIfNull(bot);

return Task.CompletedTask;
}

public Task OnBotTradeOfferResults(Bot bot, IReadOnlyCollection<ParseTradeResult> tradeResults) {
ArgumentNullException.ThrowIfNull(bot);
ArgumentNullException.ThrowIfNull(tradeResults);
Expand Down Expand Up @@ -117,6 +113,10 @@ public void OnConfiguringServices(IServiceCollection services) {

InitializeMeter();

if (Meter == null) {
throw new InvalidOperationException(nameof(Meter));
}

services.AddOpenTelemetry().WithMetrics(
builder => {
builder.AddPrometheusExporter(static config => config.ScrapeEndpointPath = "/Api/metrics");
Expand All @@ -130,29 +130,31 @@ public void OnConfiguringServices(IServiceCollection services) {

public override Task OnLoaded() => Task.CompletedTask;

[MemberNotNull(nameof(Meter))]
private void InitializeMeter() {
if (Meter != null) {
return;
}

PluginMeasurements = new HashSet<Measurement<int>>(3) {
new(PluginsCore.ActivePlugins.Count),
new(PluginsCore.ActivePlugins.Count(static plugin => plugin is OfficialPlugin), new KeyValuePair<string, object?>(TagNames.PluginType, "official")),
new(PluginsCore.ActivePlugins.Count(static plugin => plugin is not OfficialPlugin), new KeyValuePair<string, object?>(TagNames.PluginType, "custom"))
}.ToFrozenSet();

Meter = new Meter(MeterName, Version.ToString());

Meter.CreateObservableGauge(
$"{MetricNamePrefix}_build_info",
static () => BuildInfo,
static () => new Measurement<byte>(
1,
new KeyValuePair<string, object?>(TagNames.Version, SharedInfo.Version.ToString()),
new KeyValuePair<string, object?>(TagNames.Variant, BuildInfo.Variant)
),
description: "Build information about ASF in form of label values"
);

Meter.CreateObservableGauge(
$"{MetricNamePrefix}_runtime_info",
static () => RuntimeInfo,
static () => new Measurement<byte>(
1,
new KeyValuePair<string, object?>(TagNames.Framework, OS.Framework ?? UnknownLabelValueFallback),
new KeyValuePair<string, object?>(TagNames.Runtime, OS.Runtime ?? UnknownLabelValueFallback),
new KeyValuePair<string, object?>(TagNames.OS, OS.Description ?? UnknownLabelValueFallback)
),
description: "Runtime information about ASF in form of label values"
);

Expand All @@ -164,7 +166,11 @@ private void InitializeMeter() {

Meter.CreateObservableGauge(
$"{MetricNamePrefix}_active_plugins",
static () => PluginMeasurements,
static () => new HashSet<Measurement<int>>(3) {
new(PluginsCore.ActivePlugins.Count),
new(PluginsCore.ActivePlugins.Count(static plugin => plugin is OfficialPlugin), new KeyValuePair<string, object?>(TagNames.PluginType, "official")),
new(PluginsCore.ActivePlugins.Count(static plugin => plugin is not OfficialPlugin), new KeyValuePair<string, object?>(TagNames.PluginType, "custom"))
},
description: "Number of plugins currently loaded in ASF"
);

Expand Down

0 comments on commit 1302fd8

Please sign in to comment.