Skip to content

Instantly share code, notes, and snippets.

@trentm
Created May 16, 2023 22:29
// Call the following to change the global labels, and observe the changed
// values in `labels` in Elasticsearch.
// curl http://127.0.0.1:3000/?setGlobalLabel=foo=baz
process.env.ELASTIC_APM_GLOBAL_LABELS = 'foo=bar,spam=eggs'
const apm = require('./').start({
serviceName: 'use-setGlobalLabel',
centralConfig: false,
metricsInterval: '0s',
apiRequestTime: '3s'
})
const http = require('http')
const server = http.createServer(function onRequest (req, res) {
console.log('incoming request: %s %s %s', req.method, req.url, req.headers)
const u = new URL(req.url, 'http://127.0.0.1:3000')
if (u.searchParams.has('setGlobalLabel')) {
u.searchParams.get('setGlobalLabel').trim().split(',').forEach(s => {
const parts = s.split('=') // lame split on multiples like in config.js
if (parts[0].trim()) {
console.log('call setGlobalLabel(%s, %s)', parts[0], parts[1])
apm.setGlobalLabel(parts[0], parts[1])
}
})
}
req.resume()
req.on('end', function () {
res.end('pong\n')
})
})
server.listen(3000, function () {
console.log('listening at http://127.0.0.1:3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment