autometric
The aim of this module is to collect metrics automatically with the
prom-client
on different asynchronous execution pattern. It is written in
TypeScript and includes its type definitions, of course. It uses prom-client
as peer dependency, so don't forget to
add it to your module!
This module is in development. Currently supported are Promises, connect-like middlewares and a pass-through stream. The goal is to enhance the module with support for child-processes and observables patterns in future releases.
How to use
First you have to install this library from npm:
npm install --save autometric# OR yarn add autometric
Promises
TypeScript:
; ;// function to change the labels after getting the result;// options are optional; new AutometricPromise, options;
JavaScript:
; const AutometricPromise = ;// function to change the labels after getting the resultconst rewriteLabels = { if result === true return ...currentLabels status: "success" ; if result instanceof Error && resultmessage === "Value is smaller the 1%" return ...currentLabels status: "error" ; return currentLabels;};// options are optionalconst options = labels: additional: "add-additional-labels" labels: "or-overwrite-them" rewriteLabels ; <boolean> { ;} options;
The promise of autometric will collect the following metrics:
name + "_calls_total"
as Counter: counts Promises that start its execution.name + "_durations_ms"
as Summary: summary over the duration from execution start until it resolves or rejects. It will add a status (success / fail) to the metric as long as not disabled via thenoStatusLabel
option.
With the pass-through stream
TypeScript:
;; ;// function to change the labels after getting a chunk;// options are optional; ; ;; reader.pipemetricsPipe.pipewriter;
JavaScript:
;; const AutometricPipe = ;// function to change the labels after getting a chunkconst rewriteLabels = { return ...currentLabels encoding;};// options are optionalconst options = labels: additional: "add-additional-labels" labels: "or-overwrite-them" rewriteLabels ; const metricsPipe = options; const reader = ;const writer = ; reader;
The pass-through pipe of autometric will collect the following metrics:
name + "_chunk_sizes_bytes"
as Summary: summary over the size in bytes of a chunk (only for strings or Buffers)name + "_durations_ms"
as Summary: summary over the duration from the first chunk until the end event.name + "_elapsed_time_ms"
as Summary: Time between emits of chunksname + "_ends_total"
as Counter: counts streams that endsname + "_non_emits_total"
as Counter: counts streams that ends without emitting any dataname + "_throughput_bytes"
as Summary: a summary over the processed bytes in one stream
Connect-like middlware
This middleware is build for express, but it should also work with other connect-like middlewares
TypeScript:
;;; ;// function to change the labels after getting a chunk;// options are optional; app.get"/api/user", middlewareObject.createMiddlewareoptions,;
JavaScript:
const createAutometricMiddleware = ;const express = ;const app = ; const middlewareObject = ;// function to change the labels after getting a chunkconst rewriteLabels = { return ...currentLabels encoding;};// options are optionalconst options = labels: additional: "add-additional-labels" labels: "or-overwrite-them" service: "user-auth" rewriteLabels; app;
Get the metrics
This module uses the prom-client
as peer dependency. To get all metrics - including foreign ones - you can use the
prom-client
register like that:
;; createServer.listen9000;
You can also take the static property register
of the resulting classes to output only the specific metrics, or merge
them with other registers:
AutometricPromiseregister;// ORAutometricStreamPassThroughregister;// ORmiddlewareObjectregister;
Meta metrics from autometric
Autometric also collects metrics from the creation of Autometric metrics collectors. You can access them like this:
;autometricRegister.metrics;
Scripts Tasks
Scripts registered in package.json:
transpile
: Transpile TypeScript Code to JavaScriptdist
: Make a ready for distribution versionlint
: Use the linter for TypeScript Codetest
: Run software- and coverage-tests in node.doc
: Build the API documentation.
Every command is also available as dockerized version, by prefixing docker:
(ex.: docker:lint
)
License
This library is released under the ISC License.