Move prometheus metrics to it's own class

This commit is contained in:
Ahmed Rafiq 2021-11-24 22:22:42 +06:00
parent b220f1e127
commit d84db4b8e4
4 changed files with 104 additions and 53 deletions

View File

@ -35,6 +35,7 @@ using Emby.Server.Implementations.IO;
using Emby.Server.Implementations.Library;
using Emby.Server.Implementations.LiveTv;
using Emby.Server.Implementations.Localization;
using Emby.Server.Implementations.Metrics;
using Emby.Server.Implementations.Net;
using Emby.Server.Implementations.Playlists;
using Emby.Server.Implementations.Plugins;
@ -68,6 +69,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Metrics;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Persistence;
@ -102,7 +104,6 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Prometheus.DotNetRuntime;
using WebSocketManager = Emby.Server.Implementations.HttpServer.WebSocketManager;
namespace Emby.Server.Implementations
@ -517,12 +518,6 @@ namespace Emby.Server.Implementations
MigrateNetworkConfiguration();
NetManager = new NetworkManager(ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
// Initialize runtime stat collection
if (ConfigurationManager.Configuration.EnableMetrics)
{
DotNetRuntimeStatsBuilder.Default().StartCollecting();
}
var networkConfiguration = ConfigurationManager.GetNetworkConfiguration();
HttpPort = networkConfiguration.HttpServerPortNumber;
HttpsPort = networkConfiguration.HttpsPortNumber;
@ -661,6 +656,8 @@ namespace Emby.Server.Implementations
serviceCollection.AddScoped<DynamicHlsHelper>();
serviceCollection.AddScoped<IClientEventLogger, ClientEventLogger>();
serviceCollection.AddSingleton<IDirectoryService, DirectoryService>();
serviceCollection.AddSingleton<IMetricsCollector, PrometheusMetricsCollector>();
}
/// <summary>
@ -680,6 +677,12 @@ namespace Emby.Server.Implementations
var userDataRepo = (SqliteUserDataRepository)Resolve<IUserDataRepository>();
((SqliteItemRepository)Resolve<IItemRepository>()).Initialize(userDataRepo, Resolve<IUserManager>());
// Initialize runtime stat collection
if (ConfigurationManager.Configuration.EnableMetrics)
{
Resolve<IMetricsCollector>().Initialize();
}
FindParts();
}

View File

@ -30,8 +30,6 @@ using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
@ -52,7 +50,6 @@ using MediaBrowser.Model.Tasks;
using MediaBrowser.Providers.MediaInfo;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Prometheus;
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
using EpisodeInfo = Emby.Naming.TV.EpisodeInfo;
using Genre = MediaBrowser.Controller.Entities.Genre;
@ -100,16 +97,6 @@ namespace Emby.Server.Implementations.Library
private bool _wizardCompleted;
private static readonly Gauge _itemCountGauge = Metrics.CreateGauge("jellyfin_library_items_total", "The number of items in the library", new[] { "item_type" });
private static readonly string[] _metricTypes = new string[]
{
nameof(Movie),
nameof(Series), nameof(Season), nameof(Episode),
nameof(MusicArtist), nameof(MusicAlbum), nameof(MusicVideo), nameof(Audio),
nameof(Book),
nameof(PhotoAlbum), nameof(Photo)
};
/// <summary>
/// Initializes a new instance of the <see cref="LibraryManager" /> class.
/// </summary>
@ -461,18 +448,6 @@ namespace Emby.Server.Implementations.Library
_itemRepository.DeleteItem(child.Id);
}
if (_metricTypes.Contains(item.GetType().Name))
{
_itemCountGauge.WithLabels(item.GetType().Name).Dec();
}
foreach (var child in children)
{
if (_metricTypes.Contains(child.GetType().Name))
{
_itemCountGauge.WithLabels(child.GetType().Name).Dec();
}
}
_memoryCache.Remove(item.Id);
ReportItemRemoved(item, parent);
@ -811,8 +786,6 @@ namespace Emby.Server.Implementations.Library
RegisterItem(folder);
UpdateItemCountGauge();
return rootFolder;
}
@ -1140,8 +1113,6 @@ namespace Emby.Server.Implementations.Library
await RunPostScanTasks(innerProgress, cancellationToken).ConfigureAwait(false);
UpdateItemCountGauge();
progress.Report(100);
}
@ -1865,10 +1836,6 @@ namespace Emby.Server.Implementations.Library
foreach (var item in items)
{
RegisterItem(item);
if (_metricTypes.Contains(item.GetType().Name))
{
_itemCountGauge.WithLabels(item.GetType().Name).Inc();
}
}
if (ItemAdded != null)
@ -3295,18 +3262,5 @@ namespace Emby.Server.Implementations.Library
CollectionFolder.SaveLibraryOptions(virtualFolderPath, libraryOptions);
}
private void UpdateItemCountGauge()
{
foreach (var type in _metricTypes)
{
var query = new InternalItemsQuery
{
IncludeItemTypes = new[] { type }
};
int count = GetCount(query);
_itemCountGauge.WithLabels(type).Set(count);
}
}
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Metrics;
using Prometheus;
using Prometheus.DotNetRuntime;
namespace Emby.Server.Implementations.Metrics
{
/// <summary>
/// Prometheus backend for metrics.
/// </summary>
public class PrometheusMetricsCollector : IMetricsCollector
{
private readonly ILibraryManager _libraryManager;
private static readonly Gauge _itemCountGauge = Prometheus.Metrics.CreateGauge("jellyfin_library_items_total", "The number of items in the library", new[] { "item_type" });
private static readonly string[] _metricTypes = new string[]
{
nameof(Movie),
nameof(Series), nameof(Season), nameof(Episode),
nameof(MusicArtist), nameof(MusicAlbum), nameof(MusicVideo), nameof(Audio),
nameof(Book),
nameof(PhotoAlbum), nameof(Photo)
};
/// <summary>
/// Initializes a new instance of the <see cref="PrometheusMetricsCollector" /> class.
/// </summary>
/// <param name="libraryManager">The library manager</param>
public PrometheusMetricsCollector(ILibraryManager libraryManager)
{
_libraryManager = libraryManager;
_libraryManager.ItemAdded += IncrementItemCount;
_libraryManager.ItemRemoved += (s, e) => UpdateItemCount();
}
/// <inheritdoc />
public void Initialize()
{
DotNetRuntimeStatsBuilder.Default().StartCollecting();
UpdateItemCount();
}
private void UpdateItemCount()
{
foreach (var type in _metricTypes)
{
var query = new InternalItemsQuery
{
IncludeItemTypes = new[] { type }
};
int count = _libraryManager.GetCount(query);
_itemCountGauge.WithLabels(type).Set(count);
}
}
private void IncrementItemCount(object? sender, ItemChangeEventArgs e)
{
var item = e.Item;
var typeName = item.GetType().Name;
if (_metricTypes.Contains(typeName))
{
_itemCountGauge.WithLabels(typeName).Inc();
}
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Metrics
{
/// <summary>
/// Interface for metric backends.
/// </summary>
public interface IMetricsCollector
{
/// <summary>
/// Initializes metrics.
/// </summary>
public void Initialize();
}
}