Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add a profileSafe call #1

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+42 −0
Split
View
@@ -17,6 +17,11 @@ module.exports = exports = function metric() {
*/
function noop () {};
+var idGen = Date.now();
+function nextId() {
+ return ++idGen;
+}
+
/**
* Apply a context to the logger
*
@@ -46,6 +51,26 @@ exports.context = function(obj) {
return c._profiles[id] = Date.now();
}
};
+ c.profileSafe = function(id, props) {
+ var prof;
+ if (c._profiles[id]) {
+ prof = c._profiles[id];
+ var time = Date.now() - prof.start,
+ finalProps = merge(prof.props, props || {});
+ delete c._profiles[id];
+ return c(prof.id, time, "ms", finalProps);
+ }
+ else {
+ prof = {
+ start: Date.now(),
+ profId: nextId(),
+ id: id,
+ props: props || {}
+ };
+ c._profiles[prof.profId] = prof;
+ return prof.profId;
+ }
+ };
c.context = function(obj) {
return exports.context(obj).use(c);
};
View
@@ -119,6 +119,23 @@ describe("metric-log", function(){
});
});
+ describe("context.profileSafe(id)", function(){
+ it("should allow interlieved profile calls with same id", function(done) {
+ var call1 = context.profileSafe('my-api-test-call', {callId: 1}),
+ call2 = context.profileSafe('my-api-test-call', {callId: 2});
+ setTimeout(function() {
+ context.profileSafe(call2);
+ str.should.match(/measure=my-api-test-call/);
+ str.should.match(/callId=2/);
+
+ context.profileSafe(call1);
+ str.should.match(/measure=my-api-test-call/);
+ str.should.match(/callId=1/);
+ done();
+ }, 50);
+ });
+ });
+
describe("context.profile(id)", function(){
it("should profile a function call", function(done) {
context.profile('my-api-test-call');