-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from monti-apm/feature/job-monitoring
Job monitoring
- Loading branch information
Showing
18 changed files
with
797 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { checkModuleUsed } from './commonjs-utils'; | ||
|
||
export function wrapAgenda () { | ||
Meteor.startup(() => { | ||
if (checkModuleUsed('@hokify/agenda')) { | ||
instrumentAgendaTs(); | ||
} | ||
if (checkModuleUsed('agenda')) { | ||
instrumentAgenda(); | ||
} | ||
}); | ||
} | ||
|
||
function instrumentAgendaTs () { | ||
// eslint-disable-next-line global-require | ||
let agenda = require('@hokify/agenda'); | ||
let Job = agenda.Job; | ||
|
||
instrumentJob(Job.prototype); | ||
} | ||
|
||
function instrumentAgenda () { | ||
// eslint-disable-next-line global-require | ||
let Job = require('agenda/dist/job').Job; | ||
instrumentJob(Job.prototype); | ||
} | ||
|
||
function instrumentJob (JobMethods) { | ||
let oldSaveJob = JobMethods.save; | ||
JobMethods.save = function () { | ||
let id = this.attrs._id; | ||
|
||
if (!id) { | ||
let name = this.attrs.name; | ||
Kadira.models.jobs.trackNewJob(name); | ||
} | ||
|
||
return oldSaveJob.apply(this, arguments); | ||
}; | ||
|
||
let oldRun = JobMethods.run; | ||
JobMethods.run = function (...args) { | ||
let name = this.attrs.name; | ||
let waitTime = Date.now() - this.attrs.nextRunAt; | ||
let details = { | ||
name, | ||
waitTime, | ||
data: this.attrs.data | ||
}; | ||
|
||
return Kadira.traceJob(details, () => oldRun.apply(this, args)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { checkModuleUsed, tryResolve } from './commonjs-utils'; | ||
|
||
export function wrapBullMQ () { | ||
Meteor.startup(() => { | ||
if (checkModuleUsed('bullmq')) { | ||
instrumentBullMQ(tryResolve('bullmq')); | ||
} | ||
}); | ||
} | ||
|
||
function instrumentBullMQ (modulePath) { | ||
let bullMq = Npm.require(modulePath); | ||
|
||
let oldAdd = bullMq.Queue.prototype.addJob; | ||
bullMq.Queue.prototype.addJob = function () { | ||
Kadira.models.jobs.trackNewJob(this.name); | ||
return oldAdd.apply(this, arguments); | ||
}; | ||
|
||
let oldAddBulk = bullMq.Queue.prototype.addJobs; | ||
bullMq.Queue.prototype.addJobs = function (jobs) { | ||
let count = jobs && jobs.length || 0; | ||
|
||
Kadira.models.jobs.trackNewJob(this.name, count); | ||
|
||
return oldAddBulk.apply(this, arguments); | ||
}; | ||
|
||
let oldProcessJob = bullMq.Worker.prototype.callProcessJob; | ||
bullMq.Worker.prototype.callProcessJob = function (...args) { | ||
let job = args[0]; | ||
let name = this.name; | ||
|
||
return Kadira.traceJob({ | ||
name, | ||
waitTime: Date.now() - (job.timestamp + (job.delay || 0)), | ||
_attributes: { | ||
jobId: job.id, | ||
jobName: job.name, | ||
jobCreated: new Date(job.timestamp), | ||
jobDelay: job.delay || 0, | ||
queueName: job.queueName, | ||
attemptsMade: job.attemptsMade, | ||
}, | ||
data: job.data | ||
}, () => oldProcessJob.apply(this, args)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const logger = Npm.require('debug')('kadira:apm'); | ||
const path = require('path'); | ||
|
||
let meteorBootstrap = typeof __meteor_bootstrap__ === 'object' && __meteor_bootstrap__; | ||
let serverDir = meteorBootstrap ? meteorBootstrap.serverDir : process.cwd(); | ||
let nodeRequire; | ||
|
||
try { | ||
// eslint-disable-next-line global-require | ||
let nodeModule = require('node:module'); | ||
|
||
nodeRequire = nodeModule.createRequire(serverDir); | ||
} catch (err) { | ||
logger(`Failed to create native require: ${err}`); | ||
} | ||
|
||
export function tryResolve (modulePath) { | ||
if (!meteorBootstrap || !nodeRequire) { | ||
return false; | ||
} | ||
|
||
try { | ||
return nodeRequire.resolve(modulePath, { | ||
paths: [ | ||
serverDir, | ||
path.resolve(serverDir, 'npm') | ||
] | ||
}); | ||
} catch (err) { | ||
if (err.code === 'MODULE_NOT_FOUND') { | ||
return false; | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
|
||
export function checkModuleUsed (name) { | ||
let resolved = tryResolve(name); | ||
|
||
if (!resolved) { | ||
return false; | ||
} | ||
|
||
return !!nodeRequire.cache[resolved]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function wrapSyncedCron () { | ||
Meteor.startup(() => { | ||
let cronPackage = Package['littledata:synced-cron'] || Package['percolate:synced-cron']; | ||
|
||
if (!cronPackage) { | ||
return; | ||
} | ||
|
||
let cron = cronPackage.SyncedCron; | ||
|
||
Object.values(cron._entries).forEach(entry => { | ||
let oldJob = entry.job; | ||
|
||
entry.job = function (...args) { | ||
return Kadira.traceJob({ name: entry.name },() => oldJob.apply(this, args)); | ||
}; | ||
}); | ||
}); | ||
} |
Oops, something went wrong.