diff --git a/plugins/node/opentelemetry-plugin-koa/src/koa.ts b/plugins/node/opentelemetry-plugin-koa/src/koa.ts index 67204a6c49..3490e7ad1b 100644 --- a/plugins/node/opentelemetry-plugin-koa/src/koa.ts +++ b/plugins/node/opentelemetry-plugin-koa/src/koa.ts @@ -17,13 +17,14 @@ import { BasePlugin } from '@opentelemetry/core'; import * as koa from 'koa'; import * as shimmer from 'shimmer'; -import { Parameters, KoaMiddleware, KoaContext, AttributeNames } from './types'; +import { Parameters, KoaMiddleware, KoaContext, KoaComponentName } from './types'; import { VERSION } from './version'; +import { getMiddlewareMetadata } from './utils'; /** Koa instrumentation plugin for OpenTelemetry */ export class KoaPlugin extends BasePlugin { - static readonly component = 'koa'; + static readonly component = KoaComponentName; constructor(readonly moduleName: string) { super('@opentelemetry/plugin-koa', VERSION); @@ -75,24 +76,20 @@ export class KoaPlugin extends BasePlugin { } private _patchLayer (middlewareLayer: KoaMiddleware) { + const plugin = this; const patchedLayer = (context: KoaContext, next: koa.Next) => { const currentSpan = this._tracer.getCurrentSpan(); if (!currentSpan) { console.log('--- No current span'); } - var mwSpan = this._tracer.startSpan('middleware'); + const metadata = getMiddlewareMetadata(context); + const span = plugin._tracer.startSpan(metadata.name, { + attributes: metadata.attributes, + }); - mwSpan.setAttribute(AttributeNames.COMPONENT, KoaPlugin.component); - mwSpan.setAttribute(AttributeNames.PATH, context.path); - mwSpan.setAttribute(AttributeNames.PROTOCOL, context.protocol); - mwSpan.setAttribute(AttributeNames.STATUS, context.status); - mwSpan.setAttribute(AttributeNames.HOST, context.host); - mwSpan.setAttribute(AttributeNames.METHOD, context.method); - mwSpan.setAttribute(AttributeNames.KOA_TYPE, 'middleware'); - var result = middlewareLayer(context, next); - mwSpan.end(); + span.end(); return result; } diff --git a/plugins/node/opentelemetry-plugin-koa/src/types.ts b/plugins/node/opentelemetry-plugin-koa/src/types.ts index af84fc4e51..30286e20e4 100644 --- a/plugins/node/opentelemetry-plugin-koa/src/types.ts +++ b/plugins/node/opentelemetry-plugin-koa/src/types.ts @@ -31,5 +31,7 @@ export enum AttributeNames { METHOD = 'http.method', KOA_TYPE = 'koa.type', KOA_NAME = 'koa.name' -} +}; + +export const KoaComponentName : string = 'koa'; diff --git a/plugins/node/opentelemetry-plugin-koa/src/utils.ts b/plugins/node/opentelemetry-plugin-koa/src/utils.ts new file mode 100644 index 0000000000..e815c49f1e --- /dev/null +++ b/plugins/node/opentelemetry-plugin-koa/src/utils.ts @@ -0,0 +1,38 @@ +/*! + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { AttributeNames, KoaContext, KoaComponentName } from './types'; +import { Attributes } from '@opentelemetry/api'; + + +export const getMiddlewareMetadata = ( + context: KoaContext +) : { + attributes: Attributes; + name: string; +} => { + 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.COMPONENT]: KoaComponentName + }, + name: 'middleware' + } +} \ No newline at end of file