Skip to content

Commit 3101050

Browse files
committed
feat: added tracing for koa-router functions and routes
1 parent 69a4e04 commit 3101050

2 files changed

Lines changed: 92 additions & 11 deletions

File tree

plugins/node/opentelemetry-plugin-koa/src/koa.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import * as shimmer from 'shimmer';
2020
import { Parameters, KoaMiddleware, KoaContext, KoaComponentName } from './types';
2121
import { VERSION } from './version';
2222
import { getMiddlewareMetadata } from './utils';
23+
// import { Layer } from '@koa/router';
24+
import Router = require('@koa/router');
2325

2426

2527
/** Koa instrumentation plugin for OpenTelemetry */
@@ -63,38 +65,77 @@ export class KoaPlugin extends BasePlugin<typeof koa> {
6365
middlewareFunction : KoaMiddleware,
6466
...args: Parameters<typeof original>
6567
) {
66-
67-
var oldMiddleware = middlewareFunction;
68-
var patchedFunction = plugin._patchLayer(oldMiddleware);
68+
console.log("mw name: " + middlewareFunction.name);
69+
var isRouterDispatch = middlewareFunction.name == "dispatch";
70+
isRouterDispatch = (middlewareFunction as any).router;
71+
var patchedFunction;
72+
if (isRouterDispatch) {
73+
patchedFunction = plugin._patchRoutes(middlewareFunction);
74+
} else {
75+
patchedFunction = plugin._patchLayer(middlewareFunction, false);
76+
}
6977
const route = original.apply(this, [patchedFunction]);
70-
// var lastLayer = route.middleware[route.middleware.length - 1];
7178

7279
return route;
80+
7381
// tslint:disable-next-line:no-any
7482
} as any;
7583

7684
}
7785

78-
private _patchLayer (middlewareLayer: KoaMiddleware) {
86+
private _patchLayer (middlewareLayer: KoaMiddleware, isRouter: boolean, layerName?: string) {
7987
const plugin = this;
88+
console.log(".....setting up span patch for: " + layerName ?? middlewareLayer.name);
8089
const patchedLayer = (context: KoaContext, next: koa.Next) => {
8190
const currentSpan = this._tracer.getCurrentSpan();
8291
if (!currentSpan) {
8392
console.log('--- No current span');
8493
}
8594

8695
const metadata = getMiddlewareMetadata(context);
87-
const span = plugin._tracer.startSpan(metadata.name, {
96+
var spanName = layerName ?? middlewareLayer.name;
97+
if (!spanName) {
98+
spanName = metadata.name;
99+
}
100+
const span = plugin._tracer.startSpan(spanName, {
88101
attributes: metadata.attributes,
89102
});
90-
91103
var result = middlewareLayer(context, next);
92104
span.end();
93105
return result;
94-
95106
}
96107
return patchedLayer;
97108
}
109+
110+
111+
private _patchRoutes (dispatchLayer: KoaMiddleware) {
112+
const plugin = this;
113+
console.log(".....set up");
114+
115+
var smth = (dispatchLayer as any);
116+
var router = smth.router as Router;
117+
118+
var routesStack = router.stack;
119+
for (var i = 0; i < routesStack.length; i++) {
120+
var pathLayer : Router.Layer = routesStack[i];
121+
var path = pathLayer.path;
122+
var pathStack = pathLayer.stack;
123+
for (var j = 0; j < pathStack.length; j++) {
124+
var pop = pathStack[j];
125+
var newpop = plugin._patchLayer(pop, true, path);
126+
pathStack[j] = newpop;
127+
}
128+
}
129+
130+
const dispatcher = (context: KoaContext, next: koa.Next) => {
131+
console.log(".....dispatching");
132+
var result = smth(context, next);
133+
return result;
134+
}
135+
return dispatcher;
136+
137+
}
138+
98139
}
99140

100141
export const plugin = new KoaPlugin(KoaPlugin.component);

plugins/node/opentelemetry-plugin-koa/src/types.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Middleware, ParameterizedContext, DefaultContext } from 'koa';
16+
import { Middleware, ParameterizedContext} from 'koa';
17+
import { RouterParamContext} from '@koa/router';
1718

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

20-
export type KoaMiddleware = Middleware<ParameterizedContext<any, DefaultContext>>;
21+
export type KoaMiddleware = Middleware<any, RouterParamContext>;
2122

22-
export type KoaContext = ParameterizedContext<ParameterizedContext<any, DefaultContext>, DefaultContext>;
23+
// export type KoaContext = ParameterizedContext<ParameterizedContext<any, DefaultContext>, DefaultContext>;
24+
export type KoaContext = ParameterizedContext<ParameterizedContext<any, any>, any>;
25+
26+
export type KoaLayer = {
27+
handle: Function;
28+
name: string;
29+
params: { [key: string]: string };
30+
path: string;
31+
regexp: RegExp;
32+
};
2333

2434
export enum AttributeNames {
2535
COMPONENT = 'component',
@@ -35,3 +45,33 @@ export enum AttributeNames {
3545

3646
export const KoaComponentName : string = 'koa';
3747

48+
/*
49+
50+
Argument of type
51+
'Middleware<ParameterizedContext<any, RouterParamContext<any, {}>>>'
52+
is not assignable to parameter of type
53+
'Middleware<ParameterizedContext<ParameterizedContext<any, DefaultContext>, DefaultContext>>'.
54+
55+
56+
57+
Type
58+
'ParameterizedContext<ParameterizedContext<any, DefaultContext>, DefaultContext>'
59+
is not assignable to type
60+
'ParameterizedContext<any, RouterParamContext<any, {}>>'.
61+
Type 'ExtendableContext & { state: ParameterizedContext<any, DefaultContext>; } & DefaultContext' is missing the following properties from type 'RouterParamContext<any, {}>': params, router, _matchedRoute, _matchedRouteNamez
62+
63+
64+
65+
Argument of type
66+
'Middleware<ParameterizedContext<any, RouterParamContext<any, {}>>>'
67+
is not assignable to parameter of type
68+
'Middleware<ParameterizedContext<ParameterizedContext<string, DefaultContext>, DefaultContext>>'.
69+
70+
71+
Type
72+
'ParameterizedContext<ParameterizedContext<string, DefaultContext>, DefaultContext>'
73+
is not assignable to type
74+
'ParameterizedContext<any, RouterParamContext<any, {}>>'.
75+
76+
77+
*/

0 commit comments

Comments
 (0)