Create Measurement

Creating measurements is a two-step process. First, you create a new measurement using the measure() function. This is the starting point of what you are measuring. Second, you invoke either the done() or fail() method on the Instrument object returned by the measure() function.

When you create a new measurement, you must provide the eventName.

ESM
// first, create a new starting point of a measurement
const instrument = measure('event-name');

Measurement Metadata

You can also provide additional metadata to associate with the measurement by specifying the measurementMetadata as the second argument to the measure() function. This metadata will be included in the measurement event.

ESM
  // first, create a new starting point of a measurement
  const instrument = measure('event-name', {
    attribute1: 'value1',
    attribute2: 'value2',
  });

Complete Measurement

Now that we have created a new measurement, we can invoke the done or fail method on the Instrument returned by the measure function.

In this example, we'll invoke the done method.

ESM
// second, invoke the done method on the instrument
instrument.done();

The done method takes an optional resultMetadata object as a parameter. This object can contain any additional metadata you want to associate with the measurement.

ESM
  // second, invoke the done method on the instrument
  instrument.done({
    attribute1: 'value1',
    attribute2: 'value2',
  });

Failed Measurement

If whatever you are measuring has failed, you can invoke the fail method on the Instrument returned by the measure function.

ESM

The fail method takes an optional resultMetadata object as a parameter. This object can contain any additional metadata you want to associate with the measurement.

ESM
  // second, invoke the fail method on the instrument
  instrument.fail({
    attribute1: 'value1',
    attribute2: 'value2',
  });