Skip to content

Commit

Permalink
Merge branch 'master' into julio/automated-user-events-local
Browse files Browse the repository at this point in the history
  • Loading branch information
hoolioh authored Jun 29, 2023
2 parents 4eed545 + d845c56 commit 866d626
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 87 deletions.
23 changes: 23 additions & 0 deletions packages/datadog-instrumentations/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,26 @@ addHook({
})
})
})

const processParamsStartCh = channel('datadog:express:process_params:start')
const wrapProcessParamsMethod = (requestPositionInArguments) => {
return (original) => {
return function () {
if (processParamsStartCh.hasSubscribers) {
processParamsStartCh.publish({ req: arguments[requestPositionInArguments] })
}

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

addHook({ name: 'express', versions: ['>=4.0.0 <4.3.0'] }, express => {
shimmer.wrap(express.Router, 'process_params', wrapProcessParamsMethod(1))
return express
})

addHook({ name: 'express', versions: ['>=4.3.0'] }, express => {
shimmer.wrap(express.Router, 'process_params', wrapProcessParamsMethod(2))
return express
})
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'
}
}
})
102 changes: 50 additions & 52 deletions packages/datadog-plugin-next/src/index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,82 @@
'use strict'

const Plugin = require('../../dd-trace/src/plugins/plugin')
const ServerPlugin = require('../../dd-trace/src/plugins/server')
const { storage } = require('../../datadog-core')
const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
const { COMPONENT } = require('../../dd-trace/src/constants')

class NextPlugin extends Plugin {
class NextPlugin extends ServerPlugin {
static get id () {
return 'next'
}

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

this._requests = new WeakMap()
this.addSub('apm:next:page:load', message => this.pageLoad(message))
}

this.addSub('apm:next:request:start', ({ req, res }) => {
const store = storage.getStore()
const childOf = store ? store.span : store
const span = this.tracer.startSpan('next.request', {
childOf,
tags: {
[COMPONENT]: this.constructor.id,
'service.name': this.config.service || this.tracer._service,
'resource.name': req.method,
'span.type': 'web',
'span.kind': 'server',
'http.method': req.method
}
})

analyticsSampler.sample(span, this.config.measured, true)

this.enter(span, store)

this._requests.set(span, req)
start ({ req, res }) {
const store = storage.getStore()
const childOf = store ? store.span : store
const span = this.tracer.startSpan('next.request', {
childOf,
tags: {
[COMPONENT]: this.constructor.id,
'service.name': this.config.service || this.tracer._service,
'resource.name': req.method,
'span.type': 'web',
'span.kind': 'server',
'http.method': req.method
}
})

this.addSub('apm:next:request:error', this.addError)
analyticsSampler.sample(span, this.config.measured, true)

this.addSub('apm:next:request:finish', ({ req, res }) => {
const store = storage.getStore()
this.enter(span, store)

if (!store) return
this._requests.set(span, req)
}

const span = store.span
const error = span.context()._tags['error']
finish ({ req, res }) {
const store = storage.getStore()

if (!this.config.validateStatus(res.statusCode) && !error) {
span.setTag('error', true)
}
if (!store) return

span.addTags({
'http.status_code': res.statusCode
})
const span = store.span
const error = span.context()._tags['error']

this.config.hooks.request(span, req, res)
if (!this.config.validateStatus(res.statusCode) && !error) {
span.setTag('error', true)
}

span.finish()
span.addTags({
'http.status_code': res.statusCode
})

this.addSub('apm:next:page:load', ({ page }) => {
const store = storage.getStore()
this.config.hooks.request(span, req, res)

if (!store) return
span.finish()
}

const span = store.span
const req = this._requests.get(span)
pageLoad ({ page }) {
const store = storage.getStore()

// Only use error page names if there's not already a name
const current = span.context()._tags['next.page']
if (current && (page === '/404' || page === '/500' || page === '/_error')) {
return
}
if (!store) return

span.addTags({
[COMPONENT]: this.constructor.id,
'resource.name': `${req.method} ${page}`.trim(),
'next.page': page
})
const span = store.span
const req = this._requests.get(span)

// Only use error page names if there's not already a name
const current = span.context()._tags['next.page']
if (current && (page === '/404' || page === '/500' || page === '/_error')) {
return
}

span.addTags({
[COMPONENT]: this.constructor.id,
'resource.name': `${req.method} ${page}`.trim(),
'next.page': page
})
}

Expand Down
Binary file removed packages/datadog-plugin-openai/test/hal.png
Binary file not shown.
Loading

0 comments on commit 866d626

Please sign in to comment.