Skip to content

Commit

Permalink
First prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
RassK committed Apr 27, 2021
1 parent e8c6df6 commit ea62865
Show file tree
Hide file tree
Showing 20 changed files with 413 additions and 83 deletions.
21 changes: 17 additions & 4 deletions Datadog.Trace.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitattributes = .gitattributes
.gitignore = .gitignore
Datadog.Trace.proj = Datadog.Trace.proj
OpenTelemetry.AutoInstrumentation.snk = OpenTelemetry.AutoInstrumentation.snk
Directory.Build.props = Directory.Build.props
docker-compose.yml = docker-compose.yml
GlobalSuppressions.cs = GlobalSuppressions.cs
integrations.json = integrations.json
LICENSE = LICENSE
LICENSE-3rdparty.csv = LICENSE-3rdparty.csv
OpenTelemetry.AutoInstrumentation.snk = OpenTelemetry.AutoInstrumentation.snk
docs\README.md = docs\README.md
stylecop.json = stylecop.json
EndProjectSection
Expand Down Expand Up @@ -428,7 +428,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "crank", "crank", "{E3FB283A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DuplicateTypeProxy", "test\test-applications\regression\DuplicateTypeProxy\DuplicateTypeProxy.csproj", "{34B67004-7249-4EF1-8E12-6E6DA37EA6BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.AspNetCoreRazorPages", "test\test-applications\integrations\Samples.AspNetCoreRazorPages\Samples.AspNetCoreRazorPages.csproj", "{1B9E6BF4-9D48-4988-9945-248096119E46}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AspNetCoreRazorPages", "test\test-applications\integrations\Samples.AspNetCoreRazorPages\Samples.AspNetCoreRazorPages.csproj", "{1B9E6BF4-9D48-4988-9945-248096119E46}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.Vendoring", "test\test-applications\integrations\Samples.Vendoring\Samples.Vendoring.csproj", "{0D4ACA4A-44DC-4603-8992-926B5BED5D76}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Expand Down Expand Up @@ -1590,18 +1592,28 @@ Global
{34B67004-7249-4EF1-8E12-6E6DA37EA6BE}.Release|x64.Build.0 = Release|x64
{34B67004-7249-4EF1-8E12-6E6DA37EA6BE}.Release|x86.ActiveCfg = Release|x86
{34B67004-7249-4EF1-8E12-6E6DA37EA6BE}.Release|x86.Build.0 = Release|x86
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|Any CPU.Build.0 = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Debug|x64.ActiveCfg = Debug|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Debug|x64.Build.0 = Debug|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Debug|x86.ActiveCfg = Debug|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Debug|x86.Build.0 = Debug|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|Any CPU.Build.0 = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|x64.ActiveCfg = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|x64.Build.0 = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|x86.ActiveCfg = Release|Any CPU
{1B9E6BF4-9D48-4988-9945-248096119E46}.Release|x86.Build.0 = Release|Any CPU
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Debug|Any CPU.ActiveCfg = Debug|x86
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Debug|x64.ActiveCfg = Debug|x64
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Debug|x64.Build.0 = Debug|x64
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Debug|x86.ActiveCfg = Debug|x86
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Debug|x86.Build.0 = Debug|x86
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Release|Any CPU.ActiveCfg = Release|x86
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Release|x64.ActiveCfg = Release|x64
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Release|x64.Build.0 = Release|x64
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Release|x86.ActiveCfg = Release|x86
{0D4ACA4A-44DC-4603-8992-926B5BED5D76}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1727,6 +1739,7 @@ Global
{E3FB283A-B766-4887-95E1-329667671921} = {A0C5FBBB-CFB2-4FB9-B8F0-55676E9DCF06}
{34B67004-7249-4EF1-8E12-6E6DA37EA6BE} = {498A300E-D036-49B7-A43D-821D1CAF11A5}
{1B9E6BF4-9D48-4988-9945-248096119E46} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{0D4ACA4A-44DC-4603-8992-926B5BED5D76} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
36 changes: 36 additions & 0 deletions src/Datadog.Trace.ClrProfiler.Managed/Instrumentation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using Datadog.Trace.Configuration;
using Datadog.Trace.DiagnosticListeners;
Expand Down Expand Up @@ -59,6 +61,8 @@ public static void Initialize()

