Browse Source

Merge pull request #2985 from sparky8251/prometheus

Add Prometheus exporters
Bond-009 5 years ago
parent
commit
690fb65cd8

+ 7 - 0
Emby.Server.Implementations/ApplicationHost.cs

@@ -106,6 +106,7 @@ using Microsoft.AspNetCore.Http.Extensions;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
+using Prometheus.DotNetRuntime;
 
 namespace Emby.Server.Implementations
 {
@@ -259,6 +260,12 @@ namespace Emby.Server.Implementations
 
             _startupOptions = options;
 
+            // Initialize runtime stat collection
+            if (ServerConfigurationManager.Configuration.EnableMetrics)
+            {
+                DotNetRuntimeStatsBuilder.Default().StartCollecting();
+            }
+
             fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
 
             _networkManager.NetworkChanged += OnNetworkChanged;

+ 1 - 0
Emby.Server.Implementations/Emby.Server.Implementations.csproj

@@ -39,6 +39,7 @@
     <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.3" />
     <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.3" />
     <PackageReference Include="Mono.Nat" Version="2.0.1" />
+    <PackageReference Include="prometheus-net.DotNetRuntime" Version="3.3.1" />
     <PackageReference Include="ServiceStack.Text.Core" Version="5.8.0" />
     <PackageReference Include="sharpcompress" Version="0.25.0" />
     <PackageReference Include="SQLitePCL.pretty.netstandard" Version="2.1.0" />

+ 2 - 0
Jellyfin.Server/Jellyfin.Server.csproj

@@ -43,6 +43,8 @@
     <PackageReference Include="CommandLineParser" Version="2.7.82" />
     <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.3" />
     <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
+    <PackageReference Include="prometheus-net" Version="3.5.0" />
+    <PackageReference Include="prometheus-net.AspNetCore" Version="3.5.0" />
     <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
     <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
     <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />

+ 11 - 0
Jellyfin.Server/Startup.cs

@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
+using Prometheus;
 
 namespace Jellyfin.Server
 {
@@ -69,9 +70,19 @@ namespace Jellyfin.Server
             app.UseJellyfinApiSwagger();
             app.UseRouting();
             app.UseAuthorization();
+            if (_serverConfigurationManager.Configuration.EnableMetrics)
+            {
+                // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
+                app.UseHttpMetrics();
+            }
+
             app.UseEndpoints(endpoints =>
             {
                 endpoints.MapControllers();
+                if (_serverConfigurationManager.Configuration.EnableMetrics)
+                {
+                    endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
+                }
             });
 
             app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);

+ 6 - 0
MediaBrowser.Model/Configuration/ServerConfiguration.cs

@@ -19,6 +19,11 @@ namespace MediaBrowser.Model.Configuration
         /// </summary>
         public bool EnableUPnP { get; set; }
 
+        /// <summary>
+        /// Gets or sets a value indicating whether to enable prometheus metrics exporting.
+        /// </summary>
+        public bool EnableMetrics { get; set; }
+
         /// <summary>
         /// Gets or sets the public mapped port.
         /// </summary>
@@ -246,6 +251,7 @@ namespace MediaBrowser.Model.Configuration
             PublicHttpsPort = DefaultHttpsPort;
             HttpServerPortNumber = DefaultHttpPort;
             HttpsPortNumber = DefaultHttpsPort;
+            EnableMetrics = false;
             EnableHttps = false;
             EnableDashboardResponseCaching = true;
             EnableCaseSensitiveItemIds = true;