-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathnet.js
138 lines (110 loc) · 3.69 KB
/
net.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
module.exports = function initialize(agent, net, moduleName, shim) {
shim.wrap(net, ['connect', 'createConnection'], wrapCreate)
function wrapCreate(shim, fn, name) {
return function wrappedCreateConnection() {
const segment = shim.getActiveSegment()
if (!segment) {
return fn.apply(this, arguments)
}
const child = shim.createSegment({ name: 'net.' + name, parent: segment })
const sock = shim.applySegment(fn, child, true, this, arguments)
wrapSocket(sock, child)
return sock
}
}
const serverProto = net.Server.prototype
shim.wrap(serverProto, ['listen', 'close'], function wrapNoRecord(shim, fn) {
return function wrappedNoRecord() {
if (!shim.getActiveSegment()) {
return fn.apply(this, arguments)
}
const args = shim.argsToArray.apply(shim, arguments)
const cbIndex = args.length - 1
shim.bindSegment(args, cbIndex)
return fn.apply(this, args)
}
})
shim.wrap(serverProto, '_listen2', wrapListen2)
shim.wrap(net.Socket.prototype, 'connect', wrapConnect)
function wrapListen2(shim, fn) {
return function wrappedListen2() {
const context = shim.tracer.getContext()
const segment = shim.getActiveSegment()
const emit = this.emit
if (!segment || !emit) {
return fn.apply(this, arguments)
}
this.emit = wrappedEmit
return fn.apply(this, arguments)
function wrappedEmit(ev, socket) {
if (ev !== 'connection' || !socket || !socket._handle) {
return emit.apply(this, arguments)
}
const child = shim.createSegment({ name: 'net.Server.onconnection', parent: segment })
const newContext = context.enterSegment({ segment: child })
if (socket._handle.onread) {
shim.bindContext({ nodule: socket._handle, property: 'onread', context: newContext })
}
return shim.applyContext({
func: emit,
context: newContext,
full: true,
boundThis: this,
args: arguments
})
}
}
}
function wrapConnect(shim, fn) {
return function connectWrapper() {
if (!agent.getTransaction()) {
return fn.apply(this, arguments)
}
const context = agent.tracer.getContext()
const socket = this
const args = normalizeConnectArgs(arguments)
const segment = shim.createSegment({ name: 'net.Socket.connect', parent: context.segment })
if (args[1]) {
args[1] = shim.bindSegment(args[1], segment)
}
const result = shim.applySegment(fn, segment, true, this, args)
if (socket._handle) {
shim.bindSegment(socket._handle, 'onread', segment)
}
shim.bindSegment(socket, 'emit', segment)
return result
}
}
function wrapSocket(sock, segment) {
shim.wrap(sock, 'emit', function emitWrapper(shim, fn) {
return shim.bindSegment(fn, segment)
})
}
}
// taken from node master on 2013/10/30
function normalizeConnectArgs(args) {
let options = Object.create(null)
function toNumber(x) {
return (x = Number(x)) >= 0 ? x : false
}
if (typeof args[0] === 'object' && args[0] !== null) {
// connect(options, [cb])
options = args[0]
} else if (typeof args[0] === 'string' && toNumber(args[0]) === false) {
// connect(path, [cb]);
options.path = args[0]
} else {
// connect(port, [host], [cb])
options.port = args[0]
if (typeof args[1] === 'string') {
options.host = args[1]
}
}
const cb = args[args.length - 1]
return typeof cb === 'function' ? [options, cb] : [options]
}