npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-express-metrics

v1.0.0

Published

Express middleware to log the metrics traces

Downloads

22

Readme

node-express-metrics

Express middleware to log metrics traces

npm version Build Status Coverage Status

This express middleware gathers the metrics information from a metrics object stored in the process domain: process.domain.metrics.

For each request, the middleware initializes the metrics object and, at the end of the response, logs the information stored in the metrics object. It is responsibility of the service logic to enrich this metrics object.

Installation

npm install node-express-metrics

Basic usage

var express = require('express'),
    logger = require('logops'),
    expressDomain = require('express-domaining'),
    expressTracking = require('express-tracking'),
    expressMetrics = require('node-express-metrics');

var app = express();
app.use(expressDomain(logger));
app.use(expressTracking({op: 'test'}));
app.use(expressMetrics(logger));

app.get('/version', function(req, res) {
   //Add metrics information
   var metrics = process.domain.metrics;
   metrics.operationStatus = 'ok';
   metrics.clientAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
   res.status(200).json({version: '1.0.0'});
});

app.listen(3000);

Options

The express middleware may receive an object with optional settings:

| Option | Type | Default | Description | |--------|------|---------|-------------| | logger | Object | null | logger module ({info: function(msg, metricsObject)}) |

Full example

It is recommended to use this express middleware in combination with:

  • express-domaining. Express middleware to automatically create and destroy a domain.
  • express-logging. Express middleware to log each request and response.
  • express-tracking. Express middleware to track the request and response storing in the domain the operation, transactionId and correlator.
var express = require('express'),
    expressDomain = require('express-domaining'),
    expressTracking = require('express-tracking'),
    expressLogging = require('express-logging'),
    expressMetrics = require('node-express-metrics'),
    logger = require('logops');

logger.getContext = function() {
  return process.domain && process.domain.tracking;
};

var app = express();
app.use(expressDomain(logger));
app.use(expressTracking({op: 'test'}));
app.use(expressLogging(logger));
app.use(expressMetrics(logger));

app.get('/test', function(req, res) {
  res.status(200).send();
});

app.get('/version', function(req, res) {
   //Add metrics information
   var metrics = process.domain.metrics;
   metrics.operationStatus = 'ok';
   metrics.clientAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
   res.status(200).json({version: '1.0.0'});
});

app.listen(3000);

After launching the previous server, each HTTP request generates 2 log entries to trace the request and response and another log entry with the metrics information:

{"time":"2015-07-08T20:31:25.210Z","lvl":"INFO","corr":"7a051a2b-e3e8-4625-a680-1ae30105cdda","trans":"7a051a2b-e3e8-4625-a680-1ae30105cdda","op":"test","msg":"Request from 127.0.0.1: GET /version"}
{"time":"2015-07-08T20:31:25.212Z","lvl":"INFO","corr":"7a051a2b-e3e8-4625-a680-1ae30105cdda","trans":"7a051a2b-e3e8-4625-a680-1ae30105cdda","op":"test","msg":"Response with status 304 in 3 ms."}
{"time":"2015-07-08T20:31:25.214Z","lvl":"INFO","corr":"7a051a2b-e3e8-4625-a680-1ae30105cdda","trans":"7a051a2b-e3e8-4625-a680-1ae30105cdda","op":"test","msg":"metrics","operationStatus":"ok","clientAddress":"127.0.0.1"}

License

Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.