diff --git a/README.md b/README.md index aa30bc0..ecd2b7d 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,65 @@ const server = new InversifyExpressServer(container, null); }) ``` +## What is SiberMetrics and how it works. + +SiberMetrics are wrapper around Prometheus client for Node.js to record and export metrics of an http request. + +- Create a global instance of SiberMetrics + +```ts +export const Metrics = new SiberMetrics(env); +``` + +- Record the request after it has been handled + +```ts +Metrics.record(req, res); +``` + +- Create an HTTP Handler to expose your server metrics to prometheus server. + +```ts +app.get("/metrics", Metrics.send.bind(Metrics)); +``` + +## Parsing your environment variables + +Siber `autoloadEnv` function helps to parse and validate server environment variables. + +```ts +import joi from "@hapi/joi"; +import { autoloadEnv, siberConfig, mongoConfig, redisConfig, DApp } from "@random-guys/siber"; +``` + +- Create a `type` for your environment + +```ts +interface Environment extends DApp { + any_key: string +} + +`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_key: joi.string().required(), + }) +); +``` + +PS: `mongoConfig` and `redisConfig` are environment schema object for mongodb and redis. While `DAPP` is a `type` of all the basic environment variables + ## TODO - [ ] Tests 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 {