forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspan-event-recorder.js
146 lines (125 loc) · 3.84 KB
/
span-event-recorder.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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const Annotation = require('./annotation')
const DefaultAnnotationKey = require('../constant/annotation-key').DefaultAnnotationKey
const StringMetaService = require('./string-meta-service')
const AsyncId = require('./async-id')
const asyncIdGenerator = require('./sequence-generator').asyncIdGenerator
const dummyIdGenerator = require('./sequence-generator').dummyIdGenerator
const AnnotationKeyUtils = require('./annotation-key-utils')
// https://github.com/pinpoint-apm/pinpoint/blob/master/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/SpanEventRecorder.java
class SpanEventRecorder {
constructor(spanEvent, span) {
this.spanEvent = spanEvent
this.span = span
this.ended = false
this.error = null
}
recordStartTime(startTime) {
this.spanEvent.startTime = startTime
}
recordServiceType(code, ...properties) {
if (this.spanEvent && code) {
// this.spanEvent.serviceType = new ServiceType(code, properties)
this.spanEvent.serviceType = code
}
}
recordDestinationId(id) {
if (this.spanEvent && id) {
this.spanEvent.destinationId = id
}
}
recordEndPoint(endPoint) {
if (this.spanEvent && endPoint) {
this.spanEvent.endPoint = endPoint
}
}
recordNextSpanId(nextSpanId) {
if (this.spanEvent && nextSpanId) {
this.spanEvent.nextSpanId = nextSpanId
}
}
recordDummyChaining() {
this.spanEvent.nextDummyId = dummyIdGenerator.next
return this.spanEvent.nextDummyId
}
recordNextAsyncId() {
let asyncId
if (this.spanEvent.nextAsyncId === null) {
asyncId = asyncIdGenerator.next
this.spanEvent.nextAsyncId = asyncId
} else {
asyncId = this.spanEvent.nextAsyncId
}
return new AsyncId(asyncId)
}
recordApiId(apiId) {
if (this.spanEvent && apiId !== undefined) {
this.spanEvent.apiId = apiId
}
}
// https://github.com/pinpoint-apm/pinpoint/blob/master/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/AbstractRecorder.java#L70
recordApi(methodDescriptor) {
if (this.spanEvent && methodDescriptor) {
if (methodDescriptor.apiId === 0) {
this.recordAttribute(DefaultAnnotationKey.API, methodDescriptor.fullName)
this.recordApiId(0)
} else {
this.recordApiId(methodDescriptor.apiId)
}
}
}
recordApiWithParameters(methodDescriptor, parameters) {
this.recordApi(methodDescriptor)
this.recordArgs(parameters)
}
recordArgs(parameters) {
const max = Math.min(parameters.length, DefaultAnnotationKey.MAX_ARGS_SIZE)
for (let index = 0; index < max; index++) {
this.recordAttribute(AnnotationKeyUtils.getArgs(index), parameters[index])
}
}
recordApiDesc(desc) {
if (this.spanEvent && desc) {
this.recordAttribute(DefaultAnnotationKey.API, desc)
this.recordApiId(0)
}
}
recordHTTPURL(url) {
if (this.spanEvent && url) {
this.recordAttribute(DefaultAnnotationKey.HTTP_URL, url)
}
}
recordApiArguments(key, desc, valueType) {
if (this.spanEvent && desc) {
this.recordAttribute(key, desc, valueType)
this.recordApiId(0)
}
}
recordAttribute(key, value, valueType) {
if (this.spanEvent && key && value) {
this.spanEvent.annotations.push(new Annotation(key, value, valueType))
}
}
recordException(error, isError) {
if (this.spanEvent && error) {
const metaInfo = StringMetaService.get(error.name || 'Error')
this.spanEvent.exceptionInfo = {
intValue: metaInfo.stringId,
stringValue: error.toString(),
}
if (this.span && isError) {
this.span.err = 1
this.error = error
}
}
}
recordEnd() {
this.ended = true
}
}
module.exports = SpanEventRecorder