Skip to content

Commit

Permalink
Refactor to log deprecated environment variables one time during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Oct 8, 2024
1 parent a212ef8 commit b2a602e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
31 changes: 26 additions & 5 deletions src/Agent/NewRelic/Agent/Core/AgentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;

Expand Down Expand Up @@ -246,6 +247,7 @@ private void LogInitialized()
"NEW_RELIC_PROCESS_HOST_DISPLAY_NAME",
"NEW_RELIC_IGNORE_SERVER_SIDE_CONFIG",
"NEW_RELIC_LOG",
"NEW_RELIC_LOG_DIRECTORY",
"NEW_RELIC_PROFILER_LOG_DIRECTORY",
"NEW_RELIC_LOG_LEVEL",
"NEW_RELIC_LOG_ENABLED",
Expand Down Expand Up @@ -286,13 +288,23 @@ private void LogInitialized()
"NEW_RELIC_SEND_DATA_ON_EXIT",
"NEW_RELIC_SEND_DATA_ON_EXIT_THRESHOLD_MS",
"NEW_RELIC_AZURE_FUNCTION_MODE_ENABLED",
"CORECLR_NEWRELIC_HOME",
"NEWRELIC_HOME",
"NEWRELIC_INSTALL_PATH",
"NEWRELIC_PROFILER_LOG_DIRECTORY",
"NEWRELIC_LOG_LEVEL",
};

List<(string,string)> environmentVariablesDeprecated = new List<(string, string)>
{
("CORECLR_NEWRELIC_HOME","CORECLR_NEW_RELIC_HOME"),
("NEWRELIC_HOME", "NEW_RELIC_HOME"),
("NEWRELIC_INSTALL_PATH", "NEW_RELIC_INSTALL_PATH"),
("NEWRELIC_LOG_DIRECTORY", "NEW_RELIC_LOG_DIRECTORY"),
("NEWRELIC_LOG_LEVEL", "NEW_RELIC_LOG_LEVEL"),
("NEWRELIC_PROFILER_LOG_DIRECTORY", "NEW_RELIC_PROFILER_LOG_DIRECTOR"),
};

// so we can report the values as expected
environmentVariables.AddRange(environmentVariablesDeprecated.Select(tuple => tuple.Item1));
// so we can report deprecated name but not log the value
environmentVariablesDeprecated.Add(("NEWRELIC_LICENSEKEY", "NEW_RELIC_LICENSE_KEY"));

List<string> environmentVariablesSensitive = new List<string> {
"NEW_RELIC_LICENSE_KEY",
"NEWRELIC_LICENSEKEY",
Expand All @@ -319,6 +331,15 @@ private void LogInitialized()
}
}

foreach (var ev in environmentVariablesDeprecated)
{
if (!string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable(ev.Item1)))
{
Log.Warn("Environment Variable {OldName} is deprecated and may be removed in a future major version. Please use {NewName} instead.", ev.Item1, ev.Item2);
}
}


Log.Debug($".NET Runtime Version: {RuntimeInformation.FrameworkDescription}");
}

Expand Down
15 changes: 1 addition & 14 deletions src/Agent/NewRelic/Agent/Core/SharedInterfaces/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public string GetEnvironmentVariable(string variable)
public string GetEnvironmentVariableFromList(params string[] variables)
{
var envValue = (variables ?? Enumerable.Empty<string>())
.Select(v => GetEnvironmentVariableInternal(v))
.Select(System.Environment.GetEnvironmentVariable)
.FirstOrDefault(value => value != null);

return envValue == string.Empty ? null : envValue;
Expand All @@ -35,19 +35,6 @@ public string GetEnvironmentVariable(string variable, EnvironmentVariableTarget
return System.Environment.GetEnvironmentVariable(variable, environmentVariableTarget);
}

private string GetEnvironmentVariableInternal(string variable, EnvironmentVariableTarget environmentVariableTarget = EnvironmentVariableTarget.Process)
{
var value = System.Environment.GetEnvironmentVariable(variable, environmentVariableTarget);

if (value != null && variable.StartsWith("NEWRELIC_", StringComparison.OrdinalIgnoreCase))
{
var preferredVariableName = variable.ToUpper().Replace("NEWRELIC_", "NEW_RELIC_");
Log.Warn($"The environment variable {variable} is deprecated and may be removed in a future major version. Please use {preferredVariableName} instead.");
}

return value;
}

public Dictionary<string, string> GetEnvironmentVariablesWithPrefix(string prefix)
{
var environmentVariables = System.Environment.GetEnvironmentVariables();
Expand Down

0 comments on commit b2a602e

Please sign in to comment.