Skip to content

Commit d6b806f

Browse files
authored
Merge pull request #331 from stalleyj/optionsdev
Optionsdev
2 parents f2819ae + ded1cbc commit d6b806f

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ var request = require('./lib/request.js');
2222
var fs = require('fs');
2323
var nodereport = require('nodereport');
2424
var agent = require("./appmetrics")
25-
// Set the plugin search path
25+
2626
agent.spath(path.join(module_dir, "plugins"))
27-
// pass in the main file name for use in identifying the app in mqtt connection
28-
agent.start(main_filename);
27+
2928

3029
var hcAPI = require("./appmetrics-api.js");
3130
var jsonProfilingMode = false;
31+
var propertyMappings = {'mqttPort':'com.ibm.diagnostics.healthcenter.mqtt.broker.port',
32+
'mqttHost':'com.ibm.diagnostics.healthcenter.mqtt.broker.host',
33+
'applicationID':'com.ibm.diagnostics.healthcenter.mqtt.application.id',
34+
'mqtt':'com.ibm.diagnostics.healthcenter.mqtt',
35+
'profiling':'com.ibm.diagnostics.healthcenter.data.profiling'};
3236

3337
/*
3438
* Load module probes into probes array by searching the probes directory.
@@ -239,7 +243,9 @@ module.exports.emit = function (topic, data) {
239243

240244
// Export monitor() API for consuming data in-process
241245
module.exports.monitor = function() {
246+
242247
if (typeof(this.api) == 'undefined') {
248+
agent.start();
243249
this.api = hcAPI.getAPI(agent, module.exports);
244250
}
245251
return this.api;
@@ -251,6 +257,16 @@ module.exports.configure = function(options) {
251257
options = options || {};
252258
this.strongTracerInstrument =
253259
options.strongTracer ? options.strongTracer.tracer : null;
260+
for (var key in options) {
261+
if(propertyMappings[key]) {
262+
agent.setOptions(propertyMappings[key], options[key]);
263+
} else {
264+
agent.setOptions(key, options[key]);
265+
}
266+
}
267+
268+
// If user has not specified application ID, use main filename
269+
main_filename = options.applicationID ? options.applicationID : main_filename;
254270
};
255271

256272
module.exports.transactionLink = function(linkName, callback) {
@@ -266,11 +282,14 @@ module.exports.getJSONProfilingMode = function() {
266282
return jsonProfilingMode;
267283
}
268284

285+
module.exports.start = function () {
286+
agent.setOptions(propertyMappings['applicationID'], main_filename);
287+
agent.start();
288+
}
269289
module.exports.nodereport = function() {
270290
return nodereport;
271291
}
272292

273293
module.exports.writeHeapSnapshot = function() {
274294
return require('heapdump').writeSnapshot.apply(null, arguments);
275295
}
276-

src/appmetrics.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,18 @@ static bool initLoaderApi() {
287287
return (loaderApi != NULL);
288288
}
289289

290-
NAN_METHOD(start) {
291-
292-
Local<String> value = info[0]->ToString();
293-
if (info.Length() > 0) {
294-
loaderApi->setProperty("com.ibm.diagnostics.healthcenter.mqtt.application.id", toStdString(value).c_str());
290+
// set the property to given value (called from index.js)
291+
NAN_METHOD(setOption) {
292+
if (info.Length() > 1) {
293+
Local<String> value = info[0]->ToString();
294+
Local<String> value1 = info[1]->ToString();
295+
loaderApi->setProperty(toStdString(value).c_str(),toStdString(value1).c_str());
296+
} else {
297+
loaderApi->logMessage(warning, "Incorrect number of parameters passed to setOption");
295298
}
299+
}
296300

301+
NAN_METHOD(start) {
297302
if (!running) {
298303
running = true;
299304

@@ -584,20 +589,6 @@ static bool isAppMetricsFile(std::string expected, std::string potentialMatch) {
584589
return false;
585590
}
586591

587-
// Check if this appmetrics agent native module is loaded via the node-hc command.
588-
// This is actually checking if this module has appmetrics/launcher.js as it's grandparent.
589-
// For reference:
590-
// A locally loaded module would have ancestry like:
591-
// ...
592-
// ^-- some_module_that_does_require('appmetrics') (grandparent)
593-
// ^--- .../node_modules/appmetrics/index.js (parent)
594-
// ^-- .../node_modules/appmetrics/appmetrics.node (this)
595-
//
596-
// A globally loaded module would have ancestry like:
597-
// .../node_modules/appmetrics/launcher.js (grandparent)
598-
// ^--- .../node_modules/appmetrics/index.js (parent)
599-
// ^-- .../node_modules/appmetrics/appmetrics.node (this)
600-
//
601592
static bool isGlobalAgent(Local<Object> module) {
602593
Nan::HandleScope scope;
603594
Local<Value> parent = module->Get(Nan::New<String>("parent").ToLocalChecked());
@@ -653,6 +644,7 @@ void init(Local<Object> exports, Local<Object> module) {
653644
/*
654645
* Set exported functions
655646
*/
647+
exports->Set(Nan::New<String>("setOption").ToLocalChecked(), Nan::New<FunctionTemplate>(setOption)->GetFunction());
656648
exports->Set(Nan::New<String>("start").ToLocalChecked(), Nan::New<FunctionTemplate>(start)->GetFunction());
657649
exports->Set(Nan::New<String>("spath").ToLocalChecked(), Nan::New<FunctionTemplate>(spath)->GetFunction());
658650
exports->Set(Nan::New<String>("stop").ToLocalChecked(), Nan::New<FunctionTemplate>(stop)->GetFunction());

0 commit comments

Comments
 (0)