{"id":15641733,"url":"https://github.com/trygve-lie/reqspy","last_synced_at":"2026-03-05T18:01:45.919Z","repository":{"id":42395033,"uuid":"233616402","full_name":"trygve-lie/reqspy","owner":"trygve-lie","description":"A non intrusive request spy - log outgoing http(s) requests from an application.","archived":false,"fork":false,"pushed_at":"2024-12-13T23:10:03.000Z","size":35,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-08T14:07:02.863Z","etag":null,"topics":["http","metrics","monitoring","nodejs","requests"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trygve-lie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-13T14:36:47.000Z","updated_at":"2024-11-13T12:50:23.000Z","dependencies_parsed_at":"2024-01-17T07:43:30.476Z","dependency_job_id":"41c34edc-9708-4508-aa99-c8e61b4e391f","html_url":"https://github.com/trygve-lie/reqspy","commit_stats":{"total_commits":36,"total_committers":4,"mean_commits":9.0,"dds":0.5277777777777778,"last_synced_commit":"f65ac040ac5c204d7a8c2ab788e1c520bea178d4"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trygve-lie%2Freqspy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trygve-lie%2Freqspy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trygve-lie%2Freqspy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trygve-lie%2Freqspy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trygve-lie","download_url":"https://codeload.github.com/trygve-lie/reqspy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233546484,"owners_count":18692227,"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":["http","metrics","monitoring","nodejs","requests"],"created_at":"2024-10-03T11:44:57.363Z","updated_at":"2026-03-05T18:01:40.887Z","avatar_url":"https://github.com/trygve-lie.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ReqSpy\n\nA non intrusive request spy - log outgoing http(s) requests from an application.\n\n## Installation\n\n```bash\n$ npm install reqspy\n```\n\n## Example\n\n```js\nconst request = require('request');\nconst Spy = require('reqspy');\n\nconst spy = new Spy();\n\nspy.on('host', (info) =\u003e {\n    console.log('new host requested', info);\n});\n\n// Do http requests with any http lib. The Spy will log it.\nrequest('http://api.somewhere.com', (error, response, body) =\u003e {\n    console.log(body);\n});\n```\n\n## Description\n\nReqSpy is for logging outgoing http(s) requests from an application in a non intrusive way.\n\nReqSpy is non intrusive in the way that one do not need to implement it every single place\none do a http(s) call in an application. One only need to init ReqSpy one place in an application\nand it will intercept all http(s) calls. When that is said; one can init ReqSpy multiple times if\nwanted.\n\nUnder the hood ReqSpy use [async hooks](https://nodejs.org/api/async_hooks.html) to intercept\nhttp(s) calls on the net socket level so there is no altering of the global http(s) object in node\nor any global singletons.\n\nBy using this approach there is a clear separation between the code doing http(s) calls and the\ncode doing the logging.\n\nSince interaction happen on the net socket, ReqSpy should work out of the box with any node.js\nhttp request library.\n\n## Constructor\n\nCreate a new ReqSpy instance.\n\n```js\nconst Spy = require('reqspy');\nconst spy = new Spy(options);\n```\n\n### options (required)\n\nAn Object containing misc configuration. The following values can be provided:\n\n * **hostname** - `String` - The hostname of the application we are logging. Will appear in the output log - `required`\n * **metricName** - `String` - Custom name for the metric on the metric stream - `optional`\n * **enable** - `Boolean` - Enable interception of http(s) calls upon instansiation of the Spy - `optional`\n\n\n## API\n\nThe ReqSpy instance have the following API:\n\n### .enable()\n\nEnable the spy to intercept http(s) calls. Under the hood this enables the async hook\nwhich does the interception.\n\n### .disable()\n\nDisables the spy from intercept http(s) calls. Under the hood this disables the async\nhook which does the interception.\n\n### .metrics\n\nAttribute which holds a [metrics stream](https://github.com/metrics-js/client) that\nemits metrics data.\n\nThe stream will emit an event of the following character for each request it detects:\n\n```js\n{\n    name: 'requests_outgoing',\n    description: 'Outgoing requests to downstream HTTP(S) services',\n    timestamp: 1579468682.531,\n    type: 2,\n    value: 1,\n    labels: [\n      { name: 'service', value: 'turing' },\n      { name: 'hostname', value: 'github.com' },\n      { name: 'address', value: '140.82.118.3' },\n      { name: 'family', value: 4 },\n      { name: 'error', value: false }\n    ],\n    time: null,\n    meta: {}\n}\n```\n\nPlease see [@metrics/client](https://github.com/metrics-js/client) for examples\nof consuming these metrics into your favorite monitoring system.\n\n## Events\n\nFor each request the ReqSpy instance will emit one of the following events:\n\n### host\n\nEmitted when the ReqSpy encounters a request.\n\n```js\nconst spy = new Spy(options);\nspy.on('host', (info) =\u003e {\n    console.log('new host requested', info);\n});\n```\n\n## node.js compabillity\n\nThis module use [async hooks](https://nodejs.org/api/async_hooks.html) which was first\nintrodused in node.js 8.1. Despite that, this module will only work fully with node.js\nversion 10.x or newer.\n\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2020 - Trygve Lie - post@trygve-lie.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrygve-lie%2Freqspy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrygve-lie%2Freqspy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrygve-lie%2Freqspy/lists"}