Skip to content

Commit 22ac97c

Browse files
committed
agents: add runtime control for asset collection
Add comprehensive runtime control for asset collection (CPU/heap profiling, snapshots) with multiple configuration methods. Implements assetsEnabled flag with full control surface: - NSOLID_ASSETS_ENABLED environment variable - nsolid.start({ assetsEnabled: true/false }) configuration - Runtime helpers: enableAssets() and disableAssets() - Dynamic updates via gRPC reconfigure protocol - Validation in both gRPC and ZMQ agents with proper errors - Continuous profiler integration respects assetsEnabled state This allows operators to disable expensive profiling operations at runtime without process restarts, improving resource management and production safety. Also adds missing enableTraces()/disableTraces() helper methods to complete the tracing API surface. Comprehensive test coverage added for all asset types across gRPC, ZMQ, and parallel test suites.
1 parent 45abe09 commit 22ac97c

32 files changed

+1823
-116
lines changed

agents/grpc/proto/reconfigure.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ message ReconfigureBody {
1717
optional bool tracingEnabled = 10;
1818
optional uint32 tracingModulesBlacklist = 11;
1919
optional bool contCpuProfile = 12;
20+
optional bool assetsEnabled = 13;
2021
}
2122

2223
message ReconfigureEvent {

agents/grpc/src/grpc_agent.cc

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ void PopulateReconfigureEvent(grpcagent::ReconfigureEvent* reconfigure_event,
392392
if (it != config.end()) {
393393
body->set_contcpuprofile(*it);
394394
}
395+
it = config.find("assetsEnabled");
396+
if (it != config.end()) {
397+
body->set_assetsenabled(*it);
398+
}
395399
}
396400

397401
void PopulateStartupTimesEvent(grpcagent::StartupTimesEvent* st_events,
@@ -440,6 +444,7 @@ GrpcAgent::GrpcAgent(): hooks_init_(false),
440444
agent_id_(GetAgentId()),
441445
auth_retries_(0),
442446
unauthorized_(false),
447+
assets_enabled_(true),
443448
profile_on_exit_(false) {
444449
ASSERT_EQ(0, uv_loop_init(&loop_));
445450
ASSERT_EQ(0, uv_cond_init(&start_cond_));
@@ -1186,12 +1191,12 @@ int GrpcAgent::config(const json& config) {
11861191
ret = setup_metrics_timer(period);
11871192
}
11881193

1189-
// uint64_t period = NSOLID_SPANS_FLUSH_INTERVAL;
1190-
// auto spans_flush_interval =
1191-
// per_process::system_environment->Get(kNSOLID_SPANS_FLUSH_INTERVAL);
1192-
// if (spans_flush_interval.has_value()) {
1193-
// period = std::stoull(spans_flush_interval.value());
1194-
// }
1194+
{
1195+
auto it = config_.find("assetsEnabled");
1196+
if (it != config_.end()) {
1197+
assets_enabled_ = *it;
1198+
}
1199+
}
11951200

11961201
return ret;
11971202
}
@@ -2084,6 +2089,11 @@ ErrorType GrpcAgent::do_start_prof_init(
20842089
const grpcagent::CommandRequest& req,
20852090
const ProfileType& type,
20862091
ProfileOptions& options) {
2092+
2093+
if (!assets_enabled_) {
2094+
return ErrorType::EAssetsDisabled;
2095+
}
2096+
20872097
const grpcagent::ProfileArgs& args = req.args().profile();
20882098
uint64_t thread_id = args.thread_id();
20892099
uint64_t duration = args.duration();

agents/grpc/src/grpc_agent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class GrpcAgent: public std::enable_shared_from_this<GrpcAgent>,
349349
log_exporter_;
350350

351351
// Profiling
352+
std::atomic<bool> assets_enabled_;
352353
nsuv::ns_mutex profile_state_lock_;
353354
ProfileState profile_state_[ProfileType::kNumberOfProfileTypes];
354355
std::atomic<bool> profile_on_exit_;

agents/grpc/src/grpc_errors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
X(ESnapshotDisabled, 500, "Heap Snapshots disabled", 1004) \
1010
X(ENoMemory, 500, "Internal Runtime Error", 1005) \
1111
X(ENotAvailable, 404, "Resource not available", 1006) \
12-
X(ESourceCodeFileError, 500, "Internal Runtime Error", 1007)
12+
X(ESourceCodeFileError, 500, "Internal Runtime Error", 1007) \
13+
X(EAssetsDisabled, 500, "Assets collection disabled", 1008)
1314

1415
namespace node {
1516
namespace nsolid {

agents/grpc/src/proto/reconfigure.pb.cc

Lines changed: 48 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agents/grpc/src/proto/reconfigure.pb.h

Lines changed: 42 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agents/zmq/src/zmq_agent.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,13 @@ int ZmqAgent::config(const json& config) {
12791279
setup_blocked_loop_hooks();
12801280
}
12811281

1282+
{
1283+
auto it = config_.find("assetsEnabled");
1284+
if (it != config_.end()) {
1285+
assets_enabled_ = *it;
1286+
}
1287+
}
1288+
12821289
if (utils::find_any_fields_in_diff(diff, { "/app" })) {
12831290
auto it = config_.find("app");
12841291
if (it != config_.end()) {
@@ -2105,6 +2112,10 @@ void ZmqAgent::do_got_prof(ProfileType type,
21052112
int ZmqAgent::do_start_prof_init(const nlohmann::json& message,
21062113
ProfileType type,
21072114
ProfileOptions& options) {
2115+
if (!assets_enabled_) {
2116+
return UV_EINVAL;
2117+
}
2118+
21082119
StartProfiling start_profiling = nullptr;
21092120
switch (type) {
21102121
case ProfileType::kCpu:

0 commit comments

Comments
 (0)