Start Measuring

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

Learn how to install the Angular SDK.

Start a Measurement

To get started:

  • First, inject the PolarisService.
  • Next, create a new instrument by calling the getInstrument() method of the PolarisService class.

Let's take a look.

app.component.ts
export class AppComponent {
  instrument = inject(PolarisService).getInstrument('event-name');

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

Let's review the code above:

  • First, we create a new instrument by calling the getInstrument() method of the PolarisService class.
  • Next, we call the start() method of the Instrument class to start the measurement.
  • Next, we execute the code that we want to measure.

Stop a Measurement

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

  • The done() method is used to indicate that the measurement was successful.
  • The fail() method is used to indicate that the measurement failed.

Let's take a look.

Angular
export class AppComponent {
  instrument = inject(PolarisService).getInstrument('event-name');

  onClick() {
    instrument.start();
    try {
      // code you want to measure
      instrument.done();
    } catch (error) {
      instrument.fail(error);
    }
  }
}

Let's review the code above.

  • First, we call the start() method of the Instrument class to start the measurement.
  • Next, we execute the code that we want to measure.
  • Next, we call the done() method of the Instrument class to indicate that the measurement was successful.
  • If the code that we want to measure throws an error, we call the fail() method of the Instrument class to indicate that the measurement failed.

Next Steps