Skip to content

Commit 399a7df

Browse files
authored
chore: improve logging (#17)
1 parent 7a7784e commit 399a7df

File tree

7 files changed

+30
-14
lines changed

7 files changed

+30
-14
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native/Log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Log final
1818
private:
1919
struct ProfilerLoggerPolicy
2020
{
21-
inline static const std::string file_name = "DD-DotNet-Profiler-Native";
21+
inline static const std::string file_name = "Pyroscope-DotNet-Profiler-Native";
2222
#ifdef _WIN32
2323
inline static const shared::WSTRING folder_path = WStr(R"(Datadog-APM\logs\DotNet)");
2424
#endif

profiler/src/ProfilerEngine/Datadog.Profiler.Native/OpSysTools.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ bool OpSysTools::IsSafeToStartProfiler(double coresThreshold)
345345
// We assume that the profiler library is in the same folder as the wrapper library
346346
auto currentModulePath = fs::path(shared::GetCurrentModuleFileName());
347347
auto wrapperLibrary = currentModulePath.parent_path() / "Pyroscope.Linux.ApiWrapper.x64.so";
348+
if (!fs::exists(wrapperLibrary))
349+
{
350+
wrapperLibrary = currentModulePath.parent_path() / "Datadog.Linux.ApiWrapper.x64.so";
351+
}
348352
auto wrapperLibraryPath = wrapperLibrary.string();
349353

350354
auto* instance = dlopen(wrapperLibraryPath.c_str(), RTLD_LAZY | RTLD_LOCAL);

profiler/src/ProfilerEngine/Datadog.Profiler.Native/PprofBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ std::string PprofBuilder::Build()
5151
{
5252
std::lock_guard<std::mutex> lock(this->_lock);
5353
auto res = _profile.SerializeAsString();
54-
Log::Info("PprofBuilder samples: ", _samplesCount, ", serialized bytes: ", res.size());
54+
Log::Debug("PprofBuilder samples: ", _samplesCount, ", serialized bytes: ", res.size());
5555
Reset();
5656
return std::move(res);
5757
}

profiler/src/ProfilerEngine/Datadog.Profiler.Native/PyroscopePprofSink.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "OpSysTools.h"
88

99
PyroscopePprofSink::PyroscopePprofSink(std::string server, std::string appName, std::string authToken) :
10-
_appName(appName), _client(server)
10+
_appName(appName), _server(server), _client(server)
1111
{
1212
if (!authToken.empty())
1313
{
@@ -79,6 +79,6 @@ void PyroscopePprofSink::upload(Pprof pprof, ProfileTime& startTime, ProfileTime
7979
else
8080
{
8181
auto err = res.error();
82-
Log::Info("PyroscopePprofSink err ", to_string(err));
82+
Log::Info("PyroscopePprofSink err ", to_string(err), " ", _server);
8383
}
8484
}

profiler/src/ProfilerEngine/Datadog.Profiler.Native/PyroscopePprofSink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PyroscopePprofSink: public PProfExportSink
2727
void upload(Pprof pprof, ProfileTime &startTime, ProfileTime &endTime);
2828

2929
std::string _appName;
30+
std::string _server;
3031
httplib::Client _client;
3132
LockingQueue<PyroscopeRequest> _queue;
3233
std::thread _workerThread;

shared/src/native-src/logger.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,13 @@ std::shared_ptr<spdlog::logger> Logger::CreateInternalLogger()
115115

116116
try
117117
{
118-
119-
// todo
120-
//#if defined(PROFILER_LOG_TO_STDOUT)
121118
std::vector<spdlog::sink_ptr> sinks;
122-
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
119+
if (::shared::StderrLogEnabled())
120+
{
121+
sinks.push_back(std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>());
122+
}
123+
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(Logger::GetLogPath<LoggerPolicy>(file_name_suffix), 1048576 * 5, 10));
123124
logger = std::make_shared<spdlog::logger>(LoggerPolicy::file_name, begin(sinks), end(sinks));
124-
//#else
125-
logger =
126-
spdlog::rotating_logger_mt(LoggerPolicy::file_name, Logger::GetLogPath<LoggerPolicy>(file_name_suffix), 1048576 * 5, 10);
127-
//#endif
128125
}
129126
catch (...)
130127
{
@@ -157,7 +154,12 @@ std::string Logger::GetLogPath(const std::string& file_name_suffix)
157154

158155
if (!fs::exists(parent_path))
159156
{
160-
fs::create_directories(parent_path);
157+
try {
158+
159+
fs::create_directories(parent_path);
160+
} catch (std::exception &e) {
161+
std::cerr << "Logger::GetLogPath failed to create a parent directory: " << parent_path << std::endl;
162+
}
161163
}
162164
}
163165

shared/src/native-src/pal.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525

2626
namespace shared
2727
{
28+
inline bool StderrLogEnabled() {
29+
auto log_to_stderr = GetEnvironmentValue(WStr("PYROSCOPE_LOG_STDERR"));
30+
bool enabled = false;
31+
bool parsed = TryParseBooleanEnvironmentValue(log_to_stderr, enabled);
32+
if (parsed) {
33+
return enabled;
34+
}
35+
return true;
36+
}
2837

2938
template <class TLoggerPolicy>
3039
inline shared::WSTRING GetDatadogLogFilePath(const std::string& file_name_suffix)
@@ -63,7 +72,7 @@ inline shared::WSTRING GetDatadogLogFilePath(const std::string& file_name_suffix
6372
// on Windows WSTRING == wstring
6473
return (program_data_path / TLoggerPolicy::folder_path / file_name).wstring();
6574
#else
66-
return ToWSTRING("/var/log/datadog/dotnet/" + file_name);
75+
return ToWSTRING("/var/log/pyroscope/dotnet/" + file_name);
6776
#endif
6877
}
6978

0 commit comments

Comments
 (0)