try
{
TryLoadVendorPlugin();

// ensure global instance is created if it's not already
_ = Tracer.Instance;
}
Expand Down Expand Up @@ -126,5 +130,37 @@ private static void StartDiagnosticManager()
DiagnosticManager.Instance = diagnosticManager;
}
#endif

private static void TryLoadVendorPlugin()
{
string pluginName = GlobalSettings.Source.VendorPluginName;
if (!string.IsNullOrWhiteSpace(pluginName))
{
string pluginPath = Path.Combine(
Path.GetDirectoryName(typeof(Instrumentation).Assembly.Location),
pluginName);

if (File.Exists(pluginPath))
{
try
{
Assembly pluginAssembly = Assembly.LoadFrom(pluginPath);

GlobalSettings.SetVendorPlugin(pluginAssembly);

Log.Information("Vendor plugin assembly loaded '{0}'.", 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}'.", pluginPath);
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/Datadog.Trace/Configuration/ConfigurationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ public static class ConfigurationKeys
/// <seealso cref="TracerSettings.PartialFlushMinSpans"/>
public const string PartialFlushMinSpans = "OTEL_TRACE_PARTIAL_FLUSH_MIN_SPANS";

/// <summary>
/// Configuration key for specifing vendor plugin path
/// </summary>
public const string VendorPluginName = "OTEL_VENDOR_PLUGIN";

/// <summary>
/// String format patterns used to match integration-specific configuration keys.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/Datadog.Trace/Configuration/DefaultFactoryConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Datadog.Trace.Configuration.Factories;
using Datadog.Trace.Factories;

namespace Datadog.Trace.Configuration
{
/// <summary>
/// Default implementation for factories.
/// </summary>
public class DefaultFactoryConfigurator : IFactoryConfigurator
{
private IPropagatorFactory _propagatorFactory;

/// <summary>
/// Gets the default propagator factory.
/// </summary>
/// <returns>Default propagator factory.</returns>
public virtual IPropagatorFactory GetPropagatorFactory()
{
return _propagatorFactory ??= new DefaultPropagatorFactory();
}
}
}
28 changes: 28 additions & 0 deletions src/Datadog.Trace/Configuration/Factories/IPropagatorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using Datadog.Trace.Conventions;
using Datadog.Trace.Propagation;

namespace Datadog.Trace.Configuration.Factories
{
/// <summary>
/// Factory interface for propagators.
/// </summary>
public interface IPropagatorFactory
{
/// <summary>
/// Gets multiple span context propagators.
/// </summary>
/// <param name="propagatorIds">List of propagator ids.</param>
/// <param name="traceIdConvention">Trace id convention.</param>
/// <returns>Context propagators.</returns>
IEnumerable<IPropagator> GetPropagators(IEnumerable<string> propagatorIds, ITraceIdConvention traceIdConvention);

/// <summary>
/// Gets propagator for propagator id.
/// </summary>
/// <param name="propagatorId">Propagator id.</param>
/// <param name="traceIdConvention">Trace id convention.</param>
/// <returns>Context propagator.</returns>
IPropagator GetPropagator(string propagatorId, ITraceIdConvention traceIdConvention);
}
}
55 changes: 55 additions & 0 deletions src/Datadog.Trace/Configuration/GlobalSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Datadog.Trace.Logging;
using Datadog.Trace.Vendors.Serilog.Events;

Expand Down Expand Up @@ -32,6 +34,11 @@ internal GlobalSettings(IConfigurationSource source)
DiagnosticSourceEnabled = source?.GetBool(ConfigurationKeys.DiagnosticSourceEnabled) ??
// default value
true;

VendorPluginName = source?.GetString(ConfigurationKeys.VendorPluginName);

// Sets initial / fallback configuration
FactoryConfigurator = new DefaultFactoryConfigurator();
}

/// <summary>
Expand All @@ -42,6 +49,21 @@ internal GlobalSettings(IConfigurationSource source)
/// <seealso cref="ConfigurationKeys.DebugEnabled"/>
public bool DebugEnabled { get; private set; }

/// <summary>
/// Gets the vendor plugin path.
/// </summary>
public string VendorPluginName { get; private set; }

/// <summary>
/// Gets a value indicating whether vendor plugin is loaded.
/// </summary>
public bool VendorPluginLoaded { get; private set; }

/// <summary>
/// Gets the vendor plugin assembly.
/// </summary>
public Assembly VendorPlugin { get; private set; }

/// <summary>
/// Gets or sets the global settings instance.
/// </summary>
Expand All @@ -55,6 +77,11 @@ internal GlobalSettings(IConfigurationSource source)
/// </summary>
internal bool DiagnosticSourceEnabled { get; }

/// <summary>
/// Gets the factory configurator.
/// </summary>
internal IFactoryConfigurator FactoryConfigurator { get; private set; }

/// <summary>
/// Set whether debug mode is enabled.
/// Affects the level of logs written to file.
Expand All @@ -74,6 +101,18 @@ public static void SetDebugEnabled(bool enabled)
}
}

/// <summary>
/// Sets a vendor plugin on successful load.
/// </summary>
/// <param name="assembly">Loaded plugin assembly</param>
public static void SetVendorPlugin(Assembly assembly)
{
Source.VendorPluginLoaded = true;
Source.VendorPlugin = assembly;

LoadVendorFactoryConfigurator();
}

/// <summary>
/// Used to refresh global settings when environment variables or config sources change.
/// This is not necessary if changes are set via code, only environment.
Expand Down Expand Up @@ -184,5 +223,21 @@ private static bool TryLoadHostingEnvironmentPath(out string hostingPath)
hostingPath = default;
return false;
}

