From 746f30c084f8e2c9eb9dbaebb017ed3cc30304ca Mon Sep 17 00:00:00 2001 From: Shelly <17912877+6utt3rfly@users.noreply.github.com> Date: Mon, 24 Apr 2023 23:48:37 -0700 Subject: [PATCH] feat: support restify v9-v11 (#1489) Fixes #1488 (see also #1250 for reference) --- .readme-partials.yaml | 2 +- README.md | 2 +- src/plugins/plugin-restify.ts | 2 +- test/fixtures/plugin-fixtures.json | 15 +++++++++++ test/test-trace-web-frameworks.ts | 6 +++++ test/web-frameworks/restify.ts | 40 ++++++++++++------------------ 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/.readme-partials.yaml b/.readme-partials.yaml index 15f98676b..6719e1801 100644 --- a/.readme-partials.yaml +++ b/.readme-partials.yaml @@ -69,7 +69,7 @@ body: |- * [gRPC](https://www.npmjs.com/package/grpc) server (version ^1.1) * [hapi](https://www.npmjs.com/package/hapi) (versions 8 - 19) * [koa](https://www.npmjs.com/package/koa) (version 1 - 2) - * [restify](https://www.npmjs.com/package/restify) (versions 3 - 8) + * [restify](https://www.npmjs.com/package/restify) (versions 3 - 11) The agent will also automatically trace RPCs from the following modules: * Outbound HTTP requests through `http`, `https`, and `http2` core modules diff --git a/README.md b/README.md index 177a79de6..dc5b05b7d 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ The trace agent can do automatic tracing of the following web frameworks: * [gRPC](https://www.npmjs.com/package/grpc) server (version ^1.1) * [hapi](https://www.npmjs.com/package/hapi) (versions 8 - 19) * [koa](https://www.npmjs.com/package/koa) (version 1 - 2) -* [restify](https://www.npmjs.com/package/restify) (versions 3 - 8) +* [restify](https://www.npmjs.com/package/restify) (versions 3 - 11) The agent will also automatically trace RPCs from the following modules: * Outbound HTTP requests through `http`, `https`, and `http2` core modules diff --git a/src/plugins/plugin-restify.ts b/src/plugins/plugin-restify.ts index 7a2c02c4e..fcd9177a8 100644 --- a/src/plugins/plugin-restify.ts +++ b/src/plugins/plugin-restify.ts @@ -25,7 +25,7 @@ type Response = restify_5.Response; type Next = restify_5.Next; type CreateServerFn = (options?: restify_5.ServerOptions) => restify_5.Server; -const SUPPORTED_VERSIONS = '<=8.x'; +const SUPPORTED_VERSIONS = '<=11.x'; function unpatchRestify(restify: Restify5) { shimmer.unwrap(restify, 'createServer'); diff --git a/test/fixtures/plugin-fixtures.json b/test/fixtures/plugin-fixtures.json index d5552963f..4fd92fe52 100644 --- a/test/fixtures/plugin-fixtures.json +++ b/test/fixtures/plugin-fixtures.json @@ -243,5 +243,20 @@ "dependencies": { "restify": "^8.5.1" } + }, + "restify9": { + "dependencies": { + "restify": "^9.0.0" + } + }, + "restify10": { + "dependencies": { + "restify": "^10.0.0" + } + }, + "restify11": { + "dependencies": { + "restify": "^11.0.0" + } } } diff --git a/test/test-trace-web-frameworks.ts b/test/test-trace-web-frameworks.ts index 4e9ca3363..d44aa926f 100644 --- a/test/test-trace-web-frameworks.ts +++ b/test/test-trace-web-frameworks.ts @@ -42,6 +42,9 @@ import { Restify6, Restify7, Restify8, + Restify9, + Restify10, + Restify11, } from './web-frameworks/restify'; // The type of a stack trace object after being parsed from a trace span's stack @@ -70,6 +73,9 @@ const FRAMEWORKS: WebFrameworkConstructor[] = [ Restify6, Restify7, Restify8, + Restify9, + Restify10, + Restify11, ]; /** diff --git a/test/web-frameworks/restify.ts b/test/web-frameworks/restify.ts index 2f0fbb652..bdb12aacb 100644 --- a/test/web-frameworks/restify.ts +++ b/test/web-frameworks/restify.ts @@ -14,11 +14,7 @@ import {restify_5} from '../../src/plugins/types'; -import { - WebFramework, - WebFrameworkAddHandlerOptions, - WebFrameworkResponse, -} from './base'; +import {WebFramework, WebFrameworkAddHandlerOptions} from './base'; export class Restify implements WebFramework { server: restify_5.Server; @@ -35,31 +31,24 @@ export class Restify implements WebFramework { ); } if (options.hasResponse) { - this.server.get(options.path, async (req, res, next) => { - let response: WebFrameworkResponse; - try { - response = await options.fn(req.headers); - } catch (e) { - next(e); - return; - } - res.statusCode = response.statusCode; - res.end(response.message); - next(); + this.server.get(options.path, (req, res, next) => { + Promise.resolve() + .then(() => options.fn(req.headers)) + .then(response => { + res.statusCode = response.statusCode; + res.end(response.message); + }) + .then(() => next(), next); }); } else { - this.server.use(async (req, res, next) => { + this.server.use((req, res, next) => { if (req.getPath() !== options.path) { next(); return; } - try { - await options.fn(req.headers); - } catch (e) { - next(e); - return; - } - next(); + Promise.resolve() + .then(() => options.fn(req.headers)) + .then(() => next(), next); }); } } @@ -91,3 +80,6 @@ export const Restify5 = makeRestifyClass(5); export const Restify6 = makeRestifyClass(6); export const Restify7 = makeRestifyClass(7); export const Restify8 = makeRestifyClass(8); +export const Restify9 = makeRestifyClass(9, '>12'); +export const Restify10 = makeRestifyClass(10, '>12'); +export const Restify11 = makeRestifyClass(11, '>12');