@egebilgi/log-metric-service-library
v2.3.3
Published
A lightweight log/metric emitter library for Node.js projects, designed to be used with the Egebilgi Log Metric Service. It provides a simple interface for logging and metric collection, making it easy to integrate into our applications.
Readme
Log Metric Service Middleware
Overview
This package provides an Express middleware and service for logging HTTP request metrics. It allows logging of request duration, status, and other key metrics that can be sent to a metric service Prometheus. It also includes a method for sending these metrics to a service via HTTP.
Features
- Request Metrics: Logs HTTP request duration, status code, and other labels (e.g., service, customer ID).
- Metric Service: Sends metrics to a specified service via HTTP.
- Prometheus-Compatible: Compatible with Prometheus for monitoring and alerting.
- Error Handling: Gracefully handles errors while logging metrics.
Installation
To install the package, run the following:
npm install @egebilgi/log-metric-service-library
# Usage
## Adding Middleware to Express
You can use the provided middleware to log request metrics such as response time and status code. It works seamlessly with Prometheus metrics.
```typescript
const app = express();
app.use(middlewares.logRequestMetricsMiddleware("myService", "12345"));Method to Send Metrics
The LogMetricService class has a method sendMetric that logs a metric to a service. Below is an example of how this is used:
class LogMetricService {
public async sendMetric(
name: string,
type: string,
description: string,
labels: {},
value: number
): Promise<void> {
try {
const payload = { name, type, description, labels, value };
await this.req.post("/add-metric", payload); // Sends the metric to a service endpoint
this.emit("metricLogged", payload); // Emits a 'metricLogged' event after successful logging
} catch (err: any) {
this.emit("error", err); // Emits an error event if something goes wrong
}
}
}Full Example
Here is a full example integrating the middleware and service into an Express application.
import express from "express";
import LogMetricService from "@/log-metric-service";
import Middlewares from "@egebilgi/log-metric-service-library";
const app = express();
const logMetricService = new LogMetricService();
const middlewares = new Middlewares(logMetricService);
// Use the logRequestMetricsMiddleware with optional service and customer_id
app.use(middlewares.logRequestMetricsMiddleware("myService", "12345"));
// Sample route to test metrics logging
app.get("/test", (req, res) => {
res.status(200).send("Metrics logging is working!");
});
// Start the server
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});Error Handling
If metric logging fails, the middleware and service will log an error message to the console and continue processing the request. You can modify the error handling logic to fit your needs.
Events
- metricLogged: Fired when a metric is successfully logged.
- error: Fired when there is an error in logging the metric.
Configuration Options
- service: The name of the service being monitored (e.g., "myService").
- customer_id: Optional identifier for the customer.
Both service and customer_id are optional parameters in the middleware.
License
This project is licensed under the MIT License - see the LICENSE file for details.
