Skip to content

Commit

Permalink
chore: finalizing plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinee21 committed Jul 9, 2020
1 parent 1315670 commit 0200d23
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
17 changes: 5 additions & 12 deletions plugins/node/opentelemetry-plugin-koa/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { BasePlugin } from '@opentelemetry/core';
import * as koa from 'koa';
import * as shimmer from 'shimmer';
import { Parameters, KoaMiddleware, KoaContext, KoaComponentName, AttributeNames } from './types';
import { Parameters, KoaMiddleware, KoaContext, KoaComponentName } from './types';
import { VERSION } from './version';
import { getMiddlewareMetadata } from './utils';
// import { Layer } from '@koa/router';
Expand Down Expand Up @@ -86,25 +86,18 @@ export class KoaPlugin extends BasePlugin<typeof koa> {

}

private _patchLayer (middlewareLayer: KoaMiddleware, isRouter: boolean, layerName?: string) {
private _patchLayer (middlewareLayer: KoaMiddleware, isRouter: boolean, layerPath?: string) {
const plugin = this;
const patchedLayer = (context: KoaContext, next: koa.Next) => {
if (plugin._tracer.getCurrentSpan() === undefined) {

return middlewareLayer(context, next);
}
const metadata = getMiddlewareMetadata(context);
var spanName = layerName ?? middlewareLayer.name;
if (!spanName) {
spanName = metadata.name;
}

const span = plugin._tracer.startSpan(spanName, {
const metadata = getMiddlewareMetadata(context, middlewareLayer, isRouter, layerPath);
const span = plugin._tracer.startSpan(metadata.name, {
attributes: metadata.attributes,
});
if (isRouter) {
span.setAttribute(AttributeNames.PATH, layerName);
}

var result = middlewareLayer(context, next);
span.end();
return result;
Expand Down
17 changes: 11 additions & 6 deletions plugins/node/opentelemetry-plugin-koa/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ export type KoaLayer = {

export enum AttributeNames {
COMPONENT = 'component',
ROUTE = 'http.route',
PATH = 'http.path',
STATUS = 'http.status_code',
PROTOCOL = 'http.protocol',
HOST = 'http.host',
METHOD = 'http.method',
HTTP_ROUTE = 'http.route',
// PATH = 'http.path',
// STATUS = 'http.status_code',
// PROTOCOL = 'http.protocol',
// HOST = 'http.host',
// METHOD = 'http.method',
KOA_TYPE = 'koa.type',
KOA_NAME = 'koa.name'
};

export enum KoaLayerType {
ROUTER = 'router',
MIDDLEWARE = 'middleware',
}

export const KoaComponentName : string = 'koa';

/*
Expand Down
45 changes: 33 additions & 12 deletions plugins/node/opentelemetry-plugin-koa/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AttributeNames, KoaContext, KoaComponentName } from './types';
import { AttributeNames, KoaContext, KoaComponentName, KoaMiddleware, KoaLayerType } from './types';
import { Attributes } from '@opentelemetry/api';


export const getMiddlewareMetadata = (
context: KoaContext
context: KoaContext,
layer: KoaMiddleware,
isRouter: boolean,
layerPath?: string
) : {
attributes: Attributes;
name: string;
} => {
return {
if (isRouter) {
return {
attributes: {
[AttributeNames.PATH]: context.path,
[AttributeNames.PROTOCOL]: context.protocol,
[AttributeNames.STATUS]: context.status,
[AttributeNames.HOST]: context.host,
[AttributeNames.METHOD]: context.method,
[AttributeNames.KOA_TYPE]: 'middleware',
[AttributeNames.KOA_NAME]: layerPath,
[AttributeNames.KOA_TYPE]: KoaLayerType.ROUTER,
[AttributeNames.COMPONENT]: KoaComponentName,
[AttributeNames.HTTP_ROUTE]: layerPath
},
name: `router - ${layerPath}`,
};
} else {
return {
attributes: {
[AttributeNames.KOA_NAME]: layer.name ?? 'middleware',
[AttributeNames.KOA_TYPE]: KoaLayerType.MIDDLEWARE,
[AttributeNames.COMPONENT]: KoaComponentName
},
name: 'middleware'
},
name: `middleware - ${layer.name}`,
};
}
}



// [AttributeNames.PATH]: context.path,
// [AttributeNames.PROTOCOL]: context.protocol,
// [AttributeNames.STATUS]: context.status,
// [AttributeNames.HOST]: context.host,
// [AttributeNames.METHOD]: context.method,


}
8 changes: 4 additions & 4 deletions plugins/node/opentelemetry-plugin-koa/test/koa-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import * as http from 'http';
import { AddressInfo } from 'net';
import { plugin } from '../src';
import {
AttributeNames
AttributeNames, KoaLayerType, KoaComponentName
} from '../src/types';


Expand Down Expand Up @@ -121,16 +121,16 @@ describe('Koa Plugin', () => {
assert.notStrictEqual(requestHandlerSpan, undefined);
assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.COMPONENT],
'koa'
KoaComponentName
);

assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.KOA_TYPE],
'middleware'
KoaLayerType.ROUTER
);

assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.PATH],
requestHandlerSpan?.attributes[AttributeNames.HTTP_ROUTE],
'/post/:id'
);

Expand Down
8 changes: 4 additions & 4 deletions plugins/node/opentelemetry-plugin-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as http from 'http';
import { AddressInfo } from 'net';
import { plugin } from '../src';
import {
AttributeNames
AttributeNames, KoaLayerType, KoaComponentName
} from '../src/types';


Expand Down Expand Up @@ -123,12 +123,12 @@ describe('Koa Plugin', () => {
assert.notStrictEqual(requestHandlerSpan, undefined);
assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.COMPONENT],
'koa'
KoaComponentName
);

assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.KOA_TYPE],
'middleware'
KoaLayerType.MIDDLEWARE
);
let exportedRootSpan = memoryExporter
.getFinishedSpans()
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('Koa Plugin', () => {
await tracer.withSpan(rootSpan, async () => {
await httpRequest.get(`http://localhost:${port}`);
rootSpan.end();
assert.deepEqual(memoryExporter.getFinishedSpans().length, 1);
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 1);
assert.notStrictEqual(memoryExporter.getFinishedSpans()[0], undefined);
});
server.close();
Expand Down

0 comments on commit 0200d23

Please sign in to comment.