Merge pull request #452 from Moonlight-Panel/v2.1_OpenTelemetry
Added open telemetry exporter to existing metric system. Improved config section for metrics
This commit is contained in:
@@ -6,20 +6,26 @@ namespace Moonlight.ApiServer.Configuration;
|
||||
|
||||
public record AppConfiguration
|
||||
{
|
||||
[YamlMember(Description = "The public url your instance should be accessible through")]
|
||||
[YamlMember(Description = "Moonlight configuration\n\n\nThe public url your instance should be accessible through")]
|
||||
public string PublicUrl { get; set; } = "http://localhost:5165";
|
||||
|
||||
[YamlMember(Description = "The credentials of the postgres which moonlight should use")]
|
||||
[YamlMember(Description = "\nThe credentials of the postgres which moonlight should use")]
|
||||
public DatabaseConfig Database { get; set; } = new();
|
||||
|
||||
[YamlMember(Description = "Settings regarding authentication")]
|
||||
[YamlMember(Description = "\nSettings regarding authentication")]
|
||||
public AuthenticationConfig Authentication { get; set; } = new();
|
||||
|
||||
[YamlMember(Description = "These options are only meant for development purposes")]
|
||||
[YamlMember(Description = "\nThese options are only meant for development purposes")]
|
||||
public DevelopmentConfig Development { get; set; } = new();
|
||||
|
||||
[YamlMember(Description = "\nSettings for hosting the frontend")]
|
||||
public FrontendData Frontend { get; set; } = new();
|
||||
|
||||
[YamlMember(Description = "\nSettings for the internal web server moonlight is running in")]
|
||||
public KestrelConfig Kestrel { get; set; } = new();
|
||||
public MetricsData Metrics { get; set; } = new();
|
||||
|
||||
[YamlMember(Description = "\nSettings for open telemetry")]
|
||||
public OpenTelemetryData OpenTelemetry { get; set; } = new();
|
||||
|
||||
public static AppConfiguration CreateEmpty()
|
||||
{
|
||||
@@ -91,12 +97,37 @@ public record AppConfiguration
|
||||
public string[] AllowedOrigins { get; set; } = ["*"];
|
||||
}
|
||||
|
||||
public record MetricsData
|
||||
public record OpenTelemetryData
|
||||
{
|
||||
[YamlMember(Description = "This enables the collecting of metrics and allows access to the /metrics endpoint")]
|
||||
[YamlMember(Description = "This enables open telemetry for moonlight")]
|
||||
public bool Enable { get; set; } = false;
|
||||
|
||||
public OpenTelemetryMetricsData Metrics { get; set; } = new();
|
||||
public OpenTelemetryTracesData Traces { get; set; } = new();
|
||||
public OpenTelemetryLogsData Logs { get; set; } = new();
|
||||
}
|
||||
|
||||
public record OpenTelemetryMetricsData
|
||||
{
|
||||
[YamlMember(Description = "This enables the exporting of metrics")]
|
||||
public bool Enable { get; set; } = true;
|
||||
|
||||
[YamlMember(Description = "Enables the /metrics exporter for prometheus")]
|
||||
public bool EnablePrometheus { get; set; } = false;
|
||||
|
||||
[YamlMember(Description = "The interval in which metrics are created, specified in seconds")]
|
||||
public int Interval { get; set; } = 15;
|
||||
}
|
||||
|
||||
public record OpenTelemetryTracesData
|
||||
{
|
||||
[YamlMember(Description = "This enables the exporting of traces")]
|
||||
public bool Enable { get; set; } = true;
|
||||
}
|
||||
|
||||
public record OpenTelemetryLogsData
|
||||
{
|
||||
[YamlMember(Description = "This enables the exporting of logs")]
|
||||
public bool Enable { get; set; } = true;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Moonlight.ApiServer.Database;
|
||||
@@ -11,7 +12,10 @@ using Moonlight.ApiServer.Interfaces;
|
||||
using Moonlight.ApiServer.Models;
|
||||
using Moonlight.ApiServer.Plugins;
|
||||
using Moonlight.ApiServer.Services;
|
||||
using OpenTelemetry.Logs;
|
||||
using OpenTelemetry.Metrics;
|
||||
using OpenTelemetry.Resources;
|
||||
using OpenTelemetry.Trace;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations.Startup;
|
||||
|
||||
@@ -64,7 +68,17 @@ public class CoreStartup : IPluginStartup
|
||||
|
||||
#region Prometheus
|
||||
|
||||
if (configuration.Metrics.Enable)
|
||||
if (configuration.OpenTelemetry.Enable)
|
||||
{
|
||||
var openTel = builder.Services.AddOpenTelemetry();
|
||||
var openTelConfig = configuration.OpenTelemetry;
|
||||
|
||||
var resourceBuilder = ResourceBuilder.CreateDefault();
|
||||
resourceBuilder.AddService(serviceName: "moonlight");
|
||||
|
||||
openTel.ConfigureResource(x => x.AddService(serviceName: "moonlight"));
|
||||
|
||||
if (openTelConfig.Metrics.Enable)
|
||||
{
|
||||
builder.Services.AddSingleton<MetricsBackgroundService>();
|
||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<MetricsBackgroundService>());
|
||||
@@ -72,16 +86,40 @@ public class CoreStartup : IPluginStartup
|
||||
builder.Services.AddSingleton<IMetric, ApplicationMetric>();
|
||||
builder.Services.AddSingleton<IMetric, UsersMetric>();
|
||||
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithMetrics(providerBuilder =>
|
||||
openTel.WithMetrics(providerBuilder =>
|
||||
{
|
||||
providerBuilder.AddPrometheusExporter();
|
||||
providerBuilder.AddAspNetCoreInstrumentation();
|
||||
providerBuilder.AddOtlpExporter();
|
||||
|
||||
if (openTelConfig.Metrics.EnablePrometheus)
|
||||
providerBuilder.AddPrometheusExporter();
|
||||
|
||||
providerBuilder.AddMeter("moonlight");
|
||||
});
|
||||
}
|
||||
|
||||
if (openTelConfig.Logs.Enable)
|
||||
{
|
||||
openTel.WithLogging();
|
||||
|
||||
builder.Logging.AddOpenTelemetry(options =>
|
||||
{
|
||||
options.SetResourceBuilder(resourceBuilder);
|
||||
options.AddOtlpExporter();
|
||||
});
|
||||
}
|
||||
|
||||
if (openTelConfig.Traces.Enable)
|
||||
{
|
||||
openTel.WithTracing(providerBuilder =>
|
||||
{
|
||||
providerBuilder.AddAspNetCoreInstrumentation();
|
||||
|
||||
providerBuilder.AddOtlpExporter();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Client / Frontend
|
||||
@@ -110,7 +148,7 @@ public class CoreStartup : IPluginStartup
|
||||
|
||||
#region Prometheus
|
||||
|
||||
if (configuration.Metrics.Enable)
|
||||
if (configuration.OpenTelemetry is { Enable: true, Metrics.EnablePrometheus: true })
|
||||
app.UseOpenTelemetryPrometheusScrapingEndpoint();
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
<PackageReference Include="MoonCore" Version="1.9.6"/>
|
||||
<PackageReference Include="MoonCore.Extended" Version="1.3.6"/>
|
||||
<PackageReference Include="MoonCore.PluginFramework.Generator" Version="1.0.2"/>
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.12.0-beta.1"/>
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0"/>
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0"/>
|
||||
@@ -38,10 +39,4 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.2"/>
|
||||
<PackageReference Include="Ben.Demystifier" Version="0.4.1"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Remove="storage\**\*"/>
|
||||
<Content Remove="storage\**\*"/>
|
||||
<None Remove="storage\**\*"/>
|
||||
<None Remove="Properties\launchSettings.json"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -73,7 +73,7 @@ public class MetricsBackgroundService : BackgroundService
|
||||
}
|
||||
|
||||
await Task.Delay(
|
||||
TimeSpan.FromSeconds(Configuration.Metrics.Interval),
|
||||
TimeSpan.FromSeconds(Configuration.OpenTelemetry.Metrics.Interval),
|
||||
stoppingToken
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user