@onebun/metrics 0.1.0
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 +245 -0
- package/package.json +41 -0
- package/src/decorators.test.ts +531 -0
- package/src/decorators.ts +249 -0
- package/src/index.test.ts +357 -0
- package/src/index.ts +90 -0
- package/src/metrics.service.test.ts +374 -0
- package/src/metrics.service.ts +371 -0
- package/src/middleware.test.ts +295 -0
- package/src/middleware.ts +186 -0
- package/src/types.ts +133 -0
package/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# @onebun/metrics
|
|
2
|
+
|
|
3
|
+
Prometheus-compatible metrics module for OneBun framework providing automatic HTTP request metrics, system metrics, and custom metrics API.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 📊 **Automatic HTTP Metrics** - Automatically collects HTTP request duration, count, and status codes
|
|
8
|
+
- 🖥️ **System Metrics** - Memory usage, CPU usage, and process uptime
|
|
9
|
+
- 🗑️ **GC Metrics** - Garbage collection duration and frequency
|
|
10
|
+
- 🎯 **Custom Metrics** - Easy to use API for creating counters, gauges, histograms, and summaries
|
|
11
|
+
- 🏷️ **Prometheus Compatible** - Standard Prometheus exposition format
|
|
12
|
+
- 🎨 **Decorators** - Method decorators for automatic metrics collection
|
|
13
|
+
- ⚡ **Effect.js Integration** - Full integration with Effect.js ecosystem
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun add @onebun/metrics
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Basic Usage
|
|
22
|
+
|
|
23
|
+
### Application Setup
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { OneBunApplication } from '@onebun/core';
|
|
27
|
+
import { AppModule } from './app.module';
|
|
28
|
+
|
|
29
|
+
const app = new OneBunApplication(AppModule, {
|
|
30
|
+
metrics: {
|
|
31
|
+
enabled: true,
|
|
32
|
+
path: '/metrics',
|
|
33
|
+
collectHttpMetrics: true,
|
|
34
|
+
collectSystemMetrics: true,
|
|
35
|
+
collectGcMetrics: true,
|
|
36
|
+
prefix: 'myapp_'
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await app.start();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Custom Metrics in Services
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { Service } from '@onebun/core';
|
|
47
|
+
import { MetricsService, MetricType } from '@onebun/metrics';
|
|
48
|
+
|
|
49
|
+
@Service()
|
|
50
|
+
export class UserService {
|
|
51
|
+
private userLoginCounter: Counter<string>;
|
|
52
|
+
private activeUsersGauge: Gauge<string>;
|
|
53
|
+
|
|
54
|
+
constructor(private metricsService: MetricsService) {
|
|
55
|
+
// Create custom metrics
|
|
56
|
+
this.userLoginCounter = this.metricsService.createCounter({
|
|
57
|
+
name: 'user_logins_total',
|
|
58
|
+
help: 'Total number of user logins',
|
|
59
|
+
labelNames: ['method', 'status']
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
this.activeUsersGauge = this.metricsService.createGauge({
|
|
63
|
+
name: 'active_users',
|
|
64
|
+
help: 'Number of currently active users'
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async login(method: string): Promise<void> {
|
|
69
|
+
try {
|
|
70
|
+
// Login logic here
|
|
71
|
+
this.userLoginCounter.inc({ method, status: 'success' });
|
|
72
|
+
this.activeUsersGauge.inc();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
this.userLoginCounter.inc({ method, status: 'failure' });
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Method Decorators
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Controller, Get } from '@onebun/core';
|
|
85
|
+
import { MeasureTime, CountCalls } from '@onebun/metrics';
|
|
86
|
+
|
|
87
|
+
@Controller('/api')
|
|
88
|
+
export class ApiController {
|
|
89
|
+
|
|
90
|
+
@Get('/heavy-operation')
|
|
91
|
+
@MeasureTime('heavy_operation_duration')
|
|
92
|
+
@CountCalls('heavy_operation_calls')
|
|
93
|
+
async heavyOperation(): Promise<any> {
|
|
94
|
+
// This method's execution time and call count will be automatically tracked
|
|
95
|
+
await someHeavyWork();
|
|
96
|
+
return { success: true };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Configuration Options
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
interface MetricsOptions {
|
|
105
|
+
// Enable/disable metrics collection (default: true)
|
|
106
|
+
enabled?: boolean;
|
|
107
|
+
|
|
108
|
+
// HTTP path for metrics endpoint (default: '/metrics')
|
|
109
|
+
path?: string;
|
|
110
|
+
|
|
111
|
+
// Default labels for all metrics
|
|
112
|
+
defaultLabels?: Record<string, string>;
|
|
113
|
+
|
|
114
|
+
// Enable HTTP request metrics (default: true)
|
|
115
|
+
collectHttpMetrics?: boolean;
|
|
116
|
+
|
|
117
|
+
// Enable system metrics (default: true)
|
|
118
|
+
collectSystemMetrics?: boolean;
|
|
119
|
+
|
|
120
|
+
// Enable GC metrics (default: true)
|
|
121
|
+
collectGcMetrics?: boolean;
|
|
122
|
+
|
|
123
|
+
// System metrics collection interval in ms (default: 5000)
|
|
124
|
+
systemMetricsInterval?: number;
|
|
125
|
+
|
|
126
|
+
// Metric name prefix (default: 'onebun_')
|
|
127
|
+
prefix?: string;
|
|
128
|
+
|
|
129
|
+
// HTTP duration histogram buckets
|
|
130
|
+
httpDurationBuckets?: number[];
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Automatic Metrics
|
|
135
|
+
|
|
136
|
+
When enabled, the following metrics are automatically collected:
|
|
137
|
+
|
|
138
|
+
### HTTP Metrics
|
|
139
|
+
- `{prefix}http_requests_total` - Total HTTP requests by method, route, status code, controller, action
|
|
140
|
+
- `{prefix}http_request_duration_seconds` - HTTP request duration histogram
|
|
141
|
+
|
|
142
|
+
### System Metrics
|
|
143
|
+
- `{prefix}memory_usage_bytes` - Memory usage by type (rss, heap_used, heap_total, external)
|
|
144
|
+
- `{prefix}cpu_usage_ratio` - CPU usage ratio
|
|
145
|
+
- `{prefix}uptime_seconds` - Process uptime
|
|
146
|
+
|
|
147
|
+
### GC Metrics (from prom-client)
|
|
148
|
+
- `{prefix}nodejs_gc_duration_seconds` - Garbage collection duration
|
|
149
|
+
- `{prefix}nodejs_heap_*` - Various heap metrics
|
|
150
|
+
- `{prefix}nodejs_version_info` - Node.js version information
|
|
151
|
+
|
|
152
|
+
## Custom Metrics API
|
|
153
|
+
|
|
154
|
+
### Counter
|
|
155
|
+
```typescript
|
|
156
|
+
const requestCounter = metricsService.createCounter({
|
|
157
|
+
name: 'requests_total',
|
|
158
|
+
help: 'Total requests',
|
|
159
|
+
labelNames: ['method', 'endpoint']
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
requestCounter.inc({ method: 'GET', endpoint: '/api/users' });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Gauge
|
|
166
|
+
```typescript
|
|
167
|
+
const activeConnectionsGauge = metricsService.createGauge({
|
|
168
|
+
name: 'active_connections',
|
|
169
|
+
help: 'Active connections'
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
activeConnectionsGauge.set(42);
|
|
173
|
+
activeConnectionsGauge.inc();
|
|
174
|
+
activeConnectionsGauge.dec();
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Histogram
|
|
178
|
+
```typescript
|
|
179
|
+
const responseTimeHistogram = metricsService.createHistogram({
|
|
180
|
+
name: 'response_time_seconds',
|
|
181
|
+
help: 'Response time in seconds',
|
|
182
|
+
buckets: [0.1, 0.5, 1, 2, 5]
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
responseTimeHistogram.observe(1.23);
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Summary
|
|
189
|
+
```typescript
|
|
190
|
+
const requestSizeSummary = metricsService.createSummary({
|
|
191
|
+
name: 'request_size_bytes',
|
|
192
|
+
help: 'Request size in bytes',
|
|
193
|
+
percentiles: [0.5, 0.9, 0.95, 0.99]
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
requestSizeSummary.observe(1024);
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Effect.js Integration
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { Effect } from 'effect';
|
|
203
|
+
import { measureExecutionTime, recordHttpMetrics } from '@onebun/metrics';
|
|
204
|
+
|
|
205
|
+
// Measure execution time with Effect
|
|
206
|
+
const timedOperation = measureExecutionTime(
|
|
207
|
+
'my_operation_duration',
|
|
208
|
+
Effect.succeed('Hello World')
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
// Record HTTP metrics
|
|
212
|
+
const recordMetrics = recordHttpMetrics({
|
|
213
|
+
method: 'GET',
|
|
214
|
+
route: '/api/test',
|
|
215
|
+
statusCode: 200,
|
|
216
|
+
duration: 0.123,
|
|
217
|
+
controller: 'TestController',
|
|
218
|
+
action: 'test'
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Viewing Metrics
|
|
223
|
+
|
|
224
|
+
Once configured, metrics are available at the configured endpoint (default: `/metrics`):
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
curl http://localhost:3000/metrics
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
This returns metrics in Prometheus exposition format, which can be scraped by Prometheus or viewed directly.
|
|
231
|
+
|
|
232
|
+
## Performance Considerations
|
|
233
|
+
|
|
234
|
+
- System metrics collection interval can be adjusted based on your needs
|
|
235
|
+
- HTTP metrics have minimal overhead (< 1ms per request)
|
|
236
|
+
- GC metrics are collected by the underlying prom-client library
|
|
237
|
+
- Consider using sampling for high-traffic applications
|
|
238
|
+
|
|
239
|
+
## Integration with Monitoring
|
|
240
|
+
|
|
241
|
+
The metrics endpoint is compatible with:
|
|
242
|
+
- **Prometheus** - For scraping and storage
|
|
243
|
+
- **Grafana** - For visualization and dashboards
|
|
244
|
+
- **AlertManager** - For alerting based on metrics
|
|
245
|
+
- Any other Prometheus-compatible monitoring solution
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onebun/metrics",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Prometheus-compatible metrics module for OneBun framework",
|
|
5
|
+
"license": "LGPL-3.0",
|
|
6
|
+
"author": "RemRyahirev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/RemRyahirev/onebun.git",
|
|
10
|
+
"directory": "packages/metrics"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/RemRyahirev/onebun/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/RemRyahirev/onebun/tree/master/packages/metrics#readme",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"main": "src/index.ts",
|
|
25
|
+
"module": "src/index.ts",
|
|
26
|
+
"types": "src/index.ts",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "bun run --watch src/index.ts"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"effect": "^3.13.10",
|
|
32
|
+
"prom-client": "^15.1.3",
|
|
33
|
+
"@onebun/requests": "^0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"bun-types": "1.2.2"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"bun": "1.2.12"
|
|
40
|
+
}
|
|
41
|
+
}
|