forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-shared.js
171 lines (149 loc) · 5.87 KB
/
http-shared.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const endOfStream = require('end-of-stream')
const url = require('url')
const log = require('../utils/logger')
const IdGenerator = require('../context/id-generator')
const AsyncIdAccessor = require('../context/async-id-accessor')
const DefaultAnnotationKey = require('../constant/annotation-key').DefaultAnnotationKey
const RequestHeaderUtils = require('../instrumentation/request-header-utils')
const GeneralMethodDescriptor = require('../constant/method-descriptor').GeneralMethodDescriptor
const AntPathMatcher = require('../utils/ant-path-matcher')
let pathMatcher
const getPathMatcher = () => {
if (!pathMatcher) {
pathMatcher = new AntPathMatcher(require('../config').getConfig())
}
return pathMatcher
}
exports.clearPathMatcher = function () {
pathMatcher = null
}
exports.instrumentRequest = function (agent, moduleName) {
return function (original) {
return function (event, req, res) {
if (event === 'request') {
if (log.isDebug()) {
log.debug('intercepted request event call to %s.Server.prototype.emit', moduleName)
}
if (log.isInfo()) {
log.info('start instrumentRequest')
}
const requestData = RequestHeaderUtils.read(req)
if (!getPathMatcher().matchPath(requestData.rpcName)) {
const trace = agent.createTraceObject(requestData)
if (trace && trace.canSampled()) {
recordRequest(trace.spanRecorder, requestData)
trace.spanRecorder.recordApi(GeneralMethodDescriptor.SERVER_REQUEST)
if (requestData && typeof requestData.searchParams === 'string') {
trace.spanRecorder.recordAttribute(DefaultAnnotationKey.HTTP_PARAM, requestData.searchParams)
}
}
endOfStream(res, function (err) {
if (!err) {
if (trace && trace.canSampled()) {
trace.spanRecorder.recordAttribute(DefaultAnnotationKey.HTTP_STATUS_CODE, this.statusCode)
return agent.completeTraceObject(trace)
}
}
// Handle case where res.end is called after an error occurred on the
// stream (e.g. if the underlying socket was prematurely closed)
const end = res.end
res.end = function () {
const result = end.apply(this, arguments)
if (trace && trace.canSampled()) {
if (this.statusCode) {
trace.spanRecorder.recordAttribute(DefaultAnnotationKey.HTTP_STATUS_CODE, this.statusCode)
}
return agent.completeTraceObject(trace)
}
return result
}
})
}
}
return original.apply(this, arguments)
}
}
}
const ServiceTypeCode = require('../constant/service-type').ServiceTypeCode
exports.traceOutgoingRequest = function (agent, moduleName) {
return function (original) {
return function () {
const req = original.apply(this, arguments)
const trace = agent.traceContext.currentTraceObject()
if (log.isDebug()) {
if (trace) {
log.debug(`traceOutgoingRequest trace: ${trace}`)
} else {
log.debug(`traceOutgoingRequest No sampled trace`)
}
}
if (!trace) return req
if (!trace.canSampled()) {
RequestHeaderUtils.writeHTTPSampled(req)
return req
}
let spanEventRecorder
let asyncId
// If request object has an asyncId, use request object's one
if (arguments && arguments[0] && AsyncIdAccessor.getAsyncId(arguments[0])) {
asyncId = AsyncIdAccessor.getAsyncId(arguments[0])
} else {
spanEventRecorder = trace.traceBlockBegin()
spanEventRecorder.recordServiceType(ServiceTypeCode.ASYNC_HTTP_CLIENT_INTERNAL)
spanEventRecorder.recordApiDesc('http.request')
asyncId = spanEventRecorder.recordNextAsyncId()
trace.traceBlockEnd(spanEventRecorder)
}
const httpURL = req._headers.host + url.parse(req.path).pathname
const destinationId = req._headers.host
const nextSpanId = IdGenerator.next
RequestHeaderUtils.write(req, agent, nextSpanId, destinationId)
const asyncTrace = trace.newAsyncTraceWithId(asyncId)
const asyncEventRecorder = asyncTrace.traceAsyncBegin()
asyncEventRecorder.recordServiceType(ServiceTypeCode.ASYNC_HTTP_CLIENT)
asyncEventRecorder.recordApiDesc(req.method)
asyncEventRecorder.recordHTTPURL(httpURL)
asyncEventRecorder.recordNextSpanId(nextSpanId)
asyncEventRecorder.recordDestinationId(destinationId)
req.on('response', onresponse)
return req
function onresponse(res) {
log.debug('intercepted http.ClientcRequest response event %o', { id: httpURL })
// Inspired by:
// https://github.com/nodejs/node/blob/9623ce572a02632b7596452e079bba066db3a429/lib/events.js#L258-L274
if (res.prependListener) {
res.prependListener('end', onEnd)
} else {
const existing = res._events && res._events.end
if (!existing) {
res.on('end', onEnd)
} else {
if (typeof existing === 'function') {
res._events.end = [onEnd, existing]
} else {
existing.unshift(onEnd)
}
}
}
}
function onEnd() {
log.debug('intercepted http.IncomingMessage end event %o', { id: httpURL })
if (asyncTrace) {
asyncEventRecorder.recordAttribute(DefaultAnnotationKey.HTTP_STATUS_CODE, this.statusCode)
asyncTrace.traceAsyncEnd(asyncEventRecorder)
}
}
}
}
}
const recordRequest = (recorder, requestData) => {
recorder.recordRpc(requestData.rpcName)
recorder.recordEndPoint(requestData.endPoint)
recorder.recordRemoteAddr(requestData.remoteAddress)
}