Manually Measure

Not common

Creating a measurement using the measure() function is most common. However, in some cases we want to manually determine the duration of the measurement and create the measurement ourselves.

To create a new measurement, we can use the measurement() function.

ESM
  // manually calculate duration of event
  const duration = performance.now() - created;

  // create measurement
  measurement(
    'request',
    MeasurementResult.SUCCESS,
    duration
  );

In the example above, we manually calculate the duration of the request and then create the measurement.

Measurement Metadata

In addition to the duration, we can also add metadata to the measurement by specifying the measurementMetadata argument.

ESM
// manually calculate duration of event
const duration = performance.now() - created;

// create measurement
measurement(
  'request',
  MeasurementResult.SUCCESS,
  duration,
  {
    url: 'https://example.com',
    method: 'GET',
  }
);

Result Metadata

In addition to the duration and measurement metadata, we can also add metadata to the result by specifying the resultMetadata argument.

ESM
// manually calculate duration of event
const duration = performance.now() - created;

// create measurement
measurement(
  'request',
  MeasurementResult.SUCCESS,
  duration,
  {
    url: 'https://example.com',
    method: 'GET',
  },
  {
    status: 200,
    statusText: 'OK',
  }
);