Skip to content

Commit

Permalink
chore: lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jan 19, 2020
1 parent 95dfd78 commit b020813
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
58 changes: 35 additions & 23 deletions packages/opentelemetry-plugin-express/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { BasePlugin } from '@opentelemetry/core';
import { Attributes } from '@opentelemetry/types';
import * as express from 'express';
import * as core from "express-serve-static-core";
import * as core from 'express-serve-static-core';
import * as shimmer from 'shimmer';
import {
ExpressLayer,
Expand All @@ -26,13 +26,9 @@ import {
PatchedRequest,
Parameters,
PathParams,
_MIDDLEWARES_STORE_PROPERTY
_MIDDLEWARES_STORE_PROPERTY,
} from './types';
import {
getLayerMetadata,
storeLayerPath,
patchEnd,
} from './utils'
import { getLayerMetadata, storeLayerPath, patchEnd } from './utils';
import { VERSION } from './version';

/**
Expand All @@ -59,7 +55,8 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
if (this._moduleExports === undefined || this._moduleExports === null) {
return this._moduleExports;
}
const routerProto = (this._moduleExports.Router as unknown) as express.Router;
const routerProto = (this._moduleExports
.Router as unknown) as express.Router;

this._logger.debug('patching express.Router.prototype.route');
shimmer.wrap(routerProto, 'route', this._getRoutePatch.bind(this));
Expand All @@ -68,52 +65,67 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
shimmer.wrap(routerProto, 'use', this._getRouterUsePatch.bind(this));

this._logger.debug('patching express.Application.use');
shimmer.wrap(this._moduleExports.application, 'use', this._getAppUsePatch.bind(this));
shimmer.wrap(
this._moduleExports.application,
'use',
this._getAppUsePatch.bind(this)
);

return this._moduleExports;
}

/**
* Get the patch for Router.route function
* @param original
* @param original
*/
private _getRoutePatch (original: (path: PathParams) => express.IRoute) {
const plugin = this
private _getRoutePatch(original: (path: PathParams) => express.IRoute) {
const plugin = this;
return function route_trace(
this: ExpressRouter,
...args: Parameters<typeof original>
) {
const route = original.apply(this, args);
const layer = this.stack[this.stack.length - 1] as ExpressLayer;
plugin._applyPatch(layer, typeof args[0] === 'string' ? args[0] : undefined);
plugin._applyPatch(
layer,
typeof args[0] === 'string' ? args[0] : undefined
);
return route;
};
}

/**
* Get the patch for Router.use function
* @param original
* @param original
*/
private _getRouterUsePatch (original: express.IRouterHandler<express.Router> & express.IRouterMatcher<express.Router>) {
const plugin = this
private _getRouterUsePatch(
original: express.IRouterHandler<express.Router> &
express.IRouterMatcher<express.Router>
) {
const plugin = this;
return function use(
this: express.Application,
...args: Parameters<typeof original>
) {
const route = original.apply(this, args);
const layer = this.stack[this.stack.length - 1] as ExpressLayer;
plugin._applyPatch(layer, typeof args[0] === 'string' ? args[0] : undefined);
plugin._applyPatch(
layer,
typeof args[0] === 'string' ? args[0] : undefined
);
return route;
// tslint:disable-next-line:no-any
} as any
} as any;
}

/**
* Get the patch for Application.use function
* @param original
* @param original
*/
private _getAppUsePatch (original: core.ApplicationRequestHandler<express.Application>) {
const plugin = this
private _getAppUsePatch(
original: core.ApplicationRequestHandler<express.Application>
) {
const plugin = this;
return function use(
this: { _router: ExpressRouter },
...args: Parameters<typeof original>
Expand Down Expand Up @@ -157,8 +169,8 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
[AttributeNames.COMPONENT]: plugin._COMPONENT,
[AttributeNames.HTTP_ROUTE]: route.length > 0 ? route : undefined,
};
const metadata = getLayerMetadata(layer, layerPath)
const metadata = getLayerMetadata(layer, layerPath);

const span = plugin._tracer.startSpan(metadata.name, {
parent: plugin._tracer.getCurrentSpan(),
attributes: Object.assign(attributes, metadata.attributes),
Expand Down
8 changes: 5 additions & 3 deletions packages/opentelemetry-plugin-express/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import { kLayerPatched } from './express';
import { Request } from 'express';

export const _MIDDLEWARES_STORE_PROPERTY = '__ot_middlewares'
export const _MIDDLEWARES_STORE_PROPERTY = '__ot_middlewares';

export type Parameters<T> = T extends (...args: infer T) => any ? T : unknown[];
export type PatchedRequest = { [_MIDDLEWARES_STORE_PROPERTY]?: string[] } & Request;
export type PatchedRequest = {
[_MIDDLEWARES_STORE_PROPERTY]?: string[];
} & Request;
export type PathParams = string | RegExp | Array<string | RegExp>;

// https://github.com/expressjs/express/blob/master/lib/router/index.js#L53
Expand Down Expand Up @@ -54,5 +56,5 @@ export enum AttributeNames {
export enum ExpressLayerType {
ROUTER = 'router',
MIDDLEWARE = 'middleware',
REQUEST_HANDLER = 'request_handler'
REQUEST_HANDLER = 'request_handler',
}
35 changes: 19 additions & 16 deletions packages/opentelemetry-plugin-express/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
AttributeNames,
PatchedRequest,
_MIDDLEWARES_STORE_PROPERTY,
ExpressLayerType
ExpressLayerType,
} from './types';

/**
Expand All @@ -37,42 +37,45 @@ export const storeLayerPath = (request: PatchedRequest, value?: string) => {
}
if (value === undefined) return;
(request[_MIDDLEWARES_STORE_PROPERTY] as string[]).push(value);
}
};

/**
* Parse express layer context to retrieve a name and attributes.
* @param layer Express layer
* @param [layerPath] if present, the path on which the layer has been mounted
*/
export const getLayerMetadata = (layer: ExpressLayer, layerPath?: string): {
attributes: Attributes,
name: string
export const getLayerMetadata = (
layer: ExpressLayer,
layerPath?: string
): {
attributes: Attributes;
name: string;
} => {
if (layer.name === 'router') {
return {
attributes: {
[AttributeNames.EXPRESS_NAME]: layerPath,
[AttributeNames.EXPRESS_TYPE]: ExpressLayerType.ROUTER
[AttributeNames.EXPRESS_TYPE]: ExpressLayerType.ROUTER,
},
name: `router - ${layerPath}`
}
name: `router - ${layerPath}`,
};
} else if (layer.name === 'bound dispatch') {
return {
attributes: {
[AttributeNames.EXPRESS_TYPE]: ExpressLayerType.REQUEST_HANDLER
[AttributeNames.EXPRESS_TYPE]: ExpressLayerType.REQUEST_HANDLER,
},
name: 'request handler'
}
name: 'request handler',
};
} else {
return {
attributes: {
[AttributeNames.EXPRESS_NAME]: layer.name,
[AttributeNames.EXPRESS_TYPE]: ExpressLayerType.MIDDLEWARE
[AttributeNames.EXPRESS_TYPE]: ExpressLayerType.MIDDLEWARE,
},
name: `middleware - ${layer.name}`
}
name: `middleware - ${layer.name}`,
};
}
}
};

/**
* Ends a created span.
Expand All @@ -95,4 +98,4 @@ export const patchEnd = (span: Span, resultHandler: Function): Function => {
span.end();
return resultHandler.apply(this, args);
};
}
};

0 comments on commit b020813

Please sign in to comment.