Expose git version in 'pk;stats' and '/v1/meta'

This commit is contained in:
spiral 2021-08-01 15:22:23 -04:00
parent dcc15dc847
commit 1cb4bc9287
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
9 changed files with 54 additions and 5 deletions

View File

@ -6,6 +6,7 @@
!Myriad/ !Myriad/
!PluralKit.sln !PluralKit.sln
!nuget.config !nuget.config
!.git
# Re-exclude host build artifact directories # Re-exclude host build artifact directories
**/bin **/bin

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ pluralkit.*.conf
# Generated # Generated
logs/ logs/
.version

View File

@ -8,6 +8,7 @@ COPY PluralKit.API/PluralKit.API.csproj /app/PluralKit.API/
COPY PluralKit.Bot/PluralKit.Bot.csproj /app/PluralKit.Bot/ COPY PluralKit.Bot/PluralKit.Bot.csproj /app/PluralKit.Bot/
COPY PluralKit.Core/PluralKit.Core.csproj /app/PluralKit.Core/ COPY PluralKit.Core/PluralKit.Core.csproj /app/PluralKit.Core/
COPY PluralKit.Tests/PluralKit.Tests.csproj /app/PluralKit.Tests/ COPY PluralKit.Tests/PluralKit.Tests.csproj /app/PluralKit.Tests/
COPY .git/ /app/.git
RUN dotnet restore PluralKit.sln RUN dotnet restore PluralKit.sln
# Copy the rest of the code and build # Copy the rest of the code and build

View File

@ -32,6 +32,7 @@ namespace PluralKit.API
var o = new JObject(); var o = new JObject();
o.Add("shards", shards.ToJSON()); o.Add("shards", shards.ToJSON());
o.Add("version", BuildInfoService.Version);
return Ok(o); return Ok(o);
} }

View File

@ -1,4 +1,6 @@
using Autofac.Extensions.DependencyInjection; using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -12,10 +14,11 @@ namespace PluralKit.API
{ {
public class Program public class Program
{ {
public static void Main(string[] args) public static async Task Main(string[] args)
{ {
InitUtils.InitStatic(); InitUtils.InitStatic();
CreateHostBuilder(args).Build().Run(); await BuildInfoService.LoadVersion();
await CreateHostBuilder(args).Build().RunAsync();
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>

View File

@ -92,7 +92,8 @@ namespace PluralKit.Bot {
var process = Process.GetCurrentProcess(); var process = Process.GetCurrentProcess();
var memoryUsage = process.WorkingSet64; var memoryUsage = process.WorkingSet64;
var shardUptime = SystemClock.Instance.GetCurrentInstant() - shardInfo.LastConnectionTime; var now = SystemClock.Instance.GetCurrentInstant();
var shardUptime = now - shardInfo.LastConnectionTime;
var embed = new EmbedBuilder(); var embed = new EmbedBuilder();
if (messagesReceived != null) embed.Field(new("Messages processed",$"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)", true)); if (messagesReceived != null) embed.Field(new("Messages processed",$"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)", true));
@ -105,7 +106,9 @@ namespace PluralKit.Bot {
.Field(new("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true)) .Field(new("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true))
.Field(new("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true)) .Field(new("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true))
.Field(new("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true)) .Field(new("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true))
.Field(new("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalGroups:N0} groups, {totalSwitches:N0} switches, {totalMessages:N0} messages")); .Field(new("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalGroups:N0} groups, {totalSwitches:N0} switches, {totalMessages:N0} messages"))
.Timestamp(now.ToDateTimeOffset().ToString("O"))
.Footer(new($"PluralKit {BuildInfoService.Version} • https://github.com/xSke/PluralKit"));;
await ctx.Rest.EditMessage(msg.ChannelId, msg.Id, await ctx.Rest.EditMessage(msg.ChannelId, msg.Id,
new MessageEditRequest {Content = "", Embed = embed.Build()}); new MessageEditRequest {Content = "", Embed = embed.Build()});
} }

View File

@ -30,6 +30,9 @@ namespace PluralKit.Bot
return RunWrapper(services, async ct => return RunWrapper(services, async ct =>
{ {
// init version service
await BuildInfoService.LoadVersion();
var logger = services.Resolve<ILogger>().ForContext<Init>(); var logger = services.Resolve<ILogger>().ForContext<Init>();
// Initialize Sentry SDK, and make sure it gets dropped at the end // Initialize Sentry SDK, and make sure it gets dropped at the end

View File

@ -48,4 +48,16 @@
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Database/**/*.sql" /> <EmbeddedResource Include="Database/**/*.sql" />
</ItemGroup> </ItemGroup>
<Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation">
<Exec
Command="git describe --long --always --dirty --exclude=* --abbrev=8 > ../.version"
IgnoreExitCode="False"
>
</Exec>
</Target>
<ItemGroup>
<EmbeddedResource Include="..\.version" LogicalName="version" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,24 @@
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace PluralKit.Core
{
public static class BuildInfoService
{
public static string Version { get; private set; }
public static async Task LoadVersion()
{
using (var stream = typeof(BuildInfoService).Assembly.GetManifestResourceStream("version"))
{
// if this happens, something broke
if (stream == null) Version = "(unknown version) ";
else using (var reader = new StreamReader(stream)) Version = await reader.ReadToEndAsync();
}
// cheap hack to remove newline
Version = Version.Remove(Version.Length - 1);
}
}
}