mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-06-18 04:58:12 +02:00
Merge remote-tracking branch 'holahmeds/item-count-metric'
This commit is contained in:
commit
f963ce1d58
|
@ -31,6 +31,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;
|
||||
|
@ -67,6 +68,7 @@ using MediaBrowser.Controller.Library;
|
|||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Controller.Lyrics;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
using MediaBrowser.Controller.Metrics;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Controller.Playlists;
|
||||
|
@ -464,13 +466,7 @@ namespace Emby.Server.Implementations
|
|||
|
||||
ConfigurationManager.AddParts(GetExports<IConfigurationFactory>());
|
||||
|
||||
NetManager = new NetworkManager(ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
|
||||
|
||||
// Initialize runtime stat collection
|
||||
if (ConfigurationManager.Configuration.EnableMetrics)
|
||||
{
|
||||
DotNetRuntimeStatsBuilder.Default().StartCollecting();
|
||||
}
|
||||
NetManager = new NetworkManager(ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
|
||||
|
||||
var networkConfiguration = ConfigurationManager.GetNetworkConfiguration();
|
||||
HttpPort = networkConfiguration.HttpServerPortNumber;
|
||||
|
@ -633,6 +629,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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
MediaBrowser.Controller/Metrics/IMetricsCollector.cs
Normal file
19
MediaBrowser.Controller/Metrics/IMetricsCollector.cs
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue