forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
method-descriptor-builder.js
238 lines (198 loc) · 6.8 KB
/
method-descriptor-builder.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Pinpoint Node.js Agent
* Copyright 2021-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const MethodDescriptor = require('./method-descriptor')
const MethodType = require('../constant/method-type')
// https://v8.dev/docs/stack-trace-api#appendix%3A-stack-trace-format
class MethodDescriptorBuilder {
constructor(moduleName, objectPath, methodName) {
this.moduleName = moduleName
this.objectPath = objectPath
this.methodName = methodName
this.type = MethodType.DEFAULT
this.parameterDescriptor = '()'
this.apiId = 0
this.computedApiDescriptor = this.makeApiDescriptor()
this.computedFullName = this.makeFullName(moduleName, objectPath)
}
makeApiDescriptor() {
const buffer = []
const typeFunctionString = this.typeFunction()
if (typeFunctionString && typeof typeFunctionString === 'string') {
buffer.push(typeFunctionString)
}
if (this.parameterDescriptor && typeof this.parameterDescriptor === 'string') {
buffer.push(this.parameterDescriptor)
}
return buffer.join('')
}
typeFunction() {
const buffer = []
if (this.moduleName && typeof this.moduleName === 'string') {
buffer.push(this.moduleName)
}
if (this.className && typeof this.className === 'string') {
buffer.push(this.className)
}
if (this.objectPath && typeof this.objectPath === 'string') {
buffer.push(this.objectPath)
}
return buffer.join('.')
}
static make(moduleName, stackRegexNamedGroup) {
if (!stackRegexNamedGroup || !stackRegexNamedGroup.functionName) {
return
}
const functionName = makeFunctionName(stackRegexNamedGroup)
const builder = new MethodDescriptorBuilder(moduleName, functionName, makeMethodName(stackRegexNamedGroup))
.setFunctionName(functionName)
.setLineNumber(makeLineNumber(stackRegexNamedGroup))
.setClassName(stackRegexNamedGroup.type)
.setFileName(stackRegexNamedGroup.fileName)
if (typeof stackRegexNamedGroup.location === 'string') {
builder.setLocation(`${stackRegexNamedGroup.location}${stackRegexNamedGroup.fileName}`)
} else {
builder.setLocation(`${stackRegexNamedGroup.fileName}`)
}
return builder
}
static makeWithNameGroup(namedGroup) {
return MethodDescriptorBuilder.make(undefined, namedGroup)
}
getModuleName() {
return this.moduleName
}
setType(type) {
this.type = type
return this
}
setApiDescriptor(apiDescriptor) {
this.apiDescriptor = apiDescriptor
return this
}
setApiId(apiId) {
this.apiId = apiId
return this
}
setFullName(fullName) {
this.fullName = fullName
return this
}
getFullName() {
return this.fullName || this.computedFullName
}
setLineNumber(lineNumber) {
this.lineNumber = lineNumber
return this
}
getLineNumber() {
return this.lineNumber
}
setLocation(location) {
this.location = location
return this
}
getLocation() {
return this.location
}
setFileName(fileName) {
this.fileName = fileName
return this
}
getFileName() {
return this.fileName
}
build() {
const apiDescriptor = this.apiDescriptor || this.computedApiDescriptor
const fullName = this.fullName || this.computedFullName
return new MethodDescriptor(this.moduleName, this.objectPath, this.methodName, this.type, apiDescriptor, this.apiId, fullName, this.className, this.lineNumber, this.location)
}
makeFullNameWithApiDescriptor(moduleName, objectPath) {
if (moduleName && !objectPath) {
return `${moduleName}.<anonymous>`
}
return this.makeFullNameWithApiDescriptorAndParameterDescriptor()
}
makeFullNameWithApiDescriptorAndParameterDescriptor() {
let parameterDescriptor = this.parameterDescriptor
if (!parameterDescriptor || typeof parameterDescriptor !== 'string') {
parameterDescriptor = '()'
}
if (!this.moduleName && this.className && this.objectPath) {
return `${this.className}.${this.objectPath}${parameterDescriptor}`
}
return [this.objectPath, parameterDescriptor]
.filter(v => v)
.join('')
}
makeFullName(moduleName, objectPath) {
if (moduleName && !objectPath) {
return `${moduleName}.<anonymous>`
}
if (moduleName) {
return `${moduleName}.${this.makeFullNameWithApiDescriptor(moduleName, objectPath)}`
} else {
return `${this.makeFullNameWithApiDescriptor(moduleName, objectPath)}`
}
}
getFunctionName() {
return this.functionName
}
setFunctionName(functionName) {
this.functionName = functionName
return this
}
getClassName() {
return this.className
}
setClassName(className) {
this.className = className
this.computedApiDescriptor = this.makeApiDescriptor()
return this
}
setParameterDescriptor(parameterDescriptor) {
this.parameterDescriptor = parameterDescriptor
this.computedApiDescriptor = this.makeApiDescriptor()
this.computedFullName = this.makeFullName(this.moduleName, this.objectPath)
return this
}
getCacheId() {
return `${this.getFullName()}:${this.lineNumber}:${this.fileName}`
}
static makeLineNumber(namedGroups) {
return makeLineNumber(namedGroups)
}
}
function makeFunctionName(namedGroup) {
const functionName = namedGroup.functionName
if (!functionName) {
return
}
if (typeof functionName !== 'string') {
return
}
const computedGroups = functionName.match(/(?<functionName>.+)(?<=\.)<computed>$/)
if (computedGroups && computedGroups.length > 0 && namedGroup.methodName) {
return computedGroups.groups.functionName + namedGroup.methodName
}
if (functionName === '<computed>' && typeof namedGroup.type === 'string' && typeof namedGroup.methodName === 'string') {
return namedGroup.methodName
}
return namedGroup.functionName
}
function makeMethodName(namedGroups) {
if (namedGroups.functionName == '<anonymous>' && !namedGroups.methodName && namedGroups.type) {
return namedGroups.functionName
}
return namedGroups.methodName || namedGroups.functionName
}
function makeLineNumber(namedGroups) {
if (!namedGroups.lineNumber || typeof namedGroups.lineNumber !== 'string') {
return
}
return parseInt(namedGroups.lineNumber)
}
module.exports = MethodDescriptorBuilder