appmetrics.js icon indicating copy to clipboard operation
appmetrics.js copied to clipboard

Easier way to log custom timed metrics

Open mdvorscak opened this issue 8 years ago • 3 comments

In the first paint example

/**
 * Returns the browser's first paint metric (if available).
 * @return {number} The first paint time in ms.
 */
function getFirstPaintIfSupported() {/* ... */}

// Take measurement after page load.
window.addEventListener('load', function() {
  const fp = getFirstPaintIfSupported();
  if (fp) {
    const metric = new Metric('firstpaint');
    //If I call log here
    metric.log(); // I get firstpaint -1 ms
    // No need to call start()/end(). Can send a value, directly.
    metric.sendToAnalytics('load', metric.name, fp);
  }
});

Of course I can manually generate the log, but it could be nice if the log could take an overridable duration parameter (like sendToAnalytics):

  log(duration = this.duration) {
    console.info(this.name, this.duration, 'ms');
    return this;
  }

Or maybe a way to set the duration itself for these types of events?

set duration(duration){
  this.customDuration = duration;
  return this;
}

What do you think?

mdvorscak avatar Jan 08 '18 21:01 mdvorscak

Not sure I follow completely. .log() is just meant as a convenient helper for writing the console.log statement yourself. It should really throw if start and end were never called. If you want to log a custom metric, maybe just console.log what you want :) ?

ebidel avatar Jan 08 '18 23:01 ebidel

Yeah, of course I can write the log statement myself. I just thought it would be nicer if I could use metric.log to remain consistent. Rather than switching between metric.log and console.log, if I want to keep my log messages the same it means that I must know the internals of how metric is doing it's logging (and format my console.log the same).

Maybe metric.log really should throw an error if there is no duration associated to it?

mdvorscak avatar Jan 08 '18 23:01 mdvorscak

Maybe metric.log really should throw an error if there is no duration associated to it?

Yea exactly :) That ways it's not ambiguous and you don't need to understand the internal details. I'm happy to take a PR for that btw.

ebidel avatar Jan 09 '18 01:01 ebidel