Skip to content

Commit

Permalink
nsolid: add JS API for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
trevnorris committed May 22, 2024
1 parent 0e53921 commit 2fc3e37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/nsolid.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ ObjectAssign(start, {
heapSamplingStream,
heapSampling: zmq.heapSampling,
heapSamplingEnd: zmq.heapSamplingEnd,
logger: {
debug: (msg) => { writeLog(msg, 5) },

Check failure on line 133 in lib/nsolid.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
info: (msg) => { writeLog(msg, 9) },

Check failure on line 134 in lib/nsolid.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
warn: (msg) => { writeLog(msg, 13) },

Check failure on line 135 in lib/nsolid.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
error: (msg) => { writeLog(msg, 17) },

Check failure on line 136 in lib/nsolid.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
fatal: (msg) => { writeLog(msg, 21) },

Check failure on line 137 in lib/nsolid.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
},
profile: zmq.profile,
profileEnd: zmq.profileEnd,
on,
Expand Down Expand Up @@ -212,6 +219,11 @@ start.traceStats = assignGetters({}, {
module.exports = start;


function writeLog(msg, sev) {
binding.writeLog(typeof msg === 'string' ? msg : JSONStringify(msg), sev);
}


function stringifyStart() {
const ret = {};
ObjectGetOwnPropertyNames(this).forEach((e) => {
Expand Down
12 changes: 12 additions & 0 deletions src/nsolid/nsolid_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2075,6 +2075,16 @@ static void AgentId(const FunctionCallbackInfo<Value>& args) {
}


static void WriteLog(const FunctionCallbackInfo<Value>& args) {
DCHECK(args[0]->IsString());
DCHECK(args[1]->IsNumber());
String::Utf8Value s(args.GetIsolate(), args[0]);
std::string ss = *s;
EnvList::Inst()->WriteLogLine(GetLocalEnvInst(args.GetIsolate()),
{ ss, args[1].As<v8::Int32>()->Value() });
}


void BindingData::SlowPushClientBucket(
const FunctionCallbackInfo<Value>& args) {
DCHECK(args[0]->IsNumber());
Expand Down Expand Up @@ -2830,6 +2840,7 @@ void BindingData::Initialize(Local<Object> target,
&fast_push_span_data_uint64_);

SetMethod(context, target, "agentId", AgentId);
SetMethod(context, target, "writeLog", WriteLog);
SetMethod(context, target, "pushSpanDataString", PushSpanDataString);
SetMethod(context, target, "getEnvMetrics", GetEnvMetrics);
SetMethod(context, target, "getProcessMetrics", GetProcessMetrics);
Expand Down Expand Up @@ -2961,6 +2972,7 @@ void BindingData::RegisterExternalReferences(
registry->Register(fast_push_span_data_uint64_.GetTypeInfo());

registry->Register(AgentId);
registry->Register(WriteLog);
registry->Register(PushSpanDataString);
registry->Register(GetEnvMetrics);
registry->Register(GetProcessMetrics);
Expand Down
2 changes: 1 addition & 1 deletion test/addons/nsolid-log-hooks/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ std::atomic<uint32_t> cntr = { 0 };
// on the Environment of the main thread. So by this point the env_map_size
// should equal 0.
static void at_exit(void*) {
assert(cntr == 11);
assert(cntr == 66);
}

static void log_written(node::nsolid::SharedEnvInst,
Expand Down
11 changes: 11 additions & 0 deletions test/addons/nsolid-log-hooks/nsolid-log-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ const assert = require('assert');
const bindingPath = require.resolve(`./build/${common.buildType}/binding`);
const { writeLog } = require(bindingPath);
const { Worker, isMainThread } = require('worker_threads');
const { logger } = require('nsolid');

if (!isMainThread) {
writeLog('boom!');
logger.debug('hi');
logger.info('hi');
logger.warn('hi');
logger.error('hi');
logger.fatal('hi');
setTimeout(() => {}, Math.random() * 1000);
return;
}

writeLog('bam!');
logger.debug('hi');
logger.info('hi');
logger.warn('hi');
logger.error('hi');
logger.fatal('hi');

for (let i = 0; i < 10; i++) {
const worker = new Worker(__filename);
Expand Down

0 comments on commit 2fc3e37

Please sign in to comment.