Skip to content

Commit 3a9626d

Browse files
committed
avoid allocating strings and params arrays if we can't log or debug is disabled
1 parent ede7ec3 commit 3a9626d

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

tracer/src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ static Startup()
6262

6363
if (tracerHomeDirectory is null)
6464
{
65-
StartupLogger.Log($"{TracerHomePathKey} not set. Datadog SDK will be disabled.");
65+
StartupLogger.Log("{0} not set. Datadog SDK will be disabled.", TracerHomePathKey);
6666
return;
6767
}
6868

6969
ManagedProfilerDirectory = ComputeTfmDirectory(tracerHomeDirectory);
7070

7171
if (!Directory.Exists(ManagedProfilerDirectory))
7272
{
73-
StartupLogger.Log($"Datadog.Trace.dll TFM directory not found at '{ManagedProfilerDirectory}'. Datadog SDK will be disabled.");
73+
StartupLogger.Log("Datadog.Trace.dll TFM directory not found at '{0}'. Datadog SDK will be disabled.", ManagedProfilerDirectory);
7474
return;
7575
}
7676

@@ -103,14 +103,14 @@ static Startup()
103103
{
104104
// With V3, pretty much all scenarios require the trace-agent and dogstatsd, so we enable them by default
105105
const string processManagerTypeName = "Datadog.Trace.AgentProcessManager";
106-
StartupLogger.Log($"Invoking {processManagerTypeName}.{methodName}() to start external processes.");
106+
StartupLogger.Log("Invoking {0}.{1}() to start external processes.", processManagerTypeName, methodName);
107107
TryInvokeManagedMethod(processManagerTypeName, methodName, "Datadog.Trace.AgentProcessManagerLoader");
108108
}
109109

110110
// We need to initialize the managed tracer regardless of whether tracing is enabled
111111
// because other products rely on it
112112
const string instrumentationTypeName = "Datadog.Trace.ClrProfiler.Instrumentation";
113-
StartupLogger.Log($"Invoking {instrumentationTypeName}.{methodName}() to initialize instrumentation.");
113+
StartupLogger.Log("Invoking {0}.{1}() to initialize instrumentation.", instrumentationTypeName, methodName);
114114
TryInvokeManagedMethod(instrumentationTypeName, methodName, "Datadog.Trace.ClrProfiler.InstrumentationLoader");
115115
}
116116
catch (Exception ex)

tracer/src/Datadog.Trace.ClrProfiler.Managed.Loader/StartupLogger.cs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,27 @@ static StartupLogger()
4040
}
4141
}
4242

43-
public static void Log(string message, params object?[] args)
43+
public static void Log(string message)
44+
{
45+
LogCore(message, null);
46+
}
47+
48+
public static void Log(string message, object? arg0)
49+
{
50+
LogCore(message, [arg0]);
51+
}
52+
53+
public static void Log(string message, object? arg0, object? arg1)
54+
{
55+
LogCore(message, [arg0, arg1]);
56+
}
57+
58+
public static void Log(string message, object? arg0, object? arg1, object? arg2)
59+
{
60+
LogCore(message, [arg0, arg1, arg2]);
61+
}
62+
63+
private static void LogCore(string message, object?[]? args)
4464
{
4565
if (StartupLogFilePath == null)
4666
{
@@ -52,6 +72,7 @@ public static void Log(string message, params object?[] args)
5272
lock (PadLock)
5373
{
5474
using var fileSink = new FileSink(StartupLogFilePath);
75+
5576
if (DebugEnabled)
5677
{
5778
var currentDomain = AppDomain.CurrentDomain;
@@ -72,14 +93,40 @@ public static void Log(string message, params object?[] args)
7293

7394
public static void Log(Exception ex, string message, params object?[] args)
7495
{
75-
Log($"{message}{Environment.NewLine}{ex}", args);
96+
// Format the message first, then append exception
97+
var formattedMessage = args.Length > 0 ? string.Format(message, args) : message;
98+
Log("{0}{1}{2}", formattedMessage, Environment.NewLine, ex);
99+
}
100+
101+
public static void Debug(string message)
102+
{
103+
if (DebugEnabled)
104+
{
105+
LogCore(message, null);
106+
}
107+
}
108+
109+
public static void Debug(string message, object? arg0)
110+
{
111+
if (DebugEnabled)
112+
{
113+
LogCore(message, [arg0]);
114+
}
115+
}
116+
117+
public static void Debug(string message, object? arg0, object? arg1)
118+
{
119+
if (DebugEnabled)
120+
{
121+
LogCore(message, [arg0, arg1]);
122+
}
76123
}
77124

78-
public static void Debug(string message, params object?[] args)
125+
public static void Debug(string message, object? arg0, object? arg1, object? arg2)
79126
{
80127
if (DebugEnabled)
81128
{
82-
Log(message, args);
129+
LogCore(message, [arg0, arg1, arg2]);
83130
}
84131
}
85132

@@ -141,12 +188,14 @@ private static string ComputeStartupLogFilePath(string logDirectory)
141188
{
142189
// Do our best to not block other processes on write by using the process name and id
143190
using var process = Process.GetCurrentProcess();
144-
return Path.Combine(logDirectory, $"dotnet-tracer-loader-{process.ProcessName}-{process.Id}.log");
191+
var fileName = string.Concat("dotnet-tracer-loader-", process.ProcessName, "-", process.Id.ToString(), ".log");
192+
return Path.Combine(logDirectory, fileName);
145193
}
146194
catch
147195
{
148196
// We can't get the process info, use a random guid instead
149-
return Path.Combine(logDirectory, $"dotnet-tracer-loader-{Guid.NewGuid()}.log");
197+
var fileName = string.Concat("dotnet-tracer-loader-", Guid.NewGuid().ToString(), ".log");
198+
return Path.Combine(logDirectory, fileName);
150199
}
151200
}
152201
}

0 commit comments

Comments
 (0)