forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-method-descriptor-builder.js
116 lines (95 loc) · 3.6 KB
/
make-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
/**
* Pinpoint Node.js Agent
* Copyright 2021-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const MethodDescriptorBuilder = require('./method-descriptor-builder')
const makeMethodDescriptorBuilder = (moduleName, index, handlerIndex) => {
const callstack = {}
Error.captureStackTrace(callstack, makeMethodDescriptorBuilder)
const builder = MethodDescriptorBuilder.make(moduleName, captureNamedGroup(stringStackOfCallStack(callstack, index)))
if (builder && typeof handlerIndex === 'number') {
const location = locationFileNameLineNumber(stringStackOfCallStack(callstack, handlerIndex))
if (location && location.groups) {
const locationGroup = location.groups
builder.setFileName(locationGroup.fileName)
if (typeof locationGroup.location === 'string') {
builder.setLocation(`${locationGroup.location}${locationGroup.fileName}`)
} else {
builder.setLocation(`${locationGroup.fileName}`)
}
const lineNumber = MethodDescriptorBuilder.makeLineNumber(locationGroup)
if (lineNumber) {
builder.setLineNumber(lineNumber)
}
}
}
return builder
}
const stringStackOfCallStack = (callstack, index) => {
const stacks = callstack.stack.split(/\n */)
if (typeof index !== 'number' || stacks.length < index + 1) {
return
}
return stacks[index]
}
const captureNamedGroup = (stack) => {
const functionNameMatches = [typeFunctionNameMethodName, newFunctionName, ayncFunctionName]
for (const match of functionNameMatches) {
var group = match(stack)
if (group && group.groups) {
break
}
}
const locationMatches = [locationFileNameLineNumber, fileNameOfStack]
for (const match of locationMatches) {
var locationGroup = match(stack)
if (locationGroup && locationGroup.groups) {
break
}
}
if (!group || !group.groups || !locationGroup || !locationGroup.groups) {
return
}
return Object.assign(group.groups, locationGroup.groups)
}
const typeFunctionNameMethodName = (stack) => {
return stack.match(/at (?<type>\w+(?=\.))?\.?(?<functionName>[^\s]+)(?: \[as (?<methodName>\w+)\])? \((?<location>.+\/)(?<fileName>[^:/]+):(?<lineNumber>[0-9]+):(?<columnNumber>[0-9]+)\)$/)
}
const newFunctionName = (stack) => {
const group = stack.match(/at new (?<functionName>[^\s]+) /)
if (!group || !group.groups || !group.groups.functionName) {
return
}
const functionName = toUpperCaseOfFirstCharacter(group)
return { 'groups': { 'functionName': functionName } }
}
const ayncFunctionName = (stack) => {
const group = stack.match(/at async (?<functionName>[^\s]+) /)
if (!group || !group.groups || !group.groups.functionName) {
return
}
const functionName = toUpperCaseOfFirstCharacter(group)
return { 'groups': { 'functionName': functionName } }
}
const locationFileNameLineNumber = (stack) => {
return stack.match(/ \((?<location>.+\/)(?<fileName>[^:/]+):(?<lineNumber>[0-9]+):(?<columnNumber>[0-9]+)\)$/)
}
const fileNameOfStack = (stack) => {
return stack.match(/ \((?<fileName><anonymous>)\)$/)
}
module.exports = {
makeMethodDescriptorBuilder,
captureNamedGroup,
}
function toUpperCaseOfFirstCharacter(group) {
return Array.from(group.groups.functionName)
.map((element, index) => {
if (index == 0 && typeof element === 'string') {
return element.toUpperCase()
}
return element
})
.join('')
}