Skip to content

Commit

Permalink
chore: add documentation about express layer store
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jan 26, 2020
1 parent f92d939 commit b0bfb7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/opentelemetry-plugin-express/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
PatchedRequest,
Parameters,
PathParams,
_MIDDLEWARES_STORE_PROPERTY,
_LAYERS_STORE_PROPERTY,
ExpressPluginConfig,
ExpressLayerType,
} from './types';
Expand Down Expand Up @@ -174,7 +174,7 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
next: express.NextFunction
) {
storeLayerPath(req, layerPath);
const route = (req[_MIDDLEWARES_STORE_PROPERTY] as string[]).join('');
const route = (req[_LAYERS_STORE_PROPERTY] as string[]).join('');
const attributes: Attributes = {
[AttributeNames.COMPONENT]: plugin._COMPONENT,
[AttributeNames.HTTP_ROUTE]: route.length > 0 ? route : undefined,
Expand All @@ -200,7 +200,7 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
arguments[callbackIdx] = function() {
callbackHasBeenCalled = true;
if (!(req.route && arguments[0] instanceof Error)) {
(req[_MIDDLEWARES_STORE_PROPERTY] as string[]).pop();
(req[_LAYERS_STORE_PROPERTY] as string[]).pop();
}
return patchEnd(span, plugin._tracer.bind(next))();
};
Expand Down
20 changes: 18 additions & 2 deletions packages/opentelemetry-plugin-express/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ import { kLayerPatched } from './express';
import { Request } from 'express';
import { PluginConfig, Attributes } from '@opentelemetry/types';

export const _MIDDLEWARES_STORE_PROPERTY = '__ot_middlewares';
/**
* This const define where on the `request` object the plugin will mount the
* current stack of express layer.
*
* It is necessary because express doesnt store the different layers
* (ie: middleware, router etc) that it called to get to the current layer.
* Given that, the only way to know the route of a given layer is to
* store the path of where each previous layer has been mounted.
*
* ex: bodyParser > auth middleware > /users router > get /:id
* in this case the stack would be: ["/users", "/:id"]
*
* ex2: bodyParser > /api router > /v1 router > /users router > get /:id
* stack: ["/api", "/v1", "/users", ":id"]
*
*/
export const _LAYERS_STORE_PROPERTY = '__ot_middlewares';

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

Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-plugin-express/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ExpressLayer,
AttributeNames,
PatchedRequest,
_MIDDLEWARES_STORE_PROPERTY,
_LAYERS_STORE_PROPERTY,
ExpressLayerType,
IgnoreMatcher,
ExpressPluginConfig,
Expand All @@ -31,14 +31,14 @@ import {
* @param [value] the value to push into the array
*/
export const storeLayerPath = (request: PatchedRequest, value?: string) => {
if (Array.isArray(request[_MIDDLEWARES_STORE_PROPERTY]) === false) {
Object.defineProperty(request, _MIDDLEWARES_STORE_PROPERTY, {
if (Array.isArray(request[_LAYERS_STORE_PROPERTY]) === false) {
Object.defineProperty(request, _LAYERS_STORE_PROPERTY, {
enumerable: false,
value: [],
});
}
if (value === undefined) return;
(request[_MIDDLEWARES_STORE_PROPERTY] as string[]).push(value);
(request[_LAYERS_STORE_PROPERTY] as string[]).push(value);
};

/**
Expand Down

0 comments on commit b0bfb7e

Please sign in to comment.