• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 2 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily add metrics to your system – and actually understand them using automatically customized Prometheus queries

GitHub_headerImage

Autometrics

Autometrics CI status License discord server

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.

Benefits

  • ✨ autometrics() wrapper / @Autometrics() decorator instruments any function or class method to track its most useful metrics
  • 🌳 Works in Deno, NodeJS and browser environments (*)
  • πŸ’‘ Writes Prometheus queries so you can understand the data generated without knowing PromQL
  • πŸ”— Injects links to live Prometheus charts directly into each function's doc
  • πŸ” Helps you to identify commits that introduced errors or increased latency
  • πŸ“Š Grafana dashboards work out of the box and visualize the performance of instrumented functions & SLOs
  • ⚑ Minimal runtime overhead

Known issues

  • Pushing metrics from client-side and FaaS environments is currently experimental.

Advanced Features

  • 🚨 Allows you to define alerts using SLO best practices directly in your source code comments

Example

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

const createUserWithMetrics = autometrics(async function createUser(payload: User) {
  // ...
});

createUserWithMetrics();

AutometricsTS demo

Quickstart with Node.js and Prometheus

(See the recipes below for other setup scenarios.)

  1. Install the library
npm install @autometrics/autometrics @autometrics/exporter-prometheus
# or
yarn add @autometrics/autometrics @autometrics/exporter-prometheus
# or
pnpm add @autometrics/autometrics @autometrics/exporter-prometheus
  1. Instrument your code using the autometrics wrapper or Autometrics decorator
import { 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) {
    // ...
  }
}
  1. Call init() to set up a Prometheus scrape endpoint

This 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
  1. Run Prometheus locally to validate and preview the data

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.

  1. Install the IDE extension

In order to get charts in VSCode, download the Autometrics VSCode extension.

If you're on any other IDE you can install and add the TypeScript plugin directly:
npm install --save-dev @autometrics/typescript-plugin

Add the language service plugin to the tsconfig.json file:

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "@autometrics/typescript-plugin",
        "prometheusUrl": ""
      }
    ]
  }
}

Documentation

API Reference

Recipes

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.

Server-side example with Prometheus

Installation

npm install @autometrics/autometrics @autometrics/exporter-prometheus
# or
yarn add @autometrics/autometrics @autometrics/exporter-prometheus
# or
pnpm add @autometrics/autometrics @autometrics/exporter-prometheus

Usage

  1. Anywhere in your source code:
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

Recipe: Edge/Client-side example with a Prometheus Push Gateway

Installation

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

Usage

  1. Anywhere in your source code:
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

Recipe: Edge/Client-side example with the OpenTelemetry Collector

Installation

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

Usage

  1. Anywhere in your source code:
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

Contributing

Issues, feature suggestions, and pull requests are very welcome!

If you are interested in getting involved:

More Repositories

1

autometrics-rs

Easily add metrics to your code that actually help you spot and debug issues in production. Built on Prometheus and OpenTelemetry.
Rust
799
star
2

autometrics-py

Easily add metrics to your code that actually help you spot and debug issues in production. Built on Prometheus and OpenTelemetry.
Python
215
star
3

autometrics-go

Easily add metrics to your system -- and actually understand them using automatically customized Prometheus queries
Go
134
star
4

autometrics-cs

A C# Implementation of Autometrics
C#
23
star
5

diff-metrics

A github action that comments on PR to describe impact on metrics
TypeScript
13
star
6

vscode-autometrics

A vscode extension to extract information from your autometrics enabled code.
TypeScript
13
star
7

autometrics-rb

Ruby
7
star
8

autometrics-shared

Resources used by all of the autometrics implementations
6
star
9

quickmetrics

a docker compose setup with prometheus, alertmanager, grafana, an aggregation gateway
TypeScript
6
star
10

am

Autometrics Companion CLI app
Rust
4
star
11

docs

Documentation site for autometrics
TypeScript
3
star
12

am_list

A small tree-sitter powered CLI that lists function names which are decorated according to Autometrics rules
Rust
3
star
13

gettingstarted-am-go

Go
2
star
14

homebrew-tap

Homebrew formula for installing am
Ruby
2
star
15

render-prometheus

Cooking ground for Prometheus render blueprint
Python
2
star
16

gettingstarted-am-rs

Getting started with Autometrics and the Rust Axum web framework
Rust
1
star
17

gettingstarted-am-ts

Sample app to get started with Autometrics in a backend TypeScript project
TypeScript
1
star
18

gettingstarted-am-py

Getting started with Autometrics and the Python FastAPI web framework
Python
1
star
19

.github

1
star
20

instrument-pipeline

Export job execution metrics to a Prometheus aggregation gateway
TypeScript
1
star