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

moleculer - service naming #3281

Merged
merged 2 commits into from
Jun 26, 2023
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
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 Down Expand Up @@ -135,8 +147,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', hostname)
Expand All @@ -151,6 +163,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 @@ -171,6 +189,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 @@ -187,16 +211,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 @@ -252,7 +276,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 @@ -262,8 +286,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
Copy link
Member

Choose a reason for hiding this comment

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

This should be a different class. A server is not always a web server (as pointed out by the comment).

}

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