Exemple #1
0
        private static string ReadEnvironmentVariable(string key)
        {
            try
            {
                return(Environment.GetEnvironmentVariable(key));
            }
            catch (Exception ex)
            {
                StartupLogger.Log(ex, "Error while loading environment variable " + key);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Initializes static members of the <see cref="Startup"/> class.
        /// This method also attempts to load the Datadog.Trace.ClrProfiler.Managed .NET assembly.
        /// </summary>
        static Startup()
        {
            ManagedProfilerDirectory = ResolveManagedProfilerDirectory();

            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve_ManagedProfilerDependencies;
            }
            catch (Exception ex)
            {
                StartupLogger.Log(ex, "Unable to register a callback to the CurrentDomain.AssemblyResolve event.");
            }

            TryLoadManagedAssembly();
        }
Exemple #3
0
        private static void TryInvokeManagedMethod(string typeName, string methodName)
        {
            try
            {
                var assembly = Assembly.Load(AssemblyName);

                if (assembly != null)
                {
                    var type   = assembly.GetType(typeName, throwOnError: false);
                    var method = type?.GetRuntimeMethod(methodName, parameters: Type.EmptyTypes);
                    method?.Invoke(obj: null, parameters: null);
                }
            }
            catch (Exception ex)
            {
                StartupLogger.Log(ex, "Error when invoking managed method: {0}.{1}", typeName, methodName);
            }
        }
Exemple #4
0
        private static void TryLoadManagedAssembly()
        {
            try
            {
                var assembly = Assembly.Load("Datadog.Trace.ClrProfiler.Managed, Version=1.26.3.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb");

                if (assembly != null)
                {
                    // call method Datadog.Trace.ClrProfiler.Instrumentation.Initialize()
                    var type   = assembly.GetType("Datadog.Trace.ClrProfiler.Instrumentation", throwOnError: false);
                    var method = type?.GetRuntimeMethod("Initialize", parameters: new Type[0]);
                    method?.Invoke(obj: null, parameters: null);
                }
            }
            catch (Exception ex)
            {
                StartupLogger.Log(ex, "Error when loading managed assemblies.");
            }
        }
Exemple #5
0
        private static Assembly LoadAssembly(string assemblyString)
        {
            try
            {
                return(Assembly.Load(assemblyString));
            }
            catch (FileNotFoundException ex)
            {
                // In some IIS scenarios the `AssemblyResolve` event doesn't get triggered and we received this exception.
                // We will try to resolve it manually as a last chance.
                StartupLogger.Log(ex, "Error on assembly load: {0}, Trying to solve it manually...", assemblyString);

                var assembly = ResolveAssembly(assemblyString);
                if (assembly is not null)
                {
                    StartupLogger.Log("Assembly resolved manually.");
                }

                return(assembly);
            }
        }
Exemple #6
0
        private static void TryInvokeManagedMethod(string typeName, string methodName)
        {
            try
            {
                var assembly = LoadAssembly(AssemblyName);

                if (assembly == null)
                {
                    StartupLogger.Log("Assembly '{0}' cannot be loaded. The managed method ({1}.{2}) cannot be invoked", AssemblyName, typeName, methodName);
                    return;
                }

                var type   = assembly.GetType(typeName, throwOnError: false);
                var method = type?.GetRuntimeMethod(methodName, parameters: Type.EmptyTypes);
                method?.Invoke(obj: null, parameters: null);
            }
            catch (Exception ex)
            {
                StartupLogger.Log(ex, "Error when invoking managed method: {0}.{1}", typeName, methodName);
            }
        }
Exemple #7
0
        /// <summary>
        /// Initializes static members of the <see cref="Startup"/> class.
        /// This method also attempts to load the SignalFx.TracingTrace .NET assembly.
        /// </summary>
        static Startup()
        {
            ManagedProfilerDirectory = ResolveManagedProfilerDirectory();
            StartupLogger.Debug("Resolving managed profiler directory to: {0}", ManagedProfilerDirectory);

            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve_ManagedProfilerDependencies;
            }
            catch (Exception ex)
            {
                StartupLogger.Log(ex, "Unable to register a callback to the CurrentDomain.AssemblyResolve event.");
            }

            var runInAas = ReadBooleanEnvironmentVariable(AzureAppServicesKey, false);

            if (!runInAas)
            {
                TryInvokeManagedMethod("Datadog.Trace.ClrProfiler.Instrumentation", "Initialize");
                return;
            }

            // In AAS, the loader can be used to load the tracer, the traceagent only (if only custom tracing is enabled),
            // dogstatsd or all of them.
            var customTracingEnabled  = ReadBooleanEnvironmentVariable(AasCustomTracingKey, false);
            var needsDogStatsD        = ReadBooleanEnvironmentVariable(AasCustomMetricsKey, false);
            var automaticTraceEnabled = ReadBooleanEnvironmentVariable(TraceEnabledKey, true);

            if (automaticTraceEnabled || customTracingEnabled || needsDogStatsD)
            {
                StartupLogger.Log("Invoking managed method to start external processes.");
                TryInvokeManagedMethod("Datadog.Trace.AgentProcessManager", "Initialize");
            }

            if (automaticTraceEnabled)
            {
                StartupLogger.Log("Invoking managed tracer.");
                TryInvokeManagedMethod("Datadog.Trace.ClrProfiler.Instrumentation", "Initialize");
            }
        }