Skip to content

Commit

Permalink
make http server plugins ServerPlugins (#3261)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Hunter II <tlhunter@datadog.com>
  • Loading branch information
jbertran and tlhunter committed Jun 30, 2023
1 parent 85a8949 commit 8e078b3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 57 deletions.
72 changes: 38 additions & 34 deletions packages/datadog-plugin-http/src/server.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
'use strict'

const Plugin = require('../../dd-trace/src/plugins/plugin')
const ServerPlugin = require('../../dd-trace/src/plugins/server')
const { storage } = require('../../datadog-core')
const web = require('../../dd-trace/src/plugins/util/web')
const { incomingHttpRequestStart, incomingHttpRequestEnd } = require('../../dd-trace/src/appsec/channels')
const { COMPONENT } = require('../../dd-trace/src/constants')

class HttpServerPlugin extends Plugin {
class HttpServerPlugin extends ServerPlugin {
static get id () {
return 'http'
}

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

this._parentStore = undefined
this.addTraceSub('exit', message => this.exit(message))
}

addTraceSub (eventName, handler) {
this.addSub(`apm:${this.constructor.id}:server:${this.operation}:${eventName}`, handler)
}

this.addSub('apm:http:server:request:start', ({ req, res, abortController }) => {
const store = storage.getStore()
const span = web.startSpan(this.tracer, this.config, req, res, 'http.request')
start ({ req, res, abortController }) {
const store = storage.getStore()
const span = web.startSpan(this.tracer, this.config, req, res, 'web.request')

span.setTag(COMPONENT, this.constructor.id)
span.setTag(COMPONENT, this.constructor.id)

this._parentStore = store
this.enter(span, { ...store, req, res })
this._parentStore = store
this.enter(span, { ...store, req, res })

const context = web.getContext(req)
const context = web.getContext(req)

if (!context.instrumented) {
context.res.writeHead = web.wrapWriteHead(context)
context.instrumented = true
}
if (!context.instrumented) {
context.res.writeHead = web.wrapWriteHead(context)
context.instrumented = true
}

if (incomingHttpRequestStart.hasSubscribers) {
incomingHttpRequestStart.publish({ req, res, abortController }) // TODO: no need to make a new object here
}
})
if (incomingHttpRequestStart.hasSubscribers) {
incomingHttpRequestStart.publish({ req, res, abortController }) // TODO: no need to make a new object here
}
}

this.addSub('apm:http:server:request:error', (error) => {
web.addError(error)
})
error (error) {
web.addError(error)
}

this.addSub('apm:http:server:request:exit', ({ req }) => {
const span = this._parentStore && this._parentStore.span
this.enter(span, this._parentStore)
this._parentStore = undefined
})
finish ({ req }) {
const context = web.getContext(req)

this.addSub('apm:http:server:request:finish', ({ req }) => {
const context = web.getContext(req)
if (!context || !context.res) return // Not created by a http.Server instance.

if (!context || !context.res) return // Not created by a http.Server instance.
if (incomingHttpRequestEnd.hasSubscribers) {
incomingHttpRequestEnd.publish({ req, res: context.res })
}

if (incomingHttpRequestEnd.hasSubscribers) {
incomingHttpRequestEnd.publish({ req, res: context.res })
}
web.finishAll(context)
}

web.finishAll(context)
})
exit ({ req }) {
const span = this._parentStore && this._parentStore.span
this.enter(span, this._parentStore)
this._parentStore = undefined
}

configure (config) {
Expand Down
46 changes: 23 additions & 23 deletions packages/datadog-plugin-http2/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@

// Plugin temporarily disabled. See https://github.com/DataDog/dd-trace-js/issues/312

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

class Http2ServerPlugin extends Plugin {
class Http2ServerPlugin extends ServerPlugin {
static get id () {
return 'http2'
}

constructor (...args) {
super(...args)
addTraceSub (eventName, handler) {
this.addSub(`apm:${this.constructor.id}:server:${this.operation}:${eventName}`, handler)
}

this.addSub('apm:http2:server:request:start', ({ req, res }) => {
const store = storage.getStore()
const span = web.startSpan(this.tracer, this.config, req, res, 'web.request')
start ({ req, res }) {
const store = storage.getStore()
const span = web.startSpan(this.tracer, this.config, req, res, 'web.request')

span.setTag(COMPONENT, this.constructor.id)
span.setTag(COMPONENT, this.constructor.id)

this.enter(span, { ...store, req, res })
this.enter(span, { ...store, req, res })

const context = web.getContext(req)
const context = web.getContext(req)

if (!context.instrumented) {
context.res.writeHead = web.wrapWriteHead(context)
context.instrumented = true
}
})
if (!context.instrumented) {
context.res.writeHead = web.wrapWriteHead(context)
context.instrumented = true
}
}

this.addSub('apm:http2:server:request:error', (error) => {
web.addError(error)
})
finish ({ req }) {
const context = web.getContext(req)

this.addSub('apm:http2:server:request:finish', ({ req }) => {
const context = web.getContext(req)
if (!context || !context.res) return // Not created by a http.Server instance.

if (!context || !context.res) return // Not created by a http.Server instance.
web.finishAll(context)
}

web.finishAll(context)
})
error (error) {
web.addError(error)
}

configure (config) {
Expand Down

0 comments on commit 8e078b3

Please sign in to comment.