Skip to content

Commit

Permalink
feat: added kLayerPatched check
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinee21 committed Jul 10, 2020
1 parent 0200d23 commit 9b93428
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-plugin-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "OpenTelemetry Koa automatic instrumentation package.",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js",
"repository": "open-telemetry/opentelemetry-js-contrib",
"scripts": {
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.ts'",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
Expand Down
23 changes: 16 additions & 7 deletions plugins/node/opentelemetry-plugin-koa/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
* limitations under the License.
*/

import { BasePlugin } from '@opentelemetry/core';
import { BasePlugin, hrTime } from '@opentelemetry/core';
import * as koa from 'koa';
import * as shimmer from 'shimmer';
import { Parameters, KoaMiddleware, KoaContext, KoaComponentName } from './types';
import { VERSION } from './version';
import { getMiddlewareMetadata } from './utils';
// import { Layer } from '@koa/router';
import Router = require('@koa/router');


/**
* This symbol is used to mark a Koa layer as being already instrumented
* since its possible to use a given layer multiple times (ex: middlewares)
*/
export const kLayerPatched: unique symbol = Symbol('koa-layer-patched');


/** Koa instrumentation plugin for OpenTelemetry */
export class KoaPlugin extends BasePlugin<typeof koa> {
static readonly component = KoaComponentName;
Expand Down Expand Up @@ -86,8 +92,11 @@ export class KoaPlugin extends BasePlugin<typeof koa> {

}

private _patchLayer (middlewareLayer: KoaMiddleware, isRouter: boolean, layerPath?: string) {
private _patchLayer (middlewareLayer: KoaMiddleware, isRouter: boolean, layerPath?: string) : KoaMiddleware {
const plugin = this;
if (middlewareLayer[kLayerPatched] === true) return middlewareLayer;
middlewareLayer[kLayerPatched] = true;

const patchedLayer = (context: KoaContext, next: koa.Next) => {
if (plugin._tracer.getCurrentSpan() === undefined) {

Expand All @@ -97,9 +106,10 @@ export class KoaPlugin extends BasePlugin<typeof koa> {
const span = plugin._tracer.startSpan(metadata.name, {
attributes: metadata.attributes,
});
const startTime = hrTime();

var result = middlewareLayer(context, next);
span.end();
span.end(startTime);
return result;
}
return patchedLayer;
Expand All @@ -118,9 +128,8 @@ export class KoaPlugin extends BasePlugin<typeof koa> {
var path = pathLayer.path;
var pathStack = pathLayer.stack;
for (var j = 0; j < pathStack.length; j++) {
var pop = pathStack[j];
var newpop = plugin._patchLayer(pop, true, path);
pathStack[j] = newpop;
var routedMiddleware : KoaMiddleware = pathStack[j];
pathStack[j] = plugin._patchLayer(routedMiddleware, true, path);;
}
}

Expand Down
11 changes: 2 additions & 9 deletions plugins/node/opentelemetry-plugin-koa/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@
*/
import { Middleware, ParameterizedContext} from 'koa';
import { RouterParamContext} from '@koa/router';
import { kLayerPatched } from './koa';

export type Parameters<T> = T extends (...args: infer T) => any ? T : unknown[];

export type KoaMiddleware = Middleware<any, RouterParamContext>;
export type KoaMiddleware = Middleware<any, RouterParamContext> & {[kLayerPatched]?: boolean;};

// export type KoaContext = ParameterizedContext<ParameterizedContext<any, DefaultContext>, DefaultContext>;
export type KoaContext = ParameterizedContext<ParameterizedContext<any, any>, any>;

export type KoaLayer = {
handle: Function;
name: string;
params: { [key: string]: string };
path: string;
regexp: RegExp;
};

export enum AttributeNames {
COMPONENT = 'component',
HTTP_ROUTE = 'http.route',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const httpRequest = {
};


describe('Koa Plugin', () => {
describe('Koa Plugin - Router Tests', () => {
const logger = new NoopLogger();
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-plugin-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const httpRequest = {
};


describe('Koa Plugin', () => {
describe('Koa Plugin - Core Tests', () => {
const logger = new NoopLogger();
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
Expand Down

0 comments on commit 9b93428

Please sign in to comment.