Start Measuring

Instrumenting your code with measurements helps you to monitor the performance and reliability of your React 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 React application.
  • Configured the Polaris Provider with your application's API Key.

Learn how to install the React SDK.

Start a Measurement

To get started:

  • First, import the useInstrument hook from the Polaris SDK.
  • Next, call the useInstrument() hook and provide the name of the event that you want to measure.

Let's take a look.

React
export function App() {
  const instrument = useInstrument('auth');

  const handleClick = useCallback(() => {
    instrument.start();
    // code you want to measure
  }, [instrument]);
}

Let's review the code above:

  • First, we call the useInstrument() hook and provide the name of the event that we want to measure.
  • Next, we call the start() method on the Instrument object. This starts the measurement.
  • Next, we execute the code that we want to measure.

Stop a Measurement

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

React
export function Dashboard() {
  const instrument = useInstrument('auth');

  useEffect(() => {
    fetchData()
      .then(() => instrument.done())
      .catch((error) => instrument.fail({ error }));
  }, [fetchData, instrument]);
}

Let's review the code above:

  • First, we call the useInstrument() hook and provide the name of the event that we want to measure.
  • Next, we call the done() method on the Instrument object. This stops the measurement and indicates that the measurement was successful.
  • If the code that we want to measure results in a rejected promise, we call the fail() method on the Instrument object. This stops the measurement and indicates that the measurement failed. We can also optionally provide an error object that contains information about the failure.

Next Steps