Add Prometheus with 1 metric

This commit is contained in:
runebaas 2020-04-17 23:48:50 +02:00
parent 2a616f8c5d
commit ee548390a5
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
4 changed files with 27 additions and 0 deletions

View file

@ -44,6 +44,7 @@
<PackageReference Include="NLog.Config" Version="4.6.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.1.2" />
<PackageReference Include="PokeApi.NET" Version="1.1.2" />
<PackageReference Include="prometheus-net" Version="3.5.0" />
<PackageReference Include="SharpRaven" Version="2.4.0" />
<PackageReference Include="SumoLogic.Logging.NLog" Version="1.0.1.3" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />

View file

@ -15,6 +15,7 @@ using Geekbot.net.Lib.Logger;
using Geekbot.net.Lib.ReactionListener;
using Geekbot.net.Lib.UserRepository;
using Microsoft.EntityFrameworkCore;
using Prometheus;
namespace Geekbot.net
{
@ -32,6 +33,9 @@ namespace Geekbot.net
private readonly RestApplication _applicationInfo;
private readonly List<ulong> _ignoredServers;
private readonly Counter _messageCounterPrometheus =
Metrics.CreateCounter("messages", "Number of discord messages", new CounterConfiguration() {LabelNames = new[] {"guild", "channel", "user"}});
public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis,
IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository,
IReactionListener reactionListener, RestApplication applicationInfo)
@ -146,6 +150,8 @@ namespace Geekbot.net
_messageCounterDatabaseContext.SaveChanges();
}
_messageCounterPrometheus.WithLabels(channel.Guild.Id.ToString(), channel.Id.ToString(), message.Author.Id.ToString()).Inc();
if (message.Author.IsBot) return;
_logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message));
}

View file

@ -72,5 +72,14 @@ namespace Geekbot.net.Lib
[Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")]
public string ApiPort { get; set; }
/************************************
* Prometheus *
************************************/
[Option("prometheus-host", Default = "localhost", HelpText = "Host on which the Prometheus Metric Server listens")]
public string PrometheusHost { get; set; }
[Option("prometheus-port", Default = "12991", HelpText = "Port on which the Prometheus Metric Server listens")]
public string PrometheusPort { get; set; }
}
}

View file

@ -24,6 +24,7 @@ using Geekbot.net.Lib.UserRepository;
using Geekbot.net.WebApi;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Prometheus;
using WikipediaApi;
namespace Geekbot.net
@ -185,6 +186,8 @@ namespace Geekbot.net
var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi();
StartPrometheusServer();
_logger.Information(LogSource.Geekbot, "Done and ready for use");
await webserver;
@ -211,5 +214,13 @@ namespace Geekbot.net
WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager);
return Task.CompletedTask;
}
private void StartPrometheusServer()
{
var port = int.Parse(_runParameters.PrometheusPort);
var server = new MetricServer(_runParameters.PrometheusHost, port);
server.Start();
_logger.Information(LogSource.Geekbot, $"Prometheus Metric Server running on {_runParameters.PrometheusHost}:{_runParameters.PrometheusPort}");
}
}
}