From b0208137d4aef6fc7ca9d4547dc75e87a21f5c64 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sun, 19 Jan 2020 12:02:28 +0100 Subject: [PATCH] chore: lint files --- .../src/express.ts | 58 +++++++++++-------- .../opentelemetry-plugin-express/src/types.ts | 8 ++- .../opentelemetry-plugin-express/src/utils.ts | 35 ++++++----- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/packages/opentelemetry-plugin-express/src/express.ts b/packages/opentelemetry-plugin-express/src/express.ts index a1c130bc4d8..706c3a28ae5 100644 --- a/packages/opentelemetry-plugin-express/src/express.ts +++ b/packages/opentelemetry-plugin-express/src/express.ts @@ -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, @@ -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'; /** @@ -59,7 +55,8 @@ export class ExpressPlugin extends BasePlugin { 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)); @@ -68,52 +65,67 @@ export class ExpressPlugin extends BasePlugin { 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 ) { 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.IRouterMatcher) { - const plugin = this + private _getRouterUsePatch( + original: express.IRouterHandler & + express.IRouterMatcher + ) { + const plugin = this; return function use( this: express.Application, ...args: Parameters ) { 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) { - const plugin = this + private _getAppUsePatch( + original: core.ApplicationRequestHandler + ) { + const plugin = this; return function use( this: { _router: ExpressRouter }, ...args: Parameters @@ -157,8 +169,8 @@ export class ExpressPlugin extends BasePlugin { [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), diff --git a/packages/opentelemetry-plugin-express/src/types.ts b/packages/opentelemetry-plugin-express/src/types.ts index 8644e45f764..b152a237c25 100644 --- a/packages/opentelemetry-plugin-express/src/types.ts +++ b/packages/opentelemetry-plugin-express/src/types.ts @@ -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 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; // https://github.com/expressjs/express/blob/master/lib/router/index.js#L53 @@ -54,5 +56,5 @@ export enum AttributeNames { export enum ExpressLayerType { ROUTER = 'router', MIDDLEWARE = 'middleware', - REQUEST_HANDLER = 'request_handler' + REQUEST_HANDLER = 'request_handler', } diff --git a/packages/opentelemetry-plugin-express/src/utils.ts b/packages/opentelemetry-plugin-express/src/utils.ts index 589bd0a8d3e..0229964bc24 100644 --- a/packages/opentelemetry-plugin-express/src/utils.ts +++ b/packages/opentelemetry-plugin-express/src/utils.ts @@ -20,7 +20,7 @@ import { AttributeNames, PatchedRequest, _MIDDLEWARES_STORE_PROPERTY, - ExpressLayerType + ExpressLayerType, } from './types'; /** @@ -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. @@ -95,4 +98,4 @@ export const patchEnd = (span: Span, resultHandler: Function): Function => { span.end(); return resultHandler.apply(this, args); }; -} \ No newline at end of file +};