Easily add metrics to your system – and actually understand them using automatically customized Prometheus queries
MIT License
Metrics are a powerful and cost-efficient tool for understanding the health and performance of your code in production. But it's hard to decide what metrics to track and even harder to write queries to understand the data.
Autometrics provides a wrapper function and decorator to instrument functions, classes, and methods with the most useful metrics: request rate, error rate, and latency. It standardizes these metrics and then generates powerful Prometheus queries based on your function details to help you quickly identify and debug issues in production.
Learn more about Autometrics at autometrics.dev.
autometrics()
wrapper / @Autometrics()
decorator instruments any function or class method to track its most useful metricsimport { autometrics } from "@autometrics/autometrics";
const createUserWithMetrics = autometrics(async function createUser(payload: User) {
// ...
});
createUserWithMetrics();
(See the recipes below for other setup scenarios.)
npm install @autometrics/autometrics @autometrics/exporter-prometheus
# or
yarn add @autometrics/autometrics @autometrics/exporter-prometheus
# or
pnpm add @autometrics/autometrics @autometrics/exporter-prometheus
autometrics
wrapper or Autometrics
decoratorimport { autometrics } from "@autometrics/autometrics";
const createUserWithMetrics = autometrics(async function createUser(payload: User) {
// ...
});
createUserWithMetrics();
import { Autometrics } from "@autometrics/autometrics";
class User {
@Autometrics()
async createUser(payload: User) {
// ...
}
}
init()
to set up a Prometheus scrape endpointThis endpoint will serve to export the metrics from your application and allows them to be scraped by Prometheus.
import { init } from "@autometrics/exporter-prometheus";
init(); // starts the webserver with the `/metrics` endpoint on port 9464
You can use the open source Autometrics CLI to run automatically configured Prometheus locally to see the metrics that will be registered by the change. See the Autometrics CLI docs for more information.
or you can configure Prometheus manually:
scrape_configs:
- job_name: my-app
metrics_path: /metrics # The default path for the Autometrics Prometheus exporter
static_configs:
- targets: ['localhost:9464'] # The default port for the Autometrics Prometheus exporter
scrape_interval: 200ms
# For a real deployment, you would want the scrape interval to be
# longer but for testing, you want the data to show up quickly
See the docs for more Prometheus configurations.
In order to get charts in VSCode, download the Autometrics VSCode extension.
npm install --save-dev @autometrics/typescript-plugin
Add the language service plugin to the tsconfig.json
file:
{
"compilerOptions": {
"plugins": [
{
"name": "@autometrics/typescript-plugin",
"prometheusUrl": ""
}
]
}
}
Below are different recipes for using Autometrics with a server-side setup and edge/client-side setups. If you would like to see examples with specific frameworks, please have a look at the examples/ directory.
npm install @autometrics/autometrics @autometrics/exporter-prometheus
# or
yarn add @autometrics/autometrics @autometrics/exporter-prometheus
# or
pnpm add @autometrics/autometrics @autometrics/exporter-prometheus
import { autometrics } from "@autometrics/autometrics";
import { init } from "@autometrics/exporter-prometheus";
init(); // starts the webserver with the `/metrics` endpoint on port 9464
async function createUserRaw(payload: User) {
// ...
}
const createUser = autometrics(createUserRaw);
// ^ instrumented function
npm install @autometrics/autometrics @autometrics/exporter-prometheus-push-gateway
# or
yarn add @autometrics/autometrics @autometrics/exporter-prometheus-push-gateway
# or
pnpm add @autometrics/autometrics @autometrics/exporter-prometheus-push-gateway
import { autometrics } from "@autometrics/autometrics";
import { init } from "@autometrics/exporter-prometheus-push-gateway";
init({ url: "https://<your-push-gateway>" });
async function createUserRaw(payload: User) {
// ...
}
const createUser = autometrics(createUserRaw);
// ^ instrumented function
npm install @autometrics/autometrics @autometrics/exporter-otlp-http
# or
yarn add @autometrics/autometrics @autometrics/exporter-otlp-http
# or
pnpm add @autometrics/autometrics @autometrics/exporter-otlp-http
import { autometrics } from "@autometrics/autometrics";
import { init } from "@autometrics/exporter-otlp-http";
init({ url: "https://<your-otel-collector>" });
async function createUserRaw(payload: User) {
// ...
}
const createUser = autometrics(createUserRaw);
// ^ instrumented function
Issues, feature suggestions, and pull requests are very welcome!
If you are interested in getting involved: