Start Measuring

Instrumenting your code with measurements helps you to monitor the performance and reliability of your application. The instrumented code is deployed to your users and runs on their devices. This enables you to get insight into real user performance and reliability.

Each measurement is sent to Polaris using the SDK. Polaris collects measurements and monitors your indicators in real time.

What is a Measurement?

A measurement is a way to track the performance of a specific event in your React application.

At the core, a measurement is composed of:

  • The event name
  • The duration
  • The result: either success or failure
  • The timestamp
  • Additional metadata

Prerequisites

Before we get started, make sure you have:

  • Installed the Polaris SDK in your JavaScript/TypeScript application.
  • Configured the Polaris SDK with your application's API Key.

Learn how to install the TypeScript SDK.

Start a Measurement

To get started:

  • First, import the getInstrument function from the Polaris SDK.
  • Next, create a new instrument by invoking the getInstrument function with the name of the event you want to measure.

Let's take a look.

TypeScript
import { getInstrument } from '@getpolaris.ai/sdk';

const instrument = getInstrument('auth');

function onClick() {
  instrument.start();
  // code you want to measure
}

Let's review the code above.

  • First, we import the getInstrument function from the Polaris SDK.
  • Next, we create a new instrument by invoking the getInstrument function with the name of the event we want to measure.
  • Next, we invoke the start() method of the instrument to start the measurement.
  • Next, we execute the code we want to measure.

Stop a Measurement

To stop a measurement invoke either the done() or fail() method.

TypeScript
import { getInstrument } from '@getpolaris.ai/sdk';

const instrument = getInstrument('auth');

function onClick() {
  instrument.start();
  return authenticate()
    .then(() => instrument.done())
    .catch((error) => instrument.fail(error));
}

Let's review the code above.

  • First, we import the getInstrument function from the Polaris SDK.
  • Next, we create a new instrument by invoking the getInstrument function with the name of the event we want to measure.
  • Next, we invoke the start() method of the instrument to start the measurement.
  • Next, we execute the code we want to measure.
  • Next, we invoke the done() method of the instrument to stop the measurement and indicate that the measurement was successful.
  • If the code we want to measure throws an error, we invoke the fail() method of the instrument to stop the measurement and indicate that the measurement failed.