# @konso/metrics

> Client for metrics

Latest version **0.0.4** (published 2022-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @konso/metrics
pnpm add @konso/metrics
yarn add @konso/metrics
bun add @konso/metrics
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2022-03-31 |
| First published | 2022-02-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Belchenko |
| Maintainers | posokhin_anton_indevlabs, danyadanyadanya, alexlvovich |

## Links

- npm: https://www.npmjs.com/package/@konso/metrics
- Repository: https://github.com/konsoio/konso-clients-js.git#main
- Homepage: https://github.com/konsoio/konso-clients-js/tree/main/metrics
- Issues: https://github.com/konsoio/konso-clients-js/issues
- npm.io page: https://npm.io/package/@konso/metrics

## Dependencies (3)

- [nanoid](https://npm.io/package/nanoid.md) ^3.1.25
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.0.0
- [bignumber.js](https://npm.io/package/bignumber.js.md) ^9.0.1

## Recent versions

- 0.0.4 (latest) — 2022-03-31
- 0.0.3 — 2022-03-20
- 0.0.1 — 2022-02-01

## README

# Metrics

## Installation

---

`npm i @konso/metrics`

## Example

---

```javascript
import { createMetricsClient, LogEvents } from "@konso/metrics";

const client = createMetricsClient({
  apiURL: 'https://devapis.konso.io',
  bucketId: '1fc41560',
  apiKey: 'REa9xeOvk6fRxAcNEyebk+5nC5LnbeCr+bJTBpoLm5E=',
  appName: 'test'
})

client.on(LogEvents.onSuccessful, (text) => { console.log(text) })
  .on(LogEvents.onError, (text) => console.error(text));


const app = express();

function metricExpressMiddleware(client) {
  if (!client) {
    throw new Error('you should provide client for metrics')
  }

  return function (req, res, next) {
    let endMeasure = client.startMeasure(`${req.method} ${req.path}`);

    res.on("finish", () => {
      endMeasure({ responseCode: res.statusCode, tags: ['client'] });
    });

    next();
  }
}

app.use(metricExpressMiddleware(client));
```

## createMetricsClient

createMetricsClient - the function takes a parameters object and returns an instance of the Metrics class

```typescript
const client = createMetricsClient({
  apiKey: 'REa9xeOvk6fRxAcNEyebk+5nC5LnbeCr+bJTBpoLm5E=',
  apiURL: 'https://devapis.konso.io',
  bucketId: '1fc41560',
  appName: 'test'
})
```

| Parameter | Type     | Required |
|:----------|----------|:---------|
| apiKey    | string   | true     |
| apiURL    | string   | true     |
| bucketId  | string   | true     |
| appName   | string   | false    |
| tags      | string[] | false    |

## Methods

---

## startMeasure

```typescript
startMeasure(name: string) : (extraOptions: MetricsExtraOptions) => void;
```

### Parameters

| Parameter | Type   | Required | Value |
|-----------|--------|----------|-------|
| name      | string | true     |       |


### Parameters for the returned function

| Parameter    | Type   | Required | Value                                                                              |
|--------------|--------|----------|------------------------------------------------------------------------------------|
| extraOptions | Object | true     | {<br/>tags?: string[],<br/>correlationId?: string,<br/>responseCode?: number<br/>} |


## on

```typescript
on(eventName: LogEvents, callback: (text: string) => void);
```

### Parameters

| Parameter | Type     | Required | Value                                                |
|-----------|----------|----------|------------------------------------------------------|
| eventName | string   | true     | 'onSuccessful', 'onError' or keys of LogEvent object |
| callback  | function | true     | (text: string) => void                               |

---
_Source: https://npm.io/package/@konso/metrics · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
