Skip to content

Commit 799b7ef

Browse files
committed
Add ability to set options
1 parent 1e04333 commit 799b7ef

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

index.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ var aspect = require('./lib/aspect.js');
2121
var request = require('./lib/request.js');
2222
var fs = require('fs');
2323
var agent = require("./appmetrics")
24-
// Set the plugin search path
24+
2525
agent.spath(path.join(module_dir, "plugins"))
26-
// pass in the main file name for use in identifying the app in mqtt connection
27-
agent.start(main_filename);
26+
2827

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

3236
/*
3337
* Load module probes into probes array by searching the probes directory.
@@ -238,7 +242,9 @@ module.exports.emit = function (topic, data) {
238242

239243
// Export monitor() API for consuming data in-process
240244
module.exports.monitor = function() {
245+
241246
if (typeof(this.api) == 'undefined') {
247+
agent.start();
242248
this.api = hcAPI.getAPI(agent, module.exports);
243249
}
244250
return this.api;
@@ -250,6 +256,16 @@ module.exports.configure = function(options) {
250256
options = options || {};
251257
this.strongTracerInstrument =
252258
options.strongTracer ? options.strongTracer.tracer : null;
259+
for (var key in options) {
260+
if(propertyMappings[key]) {
261+
agent.setOptions(propertyMappings[key], options[key]);
262+
} else {
263+
agent.setOptions(key, options[key]);
264+
}
265+
}
266+
267+
// If user has not specified application ID, use main filename
268+
main_filename = options.applicationID ? options.applicationID : main_filename;
253269
};
254270

255271
module.exports.transactionLink = function(linkName, callback) {
@@ -264,3 +280,10 @@ module.exports.setJSONProfilingMode = function(val) {
264280
module.exports.getJSONProfilingMode = function() {
265281
return jsonProfilingMode;
266282
}
283+
284+
module.exports.start = function () {
285+
agent.setOptions(propertyMappings['applicationID'], main_filename);
286+
agent.start();
287+
}
288+
289+

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)