Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,65 @@ const server = new InversifyExpressServer(container, null);
})
```

## What is SiberMetrics and how it works.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not quite a "histogram of an HTTP request".
https://prometheus.io/docs/introduction/overview/


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<Environment>(
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
Expand Down
4 changes: 2 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class IncompleteEnvError extends Error {
* keys.
* @param schema schema to use for validation
*/
export function autoloadEnv<T extends AppConfig>(schema: ObjectSchema): T {
export function autoloadEnv<T>(schema: ObjectSchema): T {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change these?

dotenv.config();
const processedEnv = mapKeys(process.env, (_, key) => {
return key.toLowerCase();
Expand All @@ -39,7 +39,7 @@ export function autoloadEnv<T extends AppConfig>(schema: ObjectSchema): T {
* @param data env object
* @param schema schema to use for validation
*/
function validateConfig<T extends AppConfig>(
function validateConfig<T>(
data: any,
schema: ObjectSchema
): T {
Expand Down