Skip to content

Commit

Permalink
Change ContainerMetadata.TryGetInode to use a newly implemented Proce…
Browse files Browse the repository at this point in the history
…ssHelpers.RunCommand, which is a synchronous rewrite of ProcessHelpers.RunCommandAsync
  • Loading branch information
zacharycmontoya committed Jan 19, 2024
1 parent bdc4556 commit b5794d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
24 changes: 4 additions & 20 deletions tracer/src/Datadog.Trace/PlatformHelpers/ContainerMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using Datadog.Trace.Logging;
using Datadog.Trace.Util;

namespace Datadog.Trace.PlatformHelpers
{
Expand Down Expand Up @@ -151,28 +151,12 @@ public static Tuple<string, string> ParseControllerAndPathFromCgroupLine(string
internal static bool TryGetInode(string path, out long result)
{
result = 0;
var context = SynchronizationContext.Current;

try
{
using var process = new Process();
process.StartInfo.FileName = "stat";
process.StartInfo.Arguments = $"--printf=%i {path}";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();

string output = process.StandardOutput.ReadToEnd();
var isExited = process.WaitForExit(1000);

if (!isExited)
{
Log.Warning("\"{FileName} {Arguments}\" did not end after 1 second.", process.StartInfo.FileName, process.StartInfo.Arguments);
return false;
}

return process.ExitCode == 0 && long.TryParse(output, out result);
var statCommand = ProcessHelpers.RunCommand(new ProcessHelpers.Command("stat", $"--printf=%i {path}"));
return long.TryParse(statCommand.Output, out result);
}
catch (Exception ex)
{
Expand Down
45 changes: 45 additions & 0 deletions tracer/src/Datadog.Trace/Util/ProcessHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Datadog.Trace.Logging;

Expand Down Expand Up @@ -52,6 +53,50 @@ public static void GetCurrentProcessInformation(out string processName, out stri
processId = CurrentProcess.Pid;
}

/// <summary>
/// Run a command and get the standard output content as a string
/// </summary>
/// <param name="command">Command to run</param>
/// <param name="input">Standard input content</param>
/// <returns>The output of the command</returns>
public static CommandOutput RunCommand(Command command, string? input = null)
{
Log.Debug("Running command: {Command} {Args}", command.Cmd, command.Arguments);
var processStartInfo = GetProcessStartInfo(command);
if (input is not null)
{
processStartInfo.RedirectStandardInput = true;
}

using var processInfo = Process.Start(processStartInfo);
if (processInfo is null)
{
return null;
}

if (input is not null)
{
processInfo.StandardInput.Write(input);
processInfo.StandardInput.Flush();
processInfo.StandardInput.Close();
}

var outputStringBuilder = new StringBuilder();
var errorStringBuilder = new StringBuilder();
while (!processInfo.HasExited)
{
outputStringBuilder.Append(processInfo.StandardOutput.ReadToEnd());
errorStringBuilder.Append(processInfo.StandardError.ReadToEnd());
Thread.Sleep(15);
}

outputStringBuilder.Append(processInfo.StandardOutput.ReadToEnd());
errorStringBuilder.Append(processInfo.StandardError.ReadToEnd());

Log.Debug<int>("Process finished with exit code: {Value}.", processInfo.ExitCode);
return new CommandOutput(outputStringBuilder.ToString(), errorStringBuilder.ToString(), processInfo.ExitCode);
}

/// <summary>
/// Run a command and get the standard output content as a string
/// </summary>
Expand Down

0 comments on commit b5794d1

Please sign in to comment.