Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add a profileSafe call #1

Closed
wants to merge 1 commit into from

2 participants

@lynns

When tracking api calls, it's not enough for us to just pass a name as an id to keep track of the profile call. This change will return a unique id
that can be used to close the correct profile call when there are multiple
profile calls with the same name open at once.

I'm not in love with the profileSafe name so if you think of something
better I'm good with that.

@lynns lynns Add a profileSafe method so profiles with same id can be safely inter…
…leved.

When tracking api calls, it's not enough for us to just pass a name as an
id to keep track of the profile call.  This change will return an id
that can be used to close the correct profile call when there are multiple
profile calls with the same id.

I'm not in love with the `profileSafe` name so if you think of something
better I'm good with that.
ffeeed6
@camshaft
Owner

So like i said in the docs, you'll want to create a new context for that metric:

var call1 = context.context({callId: 1}).profile('my-api-test-call')
  , call2 = context.context({callId: 2}).profile('my-api-test-call');
setTimeout(function() {
  call1.profile('my-api-test-call');
  str.should.match(/measure=my-api-test-call/);
  str.should.match(/callId=1/);

  call2.profile('my-api-test-call');
  str.should.match(/measure=my-api-test-call/);
  str.should.match(/callId=2/);
  done();
}, 50);
@camshaft
Owner

you can also just get a blank context too:

var call1 = context.context().profile('my-api-test-call')
  , call2 = context.context().profile('my-api-test-call');
...
@camshaft
Owner

k. i made a typo. You can't chain the .profile call because it returns the time. You'd have to do something like this:

var call1 = context.context({callId: 1})
  , call2 = context.context({callId: 2});

call1.profile('my-api-test-call');
call2.profile('my-api-test-call');

setTimeout(function() {
  call2.profile('my-api-test-call');
  str.should.match(/measure=my-api-test-call/);
  str.should.match(/callId=2/);

  call1.profile('my-api-test-call');
  str.should.match(/measure=my-api-test-call/);
  str.should.match(/callId=1/);
  done();
}, 50);
@camshaft
Owner

I'm actually thinking this would be the best way; essentially it returns a function that you can once it's done. That way we don't have to deal with ids or new contexts.

var call1 = context.profile('my-api-test-call')
  , call2 = context.profile('my-api-test-call');

setTimeout(function() {
  call2({callId:2});
  str.should.match(/measure=my-api-test-call/);
  str.should.match(/callId=2/);

  call1({callId:1});
  str.should.match(/measure=my-api-test-call/);
  str.should.match(/callId=1/);
  done();
}, 50);
@camshaft
Owner

K. I've implemented the above api so I'll go ahead and close this. If that doesn't work, feel free to open this up again.

@camshaft camshaft closed this
@lynns

I like that. I'll start using it and let you know if I run into anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2013
  1. @lynns

    Add a profileSafe method so profiles with same id can be safely inter…

    lynns authored
    …leved.
    
    When tracking api calls, it's not enough for us to just pass a name as an
    id to keep track of the profile call.  This change will return an id
    that can be used to close the correct profile call when there are multiple
    profile calls with the same id.
    
    I'm not in love with the `profileSafe` name so if you think of something
    better I'm good with that.
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 0 deletions.
  1. +25 −0 index.js
  2. +17 −0 test/metric-log.test.js
View
25 index.js
@@ -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
17 test/metric-log.test.js
@@ -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');
Something went wrong with that request. Please try again.