Skip to content

Commit

Permalink
chore: refactor span attribute assignments into util
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinee21 committed Jul 6, 2020
1 parent aec43b1 commit c681bd0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
21 changes: 9 additions & 12 deletions plugins/node/opentelemetry-plugin-koa/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof koa> {
static readonly component = 'koa';
static readonly component = KoaComponentName;

constructor(readonly moduleName: string) {
super('@opentelemetry/plugin-koa', VERSION);
Expand Down Expand Up @@ -75,24 +76,20 @@ export class KoaPlugin extends BasePlugin<typeof koa> {
}

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;

}
Expand Down
4 changes: 3 additions & 1 deletion plugins/node/opentelemetry-plugin-koa/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ export enum AttributeNames {
METHOD = 'http.method',
KOA_TYPE = 'koa.type',
KOA_NAME = 'koa.name'
}
};

export const KoaComponentName : string = 'koa';

38 changes: 38 additions & 0 deletions plugins/node/opentelemetry-plugin-koa/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
}
}

0 comments on commit c681bd0

Please sign in to comment.