mirror of
https://github.com/navidrome/navidrome.git
synced 2025-12-23 23:18:05 -05:00
feat(insights): add plugin and multi-library information (#4391)
* feat(plugins): add PluginList method Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance insights collection with plugin awareness and expanded metrics Enhanced the insights collection system to provide more comprehensive telemetry data about Navidrome installations. This update adds plugin awareness through dependency injection integration, expands configuration detection capabilities, and includes additional library metrics. Key improvements include: - Added PluginLoader interface integration to collect plugin information when enabled - Enhanced configuration detection with proper credential validation for LastFM, Spotify, and Deezer - Added new library metrics including Libraries count and smart playlist detection - Expanded configuration insights with reverse proxy, custom PID, and custom tags detection - Updated Wire dependency injection to support the new plugin loader requirement - Added corresponding data structures for plugin information collection This enhancement provides valuable insights into feature usage patterns and plugin adoption while maintaining privacy and following existing telemetry practices. * fix: correct type assertion in plugin manager test Fixed type mismatch in test where PluginManifestCapabilitiesElem was being compared with string literal. The test now properly casts the string to the correct enum type for comparison. * refactor: move static config checks to staticData function Moved HasCustomTags, ReverseProxyConfigured, and HasCustomPID configuration checks from the dynamic collect() function to the static staticData() function where they belong. This eliminates redundant computation on every insights collection cycle and implements the actual logic for HasCustomTags instead of the hardcoded false value. The HasCustomTags field now properly detects if custom tags are configured by checking the length of conf.Server.Tags. This change improves performance by computing static configuration values only once rather than on every insights collection. * feat: add granular control for insights collection Added DevEnablePluginsInsights configuration option to allow fine-grained control over whether plugin information is collected as part of the insights data. This change enhances privacy controls by allowing users to opt-out of plugin reporting while still participating in general insights collection. The implementation includes: - New configuration option DevEnablePluginsInsights with default value true - Gated plugin collection in insights.go based on both plugin enablement and permission flag - Enhanced plugin information to include version data alongside name - Improved code organization with clearer conditional logic for data collection * refactor: rename PluginNames parameter from serviceName to capability Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
@@ -87,7 +87,8 @@ type SubsonicRouter http.Handler
|
||||
type Manager interface {
|
||||
SetSubsonicRouter(router SubsonicRouter)
|
||||
EnsureCompiled(name string) error
|
||||
PluginNames(serviceName string) []string
|
||||
PluginList() map[string]schema.PluginManifest
|
||||
PluginNames(capability string) []string
|
||||
LoadPlugin(name string, capability string) WasmPlugin
|
||||
LoadMediaAgent(name string) (agents.Interface, bool)
|
||||
LoadScrobbler(name string) (scrobbler.Scrobbler, bool)
|
||||
@@ -97,7 +98,7 @@ type Manager interface {
|
||||
// managerImpl is a singleton that manages plugins
|
||||
type managerImpl struct {
|
||||
plugins map[string]*plugin // Map of plugin folder name to plugin info
|
||||
mu sync.RWMutex // Protects plugins map
|
||||
pluginsMu sync.RWMutex // Protects plugins map
|
||||
subsonicRouter atomic.Pointer[SubsonicRouter] // Subsonic API router
|
||||
schedulerService *schedulerService // Service for handling scheduled tasks
|
||||
websocketService *websocketService // Service for handling WebSocket connections
|
||||
@@ -166,7 +167,7 @@ func (m *managerImpl) registerPlugin(pluginID, pluginDir, wasmPath string, manif
|
||||
}
|
||||
|
||||
// Register the plugin first
|
||||
m.mu.Lock()
|
||||
m.pluginsMu.Lock()
|
||||
m.plugins[pluginID] = p
|
||||
|
||||
// Register one plugin adapter for each capability
|
||||
@@ -187,7 +188,7 @@ func (m *managerImpl) registerPlugin(pluginID, pluginDir, wasmPath string, manif
|
||||
}
|
||||
m.adapters[pluginID+"_"+capabilityStr] = adapter
|
||||
}
|
||||
m.mu.Unlock()
|
||||
m.pluginsMu.Unlock()
|
||||
|
||||
log.Info("Discovered plugin", "folder", pluginID, "name", manifest.Name, "capabilities", manifest.Capabilities, "wasm", wasmPath, "dev_mode", isSymlink)
|
||||
return m.plugins[pluginID]
|
||||
@@ -210,8 +211,8 @@ func (m *managerImpl) initializePluginIfNeeded(plugin *plugin) {
|
||||
|
||||
// unregisterPlugin removes a plugin from the manager
|
||||
func (m *managerImpl) unregisterPlugin(pluginID string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.pluginsMu.Lock()
|
||||
defer m.pluginsMu.Unlock()
|
||||
|
||||
plugin, ok := m.plugins[pluginID]
|
||||
if !ok {
|
||||
@@ -234,10 +235,10 @@ func (m *managerImpl) unregisterPlugin(pluginID string) {
|
||||
// ScanPlugins scans the plugins directory, discovers all valid plugins, and registers them for use.
|
||||
func (m *managerImpl) ScanPlugins() {
|
||||
// Clear existing plugins
|
||||
m.mu.Lock()
|
||||
m.pluginsMu.Lock()
|
||||
m.plugins = make(map[string]*plugin)
|
||||
m.adapters = make(map[string]WasmPlugin)
|
||||
m.mu.Unlock()
|
||||
m.pluginsMu.Unlock()
|
||||
|
||||
// Get plugins directory from config
|
||||
root := conf.Server.Plugins.Folder
|
||||
@@ -297,10 +298,24 @@ func (m *managerImpl) ScanPlugins() {
|
||||
log.Debug("Found valid plugins", "count", len(validPluginNames), "plugins", validPluginNames)
|
||||
}
|
||||
|
||||
// PluginList returns a map of all registered plugins with their manifests
|
||||
func (m *managerImpl) PluginList() map[string]schema.PluginManifest {
|
||||
m.pluginsMu.RLock()
|
||||
defer m.pluginsMu.RUnlock()
|
||||
|
||||
// Create a map to hold the plugin manifests
|
||||
pluginList := make(map[string]schema.PluginManifest, len(m.plugins))
|
||||
for name, plugin := range m.plugins {
|
||||
// Use the plugin ID as the key and the manifest as the value
|
||||
pluginList[name] = *plugin.Manifest
|
||||
}
|
||||
return pluginList
|
||||
}
|
||||
|
||||
// PluginNames returns the folder names of all plugins that implement the specified capability
|
||||
func (m *managerImpl) PluginNames(capability string) []string {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
m.pluginsMu.RLock()
|
||||
defer m.pluginsMu.RUnlock()
|
||||
|
||||
var names []string
|
||||
for name, plugin := range m.plugins {
|
||||
@@ -315,8 +330,8 @@ func (m *managerImpl) PluginNames(capability string) []string {
|
||||
}
|
||||
|
||||
func (m *managerImpl) getPlugin(name string, capability string) (*plugin, WasmPlugin, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
m.pluginsMu.RLock()
|
||||
defer m.pluginsMu.RUnlock()
|
||||
info, infoOk := m.plugins[name]
|
||||
adapter, adapterOk := m.adapters[name+"_"+capability]
|
||||
|
||||
@@ -356,9 +371,9 @@ func (m *managerImpl) LoadPlugin(name string, capability string) WasmPlugin {
|
||||
// This is useful when you need to wait for compilation without loading a specific capability,
|
||||
// such as during plugin refresh operations or health checks.
|
||||
func (m *managerImpl) EnsureCompiled(name string) error {
|
||||
m.mu.RLock()
|
||||
m.pluginsMu.RLock()
|
||||
plugin, ok := m.plugins[name]
|
||||
m.mu.RUnlock()
|
||||
m.pluginsMu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("plugin not found: %s", name)
|
||||
@@ -393,7 +408,9 @@ func (n noopManager) SetSubsonicRouter(router SubsonicRouter) {}
|
||||
|
||||
func (n noopManager) EnsureCompiled(name string) error { return nil }
|
||||
|
||||
func (n noopManager) PluginNames(serviceName string) []string { return nil }
|
||||
func (n noopManager) PluginList() map[string]schema.PluginManifest { return nil }
|
||||
|
||||
func (n noopManager) PluginNames(capability string) []string { return nil }
|
||||
|
||||
func (n noopManager) LoadPlugin(name string, capability string) WasmPlugin { return nil }
|
||||
|
||||
|
||||
@@ -65,6 +65,13 @@ var _ = Describe("Plugin Manager", func() {
|
||||
Expect(schedulerCallbackNames).To(ContainElement("multi_plugin"))
|
||||
})
|
||||
|
||||
It("should load all plugins from folder", func() {
|
||||
all := mgr.PluginList()
|
||||
Expect(all).To(HaveLen(6))
|
||||
Expect(all["fake_artist_agent"].Name).To(Equal("fake_artist_agent"))
|
||||
Expect(all["unauthorized_plugin"].Capabilities).To(HaveExactElements(schema.PluginManifestCapabilitiesElem("MetadataAgent")))
|
||||
})
|
||||
|
||||
It("should load a MetadataAgent plugin and invoke artist-related methods", func() {
|
||||
plugin := mgr.LoadPlugin("fake_artist_agent", CapabilityMetadataAgent)
|
||||
Expect(plugin).NotTo(BeNil())
|
||||
@@ -332,9 +339,9 @@ var _ = Describe("Plugin Manager", func() {
|
||||
}
|
||||
|
||||
// Register the plugin in the manager
|
||||
mgr.mu.Lock()
|
||||
mgr.pluginsMu.Lock()
|
||||
mgr.plugins[plugin.ID] = plugin
|
||||
mgr.mu.Unlock()
|
||||
mgr.pluginsMu.Unlock()
|
||||
|
||||
// Mark the plugin as initialized in the lifecycle manager
|
||||
mgr.lifecycle.markInitialized(plugin)
|
||||
@@ -344,9 +351,9 @@ var _ = Describe("Plugin Manager", func() {
|
||||
mgr.unregisterPlugin(plugin.ID)
|
||||
|
||||
// Verify that the plugin is no longer in the manager
|
||||
mgr.mu.RLock()
|
||||
mgr.pluginsMu.RLock()
|
||||
_, exists := mgr.plugins[plugin.ID]
|
||||
mgr.mu.RUnlock()
|
||||
mgr.pluginsMu.RUnlock()
|
||||
Expect(exists).To(BeFalse())
|
||||
|
||||
// Verify that the lifecycle state has been cleared
|
||||
|
||||
Reference in New Issue
Block a user