Skip to content

Commit 2fe4a6d

Browse files
chrisnasandrewlock
andauthored
[Profiler] Support Stable Config (#7287)
## Summary of changes The managed layer of the tracer should be able to set basic configuration to the profiling including per runtimeID details ## Reason for change Support Stable Configuration ## Implementation details - add p/invoke method - update profiler configuration management and SSI ## Test coverage - update existing ones and add new needed scenarios - ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. --> --------- Co-authored-by: Andrew Lock <[email protected]>
1 parent 30f4797 commit 2fe4a6d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+909
-185
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/SystemCallsShield.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ extern "C" int (*volatile dd_set_shared_memory)(volatile int*) __attribute__((we
2020
extern "C" unsigned long long dd_inside_wrapped_functions() __attribute__((weak));
2121

2222
SystemCallsShield::SystemCallsShield(IConfiguration* configuration) :
23-
_isEnabled{ShouldEnable(configuration)}
23+
_isEnabled{ShouldEnable(configuration)},
24+
_isStarted{false}
2425
{
2526
}
2627

@@ -34,6 +35,12 @@ bool SystemCallsShield::ShouldEnable(IConfiguration* configuration)
3435

3536
bool SystemCallsShield::Start()
3637
{
38+
if (_isStarted)
39+
{
40+
return true;
41+
}
42+
_isStarted = true;
43+
3744
if (_isEnabled)
3845
{
3946
Instance = this;
@@ -45,6 +52,11 @@ bool SystemCallsShield::Start()
4552

4653
bool SystemCallsShield::Stop()
4754
{
55+
if (!_isStarted)
56+
{
57+
return false;
58+
}
59+
4860
if (_isEnabled)
4961
{
5062
dd_set_shared_memory = nullptr;
@@ -54,6 +66,11 @@ bool SystemCallsShield::Stop()
5466
return true;
5567
}
5668

69+
bool SystemCallsShield::IsStarted()
70+
{
71+
return (_isStarted);
72+
}
73+
5774
const char* SystemCallsShield::GetName()
5875
{
5976
return "Linux System Calls Shield";

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/SystemCallsShield.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SystemCallsShield : public IService
2222

2323
bool Start() override;
2424
bool Stop() override;
25+
bool IsStarted() override;
2526

2627
const char* GetName() override;
2728

@@ -34,4 +35,5 @@ class SystemCallsShield : public IService
3435
int SetSharedMemoryOnThreadInfo(volatile int* state);
3536

3637
bool _isEnabled;
38+
bool _isStarted;
3739
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
global: CreateCrashReport; GetNativeProfilerIsReadyPtr; GetPointerToNativeTraceContext; Profiler_Version; SetApplicationInfoForAppDomain; SetEndpointForTrace; SetGitMetadataForApplication; ThreadsCpuManager_Map; DllGetClassObject; DllCanUnloadNow; FlushProfile;
2+
global: SetConfiguration; CreateCrashReport; GetNativeProfilerIsReadyPtr; GetPointerToNativeTraceContext; Profiler_Version; SetApplicationInfoForAppDomain; SetEndpointForTrace; SetGitMetadataForApplication; ThreadsCpuManager_Map; DllGetClassObject; DllCanUnloadNow; FlushProfile;
33
local: *;
44
};

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Windows/Datadog.Profiler.Native.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
SetEndpointForTrace PRIVATE
1111
SetGitMetadataForApplication PRIVATE
1212
FlushProfile PRIVATE
13-
CreateCrashReport PRIVATE
13+
CreateCrashReport PRIVATE
14+
SetConfiguration PRIVATE

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "IConfiguration.h"
88
#include "IRuntimeInfo.h"
99
#include "ISsiManager.h"
10+
#include "Log.h"
1011
#include "ProfileExporter.h"
1112

1213
ApplicationStore::ApplicationStore(IConfiguration* configuration, IRuntimeInfo* runtimeInfo) :
@@ -36,12 +37,16 @@ ApplicationInfo ApplicationStore::GetApplicationInfo(const std::string& runtimeI
3637
_pConfiguration->GetGitRepositoryUrl(),
3738
_pConfiguration->GetGitCommitSha()};
3839

40+
Log::Debug("Creating new application info for runtimeId: ", runtimeId, ", serviceName: ", info.ServiceName, ", environment: ", info.Environment, ", version: ", info.Version);
41+
3942
_infos[runtimeId] = info;
4043
return info;
4144
}
4245
}
4346
void ApplicationStore::SetApplicationInfo(const std::string& runtimeId, const std::string& serviceName, const std::string& environment, const std::string& version)
4447
{
48+
Log::Debug("Setting application info for runtimeId: ", runtimeId, ", serviceName: ", serviceName, ", environment: ", environment, ", version: ", version);
49+
4550
std::lock_guard lock(_infosLock);
4651
auto& info = _infos[runtimeId];
4752
info.ServiceName = serviceName;
@@ -51,11 +56,11 @@ void ApplicationStore::SetApplicationInfo(const std::string& runtimeId, const st
5156
info.CommitSha = _pConfiguration->GetGitCommitSha();
5257
}
5358

54-
void ApplicationStore::SetGitMetadata(std::string runtimeId, std::string respositoryUrl, std::string commitSha)
59+
void ApplicationStore::SetGitMetadata(std::string runtimeId, std::string repositoryUrl, std::string commitSha)
5560
{
5661
std::lock_guard lock(_infosLock);
5762
auto& info = _infos[std::move(runtimeId)];
58-
info.RepositoryUrl = std::move(respositoryUrl);
63+
info.RepositoryUrl = std::move(repositoryUrl);
5964
info.CommitSha = std::move(commitSha);
6065
// no need to create worker, it has already been created
6166
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include "ClrLifetime.h"
55
#include "IClrLifetime.h"
66

7-
ClrLifetime::ClrLifetime(std::atomic<bool>* pIsRunning)
7+
ClrLifetime::ClrLifetime(std::atomic<bool>* pIsInitialized)
88
{
9-
_pIsRunning = pIsRunning;
9+
_pIsInitialized = pIsInitialized;
1010
}
1111

12-
bool ClrLifetime::IsRunning() const
12+
bool ClrLifetime::IsInitialized() const
1313
{
14-
auto isRunning = _pIsRunning->load();
15-
return isRunning;
14+
auto IsInitialized = _pIsInitialized->load();
15+
return IsInitialized;
1616
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
class ClrLifetime : public IClrLifetime
1010
{
1111
private:
12-
std::atomic<bool>* _pIsRunning;
12+
std::atomic<bool>* _pIsInitialized;
1313

1414
public:
15-
explicit ClrLifetime(std::atomic<bool>* pIsRunning);
15+
explicit ClrLifetime(std::atomic<bool>* pIsInitialized);
1616

17-
bool IsRunning() const override;
17+
bool IsInitialized() const override;
1818
};

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ Configuration::Configuration()
9494
}
9595

9696
_isEtwEnabled = GetEnvironmentValue(EnvironmentVariables::EtwEnabled, true, true);
97-
_deploymentMode = GetEnvironmentValue(EnvironmentVariables::SsiDeployed, DeploymentMode::Manual);
97+
_isManagedActivationEnabled = GetEnvironmentValue(EnvironmentVariables::ManagedActivationEnabled, true, true);
98+
99+
// check that the env var exists (special converter) and log the resulting value
100+
_deploymentMode = GetEnvironmentValue(EnvironmentVariables::SsiDeployed, DeploymentMode::Manual, true);
98101
_isEtwLoggingEnabled = GetEnvironmentValue(EnvironmentVariables::EtwLoggingEnabled, false);
99102
_etwReplayEndpoint = GetEnvironmentValue(EnvironmentVariables::EtwReplayEndpoint, DefaultEmptyString);
100103
_enablementStatus = ExtractEnablementStatus();
@@ -720,6 +723,13 @@ bool Configuration::IsEnvironmentValueSet(shared::WSTRING const& name, T& value)
720723

721724
EnablementStatus Configuration::ExtractEnablementStatus()
722725
{
726+
// wait for the managed layer to set the activation status
727+
if (_isManagedActivationEnabled)
728+
{
729+
return EnablementStatus::Standby;
730+
}
731+
732+
// kill switch for local environment variables
723733
if (shared::EnvironmentExist(EnvironmentVariables::ProfilerEnabled))
724734
{
725735
auto isEnabled = false;
@@ -736,25 +746,14 @@ EnablementStatus Configuration::ExtractEnablementStatus()
736746
// It is possible that a Single Step Instrumentation deployment was done
737747
// and the profiler was enabled during that step. In that case, the "auto" value
738748
// will be set and profiler should be enabled.
739-
// This should be replaced by adding "profiler" in EnvironmentVariables::SsiDeployed
740-
// later that will take into account heuristics
741749
return !enabled.empty() && enabled == WStr("auto")
742750
? EnablementStatus::Auto
743751
: EnablementStatus::ManuallyDisabled;
744752
}
745753

746-
auto r = shared::GetEnvironmentValue(EnvironmentVariables::SsiDeployed);
747-
auto pos = r.find(WStr("profiler"));
748-
auto ssiEnabled = (pos != shared::WSTRING::npos);
749-
750-
if (ssiEnabled)
751-
{
752-
return EnablementStatus::SsiEnabled;
753-
}
754-
else
755-
{
756-
return EnablementStatus::NotSet;
757-
}
754+
// Note: the initial support of adding "profiler" in EnvironmentVariables::SsiDeployed
755+
// has never been implemented, so we are not checking for it here.
756+
return EnablementStatus::NotSet;
758757
}
759758

760759
std::chrono::milliseconds Configuration::ExtractSsiLongLivedThreshold() const
@@ -782,6 +781,16 @@ bool Configuration::IsWaitHandleProfilingEnabled() const
782781
return _isWaitHandleProfilingEnabled;
783782
}
784783

784+
bool Configuration::IsManagedActivationEnabled() const
785+
{
786+
return _isManagedActivationEnabled;
787+
}
788+
789+
void Configuration::SetEnablementStatus(EnablementStatus status)
790+
{
791+
_enablementStatus = status;
792+
}
793+
785794

786795
std::chrono::milliseconds Configuration::ExtractHttpRequestDurationThreshold() const
787796
{

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class Configuration final : public IConfiguration
8383
std::chrono::milliseconds GetHttpRequestDurationThreshold() const override;
8484
bool ForceHttpSampling() const override;
8585
bool IsWaitHandleProfilingEnabled() const override;
86+
bool IsManagedActivationEnabled() const override;
87+
void SetEnablementStatus(EnablementStatus status) override;
8688

8789
private:
8890
static tags ExtractUserTags();
@@ -172,6 +174,7 @@ class Configuration final : public IConfiguration
172174
std::uint64_t _internalCIVisibilitySpanId;
173175
bool _isEtwEnabled;
174176
DeploymentMode _deploymentMode;
177+
bool _isManagedActivationEnabled;
175178
bool _isEtwLoggingEnabled;
176179
std::string _etwReplayEndpoint;
177180
EnablementStatus _enablementStatus;

0 commit comments

Comments
 (0)