From 72d336cb86e8da017c8918b4c1ca77008949f7bc Mon Sep 17 00:00:00 2001 From: Jordi Bertran de Balanda Date: Fri, 23 Jun 2023 14:51:38 +0200 Subject: [PATCH] make http server plugins `ServerPlugins` (#3261) Co-authored-by: Thomas Hunter II --- packages/datadog-plugin-http/src/server.js | 72 +++++++++++---------- packages/datadog-plugin-http2/src/server.js | 46 ++++++------- 2 files changed, 61 insertions(+), 57 deletions(-) diff --git a/packages/datadog-plugin-http/src/server.js b/packages/datadog-plugin-http/src/server.js index 21dc90c2025..e8644c9ef88 100644 --- a/packages/datadog-plugin-http/src/server.js +++ b/packages/datadog-plugin-http/src/server.js @@ -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) { diff --git a/packages/datadog-plugin-http2/src/server.js b/packages/datadog-plugin-http2/src/server.js index 52cc06b2367..8049248abdf 100644 --- a/packages/datadog-plugin-http2/src/server.js +++ b/packages/datadog-plugin-http2/src/server.js @@ -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) {