Skip to content

Commit

Permalink
Profiler updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Oct 7, 2024
1 parent 3bd7c28 commit e17be16
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te

auto systemCalls = std::make_shared<NewRelic::Profiler::Logger::Test::SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_LOG_LEVEL"] = L"FiNeSt";
systemCalls->environmentVariables[L"NEW_RELIC_LOG_LEVEL"] = L"FiNeSt";

Configuration configuration(configurationXml, _missingConfig, L"", systemCalls);
Assert::AreEqual(Logger::Level::LEVEL_TRACE, configuration.GetLoggingLevel());
Expand Down
145 changes: 1 addition & 144 deletions src/Agent/NewRelic/Profiler/Logging/DefaultFileLogLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace NewRelic { namespace Profiler { namespace Logger
return GetAzureWebSiteLogDirectory();
}

// if there is a NEWRELIC_HOME environment variable log relative to it
// if there is a NEW_RELIC_HOME environment variable log relative to it
logDirectory = _system->GetNewRelicHomePath();
if (logDirectory)
{
Expand Down Expand Up @@ -123,148 +123,5 @@ namespace NewRelic { namespace Profiler { namespace Logger
return envVarValue.find(searchValue) != std::string::npos;
}
};

#ifdef __NEVER__
class FileDestination : public IDestination
{
public:
FileDestination(IFileDestinationSystemCallsPtr system) :
_system(system)
{
#ifdef PAL_STDCPP_COMPAT
auto logFilePath = GetLogFilePath();
auto fileName = xstring_t(logFilePath) + PATH_SEPARATOR + _X("NewRelic.Profiler.") + to_xstring(_system->GetCurrentProcessId()) + _X(".log");

if (!_system->DirectoryExists(logFilePath))
{
_system->DirectoryCreate(logFilePath);
}

_fileStream = _system->OpenFile(fileName, std::ios_base::trunc);
#else
// name the file
xostringstream fileName;
fileName.exceptions(xostringstream::failbit | xostringstream::badbit);
long processId = _system->GetCurrentProcessId();
auto logFilePath = GetLogFilePath();
fileName.width(2);
fileName.fill('0');
fileName.setf(std::ios::right);
fileName << logFilePath << PATH_SEPARATOR << "NewRelic.Profiler." << processId << ".log";

if(!_system->DirectoryExists(logFilePath))
{
_system->DirectoryCreate(logFilePath);
}

// Since we name with PID, eventually we are going to re-use file names. This is okay since we don't want to flood their logs directory but shouldn't append because that would be confusing so we delete the previous and create a new one when a PID is reused.
_fileStream = _system->OpenFile(fileName.str(), std::ios_base::trunc);
#endif

_fileStream->exceptions(std::wostream::failbit | std::wostream::badbit);
}

virtual ~FileDestination()
{
_system->CloseFile(_fileStream);
}

//IDestination deprecated
//void Flush() override
//{
// _fileStream->flush();
//}

//void Write(const std::wstring& message) override
//{
// *_fileStream << message;
//}

private:
// returns path to an existing directory where the log file should be written
xstring_t GetLogFilePath()
{
// use the environment variable if it is set
auto logDirectory = _system->TryGetEnvironmentVariable(GetLogDirectoryEnvironmentVariableName());
if (logDirectory != nullptr)
{
return *logDirectory;
}

// if this is Azure WebSites use the special case for that
if (IsAzureWebSites())
{
return GetAzureWebSiteLogDirectory();
}

// if there is a NEWRELIC_HOME environment variable log relative to it
logDirectory = _system->TryGetEnvironmentVariable(_system->GetNewRelicHomePath());
if (logDirectory != nullptr)
{

return *logDirectory.get() + PATH_SEPARATOR +
#ifdef PAL_STDCPP_COMPAT
_X("logs");
#else
_X("Logs");
#endif
}

// for everything else use the standard directory
return GetStandardLogDirectory();
}

bool IsAzureWebSites()
{
auto home = _system->TryGetEnvironmentVariable(_X("HOME_EXPANDED"));
if (home == nullptr) return false;

if (StartsWith(*home, _X("C:\\DWASFiles\\Sites\\"))) return true;
else return false;
}

const xstring_t GetAzureWebSiteLogDirectory()
{
auto home = _system->TryGetEnvironmentVariable(_X("HOME"));
if (home == nullptr) return _X("");

return *home + PATH_SEPARATOR + _X("LogFiles") + PATH_SEPARATOR + _X("NewRelic");
}

const xstring_t GetStandardLogDirectory()
{
return _system->GetCommonAppDataFolderPath() + PATH_SEPARATOR + _X("New Relic") + PATH_SEPARATOR + _X(".NET Agent") + PATH_SEPARATOR + _X("Logs");
}

static const xstring_t GetLogDirectoryEnvironmentVariableName()
{
return _X("NEWRELIC_PROFILER_LOG_DIRECTORY");
}

bool StartsWith(xstring_t longerString, xstring_t shorterString)
{
// if the shorter string is not actually shorter then the longer string doesn't start with the shorter one
if (longerString.length() < shorterString.length())
{
return false;
}

// compare the strings up to the length of the shorter string, returning true if they are equal
if (longerString.compare(0, shorterString.length(), shorterString) == 0)
{
return true;
}

// return false for everything else
return false;
}

private:
std::shared_ptr<std::wostream> _fileStream;
IFileDestinationSystemCallsPtr _system;
};

typedef std::shared_ptr<FileDestination> FileDestinationPtr;
#endif
}}}

Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test

