{"id":21743930,"url":"https://github.com/telefonica/node-express-metrics","last_synced_at":"2025-04-13T05:06:46.925Z","repository":{"id":34756333,"uuid":"38737534","full_name":"Telefonica/node-express-metrics","owner":"Telefonica","description":"Express middleware to automatically log the metrics traces.","archived":false,"fork":false,"pushed_at":"2015-07-09T13:57:43.000Z","size":127,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-13T05:06:41.110Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Telefonica.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-08T06:57:34.000Z","updated_at":"2021-10-22T09:50:05.000Z","dependencies_parsed_at":"2022-09-15T06:21:08.288Z","dependency_job_id":null,"html_url":"https://github.com/Telefonica/node-express-metrics","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-express-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-express-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-express-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-express-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Telefonica","download_url":"https://codeload.github.com/Telefonica/node-express-metrics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665748,"owners_count":21142123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-26T07:09:27.003Z","updated_at":"2025-04-13T05:06:46.904Z","avatar_url":"https://github.com/Telefonica.png","language":"JavaScript","readme":"# node-express-metrics\n\nExpress middleware to log metrics traces\n\n[![npm version](https://badge.fury.io/js/node-express-metrics.svg)](http://badge.fury.io/js/node-express-metrics)\n[![Build Status](https://travis-ci.org/telefonica/node-express-metrics.svg)](https://travis-ci.org/telefonica/node-express-metrics)\n[![Coverage Status](https://img.shields.io/coveralls/telefonica/node-express-metrics.svg)](https://coveralls.io/r/telefonica/node-express-metrics)\n\nThis express middleware gathers the metrics information from a **metrics** object stored in the process [domain](https://nodejs.org/api/domain.html): **process.domain.metrics**.\n\nFor 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.\n\n## Installation\n\n```bash\nnpm install node-express-metrics\n```\n\n## Basic usage\n\n```js\nvar express = require('express'),\n    logger = require('logops'),\n    expressDomain = require('express-domaining'),\n    expressTracking = require('express-tracking'),\n    expressMetrics = require('node-express-metrics');\n\nvar app = express();\napp.use(expressDomain(logger));\napp.use(expressTracking({op: 'test'}));\napp.use(expressMetrics(logger));\n\napp.get('/version', function(req, res) {\n   //Add metrics information\n   var metrics = process.domain.metrics;\n   metrics.operationStatus = 'ok';\n   metrics.clientAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;\n   res.status(200).json({version: '1.0.0'});\n});\n\napp.listen(3000);\n```\n\n## Options\n\nThe express middleware may receive an object with optional settings:\n\n| Option | Type | Default | Description |\n|--------|------|---------|-------------|\n| logger | Object | null | logger module ({info: function(msg, metricsObject)}) |\n\n\n## Full example\n\nIt is recommended to use this express middleware in combination with:\n\n* [express-domaining](https://github.com/telefonica/node-express-domaining). Express middleware to automatically create and destroy a [domain](https://nodejs.org/api/domain.html).\n* [express-logging](https://github.com/telefonica/node-express-logging). Express middleware to log each request and response.\n* [express-tracking](https://github.com/telefonica/node-express-tracking). Express middleware to track the request and response storing in the domain the operation, transactionId and correlator.\n\n```js\nvar express = require('express'),\n    expressDomain = require('express-domaining'),\n    expressTracking = require('express-tracking'),\n    expressLogging = require('express-logging'),\n    expressMetrics = require('node-express-metrics'),\n    logger = require('logops');\n\nlogger.getContext = function() {\n  return process.domain \u0026\u0026 process.domain.tracking;\n};\n\nvar app = express();\napp.use(expressDomain(logger));\napp.use(expressTracking({op: 'test'}));\napp.use(expressLogging(logger));\napp.use(expressMetrics(logger));\n\napp.get('/test', function(req, res) {\n  res.status(200).send();\n});\n\napp.get('/version', function(req, res) {\n   //Add metrics information\n   var metrics = process.domain.metrics;\n   metrics.operationStatus = 'ok';\n   metrics.clientAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;\n   res.status(200).json({version: '1.0.0'});\n});\n\napp.listen(3000);\n```\n\nAfter 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:\n\n```json\n{\"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\"}\n{\"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.\"}\n{\"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\"}\n```\n\n\n## License\n\nCopyright 2015 [Telefónica Investigación y Desarrollo, S.A.U](http://www.tid.es)\n\nLicensed 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\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless 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.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelefonica%2Fnode-express-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftelefonica%2Fnode-express-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelefonica%2Fnode-express-metrics/lists"}