TypeScript
Quickstart

Getting started in TypeScript

Here's how to get started with TypeScript in a new or existing project.

Install Autometrics

npm install @autometrics/autometrics

Import the library in your code

import { autometrics } from "@autrometrics/autometrics";

Wrap the functions you want to measure

import { autometrics } from "@autometrics/autometrics";
 
const createUser = autometrics(async function createUser(payload: User) {
  // ...
});
 
const user = createUser(); // this function will now report metrics when called
 

Install and add an exporter

⚠️
v0.7 Breaking change!

Since the 0.7 release the core library @autometrics/autometrics does not export the metrics by default anymore. Exporting is now done by a separate package - an exporter. Currently there are 3 exporters available: Prometheus, Prometheus-compatible push gateway, and OpenTelemetry Collector-compatible over HTTP.

All exporters have a unified interface - an init function that should be called as early as possible in the code and that sets up the minimum required configuration.

Install the Prometheus Exporter

npm install @autometrics/exporter-prometheus

Add and run the init function as early as possible in your code

import { init } from "@autometrics/exporter-prometheus"
 
init()

Viewing your metrics

You can now preview your metrics locally and deploy to production.

Using Autometrics Explorer

Autometrics Explorer is an open source accompanying utility to preview and validate your metrics locally. It will automatically run Prometheus under the hood for you and show you Autometrics-instrumented functions in a web UI.

See Local Development for more information.

shell
brew install autometrics-dev/tap/am
 
am start <YOUR_APP_METRICS_ENDPOINT>

View your metrics in your IDE or dashboard

IDE

Install the Autometrics VSCode extension (opens in a new tab) to get generated queries in your tooltips in your IDE.

Alternatively, if you're using a different IDE, you can use the TypeScript plugin directly:

npm install @autometrics/typescript-plugin --save-dev

Then in your tsconfig.json:

tsconfig.json
{
  "compilerOptions": {
    "plugins": [
      {
        "name": "@autometrics/typescript-plugin"
      }
    ]
  }
}

You can now navigate your code in your IDE, hover over the instrumented functions and you will see links for generated queries for your metrics.

Grafana

If you have Grafana you can also import the Autometrics dashboard (opens in a new tab) for an overview and detailed view of the function metrics.