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

move HttpServer, Http2Server plugins to ServerPlugins #3261

Merged
merged 2 commits into from
Jun 23, 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
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, 'web.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