项目作者: LeunensMichiel

项目描述 :
An Express middleware for better monitoring of your Node.js apps
高级语言: JavaScript
项目地址: git://github.com/LeunensMichiel/express-influx-multilogger.git
创建时间: 2019-05-09T09:45:41Z
项目社区:https://github.com/LeunensMichiel/express-influx-multilogger

开源协议:MIT License

下载


Express-influx-multilogger

An Express middleware for better monitoring of your Node.js apps.
Parse important req, res and header objects to Influx and Grafana. Get an easier insight of your API without any costs.

Note: this is in active development, and could contain bugs. Please make an issue if you find some.

Getting started

Installation

  1. Install package
    • Npm
      1. npm install express-influx-multilogger
    • Yarn
      1. yarn add express-influx-multilogger
  2. Require
    1. const multilogger = require('express-influx-multilogger');
  3. (Required if you want to write to influx) Initialize database in your app.js
    1. multilogger.init({
    2. database: {
    3. server: "127.0.0.1",
    4. name: "myMultilogDb",
    5. port: 8086
    6. },
    7. interval: 10000,
    8. performance: true,
    9. });
  4. Add multilogger log function right before your router of choice
    1. app.use(multilogger.log({ development: false, extended: false }));
  5. Add multierror function before catching 404 and after your router
    1. app.use(multilogger.error());
  6. In case of using geolocation, make sure your server can make use of req.connection.remoteAddress
  7. You can also add extra custom metrics for monitoring other things, like your database calls

    Add this line in your code (make sure you require multilogger). Name and timing parameters are required. Custom is optional.

    1. multilogger.insertDatabaseCallSpeed({
    2. name: "Random name", // name of your metric (like a database call)
    3. timing: 0.2443, //in ms
    4. custom: {
    5. customOption1: "foo",
    6. foo: foo.bar,
    7. }
    8. });

    Example

  1. const express = require("express");
  2. const bodyParser = require("body-parser");
  3. const cookieParser = require("cookie-parser");
  4. const multilogger = require('express-influx-multilogger'); // Add this to imports
  5. const indexRouter = require("./routes/index");
  6. // Add this to write data to influx (not required)
  7. multilogger.init({
  8. database: {
  9. server: "127.0.0.1",
  10. name: "myMultilogDb",
  11. port: 8086
  12. },
  13. interval: 10000, // Write to Influx every 10 seconds,
  14. performance: true // Write host performance to Influx (mem, cpu,..)
  15. });
  16. multilogger.startPerformanceMetrics();
  17. const app = express();
  18. app.use(bodyParser.urlencoded({ extended: true }));
  19. app.use(bodyParser.json());
  20. app.use(cookieParser());
  21. // Add this before your router of choice
  22. app.use(multilogger.log({ development: false, extended: false }));
  23. app.use("/", indexRouter); // Your router
  24. // Add this before 404 if you want to capture your errors as well
  25. app.use(multilogger.error());
  26. module.exports = app;

Loggable headers

The data being sent to Influx consists of …

Fields

…logs of the amount of calls per statuscode per given interval

  1. amountOf1xx: Influx.FieldType.INTEGER,
  2. amountOf2xx: Influx.FieldType.INTEGER,
  3. amountOf3xx: Influx.FieldType.INTEGER,
  4. amountOf4xx: Influx.FieldType.INTEGER,
  5. amountOf5xx: Influx.FieldType.INTEGER

…logs of Response Time, CPU and memory Usage of the OS, host and ip;

  1. responseTime: Influx.FieldType.FLOAT,
  2. cpuUsage: Influx.FieldType.FLOAT,
  3. memoryUsage: Influx.FieldType.FLOAT,
  4. requests: Influx.FieldType.INTEGER,
  5. host: Influx.FieldType.STRING,
  6. ip: Influx.FieldType.STRING

Tags

…logs of basic headers to Influx

  1. "statusCode",
  2. "statusMessage",
  3. "method",
  4. "path",
  5. "url",
  6. "ip",
  7. "country",
  8. "geohash", // Used to get the geolocation of your api call from a given IP
  9. "client",
  10. "body",
  11. "query",
  12. "params",
  13. "auth", // In case you wanna know wich user that triggered the api call
  14. "errorMessage", // In case of an error: sends message and stack
  15. "errorStack"

Parameters

  1. Extended: Logs a pretty view of req, res and headers (defaults false)
  2. Development: Logs the object that Influx will recieve
  3. Database:
    • server: The address of your Influx database. (defaults: 127.0.0.1)
    • name: Name of your Influx database. (defaults: myMultilogDb)
    • port: Port of your Influx database. (defaults: 3000)
    • username: Login credentials of your Influx database. (defaults: ‘’)
    • password: Password of your Influx database. (defaults: ‘’)
  4. Interval: Defines the rate in ms of the interval you want to write your data to your Influx database

Reset Table

What if you have sent wrong info to Influx? You can make use of the Influx-cli to drop your database or a certain metric or series (table)

  1. > DROP DATABASE <db_name>
  2. > DROP MEASUREMENT <measurement_name>
  3. > DROP SERIES FROM <db_series>

For more info, you can check the docs.

Grafana





If you want to use Grafana and don’t want to write your dashboard, you can import
the dashboard JSON. You can certainly write your own dashboard, as I’m not that skilled with Grafana.

By using this dashboard, make sure you install the worldmap plugin.

License

Distributed under the MIT License. See LICENSE for more information.

Dependencies

Contributors