@biorate/prometheus 3.1.0 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -12
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,28 +1,219 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @biorate/prometheus
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Prometheus metrics for `@biorate/inversion` — create and use counters, gauges, histograms, and summaries via TypeScript decorators with automatic registry management.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Decorator‑based** — `@counter`, `@gauge`, `@histogram`, `@summary` on class properties.
|
|
8
|
+
- **Singleton metrics** — metrics are created once per name (across the whole process).
|
|
9
|
+
- **Override support** — set `override: true` to re‑create a metric with new settings.
|
|
10
|
+
- **Default metrics** — auto‑collects Node.js default metrics (event loop, GC, memory, etc.).
|
|
11
|
+
- **Shared registry** — a single `Registry` instance per process via `Prometheus.registry`.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @biorate/prometheus
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires `@biorate/inversion`, `@biorate/config`, and `prom-client`.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { counter, Counter } from '@biorate/prometheus';
|
|
25
|
+
|
|
26
|
+
class MyService {
|
|
27
|
+
@counter({ name: 'my_counter', help: 'Counts something', labelNames: ['label1'] })
|
|
28
|
+
protected counter: Counter;
|
|
29
|
+
|
|
30
|
+
public inc() {
|
|
31
|
+
this.counter.labels({ label1: 'test' }).inc();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const s = new MyService();
|
|
36
|
+
s.inc();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API Reference
|
|
40
|
+
|
|
41
|
+
### Decorators
|
|
42
|
+
|
|
43
|
+
| Decorator | Metric type | Config interface |
|
|
44
|
+
|----------------|---------------------|---------------------------------|
|
|
45
|
+
| `@counter` | `Counter<string>` | `CounterConfiguration & IdefaultSettings` |
|
|
46
|
+
| `@gauge` | `Gauge<string>` | `GaugeConfiguration & IdefaultSettings` |
|
|
47
|
+
| `@histogram` | `Histogram<string>` | `HistogramConfiguration & IdefaultSettings`|
|
|
48
|
+
| `@summary` | `Summary<string>` | `SummaryConfiguration & IdefaultSettings` |
|
|
49
|
+
|
|
50
|
+
All decorators accept an additional `IdefaultSettings` field:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
interface IdefaultSettings {
|
|
54
|
+
name: string; // metric name (required)
|
|
55
|
+
help: string; // help text (required)
|
|
56
|
+
override?: boolean; // re‑create if already exists (default false)
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### `Prometheus` class
|
|
61
|
+
|
|
62
|
+
| Member | Description |
|
|
63
|
+
|----------------------|-----------------------------------------------------|
|
|
64
|
+
| `Prometheus.registry`| Shared `Registry` instance (static). |
|
|
65
|
+
| `counter()` | Static factory — same as `@counter` decorator. |
|
|
66
|
+
| `gauge()` | Static factory — same as `@gauge` decorator. |
|
|
67
|
+
| `histogram()` | Static factory — same as `@histogram` decorator. |
|
|
68
|
+
| `summary()` | Static factory — same as `@summary` decorator. |
|
|
69
|
+
| `this.registry` | Instance getter returning `Prometheus.registry`. |
|
|
70
|
+
| `initialize()` | `@init()` — calls `collectDefaultMetrics` if config `prometheus.collectDefaultMetrics` is true. |
|
|
71
|
+
|
|
72
|
+
## Usage patterns
|
|
73
|
+
|
|
74
|
+
### `@counter`
|
|
6
75
|
|
|
7
76
|
```ts
|
|
8
77
|
import { counter, Counter } from '@biorate/prometheus';
|
|
9
78
|
|
|
10
|
-
class
|
|
79
|
+
class Service {
|
|
11
80
|
@counter({
|
|
12
|
-
name: '
|
|
13
|
-
help: '
|
|
14
|
-
labelNames: ['
|
|
81
|
+
name: 'api_requests_total',
|
|
82
|
+
help: 'Total API requests',
|
|
83
|
+
labelNames: ['method', 'path', 'status'],
|
|
15
84
|
})
|
|
16
85
|
protected counter: Counter;
|
|
17
86
|
|
|
18
|
-
public
|
|
19
|
-
this.counter.labels({
|
|
87
|
+
public track(method: string, path: string, status: number) {
|
|
88
|
+
this.counter.labels({ method, path, status: String(status) }).inc();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `@gauge`
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { gauge, Gauge } from '@biorate/prometheus';
|
|
97
|
+
|
|
98
|
+
class Service {
|
|
99
|
+
@gauge({
|
|
100
|
+
name: 'active_connections',
|
|
101
|
+
help: 'Currently active connections',
|
|
102
|
+
labelNames: ['pool'],
|
|
103
|
+
})
|
|
104
|
+
protected gauge: Gauge;
|
|
105
|
+
|
|
106
|
+
public add(n = 1) { this.gauge.labels({ pool: 'main' }).inc(n); }
|
|
107
|
+
public remove(n = 1) { this.gauge.labels({ pool: 'main' }).dec(n); }
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### `@histogram`
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { histogram, Histogram } from '@biorate/prometheus';
|
|
115
|
+
|
|
116
|
+
class Service {
|
|
117
|
+
@histogram({
|
|
118
|
+
name: 'request_duration_seconds',
|
|
119
|
+
help: 'Request duration in seconds',
|
|
120
|
+
labelNames: ['method', 'path'],
|
|
121
|
+
buckets: [0.01, 0.05, 0.1, 0.5, 1, 5],
|
|
122
|
+
})
|
|
123
|
+
protected histogram: Histogram;
|
|
124
|
+
|
|
125
|
+
public observe(method: string, path: string, duration: number) {
|
|
126
|
+
this.histogram.labels({ method, path }).observe(duration);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### `@summary`
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { summary, Summary } from '@biorate/prometheus';
|
|
135
|
+
|
|
136
|
+
class Service {
|
|
137
|
+
@summary({
|
|
138
|
+
name: 'response_size_bytes',
|
|
139
|
+
help: 'Response size in bytes',
|
|
140
|
+
percentiles: [0.5, 0.9, 0.99],
|
|
141
|
+
})
|
|
142
|
+
protected summary: Summary;
|
|
143
|
+
|
|
144
|
+
public record(bytes: number) {
|
|
145
|
+
this.summary.observe(bytes);
|
|
20
146
|
}
|
|
21
147
|
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### With DI container
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { Core, inject, container, Types } from '@biorate/inversion';
|
|
154
|
+
import { IConfig, Config } from '@biorate/config';
|
|
155
|
+
import { Prometheus, counter, Counter } from '@biorate/prometheus';
|
|
156
|
+
|
|
157
|
+
@injectable()
|
|
158
|
+
class MetricsService {
|
|
159
|
+
@counter({ name: 'events_total', help: 'Total events' })
|
|
160
|
+
public counter: Counter;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
class Root extends Core() {
|
|
164
|
+
@inject(Prometheus) public prometheus: Prometheus;
|
|
165
|
+
@inject(MetricsService) public metrics: MetricsService;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
container.bind(Types.Config).to(Config).inSingletonScope();
|
|
169
|
+
container.bind(Prometheus).toSelf().inSingletonScope();
|
|
170
|
+
container.bind(MetricsService).toSelf().inSingletonScope();
|
|
171
|
+
container.bind(Root).toSelf().inSingletonScope();
|
|
22
172
|
|
|
23
|
-
|
|
173
|
+
container.get<IConfig>(Types.Config).merge({
|
|
174
|
+
'prometheus.collectDefaultMetrics': true,
|
|
175
|
+
});
|
|
24
176
|
|
|
25
|
-
|
|
177
|
+
(async () => {
|
|
178
|
+
const root = container.get<Root>(Root);
|
|
179
|
+
await root.$run();
|
|
180
|
+
root.metrics.counter.inc();
|
|
181
|
+
})();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Default metrics configuration
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
// config
|
|
188
|
+
{
|
|
189
|
+
'prometheus.collectDefaultMetrics': true, // enable (default true)
|
|
190
|
+
'prometheus.defaultMetrics': {
|
|
191
|
+
// prom-client DefaultMetricsCollectorConfiguration
|
|
192
|
+
timeout: 5000,
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Architecture
|
|
198
|
+
|
|
199
|
+
```
|
|
200
|
+
┌────────────────────────────────────────────────────────┐
|
|
201
|
+
│ Prometheus (static) │
|
|
202
|
+
│ │
|
|
203
|
+
│ counters ─── Map<name, Counter> │
|
|
204
|
+
│ gauges ─── Map<name, Gauge> │
|
|
205
|
+
│ histograms ── Map<name, Histogram> │
|
|
206
|
+
│ summaries ─── Map<name, Summary> │
|
|
207
|
+
│ │
|
|
208
|
+
│ findOrCreate(settings, map, Class) ─── decorator │
|
|
209
|
+
│ ├── checks map for existing metric by name │
|
|
210
|
+
│ ├── removes old metric from registry if override │
|
|
211
|
+
│ ├── creates new metric on registry │
|
|
212
|
+
│ └── returns property descriptor │
|
|
213
|
+
│ │
|
|
214
|
+
│ registry ── Registry (static singleton) │
|
|
215
|
+
│ └── collectDefaultMetrics() on @init() │
|
|
216
|
+
└────────────────────────────────────────────────────────┘
|
|
26
217
|
```
|
|
27
218
|
|
|
28
219
|
### Learn
|
|
@@ -33,7 +224,7 @@ test.metric();
|
|
|
33
224
|
|
|
34
225
|
See the [CHANGELOG](https://github.com/biorate/core/blob/master/packages/%40biorate/prometheus/CHANGELOG.md)
|
|
35
226
|
|
|
36
|
-
|
|
227
|
+
## License
|
|
37
228
|
|
|
38
229
|
[MIT](https://github.com/biorate/core/blob/master/packages/%40biorate/prometheus/LICENSE)
|
|
39
230
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/prometheus",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Prometheus DI module",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"keywords": [],
|
|
46
46
|
"author": "llevkin",
|
|
47
47
|
"license": "MIT",
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "e17a03900999e261a0eb04c6afb09b9535cad952",
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@biorate/config": "3.2.
|
|
51
|
-
"@biorate/inversion": "3.1.
|
|
50
|
+
"@biorate/config": "3.2.2",
|
|
51
|
+
"@biorate/inversion": "3.1.2",
|
|
52
52
|
"prom-client": "^14.0.1"
|
|
53
53
|
}
|
|
54
54
|
}
|