Skip to content

Commit

Permalink
SC-4677: code cleanup + testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanrahic committed Dec 3, 2019
1 parent 5e7da3d commit 22494f7
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 73 deletions.
8 changes: 4 additions & 4 deletions lib/eventLoopAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
*/

'use strict'
var Agent = require('spm-agent').Agent
var monitor = require('./eventLoopStats.js')
const { Agent } = require('spm-agent')
const monitor = require('./eventLoopStats.js')

module.exports = function () {
var elAgent = new Agent(
const elAgent = new Agent(
{
start: function (agent) {
this.elListener = function (stats) {
var metric = {
const metric = {
ts: Date.now(),
measurement: 'nodejs.eventloop',
tags: {},
Expand Down
39 changes: 17 additions & 22 deletions lib/gcAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
*/
'use strict'

var SpmAgent = require('spm-agent')
var Agent = SpmAgent.Agent
// var gcStats = (require('@risingstack/gc-stats'))()
var gcStats = (require('gc-stats'))()

var config = SpmAgent.Config
const { Agent, Config } = require('spm-agent')
const gcStats = (require('gc-stats'))()

function getProcessHeapInfo () {
var pm = process.memoryUsage()
var metrics = {}
// metrics.processMemory = [pm.heapUsed, pm.heapTotal, pm.rss]
const pm = process.memoryUsage()
const metrics = {}
metrics.processHeapUsed = pm.heapUsed
metrics.processHeapTotal = pm.heapTotal
metrics.processMemoryRss = pm.rss
Expand All @@ -29,13 +24,13 @@ function getProcessHeapInfo () {
function getGcStats () {
return {
start: function (agent) {
var Measured = require('measured-core')
var fullGc = new Measured.Counter()
var incGc = new Measured.Counter()
var heapCompactions = new Measured.Counter()
var gcTimes = new Measured.Histogram()
var heapDiff = new Measured.Histogram()
var lastHeapInfo = getProcessHeapInfo()
const Measured = require('measured-core')
const fullGc = new Measured.Counter()
const incGc = new Measured.Counter()
const heapCompactions = new Measured.Counter()
const gcTimes = new Measured.Histogram()
const heapDiff = new Measured.Histogram()
let lastHeapInfo = getProcessHeapInfo()
gcStats.on('stats', function (info) {
if (info.gctype === 1) {
incGc.inc()
Expand All @@ -54,11 +49,11 @@ function getGcStats () {
heapDiff.update(info.diff.usedHeapSize * -1)
})

var timerId = setInterval(function () {
var now = new Date().getTime()
var heapInfo = lastHeapInfo
var heapCompactionsJSON = heapCompactions.toJSON()
var metricValues = {
const timerId = setInterval(function () {
const now = new Date().getTime()
const heapInfo = lastHeapInfo
const heapCompactionsJSON = heapCompactions.toJSON()
const metricValues = {
'gc.inc': incGc.toJSON() || 0,
'gc.full': fullGc.toJSON() || 0,
'gc.time': gcTimes.toJSON().sum || 0,
Expand All @@ -74,7 +69,7 @@ function getGcStats () {
heapCompactions.reset(0)
gcTimes.reset()
heapDiff.reset()
}, config.collectionInterval)
}, Config.collectionInterval)
if (timerId.unref) {
timerId.unref()
}
Expand Down
60 changes: 28 additions & 32 deletions lib/httpServerAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,44 @@
*
*/

var http = require('http')
var https = require('https')
const http = require('http')
const https = require('https')

// constants
var FINISH_EVENT_NAME = 'finish'
var ABRUPT_CLOSE_EVENT_NAME = 'close'
const FINISH_EVENT_NAME = 'finish'
const ABRUPT_CLOSE_EVENT_NAME = 'close'

module.exports = function httpServerAgent () {
// var SpmAgent = require('spm-agent')
var SpmAgent = require('../../spm-agent/lib/index')
var Agent = SpmAgent.Agent
var config = SpmAgent.Config
var logger = SpmAgent.Logger
var Measured = require('measured-core')
var histogram = new Measured.Histogram()
var timer = new Measured.Timer()
const { Agent, Config, Logger } = require('spm-agent')
const Measured = require('measured-core')
const histogram = new Measured.Histogram()
const timer = new Measured.Timer()

// setup context for monitoring
var ctx = {
const ctx = {
reqSize: 0,
resSize: 0,
timer: timer,
logger: logger,
logger: Logger,
histogram: histogram,
stats: Measured.createCollection()
}

// bind monitorHttp safely
var monitor = safeProcess(ctx, monitorHttp)
const monitor = safeProcess(ctx, monitorHttp)

// setup monitoring agent
var hAgent = new Agent({
const hAgent = new Agent({
start: function (agent) {
this._agent = agent
patchHttpServer(monitor)
patchHttpsServer(monitor)
var timerId = setInterval(function () {
var stats = ctx.stats
var httpStats = stats.toJSON()
var responseTimes = histogram.toJSON()
var now = Date.now()
var metricValue = {
const timerId = setInterval(function () {
const stats = ctx.stats
const httpStats = stats.toJSON()
const responseTimes = histogram.toJSON()
const now = Date.now()
const metricValue = {
requests: httpStats.requests ? httpStats.requests.count : 0, // http.requestCount (int)
errors: httpStats.errRate ? httpStats.errRate.count : 0, // http.errorCount (int)
'errors.3xx': httpStats['3xxRate'] ? httpStats['3xxRate'].count : 0, // http.3xx (int)
Expand All @@ -82,7 +78,7 @@ module.exports = function httpServerAgent () {
timer.reset()
ctx.reqSize = 0
ctx.resSize = 0
}, config.collectionInterval)
}, Config.collectionInterval)

// unref so we can gracefully exit
if (timerId.unref) {
Expand Down Expand Up @@ -114,15 +110,15 @@ function safeProcess (ctx, fn) {
* @param {http.ServerResponse} res
*/
function monitorHttp (ctx, req, res) {
var stopwatch = ctx.timer.start()
let stopwatch = ctx.timer.start()

function endOfConnectionHandler () {
// cached vars
var stats = ctx.stats
var histogram = ctx.histogram
// cached consts
const stats = ctx.stats
const histogram = ctx.histogram

// capture duration
var duration = stopwatch.end()
const duration = stopwatch.end()
stopwatch = null
stats.meter('requests').mark()

Expand Down Expand Up @@ -164,12 +160,12 @@ function monitorHttp (ctx, req, res) {
/**
* Monkey-patching!
*/
var origHttpCreateServer = http.createServer
var origHttpsCreateServer = https.createServer
const origHttpCreateServer = http.createServer
const origHttpsCreateServer = https.createServer

function patchHttpServer (monitorReqHandler) {
http.createServer = function () {
var server = origHttpCreateServer.apply(http, arguments)
const server = origHttpCreateServer.apply(http, arguments)
server.on('request', monitorReqHandler)
return server
}
Expand All @@ -178,7 +174,7 @@ function patchHttpServer (monitorReqHandler) {

function patchHttpsServer (monitorReqHandler) {
https.createServer = function () {
var server = origHttpsCreateServer.apply(https, arguments)
const server = origHttpsCreateServer.apply(https, arguments)
server.on('request', monitorReqHandler)
return server
}
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* This source code is to be used exclusively by users and customers of Sematext.
* Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
*/
var SpmAgent = require('spm-agent')
// var SpmAgent = require('spm-agent')
var SpmAgent = require('../../spm-agent/lib/index')

function NodeJSAgent () {
if (process.env.SPM_TOKEN && !SpmAgent.Config.tokens.spm) {
Expand Down
14 changes: 6 additions & 8 deletions lib/processAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*/
'use strict'

// var SpmAgent = require('spm-agent')
var SpmAgent = require('../../spm-agent/lib/index')
var Agent = SpmAgent.Agent
var config = SpmAgent.Config
const { Agent, Config } = require('spm-agent')

function getProcessInfo () {
const { uptime, pid, ppid, send } = process
Expand All @@ -29,9 +26,9 @@ function getProcessInfo () {
function getProcessStats () {
return {
start: function (agent) {
var timerId = setInterval(function () {
var now = new Date().getTime()
var {
const timerId = setInterval(function () {
const now = new Date().getTime()
const {
uptime,
pid,
ppid,
Expand All @@ -51,14 +48,15 @@ function getProcessStats () {
processes: 1
}
})
}, config.collectionInterval)
}, Config.collectionInterval)
if (timerId.unref) {
timerId.unref()
}
},
stop: function () { }
}
}

module.exports = function () {
return new Agent(getProcessStats())
}
7 changes: 2 additions & 5 deletions lib/workerAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
'use strict'

const cluster = require('cluster')
// const SpmAgent = require('spm-agent')
const SpmAgent = require('../../spm-agent/lib/index')
const Agent = SpmAgent.Agent
const config = SpmAgent.Config
const { Agent, Config } = require('spm-agent')

function getWorkers () {
return {
Expand All @@ -28,7 +25,7 @@ function getWorkers () {
fields: { workers: Object.keys(cluster.workers || {}).length || 1 }
})
}
}, config.collectionInterval)
}, Config.collectionInterval)
if (timerId.unref) {
timerId.unref()
}
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/
/* global describe, it */
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
var config = require('spm-agent').Config
var SpmAgent = require('spm-agent')
var config = SpmAgent.Config
var port = (process.env.NJS_TEST_PORT || 8097)
var receiverUrl = 'http://127.0.0.1:' + port
config.rcFlat.spmSenderBulkInsertUrl = receiverUrl
Expand Down

0 comments on commit 22494f7

Please sign in to comment.