A lightweight TypeScript library for Real User Monitoring (RUM) that collects browser performance metrics and sends them to a Prometheus-compatible backend.
Bretrics enables you to monitor the performance of your web applications from the perspective of real users. It collects Core Web Vitals, custom performance metrics, and business metrics, then transmits them to your metrics collection endpoint using the efficient navigator.sendBeacon API.
- Core Web Vitals: Collects LCP, CLS, FCP, INP, and TTFB metrics from the Web Vitals initiative
- Custom Metrics: Track application-specific performance indicators
- Business Metrics: Monitor conversion funnels and user behavior
- Prometheus Labels: Attach metadata to metrics for detailed analysis
- Device Detection: Automatic classification of desktop, mobile, and tablet devices
- Lightweight: Minimal bundle size with zero runtime dependencies beyond web-vitals
- TypeScript: Full type safety and IntelliSense support
- Extensible: OOP-based architecture allows custom implementations
npm install @mts-pjsc/bretricsyarn add @mts-pjsc/bretricspnpm add @mts-pjsc/bretricsimport { bretrics } from "@mts-pjsc/bretrics";
bretrics
.setup({ apiPath: "/bretrics" })
.useDefaultMonitoring();This configuration enables automatic collection of:
- Core Web Vitals (LCP, CLS, FCP, INP, TTFB)
- DOM element count
- Device type detection
- Current page path
import { bretrics } from "@mts-pjsc/bretrics";
bretrics
.setup({ apiPath: "/api/metrics" })
.useDefaultMonitoring()
.sendMetrics({ custom_metric: 42 });Labels provide additional context for metric aggregation and filtering in Prometheus.
import { bretrics } from "@mts-pjsc/bretrics";
bretrics
.setup({ apiPath: "/bretrics" })
.useDefaultMonitoring()
.setLabels({
environment: "production",
version: "2.1.0"
})
.sendMetrics({
page_load_time: 1250,
api_response_time: {
value: 340,
labels: { endpoint: "/api/users" }
}
});For advanced use cases, extend the Bretrics class to implement custom behavior.
import { Bretrics } from "@mts-pjsc/bretrics";
export class CustomMetricsService extends Bretrics {
public override sendMetrics(metrics: Record<string, number>): this {
// Add preprocessing logic
const enrichedMetrics = this.enrichMetrics(metrics);
super.sendMetrics(enrichedMetrics);
return this;
}
private enrichMetrics(metrics: Record<string, number>): Record<string, number> {
return {
...metrics,
timestamp: Date.now()
};
}
}
export const metricsService = new CustomMetricsService();| Property | Type | Default | Description |
|---|---|---|---|
apiPath |
string |
"/bretrics" |
Endpoint path for metrics submission |
labels |
Record<string, number | string> |
{} |
Default labels applied to all metrics |
| Method | Returns | Description |
|---|---|---|
setup(config) |
this |
Configure the instance with custom settings |
useDefaultMonitoring() |
this |
Enable automatic Web Vitals and DOM metrics collection |
useDefaultLabels() |
this |
Set default labels (device_type, path) |
sendWebVitalsMetrics() |
this |
Subscribe to Web Vitals events |
sendMetrics(metrics) |
this |
Send custom metrics to the backend |
setLabels(labels) |
this |
Add or update metric labels |
interface IMetric {
value: number;
labels: Record<string, number | string>;
}| Metric | Description |
|---|---|
lcp |
Largest Contentful Paint (ms) |
cls |
Cumulative Layout Shift (score) |
fcp |
First Contentful Paint (ms) |
inp |
Interaction to Next Paint (ms) |
ttfb |
Time to First Byte (ms) |
| Metric | Description |
|---|---|
elements_count |
Total DOM elements on page load |
| Label | Description |
|---|---|
device_type |
Device category: desktop, mobile, or tablet |
path |
Current page pathname |
Bretrics sends metrics via HTTP POST to {apiPath}/send-metrics/metrics. The request body contains a JSON object with metric names as keys and values or IMetric objects.
A ready-to-use backend microservice for collecting and exposing metrics to Prometheus is available at MobileTeleSystems/bretrics.
Example payload:
{
"lcp": {
"value": 2500,
"labels": {
"device_type": "mobile",
"path": "/products"
}
},
"custom_metric": 42
}Bretrics supports all modern browsers. The library uses navigator.sendBeacon when available, with a fetch fallback for older browsers.
Contributions are welcome. Please read the Contributing Guide before submitting a pull request.
This project is licensed under the MIT License.
- web-vitals - Library for measuring Web Vitals metrics