diff --git a/src/index.ts b/src/index.ts index 6d3631b..779551c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -71,4 +71,34 @@ export class Logger { method: "POST", }); } + + /** + * Sends a counter metric to the alerting service. + * The alerting service is configured to handle aggregation, so you can send + * summative counts or individual increments. (e.g. Send a count of all users, or send a count of 1 every time a user joins). + * @param name -- The name of the metric to track. + * @param value -- The value of the metric to insert. + * @param metadata -- Any metadata to attach to the metric. + */ + public async metric( + name: string, + value: number, + metadata: Record, + ): Promise { + await fetch(`${this.url}/metric`, { + body: JSON.stringify({ + application: this.application, + metadata: metadata, + name: name, + value: value, + }), + headers: { + // eslint-disable-next-line @typescript-eslint/naming-convention -- Standard header name. + "Authorization": this.token, + // eslint-disable-next-line @typescript-eslint/naming-convention -- Standard header name. + "Content-Type": "application/json", + }, + method: "POST", + }); + } }