virtual std::unique_ptr<xstring_t> GetNewRelicHomePath() override
{
return TryGetEnvironmentVariable(L"NEWRELIC_HOME");
return TryGetEnvironmentVariable(L"NEW_RELIC_HOME");
}

virtual std::unique_ptr<xstring_t> GetNewRelicProfilerLogDirectory() override
{
return TryGetEnvironmentVariable(L"NEWRELIC_PROFILER_LOG_DIRECTORY");
return TryGetEnvironmentVariable(L"NEW_RELIC_PROFILER_LOG_DIRECTORY");
}

virtual std::unique_ptr<xstring_t> GetNewRelicLogDirectory() override
{
return TryGetEnvironmentVariable(L"NEWRELIC_LOG_DIRECTORY");
return TryGetEnvironmentVariable(L"NEW_RELIC_LOG_DIRECTORY");
}

virtual std::unique_ptr<xstring_t> GetNewRelicLogLevel() override
{
return TryGetEnvironmentVariable(L"NEWRELIC_LOG_LEVEL");
return TryGetEnvironmentVariable(L"NEW_RELIC_LOG_LEVEL");
}
};

Expand Down Expand Up @@ -124,7 +124,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";

systemCalls->OpenFileHandler = [](std::wstring fileName, std::ios_base::openmode)
{
Expand All @@ -140,7 +140,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo\\";
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo\\";

auto fileName = DefaultFileLogLocation(systemCalls).GetPathAndFileName();
Assert::AreEqual(std::wstring(L"C:\\Foo\\\\NewRelic.Profiler.1234.log"), fileName);
Expand All @@ -150,9 +150,9 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_HOME"] = L"C:\\Foo\\Home";
systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo\\Profiler";
systemCalls->environmentVariables[L"NEWRELIC_LOG_DIRECTORY"] = L"C:\\Foo\\General";
systemCalls->environmentVariables[L"NEW_RELIC_HOME"] = L"C:\\Foo\\Home";
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo\\Profiler";
systemCalls->environmentVariables[L"NEW_RELIC_LOG_DIRECTORY"] = L"C:\\Foo\\General";

auto fileName = DefaultFileLogLocation(systemCalls).GetPathAndFileName();
Assert::AreEqual(std::wstring(L"C:\\Foo\\Profiler\\NewRelic.Profiler.1234.log"), fileName);
Expand All @@ -162,8 +162,8 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_HOME"] = L"C:\\Foo\\Home";
systemCalls->environmentVariables[L"NEWRELIC_LOG_DIRECTORY"] = L"C:\\Foo\\General";
systemCalls->environmentVariables[L"NEW_RELIC_HOME"] = L"C:\\Foo\\Home";
systemCalls->environmentVariables[L"NEW_RELIC_LOG_DIRECTORY"] = L"C:\\Foo\\General";

auto fileName = DefaultFileLogLocation(systemCalls).GetPathAndFileName();
Assert::AreEqual(std::wstring(L"C:\\Foo\\General\\NewRelic.Profiler.1234.log"), fileName);
Expand All @@ -182,7 +182,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test

systemCalls->environmentVariables[L"ALLUSERSPROFILE"] = L"D:\\Foo\\ProgramData\\Bar";
systemCalls->environmentVariables[L"HOME"] = L"C:\\DWASFiles\\Sites\\MySite\\";
systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";;
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";;

auto fileName = DefaultFileLogLocation(systemCalls).GetPathAndFileName();
Assert::AreEqual(std::wstring(L"C:\\Foo\\NewRelic.Profiler.1234.log"), fileName);
Expand Down Expand Up @@ -257,7 +257,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
TEST_METHOD(newrelic_home_environment_variable)
{
auto systemCalls = std::make_shared<SystemCalls>();
systemCalls->environmentVariables[L"NEWRELIC_HOME"] = L"C:\\Foo";
systemCalls->environmentVariables[L"NEW_RELIC_HOME"] = L"C:\\Foo";

auto fileName = DefaultFileLogLocation(systemCalls).GetPathAndFileName();
Assert::AreEqual(std::wstring(L"C:\\Foo\\Logs\\NewRelic.Profiler.1234.log"), fileName);
Expand All @@ -267,7 +267,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_HOME"] = L"C:\\Foo";
systemCalls->environmentVariables[L"NEW_RELIC_HOME"] = L"C:\\Foo";

auto fileName = DefaultFileLogLocation(systemCalls).GetPathAndFileName();
Assert::AreEqual(std::wstring(L"C:\\Foo\\Logs\\NewRelic.Profiler.1234.log"), fileName);
Expand Down Expand Up @@ -313,7 +313,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";

systemCalls->OpenFileHandler = [](std::wstring fileName, std::ios_base::openmode)
{
Expand All @@ -328,7 +328,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo\\";
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo\\";

systemCalls->OpenFileHandler = [](std::wstring fileName, std::ios_base::openmode)
{
Expand Down Expand Up @@ -358,7 +358,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test

systemCalls->environmentVariables[L"ALLUSERSPROFILE"] = L"D:\\Foo\\ProgramData\\Bar";
systemCalls->environmentVariables[L"HOME"] = L"C:\\DWASFiles\\Sites\\MySite\\";
systemCalls->environmentVariables[L"NEWRELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";;
systemCalls->environmentVariables[L"NEW_RELIC_PROFILER_LOG_DIRECTORY"] = L"C:\\Foo";;

systemCalls->OpenFileHandler = [](std::wstring fileName, std::ios_base::openmode)
{
Expand Down Expand Up @@ -430,7 +430,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_HOME"] = L"C:\\Foo";
systemCalls->environmentVariables[L"NEW_RELIC_HOME"] = L"C:\\Foo";

systemCalls->OpenFileHandler = [](std::wstring fileName, std::ios_base::openmode)
{
Expand All @@ -445,7 +445,7 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
{
auto systemCalls = std::make_shared<SystemCalls>();

systemCalls->environmentVariables[L"NEWRELIC_HOME"] = L"C:\\Foo";
systemCalls->environmentVariables[L"NEW_RELIC_HOME"] = L"C:\\Foo";

systemCalls->OpenFileHandler = [](std::wstring fileName, std::ios_base::openmode)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Agent/NewRelic/Profiler/LoggingTest/LoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ namespace NewRelic { namespace Profiler { namespace Logger { namespace Test
void ResetStdLog()
{
StdLog.get_dest().str(L"");
StdLog.SetAzureFunctionMode(false);
StdLog.SetAzureFunctionLogLevelOverride(false);
}

MessageList GetMessages()
Expand Down
Loading

0 comments on commit e17be16

Please sign in to comment.