-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use plugin manager to organize plugin loading. Add possibility to loa…
…d multiple plugins.
- Loading branch information
Showing
20 changed files
with
340 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
src/Datadog.Trace/Configuration/DefaultFactoryConfigurator.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Datadog.Trace.Propagation; | ||
|
||
namespace Datadog.Trace.Plugins | ||
{ | ||
/// <summary> | ||
/// Provides extendibility points for tracer | ||
/// </summary> | ||
public interface IOTelPlugin | ||
{ | ||
/// <summary> | ||
/// Get the propagator provider. | ||
/// </summary> | ||
/// <returns>Propagator factory.</returns> | ||
IPropagatorsProvider GetPropagatorsProvider(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Datadog.Trace.Configuration; | ||
using Datadog.Trace.Logging; | ||
using Datadog.Trace.Util; | ||
using Datadog.Trace.Vendors.Newtonsoft.Json.Linq; | ||
|
||
namespace Datadog.Trace.Plugins | ||
{ | ||
internal static class PluginManager | ||
{ | ||
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(PluginManager)); | ||
|
||
public static IReadOnlyCollection<IOTelPlugin> Loaded { get; private set; } = ArrayHelper.Empty<IOTelPlugin>(); | ||
|
||
internal static void TryLoadPlugins(JsonConfigurationSource pluginsConfig) | ||
{ | ||
if (pluginsConfig == null) | ||
{ | ||
return; | ||
} | ||
|
||
var runtimeName = GetPluginsRuntimeName(); | ||
|
||
Log.Debug("Executing plugins configuration: {0}", pluginsConfig); | ||
Log.Information("Trying to load plugins for '{0}' runtime.", runtimeName); | ||
|
||
// TODO: Detect if objects instead of path | ||
var pluginFiles = pluginsConfig?.GetValue<JToken>($"['{runtimeName}']").ToObject<string[]>(); | ||
|
||
if (pluginFiles == null || !pluginFiles.Any()) | ||
{ | ||
Log.Information("Skipping plugins load. Could not find any plugins for '{0}' runtime.", runtimeName); | ||
|
||
return; | ||
} | ||
|
||
var loadedPlugins = TryLoadPlugins(pluginFiles); | ||
|
||
Log.Information("Successfully loaded '{0}' plugin(s).", property: loadedPlugins.Count); | ||
|
||
Loaded = loadedPlugins; | ||
} | ||
|
||
private static IReadOnlyCollection<IOTelPlugin> TryLoadPlugins(string[] pluginFiles) | ||
{ | ||
var loaded = new List<IOTelPlugin>(); | ||
|
||
foreach (string file in pluginFiles) | ||
{ | ||
string fullPath = Path.GetFullPath(file); | ||
|
||
if (File.Exists(fullPath)) | ||
{ | ||
try | ||
{ | ||
Assembly pluginAssembly = Assembly.LoadFrom(fullPath); | ||
IOTelPlugin plugin = ConvertToPlugin(pluginAssembly); | ||
|
||
if (plugin != null) | ||
{ | ||
loaded.Add(plugin); | ||
|
||
Log.Information("Plugin assembly loaded '{0}'.", pluginAssembly.FullName); | ||
} | ||
else | ||
{ | ||
Log.Warning("Could not load {0} from '{1}'.", nameof(IOTelPlugin), pluginAssembly.FullName); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Warning(ex, "Plugin assembly could not be loaded."); | ||
Log.Information("Skipping vendor plugin load."); | ||
} | ||
} | ||
else | ||
{ | ||
Log.Warning("Plugin path is defined but could not find the path '{0}'.", fullPath); | ||
} | ||
} | ||
|
||
return loaded; | ||
} | ||
|
||
private static IOTelPlugin ConvertToPlugin(Assembly assembly) | ||
{ | ||
var pluginType = typeof(IOTelPlugin); | ||
|
||
var pluginInstance = assembly | ||
.GetTypes() | ||
.Where(p => pluginType.IsAssignableFrom(p)) | ||
.Select(p => (IOTelPlugin)Activator.CreateInstance(p)) | ||
.FirstOrDefault(); | ||
|
||
return pluginInstance; | ||
} | ||
|
||
private static string GetPluginsRuntimeName() | ||
{ | ||
// returns the runtime directory of the current running tracer | ||
return Directory.GetParent(typeof(PluginManager).Assembly.Location).Name; | ||
} | ||
} | ||
} |
Oops, something went wrong.