Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add net integration #406

Merged
merged 1 commit into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'mongodb-core': require('./mongodb-core'),
'mysql': require('./mysql'),
'mysql2': require('./mysql2'),
'net': require('./net'),
'pg': require('./pg'),
'q': require('./q'),
'redis': require('./redis'),
Expand Down
78 changes: 78 additions & 0 deletions src/plugins/net.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'

const tx = require('./util/tx')

function createWrapConnect (tracer, config) {
return function wrapConnect (connect) {
return function connectWithTrace () {
const options = getOptions(arguments)

if (!options) return connect.apply(this, arguments)

const span = startSpan(tracer, config)

if (options.path) {
span.addTags({
'resource.name': `${options.path}`,
'socket.type': 'ipc',
'socket.path': options.path
})
} else if (options.port) {
span.addTags({
'resource.name': `${options.host}:${options.port}`,
'socket.type': 'tcp',
'socket.hostname': options.host,
'socket.port': options.port
})
}

this.once('connect', tx.wrap(span).bind(null))
this.once('error', tx.wrap(span))

return connect.apply(this, arguments)
}
}
}

function startSpan (tracer, config) {
const scope = tracer.scopeManager().active()
const span = tracer.startSpan('net.connect', {
childOf: scope && scope.span(),
tags: {
'span.kind': 'client',
'service.name': config.service || `${tracer._service}-net`
}
})

return span
}

function getOptions (args) {
if (!args[0]) return

switch (typeof args[0]) {
case 'object':
if (Array.isArray(args[0])) return getOptions(args[0])
if (typeof args[0].port === 'undefined' && typeof args[0].path === 'undefined') return
return args[0]
case 'number':
return {
port: args[0],
host: typeof args[1] === 'string' ? args[1] : 'localhost'
}
case 'string':
return {
path: args[0]
}
}
}

module.exports = {
name: 'net',
patch (net, tracer, config) {
this.wrap(net.Socket.prototype, 'connect', createWrapConnect(tracer, config))
},
unpatch (net) {
this.unwrap(net.Socket.prototype, 'connect')
}
}
140 changes: 140 additions & 0 deletions test/plugins/net.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
'use strict'

const getPort = require('get-port')
const agent = require('./agent')
const plugin = require('../../src/plugins/net')

wrapIt()

describe('Plugin', () => {
let net
let tcp
let ipc
let port

describe('net', () => {
afterEach(() => {
return agent.close()
})

afterEach(() => {
tcp.close()
})

afterEach(() => {
ipc.close()
})

beforeEach(() => {
return agent.load(plugin, 'net')
.then(() => {
net = require(`net`)

return getPort().then(_port => {
port = _port
})
})
})

beforeEach(done => {
tcp = new net.Server(socket => {
socket.write('')
})
tcp.listen(port, () => done())
})

beforeEach(done => {
ipc = new net.Server(socket => {
socket.write('')
})
ipc.listen('/tmp/dd-trace.sock', () => done())
})

it('should instrument connect with a path', done => {
agent
.use(traces => {
expect(traces[0][0]).to.deep.include({
name: 'net.connect',
service: 'test-net',
resource: '/tmp/dd-trace.sock',
meta: {
'span.kind': 'client',
'socket.type': 'ipc',
'socket.path': '/tmp/dd-trace.sock'
}
})
})
.then(done)
.catch(done)

net.connect('/tmp/dd-trace.sock')
})

it('should instrument connect with a port', done => {
agent
.use(traces => {
expect(traces[0][0]).to.deep.include({
name: 'net.connect',
service: 'test-net',
resource: `localhost:${port}`,
meta: {
'span.kind': 'client',
'socket.type': 'tcp',
'socket.port': `${port}`,
'socket.hostname': 'localhost'
}
})
})
.then(done)
.catch(done)

net.connect(port, 'localhost')
})

it('should instrument connect with TCP options', done => {
agent
.use(traces => {
expect(traces[0][0]).to.deep.include({
name: 'net.connect',
service: 'test-net',
resource: `localhost:${port}`,
meta: {
'span.kind': 'client',
'socket.type': 'tcp',
'socket.port': `${port}`,
'socket.hostname': 'localhost'
}
})
})
.then(done)
.catch(done)

net.connect({
port,
host: 'localhost'
})
})

it('should instrument connect with IPC options', done => {
agent
.use(traces => {
expect(traces[0][0]).to.deep.include({
name: 'net.connect',
service: 'test-net',
resource: '/tmp/dd-trace.sock',
meta: {
'span.kind': 'client',
'socket.type': 'ipc',
'socket.path': '/tmp/dd-trace.sock'
}
})
})
.then(done)
.catch(done)

net.connect({
path: '/tmp/dd-trace.sock'
})
})
})
})