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
/
plugin.js
137 lines (109 loc) · 3.13 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
const fp = require('fastify-plugin')
function plugin (fastify, options, next) {
const AWSXRay = options.AWSXRay || fastify.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.raw
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 +
' }'
)
if (AWSXRay.isAutomaticMode()) {
const ns = AWSXRay.getNamespace()
ns.bindEmitter(req)
ns.bindEmitter(res)
ns.run(function () {
AWSXRay.setSegment(segment)
done()
})
} else {
request.segment = segment
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()
}
const statusCode = reply.statusCode || reply.res.statusCode
if (statusCode === 429) {
segment.addThrottleFlag()
}
const cause = AWSXRay.utils.getCauseTypeFromHttpStatus(statusCode)
if (cause) {
segment[cause] = true
}
if (segment.http && reply.raw) {
segment.http.close(reply.raw)
}
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()
}
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: '>=3.0.0',
name: 'fastify-xray'
})