Skip to content

Commit

Permalink
docs: add comments to plugin patching operations
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinee21 committed Jul 16, 2020
1 parent 9b7a799 commit daa8df8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
25 changes: 24 additions & 1 deletion plugins/node/opentelemetry-koa-instrumentation/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
super('@opentelemetry/koa-instrumentation', VERSION);
}

/**
* Patches Koa operations by wrapping the Koa.use function
*/
protected patch(): typeof koa {
this._logger.debug('Patching Koa');

Expand All @@ -54,6 +57,10 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {

return this._moduleExports;
}

/**
* Unpatches all Koa operations
*/
protected unpatch(): void {
const appProto = this._moduleExports.prototype;
shimmer.unwrap(appProto, 'use');
Expand All @@ -62,7 +69,7 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
/**
* Patches the Koa.use function in order to instrument each original
* middleware layer which is introduced
* @param original
* @param {KoaMiddleware} middleware - the original middleware function
*/
private _getKoaUsePatch(original: (middleware: KoaMiddleware) => koa) {
return function use(
Expand All @@ -86,6 +93,13 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
} as any;
}

/**
* Patches the dispatch function used by @koa/router. This function
* goes through each routed middleware and adds instrumentation via a call
* to the @function _patchLayer function.
* @param {KoaMiddleware} dispatchLayer - the original dispatch function which dispatches
* routed middleware
*/
private _patchRouterDispatch(dispatchLayer: KoaMiddleware) {
this._logger.debug('Patching @koa/router dispatch');

Expand All @@ -108,6 +122,15 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
return dispatcher;
}

/**
* Patches each individual @param middlewareLayer function in order to create the
* span and propagate context. It does not create spans when there is no parent span.
* @param {KoaMiddleware} middlewareLayer - the original middleware function.
* @param {boolean} isRouter - tracks whether the original middleware function
* was dispatched by the router originally
* @param {string?} layerPath - if present, provides additional data from the
* router about the routed path which the middleware is attached to
*/
private _patchLayer(
middlewareLayer: KoaMiddleware,
isRouter: boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ describe('Koa Instrumentation - Core Tests', () => {
app.use(customMiddleware);
app.use(simpleResponse);


const server = http.createServer(app.callback());
await new Promise(resolve => server.listen(0, resolve));
const port = (server.address() as AddressInfo).port;
Expand Down

0 comments on commit daa8df8

Please sign in to comment.