diff --git a/README.md b/README.md index a426ef4..ab9cc48 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,9 @@ Get a free account and create a Node.js API token at [sematext.com/spm](https:// We use https://www.npmjs.com/package/rc for configuration. This means config parameters can be passed via several config locations command-line args or ENV variables. We recommend to use a file in current directory in INI or JSON format called ".spmagentrc". -This file can be generated by providing setting and environment variable and calling a helper script: +This file can be generated by calling a helper script: - export SPM_TOKEN=YOUR-SPM-TOKEN - node ./node_modules/spm-agent-nodejs/bin/spmconfig.js + node ./node_modules/spm-agent-nodejs/bin/spmconfig.js YOUR-NODEJS-MONITORING-TOKEN YOUR-INFRA-MONITORING-TOKEN The command above generates following default configuration file (YAML format): @@ -57,7 +56,8 @@ The command above generates following default configuration file (YAML format): # Application Token for SPM tokens: - spm: YOUR-SPM-TOKEN + monitoring: YOUR-NODEJS-MONITORING-TOKEN + infra: YOUR-INFRA-MONITORING-TOKEN logger # log file directory default is ./spmlogs @@ -70,14 +70,15 @@ The command above generates following default configuration file (YAML format): The only required setting is the SPM Application Token, this could be set via config file ".spmagentrc" or environment variable: - export spmagent_tokens__spm=YOUR-SPM-APP-TOKEN + export spmagent_tokens__monitoring=YOUR-NODEJS-MONITORING-TOKEN Please note the use of double "_" for nested properties ## Configuration via Environment Variables - export SPM_TOKEN=token + export MONITORING_TOKEN=YOUR-NODEJS-MONITORING-TOKEN + export INFRA_TOKEN=YOUR-INFRA-MONITORING-TOKEN # default is SaaS at sematext.com, URL needs to be changed for on-prem to the local SPM receiver export SPM_RECEIVER_URL=https://local-spm-server:8084/_bulk export EVENTS_RECEIVER_URL=https://local-event-receiver/ diff --git a/bin/spmconfig.js b/bin/spmconfig.js index 8d2e655..4b37ff0 100755 --- a/bin/spmconfig.js +++ b/bin/spmconfig.js @@ -12,11 +12,31 @@ var fs = require('fs') var os = require('os') var path = require('path') -var spmToken = '' -if (process.argv.length === 3) { - spmToken = process.argv[2] +var monitoringToken = '' +var infraToken = '' + +if (process.argv.length < 3) { + if (!(process.env.SPM_TOKEN || process.env.MONITORING_TOKEN) || + !process.env.INFRA_TOKEN) { + console.log('Usage: spmconfig.js YOUR_NODE_APP_TOKEN YOUR_INFRA_TOKEN') + } +} + +if (process.argv.length > 2) { + monitoringToken = process.argv[2] } -if (process.env.SPM_TOKEN) { spmToken = process.env.SPM_TOKEN } + +if (process.argv.length > 3) { + infraToken = process.argv[3] +} else { + console.log('Usage: spmconfig.js YOUR_NODE_APP_TOKEN YOUR_INFRA_TOKEN') + process.exit(0) +} + +if (process.env.SPM_TOKEN) { monitoringToken = process.env.SPM_TOKEN } +if (process.env.MONITORING_TOKEN) { monitoringToken = process.env.MONITORING_TOKEN } +if (process.env.INFRA_TOKEN) { monitoringToken = process.env.SPM_TOKEN } + var useLinuxAgent = 'false' if (os.platform() === 'linux') { useLinuxAgent = 'true' @@ -25,16 +45,18 @@ var cfgLines = [ "# Please don't change this configuration", '# Directory for buffered metrics', 'useLinuxAgent: ' + useLinuxAgent, - 'dbDir: ' + path.join(__dirname, 'spmdb'), + `dbDir: ${path.join(process.cwd(), 'spmdb')}`, ' ', '# SPM_MONITOR_TAGS=project:frontend,environment:test,role:testserver', '# Application Token for SPM', 'tokens:', - ' spm: ' + spmToken, + ` spm: ${monitoringToken}`, + ` monitoring: ${monitoringToken}`, + ` infra: ${infraToken}`, ' ', 'logger:', ' # log file directory default is __dirname / spmlogs', - ' dir: ' + path.join(__dirname, 'spmlogs'), + ` dir: ${path.join(process.cwd(), 'spmlogs')}`, ' # silent = true means no creation of log files', ' silent: false ', ' # log level for output - debug, info, error, defaults to error to be quiet', diff --git a/lib/index.js b/lib/index.js index 0083d53..248d60e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,8 +10,17 @@ var SpmAgent = require('spm-agent') function NodeJSAgent () { + // prepare move from SPM_TOKEN to MONITORING_TOKEN + backward compatibility if (process.env.SPM_TOKEN && !SpmAgent.Config.tokens.spm) { SpmAgent.Config.tokens.spm = process.env.SPM_TOKEN + SpmAgent.Config.tokens.monitoring = process.env.SPM_TOKEN + } + if (process.env.MONITORING_TOKEN && !SpmAgent.Config.tokens.monitoring) { + SpmAgent.Config.tokens.spm = process.env.MONITORING_TOKEN + SpmAgent.Config.tokens.monitoring = process.env.MONITORING_TOKEN + } + if (process.env.INFRA_TOKEN && !SpmAgent.Config.tokens.infra) { + SpmAgent.Config.tokens.infra = process.env.INFRA_TOKEN } var njsAgent = new SpmAgent() var agentsToLoad = [ @@ -27,6 +36,7 @@ function NodeJSAgent () { var Monitor = require(a) njsAgent.createAgent(new Monitor()) } catch (err) { + console.error(err) SpmAgent.Logger.error('Error loading agent ' + a + ' ' + err) } })