-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
181 additions
and
0 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
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,61 @@ | ||
var debug = require('debug')('risingstack/trace') | ||
|
||
var Timer = require('../../timer') | ||
|
||
function CustomMetrics (options) { | ||
this.name = 'Metrics/Custom' | ||
var _this = this | ||
this.collectorApi = options.collectorApi | ||
this.config = options.config | ||
this.collectInterval = this.config.collectInterval | ||
|
||
this.incrementMetrics = {} | ||
this.recordMetrics = {} | ||
|
||
this.timer = new Timer(function () { | ||
_this.sendMetrics() | ||
}, this.collectInterval) | ||
} | ||
|
||
CustomMetrics.prototype.increment = function (name, amount) { | ||
if (!name) { | ||
throw new Error('Name is needed for CustomMetrics.increment') | ||
} | ||
amount = amount || 1 | ||
if (this.incrementMetrics[name]) { | ||
this.incrementMetrics[name] += amount | ||
} else { | ||
this.incrementMetrics[name] = amount | ||
} | ||
} | ||
|
||
CustomMetrics.prototype.record = function (name, value) { | ||
if (!name) { | ||
throw new Error('Name is needed for CustomMetrics.record') | ||
} | ||
if (typeof value === 'undefined') { | ||
throw new Error('Name is needed for CustomMetrics.record') | ||
} | ||
|
||
if (this.recordMetrics[name]) { | ||
this.recordMetrics[name].push(value) | ||
} else { | ||
this.recordMetrics[name] = [value] | ||
} | ||
} | ||
|
||
CustomMetrics.prototype.sendMetrics = function () { | ||
this.collectorApi.sendCustomMetrics({ | ||
incrementMetrics: this.incrementMetrics, | ||
recordMetrics: this.recordMetrics | ||
}) | ||
|
||
this.incrementMetrics = {} | ||
this.recordMetrics = {} | ||
} | ||
|
||
function create (options) { | ||
return new CustomMetrics(options) | ||
} | ||
|
||
module.exports.create = create |
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,41 @@ | ||
var expect = require('chai').expect | ||
|
||
var CustomMetrics = require('./') | ||
|
||
describe('The CustomMetrics module', function () { | ||
it('sends metrics', function () { | ||
var ISOString = 'date-string' | ||
var collectorApi = { | ||
sendCustomMetrics: this.sandbox.spy() | ||
} | ||
|
||
this.sandbox.stub(Date.prototype, 'toISOString', function () { | ||
return ISOString | ||
}) | ||
|
||
var customMetrics = CustomMetrics.create({ | ||
collectorApi: collectorApi, | ||
config: { | ||
collectInterval: 1 | ||
} | ||
}) | ||
|
||
customMetrics.increment('/TestCategory/TestName') | ||
customMetrics.increment('/TestCategory/TestName') | ||
customMetrics.increment('/TestCategory/TestName', 3) | ||
|
||
customMetrics.record('/TestCategory/TestRecord', 10) | ||
customMetrics.record('/TestCategory/TestRecord', 2) | ||
|
||
customMetrics.sendMetrics() | ||
|
||
expect(collectorApi.sendCustomMetrics).to.be.calledWith({ | ||
incrementMetrics: { | ||
'/TestCategory/TestName': 5 | ||
}, | ||
recordMetrics: { | ||
'/TestCategory/TestRecord': [10, 2] | ||
} | ||
}) | ||
}) | ||
}) |
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
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