Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow profiler to respond to log level changes. #2234

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Agent/NewRelic/Agent/Core/Config/ConfigurationTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public class ConfigurationTracker : IDisposable

private DateTime _lastWriteTime;

public ConfigurationTracker(IConfigurationService configurationService)
private readonly INativeMethods _nativeMethods;

public ConfigurationTracker(IConfigurationService configurationService, INativeMethods nativeMethods)
{
_nativeMethods = nativeMethods;
var fileName = configurationService.Configuration.NewRelicConfigFilePath;
if (fileName == null)
return;
Expand All @@ -39,6 +42,7 @@ public ConfigurationTracker(IConfigurationService configurationService)
Log.Debug("newrelic.config file changed, reloading.");
ConfigurationLoader.Initialize(fileName);
_lastWriteTime = lastWriteTime;
_nativeMethods.ReloadConfiguration();
}
}, TimeSpan.FromMinutes(1));
}
Expand Down
1 change: 1 addition & 0 deletions src/Agent/NewRelic/Agent/Core/INativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface INativeMethods
void ShutdownNativeThreadProfiler();

int InstrumentationRefresh();
int ReloadConfiguration();
int AddCustomInstrumentation(string fileName, string xml);
int ApplyCustomInstrumentation();
}
Expand Down
32 changes: 32 additions & 0 deletions src/Agent/NewRelic/Agent/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class LinuxNativeMethods : INativeMethods
[DllImport(DllName, EntryPoint = "InstrumentationRefresh", CallingConvention = CallingConvention.Cdecl)]
private static extern int ExternInstrumentationRefresh();

[DllImport(DllName, EntryPoint = "ReloadConfiguration", CallingConvention = CallingConvention.Cdecl)]
private static extern int ExternReloadConfiguration();

[DllImport(DllName, EntryPoint = "AddCustomInstrumentation", CallingConvention = CallingConvention.Cdecl)]
private static extern int ExternAddCustomInstrumentation(string fileName, string xml);

Expand All @@ -34,6 +37,19 @@ public int InstrumentationRefresh()
}
}

public int ReloadConfiguration()
{
try
{
return ExternReloadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, "LinuxNativeMethods.ReloadConfiguration() exception");
return -1;
}
}

public int AddCustomInstrumentation(string fileName, string xml)
{
return ExternAddCustomInstrumentation(fileName, xml);
Expand Down Expand Up @@ -84,6 +100,9 @@ public class WindowsNativeMethods : INativeMethods
[DllImport(DllName, EntryPoint = "InstrumentationRefresh", CallingConvention = CallingConvention.Cdecl)]
private static extern int ExternInstrumentationRefresh();

[DllImport(DllName, EntryPoint = "ReloadConfiguration", CallingConvention = CallingConvention.Cdecl)]
private static extern int ExternReloadConfiguration();

[DllImport(DllName, EntryPoint = "AddCustomInstrumentation", CallingConvention = CallingConvention.Cdecl)]
private static extern int ExternAddCustomInstrumentation(string fileName, string xml);

Expand All @@ -103,6 +122,19 @@ public int InstrumentationRefresh()
}
}

public int ReloadConfiguration()
{
try
{
return ExternReloadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, "WindowsNativeMethods.ReloadConfiguration() exception");
return -1;
}
}

public int AddCustomInstrumentation(string fileName, string xml)
{
return ExternAddCustomInstrumentation(fileName, xml);
Expand Down
25 changes: 24 additions & 1 deletion src/Agent/NewRelic/Profiler/Profiler/CorProfilerCallbackImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ namespace NewRelic { namespace Profiler {
return S_OK;
}

HRESULT ReloadConfiguration()
{
LogTrace(L"Enter: ", __func__);

auto newConfiguration = InitializeConfigAndSetLogLevel();

return S_OK;
}

std::shared_ptr<std::set<mdMethodDef>> GetMethodDefsForAssembly(
ModuleID moduleId,
xstring_t assemblyName,
Expand Down Expand Up @@ -1314,7 +1323,8 @@ namespace NewRelic { namespace Profiler {
}
};

// called by managed code to get function information from function IDs
// Called by managed code to get the profiler to update the which methods
// are instrumented at runtime.
extern "C" __declspec(dllexport) HRESULT __cdecl InstrumentationRefresh()
{
LogInfo("Refreshing instrumentation");
Expand All @@ -1326,6 +1336,19 @@ namespace NewRelic { namespace Profiler {
return profiler->InstrumentationRefresh();
}

// Called by managed code to get the profiler to reload configuration settings
// from the newrelic.config files.
extern "C" __declspec(dllexport) HRESULT __cdecl ReloadConfiguration()
{
LogInfo(L"Reloading newrelic.config files.");
auto profiler = CorProfilerCallbackImpl::GetSingletonish();
if (profiler == nullptr) {
LogError(L"Unable to reload newrelic.config files because the profiler reference is invalid.");
return E_FAIL;
}
return profiler->ReloadConfiguration();
}

extern "C" __declspec(dllexport) HRESULT __cdecl AddCustomInstrumentation(const char* fileName, const char* xml)
{
LogTrace("Adding custom instrumentation");
Expand Down
Loading