Skip to content

Commit

Permalink
add infra-token to config script, agent startup & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
megastef committed Jan 16, 2020
1 parent bc07e5f commit 2099b6a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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
Expand All @@ -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/
Expand Down
36 changes: 29 additions & 7 deletions bin/spmconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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)
}
})
Expand Down

0 comments on commit 2099b6a

Please sign in to comment.