Using wrappers and decorators
Node.js
Use the Autometrics wrappers and decorators to instrument the functions you want to track. Usually it makes sense to wrap functions that are doing some business logic, making network calls, or are otherwise critical to the service (e.g.: request handlers or database calls).
Wrapping plain old functions
Wrappers are simple functions that wrap the original function declaration and instrument it with metrics. They also enable the language service plugin to add additional information in the type docs and the VSCode extension to link to the graphs.
Use function wrappers to wrap request handlers, database calls, or other pieces of important business logic that you want to measure.
Wrapped functions must be named. Anonymous (arrow) functions will not be instrumented.
Example:
import { Autometrics } from '@autometrics/autometrics';
const createUserWithMetrics = autometrics(async function createUser(payload: User) {
// ...
});
createUserWithMetrics();
Decorating class methods
When using a decorator for a class method, it is wrapped in additional code that instruments the method with metrics.
Here's a snippet from a NestJS example project that uses Autometrics:
import { Controller, Get } from "@nestjs/common";
import { AppService } from "./app.service";
import { Autometrics } from '@autometrics/autometrics';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
@Autometrics()
getHello(): string {
return this.appService.getHello();
}
}
Alternatively, you can apply the same decorator to the entire class and instrument all of its methods:
// ...
@Autometrics()
export class AppController {
// ...
@Get()
getHello(): string {
return this.appService.getHello();
}
}
Browser (Experimental)
Support for client-side use of the Autometrics library is limited and experimental. Things are likely to break in unexpected places.
Set up the push gateway
In order for Prometheus to succesfully get your client-side app metrics, you will need to push them to an aggregating push gateway like this one (opens in a new tab).
Use the init
function to configure the gateway URL that Autometrics should
push the data to. You can also set the push interval with the pushInterval
property (default is every 5000 miliseconds);
init({ pushGateway: "<link_to_gateway>" });
Use Autometrics wrapper with options
The same wrapper functions can be used in browser environments. Note that bundlers minify code and obfuscate the names of functions and modules in production.
To get around this issue, wrappers accept an options object as their first argument, which explicitly assigns a function and module name.
const myFunction = autometrics(
{
functionName: "myFunction",
moduleName: "Module",
},
async () => {
// ... myFunction body
}
);