diff --git a/README.md b/README.md index be79cf3..06f1739 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,9 @@ Here is the list of data the module reports periodically: utcstart: , events: ,, cpu: , - mem: , + mem: , + usedheap: , + totalheap: , cpuperreq: , oreqs: , sys_cpu: , @@ -83,6 +85,24 @@ monitor.setIpcMonitorPath('/tmp/my-process-stats.mon'); ``` Sets the datagram socket name to write the stats. Defaults to /tmp/nodejs.mon +## setReportInterval(ms) +```js +monitor.setReportInterval(1000); +``` +Sets the report interval. Defaults to 1000ms. Integer. + +## setCustomName(name) +```js +monitor.setCustomName("processname"); +``` +Sets the custom process name. This will be sent as custom_name inside the JSON payload. String. + +## setCustomId(id) +```js +monitor.setCustomName("process-id"); +``` +Sets the custom process id. This will be sent as custom_id inside the JSON payload. String. + # Health Status Monitr supports custom health functionality whereby the app can report its own health. The following methods are added to process.monitor to set and get the health information. diff --git a/src/monitor.cc b/src/monitor.cc index 5d1f1bf..7ab3b2d 100644 --- a/src/monitor.cc +++ b/src/monitor.cc @@ -47,13 +47,16 @@ using namespace v8; // This is the default IPC path where the stats are written to // Could use the setter method to change this static string _ipcMonitorPath = "/tmp/nodejs.mon"; +static string _customName = ""; +static string _customId = ""; + static bool _show_backtrace = false; //< default to false for performance // Normally reports will be sent every REPORT_INTERVAL_MS // However, if there is no receiver on the other end (i.e. sendmsg() // returns -1), then the reporting thread will wait MAX_INACTIVITY_RETRIES // before trying again. -static const int REPORT_INTERVAL_MS = 1000; +static int REPORT_INTERVAL_MS = 1000; static const int MAX_INACTIVITY_RETRIES = 5; /* globals used for signal catching, etc */ @@ -403,7 +406,9 @@ void NodeMonitor::setStatistics() { v8::HeapStatistics v8stats; Nan::GetHeapStatistics(&v8stats); - stats_.pmem_ = (v8stats.used_heap_size() / (double) v8stats.total_heap_size()); + stats_.usedheap_ = v8stats.used_heap_size(); + stats_.totalheap_ = v8stats.total_heap_size(); + stats_.pmem_ = (stats_.usedheap_ / (double) stats_.totalheap_); } { // Obtains the CPU usage @@ -786,6 +791,18 @@ bool NodeMonitor::sendReport() { data.append(buffer); } + snprintf(buffer, sizeof(buffer), "\"custom_name\":\"%s\",", _customName.c_str()); + data.append(buffer); + + snprintf(buffer, sizeof(buffer), "\"custom_id\":\"%s\",", _customId.c_str()); + data.append(buffer); + + snprintf(buffer, sizeof(buffer), "\"usedheap\":%d,", stats.usedheap_); + data.append(buffer); + + snprintf(buffer, sizeof(buffer), "\"totalheap\":%d,", stats.totalheap_); + data.append(buffer); + // requests served since beginning snprintf(buffer, sizeof(buffer), "\"reqstotal\":%d,", stats.lastRequests_); data.append(buffer); @@ -1027,6 +1044,18 @@ static NAN_GETTER(GetterIPCMonitorPath) { info.GetReturnValue().Set(Nan::New(_ipcMonitorPath.c_str()).ToLocalChecked()); } +static NAN_GETTER(GetterCustomName) { + info.GetReturnValue().Set(Nan::New(_customName.c_str()).ToLocalChecked()); +} + +static NAN_GETTER(GetterReportInterval) { + info.GetReturnValue().Set(Nan::New(REPORT_INTERVAL_MS)); +} + +static NAN_GETTER(GetterCustomId) { + info.GetReturnValue().Set(Nan::New(_customId.c_str()).ToLocalChecked()); +} + static NAN_GETTER(GetterShowBackTrace) { info.GetReturnValue().Set(_show_backtrace); } @@ -1050,6 +1079,35 @@ static NAN_METHOD(SetterIPCMonitorPath) { info.GetReturnValue().SetUndefined(); } +static NAN_METHOD(SetterReportInterval) { + if (info.Length() < 1 || + (!info[0]->IsNumber() && !info[0]->IsUndefined() && !info[0]->IsNull())) { + THROW_BAD_ARGS(); + } + REPORT_INTERVAL_MS = info[0]->Uint32Value(); + info.GetReturnValue().SetUndefined(); +} + +static NAN_METHOD(SetterCustomName) { + if (info.Length() < 1 || + (!info[0]->IsString() && !info[0]->IsUndefined() && !info[0]->IsNull())) { + THROW_BAD_ARGS(); + } + String::Utf8Value customName(info[0]); + _customName = *customName; + info.GetReturnValue().SetUndefined(); +} + +static NAN_METHOD(SetterCustomId) { + if (info.Length() < 1 || + (!info[0]->IsString() && !info[0]->IsUndefined() && !info[0]->IsNull())) { + THROW_BAD_ARGS(); + } + String::Utf8Value customId(info[0]); + _customId = *customId; + info.GetReturnValue().SetUndefined(); +} + static NAN_METHOD(StartMonitor) { NodeMonitor::getInstance().Start(); info.GetReturnValue().SetUndefined(); @@ -1076,9 +1134,21 @@ NAN_MODULE_INIT(init) { Nan::SetAccessor( exports, Nan::New("ipcMonitorPath").ToLocalChecked(), GetterIPCMonitorPath, 0, v8::Local(), v8::PROHIBITS_OVERWRITING, v8::DontDelete ); + Nan::SetAccessor( exports, Nan::New("customName").ToLocalChecked(), + GetterCustomName, 0, v8::Local(), + v8::PROHIBITS_OVERWRITING, v8::DontDelete ); + Nan::SetAccessor( exports, Nan::New("customId").ToLocalChecked(), + GetterCustomId, 0, v8::Local(), + v8::PROHIBITS_OVERWRITING, v8::DontDelete ); + Nan::SetAccessor( exports, Nan::New("reportInterval").ToLocalChecked(), + GetterReportInterval, 0, v8::Local(), + v8::PROHIBITS_OVERWRITING, v8::DontDelete ); Nan::SetAccessor( exports, Nan::New("showBacktrace").ToLocalChecked(), GetterShowBackTrace, SetterShowBackTrace ); Nan::Export( exports, "setIpcMonitorPath", SetterIPCMonitorPath); + Nan::Export( exports, "setReportInterval", SetterReportInterval); + Nan::Export( exports, "setCustomName", SetterCustomName); + Nan::Export( exports, "setCustomId", SetterCustomId); Nan::Export( exports, "start", StartMonitor); Nan::Export( exports, "stop", StopMonitor); diff --git a/src/monitor.h b/src/monitor.h index 71db212..fa2972c 100644 --- a/src/monitor.h +++ b/src/monitor.h @@ -224,6 +224,10 @@ typedef struct { volatile time_t healthStatusTimestamp_; volatile double pmem_; + + volatile int usedheap_; + + volatile int totalheap_; } Statistics;