Skip to content

Commit

Permalink
moleculer - service naming
Browse files Browse the repository at this point in the history
  • Loading branch information
amarziali committed Jun 22, 2023
1 parent e4b9a26 commit 4bdfaee
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/datadog-plugin-moleculer/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class MoleculerClientPlugin extends ClientPlugin {
static get operation () { return 'call' }

start ({ actionName, opts }) {
const span = this.startSpan('moleculer.call', {
service: this.config.service,
const span = this.startSpan(this.operationName(), {
service: this.config.service || this.serviceName(),
resource: actionName,
kind: 'client'
})
Expand Down
4 changes: 2 additions & 2 deletions packages/datadog-plugin-moleculer/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class MoleculerServerPlugin extends ServerPlugin {
start ({ action, ctx, broker }) {
const followsFrom = this.tracer.extract('text_map', ctx.meta)

this.startSpan('moleculer.action', {
this.startSpan(this.operationName(), {
childOf: followsFrom || this.activeSpan,
service: this.config.service,
service: this.config.service || this.serviceName(),
resource: action.name,
kind: 'server',
type: 'web',
Expand Down
48 changes: 36 additions & 12 deletions packages/datadog-plugin-moleculer/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { expect } = require('chai')
const getPort = require('get-port')
const os = require('os')
const agent = require('../../dd-trace/test/plugins/agent')
const namingSchema = require('./naming')

const sort = trace => trace.sort((a, b) => a.start.toNumber() - b.start.toNumber())

Expand Down Expand Up @@ -54,8 +55,8 @@ describe('Plugin', () => {
agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('name', 'moleculer.action')
expect(spans[0]).to.have.property('service', 'test')
expect(spans[0]).to.have.property('name', namingSchema.server.opName)
expect(spans[0]).to.have.property('service', namingSchema.server.serviceName)
expect(spans[0]).to.have.property('type', 'web')
expect(spans[0]).to.have.property('resource', 'math.add')
expect(spans[0].meta).to.have.property('span.kind', 'server')
Expand All @@ -67,8 +68,8 @@ describe('Plugin', () => {
expect(spans[0].meta).to.have.property('moleculer.node_id', `server-${process.pid}`)
expect(spans[0].meta).to.have.property('component', 'moleculer')

expect(spans[1]).to.have.property('name', 'moleculer.action')
expect(spans[1]).to.have.property('service', 'test')
expect(spans[1]).to.have.property('name', namingSchema.server.opName)
expect(spans[1]).to.have.property('service', namingSchema.server.serviceName)
expect(spans[1]).to.have.property('type', 'web')
expect(spans[1]).to.have.property('resource', 'math.numerify')
expect(spans[1].meta).to.have.property('span.kind', 'server')
Expand All @@ -83,6 +84,11 @@ describe('Plugin', () => {

broker.call('math.add', { a: 5, b: 3 }).catch(done)
})
withNamingSchema(
(done) => broker.call('math.add', { a: 5, b: 3 }).then(done, done),
() => namingSchema.server.opName,
() => namingSchema.server.serviceName
)
})

describe('with configuration', () => {
Expand All @@ -103,6 +109,12 @@ describe('Plugin', () => {

broker.call('math.add', { a: 5, b: 3 }).catch(done)
})

withNamingSchema(
(done) => broker.call('math.add', { a: 5, b: 3 }).then(done, done),
() => namingSchema.server.opName,
() => 'custom'
)
})
})

Expand All @@ -117,8 +129,8 @@ describe('Plugin', () => {
agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('name', 'moleculer.call')
expect(spans[0]).to.have.property('service', 'test')
expect(spans[0]).to.have.property('name', namingSchema.client.opName)
expect(spans[0]).to.have.property('service', namingSchema.client.serviceName)
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())
Expand All @@ -133,6 +145,12 @@ describe('Plugin', () => {

broker.call('math.add', { a: 5, b: 3 }).catch(done)
})

withNamingSchema(
(done) => broker.call('math.add', { a: 5, b: 3 }).then(done, done),
() => namingSchema.client.opName,
() => namingSchema.client.serviceName
)
})

describe('with configuration', () => {
Expand All @@ -153,6 +171,12 @@ describe('Plugin', () => {

broker.call('math.add', { a: 5, b: 3 }).catch(done)
})

withNamingSchema(
(done) => broker.call('math.add', { a: 5, b: 3 }).then(done, done),
() => namingSchema.client.opName,
() => 'custom'
)
})
})

Expand All @@ -169,16 +193,16 @@ describe('Plugin', () => {
const clientPromise = agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('name', 'moleculer.call')
expect(spans[0]).to.have.property('name', namingSchema.client.opName)

spanId = spans[0].span_id
})

const serverPromise = agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('name', 'moleculer.action')
expect(spans[1]).to.have.property('name', 'moleculer.action')
expect(spans[0]).to.have.property('name', namingSchema.server.opName)
expect(spans[1]).to.have.property('name', namingSchema.server.opName)

parentId = spans[0].parent_id
})
Expand Down Expand Up @@ -234,7 +258,7 @@ describe('Plugin', () => {
const clientPromise = agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('name', 'moleculer.call')
expect(spans[0]).to.have.property('name', namingSchema.client.opName)
expect(spans[0].meta).to.have.property('moleculer.context.node_id', `server-${process.pid}`)
expect(spans[0].meta).to.have.property('moleculer.node_id', `client-${process.pid}`)

Expand All @@ -244,8 +268,8 @@ describe('Plugin', () => {
const serverPromise = agent.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('name', 'moleculer.action')
expect(spans[1]).to.have.property('name', 'moleculer.action')
expect(spans[0]).to.have.property('name', namingSchema.server.opName)
expect(spans[1]).to.have.property('name', namingSchema.server.opName)

parentId = spans[0].parent_id
})
Expand Down
24 changes: 24 additions & 0 deletions packages/datadog-plugin-moleculer/test/naming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')

module.exports = resolveNaming({
client: {
v0: {
opName: 'moleculer.call',
serviceName: 'test'
},
v1: {
opName: 'moleculer.client.request',
serviceName: 'test'
}
},
server: {
v0: {
opName: 'moleculer.action',
serviceName: 'test'
},
v1: {
opName: 'moleculer.server.request',
serviceName: 'test'
}
}
})
1 change: 1 addition & 0 deletions packages/dd-trace/src/plugins/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const OutboundPlugin = require('./outbound')
class ClientPlugin extends OutboundPlugin {
static get operation () { return 'request' }
static get kind () { return 'client' }
static get type () { return 'web' } // overridden by storage and other client type plugins
}

module.exports = ClientPlugin
2 changes: 2 additions & 0 deletions packages/dd-trace/src/plugins/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const InboundPlugin = require('./inbound')

class ServerPlugin extends InboundPlugin {
static get operation () { return 'request' }
static get kind () { return 'server' }
static get type () { return 'web' } // a default that may eventually be overriden by nonweb servers
}

module.exports = ServerPlugin
3 changes: 2 additions & 1 deletion packages/dd-trace/src/service-naming/schemas/v0/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const SchemaDefinition = require('../definition')
const messaging = require('./messaging')
const storage = require('./storage')
const web = require('./web')

module.exports = new SchemaDefinition({ messaging, storage })
module.exports = new SchemaDefinition({ messaging, storage, web })
18 changes: 18 additions & 0 deletions packages/dd-trace/src/service-naming/schemas/v0/web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { identityService } = require('../util')

const web = {
client: {
moleculer: {
opName: () => 'moleculer.call',
serviceName: identityService
}
},
server: {
moleculer: {
opName: () => 'moleculer.action',
serviceName: identityService
}
}
}

module.exports = web
3 changes: 2 additions & 1 deletion packages/dd-trace/src/service-naming/schemas/v1/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const SchemaDefinition = require('../definition')
const messaging = require('./messaging')
const storage = require('./storage')
const web = require('./web')

module.exports = new SchemaDefinition({ messaging, storage })
module.exports = new SchemaDefinition({ messaging, storage, web })
18 changes: 18 additions & 0 deletions packages/dd-trace/src/service-naming/schemas/v1/web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { identityService } = require('../util')

const web = {
client: {
moleculer: {
opName: () => 'moleculer.client.request',
serviceName: identityService
}
},
server: {
moleculer: {
opName: () => 'moleculer.server.request',
serviceName: identityService
}
}
}

module.exports = web

0 comments on commit 4bdfaee

Please sign in to comment.