Skip to content

Commit

Permalink
Peer service computation (#3177)
Browse files Browse the repository at this point in the history
* introduce peer service computation for outbound edges:
    * add DD_TRACE_SPAN_PEER_SERVICE environment variable
    * add peer service computation logic
    * TODO: add tests for config, outbound, tracer (?)
* provide a helper for testing peer service computation
* defer plugin `finish` to TracingPlugin in children
  • Loading branch information
jbertran authored Jun 23, 2023
1 parent 38d5699 commit 4e46109
Show file tree
Hide file tree
Showing 29 changed files with 287 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"proxyquire": true,
"withNamingSchema": true,
"withVersions": true,
"withExports": true
"withExports": true,
"withPeerService": true
},
"rules": {
"no-unused-expressions": 0,
Expand Down
7 changes: 7 additions & 0 deletions packages/datadog-plugin-amqp10/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ describe('Plugin', () => {
})

describe('when sending messages', () => {
withPeerService(
() => tracer,
() => sender.send({ key: 'value' }),
'localhost',
'out.host'
)

it('should do automatic instrumentation', done => {
agent
.use(traces => {
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-amqplib/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ describe('Plugin', () => {
})

describe('when sending commands', () => {
withPeerService(
() => tracer,
() => channel.assertQueue('test', {}, () => {}),
'localhost',
'out.host'
)

it('should do automatic instrumentation for immediate commands', done => {
agent
.use(traces => {
Expand Down Expand Up @@ -124,6 +131,13 @@ describe('Plugin', () => {
})

describe('when publishing messages', () => {
withPeerService(
() => tracer,
() => channel.assertQueue('test', {}, () => {}),
'localhost',
'out.host'
)

it('should do automatic instrumentation', done => {
agent
.use(traces => {
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-dns/src/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DNSLookupPlugin extends ClientPlugin {
span.setTag('dns.address', result)
}

span.finish()
super.finish()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GoogleCloudPubsubConsumerPlugin extends ConsumerPlugin {
span.setTag('pubsub.ack', 1)
}

span.finish()
super.finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-graphql/src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GraphQLExecutePlugin extends TracingPlugin {
finish ({ res, args }) {
const span = this.activeSpan
this.config.hooks.execute(span, args, res)
span.finish()
super.finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-graphql/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GraphQLParsePlugin extends TracingPlugin {

this.config.hooks.parse(span, source, document)

span.finish()
super.finish()
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/datadog-plugin-graphql/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ class GraphQLResolvePlugin extends TracingPlugin {
}
}

finish (finishTime) {
const span = this.activeSpan
span.finish(finishTime)
}

constructor (...args) {
super(...args)

Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-graphql/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GraphQLValidatePlugin extends TracingPlugin {
finish ({ document, errors }) {
const span = this.activeSpan
this.config.hooks.validate(span, document, errors)
span.finish()
super.finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-grpc/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class GrpcClientPlugin extends ClientPlugin {
addMetadataTags(span, metadata, metadataFilter, 'response')
}

span.finish()
super.finish()
}

configure (config) {
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-grpc/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class GrpcServerPlugin extends ServerPlugin {
addMetadataTags(span, trailer, metadataFilter, 'response')
}

span.finish()
super.finish()
}

configure (config) {
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-http/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class HttpClientPlugin extends ClientPlugin {
addRequestHeaders(req, span, this.config)

this.config.hooks.request(span, req, res)
span.finish()
super.finish()
}

error (err) {
Expand Down
20 changes: 20 additions & 0 deletions packages/datadog-plugin-http/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ describe('Plugin', () => {
})
})

withPeerService(
() => tracer,
() => {
const app = express()
app.get('/user', (req, res) => {
res.status(200).send()
})
getPort().then(port => {
appListener = server(app, port, () => {
const req = http.request(`${protocol}://localhost:${port}/user`, res => {
res.on('data', () => {})
})
req.end()
})
})
},
'localhost',
'out.host'
)

it('should do automatic instrumentation', done => {
const app = express()
app.get('/user', (req, res) => {
Expand Down
5 changes: 0 additions & 5 deletions packages/datadog-plugin-http2/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ class Http2ClientPlugin extends ClientPlugin {
this.enter(span, store)
}

finish () {
const span = storage.getStore().span
span.finish()
}

configure (config) {
return super.configure(normalizeConfig(config))
}
Expand Down
25 changes: 25 additions & 0 deletions packages/datadog-plugin-http2/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ describe('Plugin', () => {
})
})

withPeerService(
() => tracer,
done => {
getPort().then(port => {
const app = (stream, headers) => {
stream.respond({
':status': 200
})
stream.end()
}
appListener = server(app, port, () => {
const client = http2
.connect(`${protocol}://localhost:${port}`)
.on('error', done)

const req = client.request({ ':path': '/user', ':method': 'GET' })
req.on('error', done)

req.end()
})
})
},
'localhost', 'out.host'
)

it('should do automatic instrumentation', done => {
const app = (stream, headers) => {
stream.respond({
Expand Down
6 changes: 6 additions & 0 deletions packages/datadog-plugin-memcached/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ describe('Plugin', () => {
Memcached = proxyquire(`../../../versions/memcached@${version}/node_modules/memcached`, {})
})

withPeerService(
() => tracer,
done => memcached.get('test', err => err && done(err)),
'localhost',
'out.host'
)
it('should do automatic instrumentation when using callbacks', done => {
memcached = new Memcached('localhost:11211', { retries: 0 })

Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-moleculer/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MoleculerClientPlugin extends ClientPlugin {
span.addTags(moleculerTags(broker, ctx, this.config))
}

span.finish()
super.finish()
}
}

Expand Down
28 changes: 23 additions & 5 deletions packages/datadog-plugin-moleculer/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,28 @@ describe('Plugin', () => {

describe('client', () => {
describe('without configuration', () => {
before(() => agent.load('moleculer', { server: false }))
before(() => startBroker())
after(() => broker.stop())
after(() => agent.close({ ritmReset: false }))
const hostname = os.hostname()
let tracer

beforeEach(() => startBroker())
afterEach(() => broker.stop())

beforeEach(done => {
agent.load('moleculer', { server: false })
.then(() => { tracer = require('../../dd-trace') })
.then(done)
.catch(done)
})
afterEach(() => agent.close({ ritmReset: false }))

withPeerService(
() => tracer,
done => {
broker.call('math.add', { a: 5, b: 3 }).catch(done)
},
hostname,
'out.host'
)

it('should do automatic instrumentation', done => {
agent.use(traces => {
Expand All @@ -121,7 +139,7 @@ describe('Plugin', () => {
expect(spans[0]).to.have.property('service', 'test')
expect(spans[0]).to.have.property('resource', 'math.add')
expect(spans[0].meta).to.have.property('span.kind', 'client')
expect(spans[0].meta).to.have.property('out.host', os.hostname())
expect(spans[0].meta).to.have.property('out.host', hostname)
expect(spans[0].meta).to.have.property('moleculer.context.action', 'math.add')
expect(spans[0].meta).to.have.property('moleculer.context.node_id', `server-${process.pid}`)
expect(spans[0].meta).to.have.property('moleculer.context.request_id')
Expand Down
10 changes: 10 additions & 0 deletions packages/datadog-plugin-net/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ describe('Plugin', () => {
})
})

withPeerService(
() => tracer,
() => {
const socket = new net.Socket()
socket.connect(port, 'localhost')
},
'localhost',
'out.host'
)

it('should instrument connect with a port', done => {
const socket = new net.Socket()
tracer.scope().activate(parent, () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/datadog-plugin-redis/test/legacy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ describe('Legacy Plugin', () => {
sub = redis.createClient()
})

withPeerService(
() => tracer,
() => client.get('foo'),
'127.0.0.1',
'out.host'
)

it('should do automatic instrumentation when using callbacks', done => {
client.on('error', done)
agent
Expand Down
7 changes: 7 additions & 0 deletions packages/datadog-plugin-rhea/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ describe('Plugin', () => {
})

describe('sending a message', () => {
withPeerService(
() => tracer,
() => context.sender.send({ body: 'Hello World!' }),
'localhost',
'out.host'
)

it('should automatically instrument', (done) => {
agent.use(traces => {
const span = traces[0][0]
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-sharedb/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SharedbPlugin extends ServerPlugin {
if (this.config.hooks && this.config.hooks.reply) {
this.config.hooks.reply(span, request, res)
}
span.finish()
super.finish()
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/dd-trace/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ class Config {
const DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = validateNamingVersion(
process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA
)
const DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED

const DD_TRACE_X_DATADOG_TAGS_MAX_LENGTH = coalesce(
process.env.DD_TRACE_X_DATADOG_TAGS_MAX_LENGTH,
'512'
Expand Down Expand Up @@ -524,6 +526,10 @@ ken|consumer_?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?)
exporters: DD_PROFILING_EXPORTERS
}
this.spanAttributeSchema = DD_TRACE_SPAN_ATTRIBUTE_SCHEMA
this.spanComputePeerService = (this.spanAttributeSchema === 'v0'
? isTrue(DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED)
: true
)
this.lookup = options.lookup
this.startupLogs = isTrue(DD_TRACE_STARTUP_LOGS)
// Disabled for CI Visibility's agentless
Expand Down
2 changes: 2 additions & 0 deletions packages/dd-trace/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = {
ERROR_STACK: 'error.stack',
COMPONENT: 'component',
CLIENT_PORT_KEY: 'network.destination.port',
PEER_SERVICE_KEY: 'peer.service',
PEER_SERVICE_SOURCE_KEY: '_dd.peer.service.source',
SCI_REPOSITORY_URL: '_dd.git.repository_url',
SCI_COMMIT_SHA: '_dd.git.commit.sha'
}
1 change: 1 addition & 0 deletions packages/dd-trace/src/opentracing/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DatadogTracer {
this._version = config.version
this._env = config.env
this._tags = config.tags
this._computePeerService = config.spanComputePeerService
this._logInjection = config.logInjection
this._debug = config.debug
this._prioritySampler = new PrioritySampler(config.env, config.sampler)
Expand Down
Loading

0 comments on commit 4e46109

Please sign in to comment.