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

fix missing out.* tags when using a mongodb replica set #388

Merged
merged 2 commits into from
Nov 23, 2018
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
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ jobs:
environment:
- SERVICES=mongo
- PLUGINS=mongodb-core
- image: mongo:3.6
- image: bitnami/mongodb:3.6
environment:
- MONGODB_REPLICA_SET_MODE=primary
- MONGODB_ADVERTISED_HOSTNAME=localhost

node-postgres:
<<: *node-plugin-base
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ services:
ports:
- "127.0.0.1:6379:6379"
mongo:
image: mongo:3.6
image: bitnami/mongodb:3.6
environment:
- MONGODB_REPLICA_SET_MODE=primary
- MONGODB_ADVERTISED_HOSTNAME=localhost
ports:
- "127.0.0.1:27017:27017"
elasticsearch:
Expand Down
10 changes: 5 additions & 5 deletions scripts/install_plugin_modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ function assertVersions () {
function assertInstrumentation (instrumentation) {
[].concat(instrumentation.versions).forEach(version => {
if (version) {
assertModules(instrumentation.name, version)
assertModules(instrumentation.name, semver.coerce(version).version)
assertModules(instrumentation.name, version)
}
})
}
Expand All @@ -57,8 +57,8 @@ function assertModules (name, version) {
addFolder(name, version)
assertFolder(name)
assertFolder(name, version)
assertPackage(name)
assertPackage(name, version)
assertPackage(name, null, version)
assertPackage(name, version, version)
assertIndex(name)
assertIndex(name, version)
}
Expand All @@ -73,14 +73,14 @@ function assertFolder (name, version) {
}
}

function assertPackage (name, version) {
function assertPackage (name, version, dependency) {
fs.writeFileSync(filename(name, version, 'package.json'), JSON.stringify({
name: [name, sha1(name).substr(0, 8), sha1(version)].filter(val => val).join('-'),
version: '1.0.0',
license: 'BSD-3-Clause',
private: true,
optionalDependencies: {
[name]: version
[name]: dependency
}
}, null, 2) + '\n')
}
Expand Down
16 changes: 13 additions & 3 deletions src/plugins/mongodb-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function createWrapNext (tracer, config) {
})
}

next.call(this, wrapCallback(tracer, span, cb))
next.call(this, wrapCallback(tracer, span, cb, this))
}
}
}
Expand All @@ -57,15 +57,21 @@ function addTags (span, tracer, config, ns, cmd, topology, operationName) {
span.setTag('mongodb.query', query)
}

if (topology.s && topology.s.options) {
addHost(span, topology)
}

function addHost (span, topology) {
const options = topology && topology.s && topology.s.options

if (options && options.host && options.port) {
span.addTags({
'out.host': topology.s.options.host,
'out.port': topology.s.options.port
})
}
}

function wrapCallback (tracer, span, done) {
function wrapCallback (tracer, span, done, cursor) {
return (err, res) => {
if (err) {
span.addTags({
Expand All @@ -75,6 +81,10 @@ function wrapCallback (tracer, span, done) {
})
}

if (cursor) {
addHost(span, cursor.server)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

span.finish()

if (done) {
Expand Down
50 changes: 50 additions & 0 deletions test/plugins/mongodb-core.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const semver = require('semver')
const agent = require('./agent')
const Buffer = require('safe-buffer').Buffer
const plugin = require('../../src/plugins/mongodb-core')
Expand Down Expand Up @@ -273,6 +274,55 @@ describe('Plugin', () => {
})
})

describe('with a replica set', () => {
if (!semver.intersects(version, '>=2.0.5')) return // bug with replica sets before 2.0.5

before(() => {
return agent.load(plugin, 'mongodb-core')
})

after(() => {
return agent.close()
})

beforeEach(done => {
mongo = require(`../../versions/mongodb-core@${version}`).get()

server = new mongo.ReplSet([{
host: 'localhost',
port: 27017
}], {
setName: 'replicaset',
reconnect: false,
connectionTimeout: 1000
})

server.on('connect', () => done())
server.on('error', done)

server.connect()
})

it('should set the correct host/port tags', done => {
agent
.use(traces => {
const span = traces[0][0]

expect(span.meta).to.have.property('out.host', 'localhost')
expect(span.meta).to.have.property('out.port', '27017')
})
.then(done)
.catch(done)

const cursor = server.cursor(`test.${collection}`, {
insert: `test.${collection}`,
documents: [{ a: 1 }]
}, {})

cursor.next()
})
})

describe('with configuration', () => {
before(() => {
return agent.load(plugin, 'mongodb-core', { service: 'custom' })
Expand Down
6 changes: 4 additions & 2 deletions test/setup/services/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ function waitForMongo () {
const operation = new RetryOperation('mongo')

operation.attempt(currentAttempt => {
const server = new mongo.Server({
const server = new mongo.ReplSet([{
host: 'localhost',
port: 27017,
port: 27017
}], {
setName: 'replicaset',
reconnect: false
})

Expand Down