From 3bd5e44c949da4a05a34aaea82dd83733cc36a23 Mon Sep 17 00:00:00 2001 From: tobslob Date: Wed, 30 Jun 2021 03:54:24 +0100 Subject: [PATCH 1/6] chore: update usage documentation --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README.md b/README.md index aa30bc0..ea05ef6 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,71 @@ const server = new InversifyExpressServer(container, null); }) ``` +## To record metrics in your application + +in your server common -> services -> `index.ts` directory, create a new instance of SiberMetrics + +```ts +export const Metrics = new SiberMetrics(env); +``` + +Send prometeus metrics, in `app.ts` + +```ts +app.get("/metrics", Metrics.send.bind(Metrics)); +``` + +in your server base controller inherit the the methods of SiberMetrics + +```ts +export class ProController extends Controller { + constructor() { + super(Log, Metrics); + } +} +``` + +Now you can record your requests metrics directly in your endpoints +```ts +this.metrics.record(req, res); +``` + +Finally, you can also record errors metric + +```ts +handleError(req: Request, res: Response, err: Error, message?: string) { + // your error handling logic here + + Metrics.record(req, res); +} +``` + +## To use siber for your env import + +If you want the full package i.e SessionedApp MongoConfig AppConfig and RedisConfig, import DApp otherwise +import the class or package, you want. + +```ts +import joi from "@hapi/joi"; +import { autoloadEnv, DBApp, mongoConfig, redisConfig, siberConfig } from "@random-guys/siber"; + +export interface ServerEnv extends DBApp { + // environmental values goes here i.e + amqp_url: string; +} + +export const env = autoloadEnv( + siberConfig({ + // ServerEnv validation goes here + ...redisConfig, + ...mongoConfig, + amqp_url: joi.string().uri().required(), + }) +); + +you can now have `env.amqp_url` +``` + ## TODO - [ ] Tests From ea39f844ddb90a84be4187e377b3a8e3d4325be8 Mon Sep 17 00:00:00 2001 From: tobslob Date: Fri, 2 Jul 2021 05:08:29 +0100 Subject: [PATCH 2/6] fix: AppConfig has been define by siberBasicConfig --- src/env.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/env.ts b/src/env.ts index 20381f0..7f5c82b 100644 --- a/src/env.ts +++ b/src/env.ts @@ -23,7 +23,7 @@ export class IncompleteEnvError extends Error { * keys. * @param schema schema to use for validation */ -export function autoloadEnv(schema: ObjectSchema): T { +export function autoloadEnv(schema: ObjectSchema): T { dotenv.config(); const processedEnv = mapKeys(process.env, (_, key) => { return key.toLowerCase(); @@ -39,7 +39,7 @@ export function autoloadEnv(schema: ObjectSchema): T { * @param data env object * @param schema schema to use for validation */ -function validateConfig( +function validateConfig( data: any, schema: ObjectSchema ): T { From c05567370952bb5525923f880e706a503c42b5d6 Mon Sep 17 00:00:00 2001 From: tobslob Date: Fri, 2 Jul 2021 05:15:10 +0100 Subject: [PATCH 3/6] fix: better readme description --- README.md | 61 ++++++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index ea05ef6..f84c578 100644 --- a/README.md +++ b/README.md @@ -49,71 +49,62 @@ const server = new InversifyExpressServer(container, null); }) ``` -## To record metrics in your application +## What is SiberMetrics and how it works. -in your server common -> services -> `index.ts` directory, create a new instance of SiberMetrics +- Record metrics from HTTP Request +- Metrics exporter + +Create an instance of the SiberMetrics in your server: ```ts export const Metrics = new SiberMetrics(env); ``` -Send prometeus metrics, in `app.ts` +SiberMetrics allow you to record `success` or `failure` requests. You can either record metrics in your `Controller` or `Error` handler using its `record` method: ```ts -app.get("/metrics", Metrics.send.bind(Metrics)); +Metrics.record(req, res); ``` -in your server base controller inherit the the methods of SiberMetrics +Also siberMetrics allow the export of all recorded metrics to prometeus server through the `send` method: -```ts -export class ProController extends Controller { - constructor() { - super(Log, Metrics); - } -} -``` +Create an HTTP Handler to expose your server metrics to prometeus server. -Now you can record your requests metrics directly in your endpoints ```ts -this.metrics.record(req, res); +app.get("/metrics", Metrics.send.bind(Metrics)); ``` -Finally, you can also record errors metric - -```ts -handleError(req: Request, res: Response, err: Error, message?: string) { - // your error handling logic here +## Parsing your environment variables - Metrics.record(req, res); -} -``` +Siber `autoloadEnv` function helps to parse and validate server environment variables. -## To use siber for your env import +# Usage +To use `autoloadEnv` you need to define; -If you want the full package i.e SessionedApp MongoConfig AppConfig and RedisConfig, import DApp otherwise -import the class or package, you want. +- The `type` definition of your server environment configurations +- joi validation schema for the `type` definition ```ts import joi from "@hapi/joi"; -import { autoloadEnv, DBApp, mongoConfig, redisConfig, siberConfig } from "@random-guys/siber"; +import { autoloadEnv, siberConfig, mongoConfig, redisConfig } from "@random-guys/siber"; -export interface ServerEnv extends DBApp { - // environmental values goes here i.e - amqp_url: string; +interface EnvironmentConfig { + any_variable: string } -export const env = autoloadEnv( +export const env = autoloadEnv( siberConfig({ - // ServerEnv validation goes here - ...redisConfig, ...mongoConfig, - amqp_url: joi.string().uri().required(), + ...redisConfig, + any_variable: joi.string().required(), }) ); - -you can now have `env.amqp_url` ``` +`siberConfig` creates a joi schema to determine and validate the configuration. + +PS: `mongoConfig` and `redisConfig` are environment schema object for mongodb and redis. + ## TODO - [ ] Tests From a2500cb0f2c07ec35c1ea525f76b84eb7776c325 Mon Sep 17 00:00:00 2001 From: tobslob Date: Wed, 7 Jul 2021 09:34:05 +0100 Subject: [PATCH 4/6] chore: update readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f84c578..07937ee 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ const server = new InversifyExpressServer(container, null); - Record metrics from HTTP Request - Metrics exporter -Create an instance of the SiberMetrics in your server: +Use SiberMetrics in your typescript express server by Creating an instance of `SiberMetrics`. ```ts export const Metrics = new SiberMetrics(env); @@ -66,7 +66,7 @@ SiberMetrics allow you to record `success` or `failure` requests. You can either Metrics.record(req, res); ``` -Also siberMetrics allow the export of all recorded metrics to prometeus server through the `send` method: +Also SiberMetrics allow the export of all recorded metrics to prometeus server through the `send` method: Create an HTTP Handler to expose your server metrics to prometeus server. @@ -81,14 +81,14 @@ Siber `autoloadEnv` function helps to parse and validate server environment vari # Usage To use `autoloadEnv` you need to define; -- The `type` definition of your server environment configurations +- The `type` definition of your server environment variables - joi validation schema for the `type` definition ```ts import joi from "@hapi/joi"; import { autoloadEnv, siberConfig, mongoConfig, redisConfig } from "@random-guys/siber"; -interface EnvironmentConfig { +interface EnvironmentConfig extends DApp{ any_variable: string } @@ -103,7 +103,7 @@ export const env = autoloadEnv( `siberConfig` creates a joi schema to determine and validate the configuration. -PS: `mongoConfig` and `redisConfig` are environment schema object for mongodb and redis. +PS: `mongoConfig` and `redisConfig` are environment schema object for mongodb and redis. While `DAPP` is a `type` of all the basic environment variables ## TODO From c49a69edb1573c85a879a17fabb9fec8b7d49c32 Mon Sep 17 00:00:00 2001 From: tobslob Date: Fri, 16 Jul 2021 04:06:10 +0100 Subject: [PATCH 5/6] chore: update readme --- README.md | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 07937ee..ddccc8c 100644 --- a/README.md +++ b/README.md @@ -51,24 +51,21 @@ const server = new InversifyExpressServer(container, null); ## What is SiberMetrics and how it works. -- Record metrics from HTTP Request -- Metrics exporter +SiberMetrics are routers around prometheus client for node.js to record and export a histogram of an http request. -Use SiberMetrics in your typescript express server by Creating an instance of `SiberMetrics`. +- Create a global instance of SiberMetrics ```ts export const Metrics = new SiberMetrics(env); ``` -SiberMetrics allow you to record `success` or `failure` requests. You can either record metrics in your `Controller` or `Error` handler using its `record` method: +- Record the request after it has been handled ```ts Metrics.record(req, res); ``` -Also SiberMetrics allow the export of all recorded metrics to prometeus server through the `send` method: - -Create an HTTP Handler to expose your server metrics to prometeus server. +- Create an HTTP Handler to expose your server metrics to prometheus server. ```ts app.get("/metrics", Metrics.send.bind(Metrics)); @@ -78,31 +75,37 @@ app.get("/metrics", Metrics.send.bind(Metrics)); Siber `autoloadEnv` function helps to parse and validate server environment variables. -# Usage -To use `autoloadEnv` you need to define; - -- The `type` definition of your server environment variables -- joi validation schema for the `type` definition - ```ts import joi from "@hapi/joi"; -import { autoloadEnv, siberConfig, mongoConfig, redisConfig } from "@random-guys/siber"; +import { autoloadEnv, siberConfig, mongoConfig, redisConfig, DApp } from "@random-guys/siber"; +``` -interface EnvironmentConfig extends DApp{ - any_variable: string +- Create a `type` for your environment + +```ts +interface Environment extends DApp { + any_key: string } -export const env = autoloadEnv( +`siberConfig` return a joi schema of the environment variables. + + siberConfig({ + ...mongoConfig, + ...redisConfig, + any_key: joi.string().required(), + }) + +`autoloadEnv` loads the validated environment + +export const env = autoloadEnv( siberConfig({ ...mongoConfig, ...redisConfig, - any_variable: joi.string().required(), + any_key: joi.string().required(), }) ); ``` -`siberConfig` creates a joi schema to determine and validate the configuration. - PS: `mongoConfig` and `redisConfig` are environment schema object for mongodb and redis. While `DAPP` is a `type` of all the basic environment variables ## TODO From 43b26bf1737279ee1a1998cb143093917fb06ee2 Mon Sep 17 00:00:00 2001 From: tobslob Date: Mon, 19 Jul 2021 11:49:53 +0100 Subject: [PATCH 6/6] chore: update document --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ddccc8c..ecd2b7d 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ const server = new InversifyExpressServer(container, null); ## What is SiberMetrics and how it works. -SiberMetrics are routers around prometheus client for node.js to record and export a histogram of an http request. +SiberMetrics are wrapper around Prometheus client for Node.js to record and export metrics of an http request. - Create a global instance of SiberMetrics