Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a0294aa
fix(restify): upgrade package to fix type problem
jscherer92 Aug 30, 2022
f6b409b
Merge branch 'main' into fix-restify
jscherer92 Sep 1, 2022
2872079
fix(restify): updated so types are not leaking through #1132
jscherer92 Sep 2, 2022
f8fc832
Merge branch 'main' into fix-restify
jscherer92 Sep 2, 2022
2cd3f88
fix(restify): lint fix
jscherer92 Sep 2, 2022
df1877d
fix(restify): final fix up for linter
jscherer92 Sep 2, 2022
6f47c24
style: avoid checking in manually edited package.json
rauno56 Sep 20, 2022
2cba0d6
Merge branch 'main' into fix/avoid-type-imports
rauno56 Sep 21, 2022
937ae41
test: require restify again for every test
rauno56 Sep 20, 2022
d468ce5
test: differenciate between thorwing and failing gracefully
rauno56 Sep 20, 2022
7b4289a
test: use custom error
rauno56 Sep 20, 2022
98eb1d5
feat: support restify@5
rauno56 Sep 20, 2022
9cd7900
feat: support restify@7
rauno56 Sep 20, 2022
6f40f94
feat: support restify@8
rauno56 Sep 20, 2022
837b84d
test: cleanup
rauno56 Sep 20, 2022
4ae5ad4
test: anyfy arguments on the anonymous handler
rauno56 Sep 20, 2022
3f1baf4
chore: add tav + update supported versions
rauno56 Sep 20, 2022
73cb491
fix: turn off tests on node@18 which is not supported for restify
rauno56 Sep 20, 2022
c90812c
Merge branch 'main' into fix/avoid-type-imports
rauno56 Sep 22, 2022
3535375
refactor: remove the import to after setting up instrumentation
rauno56 Sep 20, 2022
0661303
Merge remote-tracking branch 'upstream/main' into fix/avoid-type-imports
rauno56 Sep 22, 2022
52f0650
Merge branch 'main' into fix/avoid-type-imports
rauno56 Sep 23, 2022
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
Prev Previous commit
Next Next commit
fix(restify): final fix up for linter
  • Loading branch information
jscherer92 committed Sep 2, 2022
commit df1877dea7298e7c95e10fc73eb3b7b59bc13d4e
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
* limitations under the License.
*/

import type * as types from './types';
import type * as restify from 'restify';

import * as api from '@opentelemetry/api';
import { Server } from 'restify';
import * as types from './types';
import { LayerType } from './types';
import * as AttributeNames from './enums/AttributeNames';
import { VERSION } from './version';
import * as constants from './constants';
Expand All @@ -33,7 +36,7 @@ import { getRPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';

const { diag } = api;

export class RestifyInstrumentation extends InstrumentationBase<unknown> {
export class RestifyInstrumentation extends InstrumentationBase<any> {
constructor(config: InstrumentationConfig = {}) {
super(`@opentelemetry/instrumentation-${constants.MODULE_NAME}`, VERSION);
}
Expand All @@ -42,7 +45,7 @@ export class RestifyInstrumentation extends InstrumentationBase<unknown> {
private _isDisabled = false;

init() {
const module = new InstrumentationNodeModuleDefinition<unknown>(
const module = new InstrumentationNodeModuleDefinition<any>(
constants.MODULE_NAME,
constants.SUPPORTED_VERSIONS,
(moduleExports, moduleVersion) => {
Expand All @@ -52,7 +55,7 @@ export class RestifyInstrumentation extends InstrumentationBase<unknown> {
);

module.files.push(
new InstrumentationNodeModuleFile<unknown>(
new InstrumentationNodeModuleFile<any>(
'restify/lib/server.js',
constants.SUPPORTED_VERSIONS,
(moduleExports, moduleVersion) => {
Expand Down Expand Up @@ -106,11 +109,11 @@ export class RestifyInstrumentation extends InstrumentationBase<unknown> {

private _middlewarePatcher(original: Function, methodName?: string) {
const instrumentation = this;
return function (this: Server, ...handler: any) {
return function (this: Server, ...handler: types.NestedRequestHandlers) {
return original.call(
this,
instrumentation._handlerPatcher(
{ type: types.LayerType.MIDDLEWARE, methodName },
{ type: LayerType.MIDDLEWARE, methodName },
handler
)
);
Expand All @@ -119,25 +122,36 @@ export class RestifyInstrumentation extends InstrumentationBase<unknown> {

private _methodPatcher(original: Function, methodName?: string) {
const instrumentation = this;
return function (this: Server, path: any, ...handler: any) {
return function (
this: Server,
path: any,
...handler: types.NestedRequestHandlers
) {
return original.call(
this,
path,
...instrumentation._handlerPatcher(
{ type: types.LayerType.REQUEST_HANDLER, path, methodName },
{ type: LayerType.REQUEST_HANDLER, path, methodName },
handler
)
);
};
}

// will return the same type as `handler`, but all functions recusively patched
private _handlerPatcher(metadata: types.Metadata, handler: any): any {
private _handlerPatcher(
metadata: types.Metadata,
handler: restify.RequestHandler | types.NestedRequestHandlers
): any {
if (Array.isArray(handler)) {
return handler.map(handler => this._handlerPatcher(metadata, handler));
}
if (typeof handler === 'function') {
return (req: any, res: any, next: any) => {
return (
req: types.Request,
res: restify.Response,
next: restify.Next
) => {
if (this._isDisabled) {
return handler(req, res, next);
}
Expand All @@ -154,7 +168,7 @@ export class RestifyInstrumentation extends InstrumentationBase<unknown> {

const fnName = handler.name || undefined;
const spanName =
metadata.type === types.LayerType.REQUEST_HANDLER
metadata.type === LayerType.REQUEST_HANDLER
? `request handler - ${route}`
: `middleware - ${fnName || 'anonymous'}`;
const attributes = {
Expand Down Expand Up @@ -199,7 +213,7 @@ export class RestifyInstrumentation extends InstrumentationBase<unknown> {
}
return api.context.with(
newContext,
(req: unknown, res: unknown, next: unknown) => {
(req: types.Request, res: restify.Response, next: restify.Next) => {
if (isAsyncFunction(handler)) {
return wrapPromise(handler(req, res, next));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@
* limitations under the License.
*/
import { Span } from '@opentelemetry/api';
import type * as restify from 'restify';

export enum LayerType {
MIDDLEWARE = 'middleware',
REQUEST_HANDLER = 'request_handler',
}

export type Metadata = {
declare interface RequestWithRoute extends restify.Request {
route: { path: string };
getRoute: () => { path: string };
}

export declare type Request = RequestWithRoute;
export declare type Metadata = {
path?: string;
methodName?: string;
type: LayerType;
};

export type NestedRequestHandlers = Array<
NestedRequestHandlers | restify.RequestHandler
>;

/**
* extends opentelemetry/api Span object to instrument the root span name of http instrumentation
*/
Expand Down