This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4786173
Showing
12 changed files
with
601 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
|
||
# [*.md] | ||
# trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# 0x | ||
.__browserify_string_empty.js | ||
profile-* | ||
|
||
# tap --cov | ||
.nyc_output/ | ||
|
||
# JetBrains IntelliJ IDEA | ||
.idea/ | ||
*.iml | ||
|
||
# VS Code | ||
.vscode/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "12" | ||
- "10" | ||
- "8" | ||
|
||
script: | ||
- npm run lint-ci | ||
- npm run test-ci | ||
- npm run typescript | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
on_failure: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Jérôme Macias | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# fastify-xray | ||
|
||
[](https://travis-ci.org/jeromemacias/fastify-xray) | ||
|
||
The AWS X-Ray [Fastify](http://fastify.io/) package automatically records information | ||
for incoming and outgoing requests and responses, via the 'enable' function in this | ||
package. To configure sampling, dynamic naming, and more see the [set up section](https://github.com/aws/aws-xray-sdk-node/tree/master/packages/core#setup). | ||
|
||
The AWS X-Ray SDK Core has two modes - `manual` and `automatic`. | ||
Automatic mode uses the `cls-hooked` package and automatically | ||
tracks the current segment and subsegment. This is the default mode. | ||
Manual mode requires that you pass around the segment reference. | ||
|
||
In automatic mode, you can get the current segment or subsegment at any time: | ||
|
||
```js | ||
const segment = AWSXRay.getSegment(); | ||
``` | ||
|
||
In manual mode, you can get the base segment off of the request object: | ||
|
||
```js | ||
const segment = req.segment; | ||
|
||
// If the restify context plugin is being used, it is placed under 'XRaySegment' | ||
const segment = request.XRaySegment; | ||
``` | ||
|
||
This plugin should be register as soon as possible after fastify initialisation. | ||
|
||
## Automatic mode examples | ||
|
||
For more automatic mode examples, see the [example code](https://github.com/aws/aws-xray-sdk-node/tree/master/packages/core#example-code). | ||
|
||
```js | ||
const fastify = require("fastify")(); | ||
|
||
fastify.register(require("fastify-xray"), { | ||
defaultName: "My App Name", | ||
}); | ||
|
||
// Error capturing is attached to the fastify onError hook | ||
``` | ||
|
||
## Manual mode examples | ||
|
||
For more manual mode examples, see [manual mode examples for Express](https://github.com/aws/aws-xray-sdk-node/tree/master/packages/express#manual-mode-examples). The X-Ray SDK can be used identically inside Restify routes. | ||
|
||
```js | ||
const fastify = require("fastify")(); | ||
|
||
fastify.register(require("fastify-xray"), { | ||
defaultName: "My App Name", | ||
}); | ||
|
||
//... | ||
|
||
fastify.get("/", async function (request, reply) { | ||
const segment = request.XRaySegment; | ||
|
||
//... | ||
|
||
return "hello"; | ||
}); | ||
|
||
// Error capturing is attached to the fastify onError hook | ||
``` | ||
|
||
## License | ||
|
||
[MIT License](LICENSE.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "fastify-xray", | ||
"version": "1.0.0", | ||
"description": "Plugin for fastify for AWS XRay recording", | ||
"main": "plugin.js", | ||
"types": "plugin.d.ts", | ||
"scripts": { | ||
"test": "tap test/*.test.js && npm run typescript", | ||
"test-ci": "tap --cov test/*.test.js && npm run typescript", | ||
"lint": "standard | snazzy", | ||
"lint-ci": "standard", | ||
"typescript": "tsc --project ./tsconfig.json" | ||
}, | ||
"precommit": [ | ||
"lint", | ||
"test" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/jeromemacias/fastify-xray.git" | ||
}, | ||
"keywords": [ | ||
"fastify", | ||
"cookie" | ||
], | ||
"author": "Jérôme Macias <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/jeromemacias/fastify-xray/issues" | ||
}, | ||
"homepage": "https://github.com/jeromemacias/fastify-xray#readme", | ||
"devDependencies": { | ||
"@types/node": "^11.9.3", | ||
"fastify": "^2.0.0", | ||
"pre-commit": "^1.2.2", | ||
"sinon": "~9.0.2", | ||
"snazzy": "^8.0.0", | ||
"standard": "^12.0.1", | ||
"tap": "^12.5.2", | ||
"typescript": "^3.3.1" | ||
}, | ||
"dependencies": { | ||
"aws-xray-sdk-core": "^3.1.0", | ||
"fastify-plugin": "^1.6.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/// <reference types="node" /> | ||
import * as fastify from "fastify"; | ||
import { FastifyRequest } from "fastify"; | ||
import { IncomingMessage } from "http"; | ||
import { Http2ServerRequest } from "http2"; | ||
import * as AWSXRay from "aws-xray-sdk-core"; | ||
|
||
type HttpRequest = IncomingMessage | Http2ServerRequest; | ||
|
||
declare module "fastify" { | ||
interface FastifyRequest< | ||
HttpRequest, | ||
Query = fastify.DefaultQuery, | ||
Params = fastify.DefaultParams, | ||
Headers = fastify.DefaultHeaders, | ||
Body = any | ||
> { | ||
/** | ||
* Request XRay Segment | ||
*/ | ||
XRaySegment?: AWSXRay.Segment; | ||
} | ||
} | ||
|
||
declare function fastifyXray(): void; | ||
|
||
declare namespace fastifyXray { | ||
interface FastifyXrayOptions { | ||
defaultName: string; | ||
} | ||
} | ||
|
||
export = fastifyXray; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
"use strict"; | ||
|
||
const fp = require("fastify-plugin"); | ||
const AWSXRay = require("aws-xray-sdk-core"); | ||
|
||
const mwUtils = AWSXRay.middleware; | ||
const IncomingRequestData = mwUtils.IncomingRequestData; | ||
const Segment = AWSXRay.Segment; | ||
|
||
function fastifyXrayOnRequest(request, reply, done) { | ||
const req = request.raw; | ||
const res = reply.res; | ||
res.req = req; | ||
res.header = {}; | ||
|
||
const amznTraceHeader = mwUtils.processHeaders(req); | ||
const name = mwUtils.resolveName(req.headers.host); | ||
const segment = new Segment( | ||
name, | ||
amznTraceHeader.Root || amznTraceHeader.root, | ||
amznTraceHeader.Parent || amznTraceHeader.parent | ||
); | ||
|
||
mwUtils.resolveSampling(amznTraceHeader, segment, res); | ||
segment.addIncomingRequestData(new IncomingRequestData(req)); | ||
|
||
for (const headerKey in res.header) { | ||
reply.header(headerKey, res.header[headerKey]); | ||
} | ||
|
||
AWSXRay.getLogger().debug( | ||
"Starting Fastify segment: { url: " + | ||
req.url + | ||
", name: " + | ||
segment.name + | ||
", trace_id: " + | ||
segment.trace_id + | ||
", id: " + | ||
segment.id + | ||
", sampled: " + | ||
!segment.notTraced + | ||
" }" | ||
); | ||
|
||
request.segment = segment; | ||
|
||
if (AWSXRay.isAutomaticMode()) { | ||
var ns = AWSXRay.getNamespace(); | ||
ns.bindEmitter(req); | ||
ns.bindEmitter(res); | ||
|
||
ns.run(function () { | ||
AWSXRay.setSegment(segment); | ||
|
||
done && done(); | ||
}); | ||
} else { | ||
req.segment = segment; | ||
|
||
done && done(); | ||
} | ||
} | ||
|
||
function fastifyXrayOnError(request, reply, error, done) { | ||
const segment = AWSXRay.resolveSegment(request.segment); | ||
|
||
if (segment && error) { | ||
segment.addError(error); | ||
} | ||
|
||
done(); | ||
} | ||
|
||
function fastifyXrayOnResponse(request, reply, done) { | ||
const segment = AWSXRay.resolveSegment(request.segment); | ||
if (!segment || segment.isClosed()) { | ||
return done(); | ||
} | ||
|
||
if (reply.statusCode === 429) segment.addThrottleFlag(); | ||
if (AWSXRay.utils.getCauseTypeFromHttpStatus(reply.statusCode)) { | ||
segment[AWSXRay.utils.getCauseTypeFromHttpStatus(reply.statusCode)] = true; | ||
} | ||
|
||
if (segment.http && reply.res) { | ||
segment.http.close(reply.res); | ||
} | ||
segment.close(); | ||
|
||
AWSXRay.getLogger().debug( | ||
"Closed Fastify segment successfully: { url: " + | ||
request.raw.url + | ||
", name: " + | ||
segment.name + | ||
", trace_id: " + | ||
segment.trace_id + | ||
", id: " + | ||
segment.id + | ||
", sampled: " + | ||
!segment.notTraced + | ||
" }" | ||
); | ||
|
||
done(); | ||
} | ||
|
||
function plugin(fastify, options, next) { | ||
const { defaultName } = options; | ||
|
||
if (!defaultName || typeof defaultName !== "string") | ||
return next( | ||
new Error( | ||
"Default segment name was not supplied. Please provide a string." | ||
) | ||
); | ||
|
||
mwUtils.setDefaultName(defaultName); | ||
AWSXRay.getLogger().debug("Enabling AWS X-Ray for Fastify."); | ||
|
||
fastify.decorateRequest("XRaySegment", null); | ||
fastify.addHook("onRequest", fastifyXrayOnRequest); | ||
fastify.addHook("onError", fastifyXrayOnError); | ||
fastify.addHook("onResponse", fastifyXrayOnResponse); | ||
|
||
next(); | ||
} | ||
|
||
module.exports = fp(plugin, { | ||
fastify: ">=2.0.0", | ||
name: "fastify-xray", | ||
}); |
Oops, something went wrong.