private static void LoadVendorFactoryConfigurator()
{
if (Source.VendorPluginLoaded)
{
var factoryInterface = typeof(IFactoryConfigurator);
var factoryType = Source.VendorPlugin
.GetTypes()
.FirstOrDefault(x => factoryInterface.IsAssignableFrom(x));

if (factoryType != null)
{
Source.FactoryConfigurator = (IFactoryConfigurator)Activator.CreateInstance(factoryType);
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/Datadog.Trace/Configuration/IFactoryConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Datadog.Trace.Configuration.Factories;

namespace Datadog.Trace.Configuration
{
/// <summary>
/// Provides configuration for factories
/// </summary>
public interface IFactoryConfigurator
{
/// <summary>
/// Get the propagator factory.
/// </summary>
/// <returns>Propagator factory.</returns>
IPropagatorFactory GetPropagatorFactory();
}
}
29 changes: 0 additions & 29 deletions src/Datadog.Trace/Configuration/PropagatorType.cs

This file was deleted.

11 changes: 6 additions & 5 deletions src/Datadog.Trace/Configuration/TracerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Datadog.Trace.Configuration.Types;
using Datadog.Trace.ExtensionMethods;
using Datadog.Trace.PlatformHelpers;
using Datadog.Trace.Util;
Expand Down Expand Up @@ -369,7 +370,7 @@ public TracerSettings(IConfigurationSource source)
/// Default is <c>Datadog</c>
/// <seealso cref="ConfigurationKeys.Propagators"/>
/// </summary>
public HashSet<PropagatorType> Propagators { get; set; }
public HashSet<string> Propagators { get; set; }

/// <summary>
/// Gets or sets a value indicating whether runtime metrics
Expand Down Expand Up @@ -640,18 +641,18 @@ internal string GetServiceName(Tracer tracer, string serviceName)
return ServiceNameMappings.GetServiceName(tracer.DefaultServiceName, serviceName);
}

private static HashSet<PropagatorType> GetPropagators(IConfigurationSource source)
private static HashSet<string> GetPropagators(IConfigurationSource source)
{
var propagators = source.GetTypedValues<PropagatorType>(ConfigurationKeys.Propagators);
var propagators = source.GetStrings(ConfigurationKeys.Propagators);

if (!propagators.Any())
{
// TODO: Default to W3C (be aware of integration tests)
// see more: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md#global-propagators
return new HashSet<PropagatorType>() { PropagatorType.Datadog };
return new HashSet<string>() { PropagatorTypes.Datadog };
}

return new HashSet<PropagatorType>(propagators);
return new HashSet<string>(propagators);
}
}
}
23 changes: 23 additions & 0 deletions src/Datadog.Trace/Configuration/Types/PropagatorTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Datadog.Trace.Configuration.Types
{
/// <summary>
/// Contains default available propagator types.
/// </summary>
public static class PropagatorTypes
{
/// <summary>
/// The Datadog propagator.
/// </summary>
public const string Datadog = "Datadog";

/// <summary>
/// The B3 propagator
/// </summary>
public const string B3 = "B3";

/// <summary>
/// The W3C propagator
/// </summary>
public const string W3C = "W3C";
}
}
Loading

0 comments on commit ea62865

Please sign in to comment.