-
Notifications
You must be signed in to change notification settings - Fork 166
Add continous profiler checks to dd-trace #3167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9191dc8
Add diagnostic checks for the profiler
kevingosse 7912078
Start adding tests
kevingosse 6a44933
More tests
kevingosse fd844fa
Fix assertion
kevingosse 531ee10
Fix paths
kevingosse a6480e9
Fix result of profiler tests
kevingosse 6c305fd
Fix module path
kevingosse 85a358b
Fix path on Alpine
kevingosse a96ef7c
Fix path²
kevingosse 3b874fa
Update tracer/src/Datadog.Trace.Tools.Runner/Checks/ProcessBasicCheck.cs
c9de3e7
Validate that LD_PRELOAD points to the right file
kevingosse cc19b5d
Fix test case
kevingosse 1757dc4
Fix test
kevingosse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,7 +9,6 @@ | |||||
| using System.Diagnostics; | ||||||
| using System.IO; | ||||||
| using System.Linq; | ||||||
| using System.Reflection.PortableExecutable; | ||||||
| using System.Runtime.InteropServices; | ||||||
| using Spectre.Console; | ||||||
|
|
||||||
|
|
@@ -41,11 +40,38 @@ public static bool Run(ProcessInfo process, IRegistryService? registryService = | |||||
| runtime = ProcessInfo.Runtime.NetFx; | ||||||
| } | ||||||
|
|
||||||
| var profilerModule = FindProfilerModule(process); | ||||||
| bool isContinuousProfilerEnabled; | ||||||
|
|
||||||
| if (profilerModule == null) | ||||||
| if (process.EnvironmentVariables.TryGetValue("DD_PROFILING_ENABLED", out var profilingEnabled)) | ||||||
| { | ||||||
| Utils.WriteWarning(ProfilerNotLoaded); | ||||||
| if (ParseBooleanConfigurationValue(profilingEnabled)) | ||||||
| { | ||||||
| AnsiConsole.WriteLine(ContinuousProfilerEnabled); | ||||||
| isContinuousProfilerEnabled = true; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| AnsiConsole.WriteLine(ContinuousProfilerDisabled); | ||||||
| isContinuousProfilerEnabled = false; | ||||||
| } | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| AnsiConsole.WriteLine(ContinuousProfilerNotSet); | ||||||
| isContinuousProfilerEnabled = false; | ||||||
| } | ||||||
|
|
||||||
| var loaderModule = FindLoader(process); | ||||||
| var nativeTracerModule = FindNativeTracerModule(process, loaderModule != null); | ||||||
|
|
||||||
| if (loaderModule == null) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user is running a pretty old (pre-loader DLL), then the loader isn't relevant and the user could get concerned over this message. What about strengthening the check like this?
Suggested change
|
||||||
| { | ||||||
| AnsiConsole.WriteLine(LoaderNotLoaded); | ||||||
| } | ||||||
|
|
||||||
| if (nativeTracerModule == null) | ||||||
| { | ||||||
| Utils.WriteWarning(NativeTracerNotLoaded); | ||||||
| ok = false; | ||||||
| } | ||||||
| else | ||||||
|
|
@@ -54,11 +80,16 @@ public static bool Run(ProcessInfo process, IRegistryService? registryService = | |||||
| // .so modules don't have version metadata | ||||||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||||||
| { | ||||||
| var version = FileVersionInfo.GetVersionInfo(profilerModule); | ||||||
| var version = FileVersionInfo.GetVersionInfo(nativeTracerModule); | ||||||
| AnsiConsole.WriteLine(ProfilerVersion(version.FileVersion ?? "{empty}")); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (isContinuousProfilerEnabled) | ||||||
| { | ||||||
| ok &= CheckContinousProfiler(process, loaderModule); | ||||||
| } | ||||||
|
|
||||||
| var tracerModules = FindTracerModules(process).ToArray(); | ||||||
|
|
||||||
| if (tracerModules.Length == 0) | ||||||
|
|
@@ -136,7 +167,7 @@ public static bool Run(ProcessInfo process, IRegistryService? registryService = | |||||
| ok &= CheckProfilerPath(process, runtime == ProcessInfo.Runtime.NetCore ? "CORECLR_PROFILER_PATH_32" : "COR_PROFILER_PATH_32", requiredOnLinux: false); | ||||||
| ok &= CheckProfilerPath(process, runtime == ProcessInfo.Runtime.NetCore ? "CORECLR_PROFILER_PATH_64" : "COR_PROFILER_PATH_64", requiredOnLinux: false); | ||||||
|
|
||||||
| if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) | ||||||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||||||
| { | ||||||
| if (!CheckRegistry(registryService)) | ||||||
| { | ||||||
|
|
@@ -147,6 +178,51 @@ public static bool Run(ProcessInfo process, IRegistryService? registryService = | |||||
| return ok; | ||||||
| } | ||||||
|
|
||||||
| internal static bool CheckContinousProfiler(ProcessInfo process, string? loaderModule) | ||||||
| { | ||||||
| bool ok = true; | ||||||
|
|
||||||
| var continuousProfilerModule = FindContinousProfilerModule(process); | ||||||
|
|
||||||
| if (continuousProfilerModule == null) | ||||||
| { | ||||||
| Utils.WriteWarning(ContinuousProfilerNotLoaded); | ||||||
| ok = false; | ||||||
| } | ||||||
|
|
||||||
| if (loaderModule == null) | ||||||
| { | ||||||
| Utils.WriteError(ContinuousProfilerWithoutLoader); | ||||||
| ok = false; | ||||||
| } | ||||||
|
|
||||||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||||||
| { | ||||||
| process.EnvironmentVariables.TryGetValue("LD_PRELOAD", out var ldPreload); | ||||||
|
|
||||||
| if (ldPreload == null) | ||||||
| { | ||||||
| Utils.WriteError(LdPreloadNotSet); | ||||||
| ok = false; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| if (Path.GetFileName(ldPreload) != "Datadog.Linux.ApiWrapper.x64.so") | ||||||
| { | ||||||
| Utils.WriteError(WrongLdPreload(ldPreload)); | ||||||
| ok = false; | ||||||
| } | ||||||
| else if (!File.Exists(ldPreload)) | ||||||
| { | ||||||
| Utils.WriteError(ApiWrapperNotFound(ldPreload)); | ||||||
| ok = false; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return ok; | ||||||
| } | ||||||
|
|
||||||
| internal static bool CheckRegistry(IRegistryService? registry = null) | ||||||
| { | ||||||
| registry ??= new Windows.RegistryService(); | ||||||
|
|
@@ -219,7 +295,7 @@ private static bool CheckProfilerPath(ProcessInfo process, string key, bool requ | |||||
| } | ||||||
| else if (requiredOnLinux) | ||||||
| { | ||||||
| if (!RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) | ||||||
| if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||||||
| { | ||||||
| Utils.WriteError(EnvironmentVariableNotSet(key)); | ||||||
| ok = false; | ||||||
|
|
@@ -258,7 +334,7 @@ private static bool IsExpectedProfilerFile(string fullPath) | |||||
| { | ||||||
| var fileName = Path.GetFileName(fullPath); | ||||||
|
|
||||||
| if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) | ||||||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||||||
| { | ||||||
| return "Datadog.Trace.ClrProfiler.Native.dll".Equals(fileName, StringComparison.OrdinalIgnoreCase) || | ||||||
| "Datadog.Tracer.Native.dll".Equals(fileName, StringComparison.OrdinalIgnoreCase) || | ||||||
|
|
@@ -270,18 +346,67 @@ private static bool IsExpectedProfilerFile(string fullPath) | |||||
| // Paths are case-sensitive on Linux | ||||||
| return "Datadog.Trace.ClrProfiler.Native.so".Equals(fileName, StringComparison.Ordinal) || | ||||||
| "Datadog.Tracer.Native.so".Equals(fileName, StringComparison.Ordinal) || | ||||||
| // Check for legacy names | ||||||
| // Check for legacy names | ||||||
| "Datadog.AutoInstrumentation.NativeLoader.so".Equals(fileName, StringComparison.Ordinal); | ||||||
| } | ||||||
|
|
||||||
| private static string? FindProfilerModule(ProcessInfo process) | ||||||
| private static string? FindLoader(ProcessInfo process) | ||||||
| { | ||||||
| foreach (var module in process.Modules) | ||||||
| { | ||||||
| var fileName = Path.GetFileName(module); | ||||||
|
|
||||||
| if (fileName.Equals("Datadog.Trace.ClrProfiler.Native.dll", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals("Datadog.Trace.ClrProfiler.Native.so", StringComparison.OrdinalIgnoreCase)) | ||||||
| || fileName.Equals("Datadog.Trace.ClrProfiler.Native.so", StringComparison.Ordinal)) | ||||||
| { | ||||||
| // This could be either the native tracer or the loader. | ||||||
| // If it's the loader then there should be a loader.conf file next to it. | ||||||
| var folder = Path.GetDirectoryName(module)!; | ||||||
|
|
||||||
| if (File.Exists(Path.Combine(folder, "loader.conf"))) | ||||||
| { | ||||||
| return module; | ||||||
| } | ||||||
| } | ||||||
| else if (fileName.Equals("Datadog.AutoInstrumentation.NativeLoader.x64.dll", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals("Datadog.AutoInstrumentation.NativeLoader.x86.dll", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals("Datadog.AutoInstrumentation.NativeLoader.so", StringComparison.Ordinal)) | ||||||
| { | ||||||
| return module; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| private static string? FindContinousProfilerModule(ProcessInfo process) | ||||||
| { | ||||||
| foreach (var module in process.Modules) | ||||||
| { | ||||||
| var fileName = Path.GetFileName(module); | ||||||
|
|
||||||
| if (fileName.Equals("Datadog.Profiler.Native.dll", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals("Datadog.Profiler.Native.so", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals("Datadog.AutoInstrumentation.Profiler.Native.x64.dll", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals("Datadog.AutoInstrumentation.Profiler.Native.x86.dll", StringComparison.OrdinalIgnoreCase)) | ||||||
| { | ||||||
| return module; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| private static string? FindNativeTracerModule(ProcessInfo process, bool foundLoader) | ||||||
| { | ||||||
| var expectedFileName = foundLoader ? "Datadog.Tracer.Native" : "Datadog.Trace.ClrProfiler.Native"; | ||||||
|
|
||||||
| foreach (var module in process.Modules) | ||||||
| { | ||||||
| var fileName = Path.GetFileName(module); | ||||||
|
|
||||||
| if (fileName.Equals($"{expectedFileName}.dll", StringComparison.OrdinalIgnoreCase) | ||||||
| || fileName.Equals($"{expectedFileName}.so", StringComparison.OrdinalIgnoreCase)) | ||||||
| { | ||||||
| return module; | ||||||
| } | ||||||
|
|
@@ -302,5 +427,22 @@ private static IEnumerable<string> FindTracerModules(ProcessInfo process) | |||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private static bool ParseBooleanConfigurationValue(string value) | ||||||
| { | ||||||
| var trimmedValue = value.Trim(); | ||||||
|
|
||||||
| return trimmedValue is "true" | ||||||
| or "True" | ||||||
| or "TRUE" | ||||||
| or "yes" | ||||||
| or "Yes" | ||||||
| or "YES" | ||||||
| or "t" | ||||||
| or "T" | ||||||
| or "Y" | ||||||
| or "y" | ||||||
| or "1"; | ||||||
| } | ||||||
|
andrewlock marked this conversation as resolved.
|
||||||
| } | ||||||
| } | ||||||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to run all of the ContinuousProfiler checks after identifying the loader module and the native tracer module because these console messages aren't actionable if the profiler isn't a part of the product they installed