Skip to content
This repository

Add a profileSafe call #1

Closed
wants to merge 1 commit into from

2 participants

Stephen Lynn Cameron Bytheway
Stephen Lynn
lynns commented

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.

Stephen Lynn 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
Cameron Bytheway
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);
Cameron Bytheway
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');
...
Cameron Bytheway
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);
Cameron Bytheway
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);
Cameron Bytheway
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.

Cameron Bytheway camshaft closed this
Stephen Lynn
lynns commented

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

Showing 1 unique commit by 1 author.

Mar 27, 2013
Stephen Lynn 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
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 42 additions and 0 deletions. Show diff stats Hide diff stats

  1. +25 0 index.js
  2. +17 0 test/metric-log.test.js
25 index.js
@@ -17,6 +17,11 @@ module.exports = exports = function metric() {
17 17 */
18 18 function noop () {};
19 19
  20 +var idGen = Date.now();
  21 +function nextId() {
  22 + return ++idGen;
  23 +}
  24 +
20 25 /**
21 26 * Apply a context to the logger
22 27 *
@@ -46,6 +51,26 @@ exports.context = function(obj) {
46 51 return c._profiles[id] = Date.now();
47 52 }
48 53 };
  54 + c.profileSafe = function(id, props) {
  55 + var prof;
  56 + if (c._profiles[id]) {
  57 + prof = c._profiles[id];
  58 + var time = Date.now() - prof.start,
  59 + finalProps = merge(prof.props, props || {});
  60 + delete c._profiles[id];
  61 + return c(prof.id, time, "ms", finalProps);
  62 + }
  63 + else {
  64 + prof = {
  65 + start: Date.now(),
  66 + profId: nextId(),
  67 + id: id,
  68 + props: props || {}
  69 + };
  70 + c._profiles[prof.profId] = prof;
  71 + return prof.profId;
  72 + }
  73 + };
49 74 c.context = function(obj) {
50 75 return exports.context(obj).use(c);
51 76 };
17 test/metric-log.test.js
@@ -119,6 +119,23 @@ describe("metric-log", function(){
119 119 });
120 120 });
121 121
  122 + describe("context.profileSafe(id)", function(){
  123 + it("should allow interlieved profile calls with same id", function(done) {
  124 + var call1 = context.profileSafe('my-api-test-call', {callId: 1}),
  125 + call2 = context.profileSafe('my-api-test-call', {callId: 2});
  126 + setTimeout(function() {
  127 + context.profileSafe(call2);
  128 + str.should.match(/measure=my-api-test-call/);
  129 + str.should.match(/callId=2/);
  130 +
  131 + context.profileSafe(call1);
  132 + str.should.match(/measure=my-api-test-call/);
  133 + str.should.match(/callId=1/);
  134 + done();
  135 + }, 50);
  136 + });
  137 + });
  138 +
122 139 describe("context.profile(id)", function(){
123 140 it("should profile a function call", function(done) {
124 141 context.profile('my-api-test-call');

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.