Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Americas committed Nov 15, 2023
1 parent ce71aee commit ef0c078
Show file tree
Hide file tree
Showing 14 changed files with 881 additions and 1 deletion.
630 changes: 629 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"devDependencies": {
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/core": "^1.17.1",
"@opentelemetry/instrumentation": "^0.45.1",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"@uphold/github-changelog-generator": "^3.4.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules:
no-underscore-dangle: 0
21 changes: 21 additions & 0 deletions packages/opentelemetry-instrumentation-koa-compose/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Uphold

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions packages/opentelemetry-instrumentation-koa-compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# @uphold/opentelemetry-instrumentation-koa-compose

Package that instruments koa-compose.

## Installation

```sh
npm install @uphold/opentelemetry-instrumentation-koa-compose
```

## Why?

When instrumenting koa, if koa-context is used, it can hide and obfuscate some middleware. This package allows these composed middlewares to be spanned.

## Usage

Add `KoaComposeInstrumentation` to the list of instrumentations. Here's how it looks if you are using the NodeSDK:

```js
import { KoaComposeInstrumentation } from '@uphold/opentelemetry-instrumentation-koa-compose';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { ProxyTracerProvider, trace } from '@opentelemetry/api';

const sdk = new NodeSDK({});

sdk.start();

const proxyTracerProvider = trace.getTracerProvider() as ProxyTracerProvider;
const tracerProvider = proxyTracerProvider.getDelegate() as NodeTracerProvider;

tracerProvider.addSpanProcessor(new KoaComposeInstrumentation());
```

## Tests

```sh
npm test
```

## License

Licensed under MIT.
49 changes: 49 additions & 0 deletions packages/opentelemetry-instrumentation-koa-compose/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@uphold/opentelemetry-instrumentation-koa-compose",
"version": "0.0.2",
"description": "Package that instruments koa-compose.",
"main": "dist/index.js",
"files": [
"dist"
],
"author": "Uphold",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/uphold/opentelemetry-js-contrib.git"
},
"keywords": [
"opentelemetry",
"instrumentation",
"nodejs",
"tracing",
"koa-compose",
"span"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=18"
},
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc",
"lint": "eslint --ext .ts,.js .",
"postlint": "tsc --noEmit",
"release": "release-it",
"test": "vitest"
},
"lint-staged": {
"*.{ts,js}": [
"eslint --ext .ts,.js"
]
},
"devDependencies": {
"@opentelemetry/instrumentation": "^0.45.1",
"@types/koa": "^2.13.11",
"@types/koa-compose": "^3.2.8",
"koa": "^2.14.2",
"koa-compose": "^4.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as exports from '.';
import { expect, it } from 'vitest';

it('should have the correct exports', () => {
expect({ ...exports }).toEqual({
KoaComposeInstrumentation: expect.any(Function)
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { KoaComposeInstrumentation } from './instrumentation';
export { KoaComposeInstrumentationConfig } from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as api from '@opentelemetry/api';
import type * as koaCompose from 'koa-compose';
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
import { KoaComposeInstrumentationConfig } from './types';
import type { Middleware } from 'koa';
import { VERSION } from './version';
import { isLayerIgnored } from './utils';

export class KoaComposeInstrumentation extends InstrumentationBase<typeof koaCompose> {
private originalCompose?: typeof koaCompose;

constructor(config: KoaComposeInstrumentationConfig = {}) {
super('@uphold/opentelemetry-instrumentation-koa-compose', VERSION, Object.assign({}, config));
}

override setConfig(config: KoaComposeInstrumentationConfig = {}) {
this._config = Object.assign({}, config);
}

override getConfig(): KoaComposeInstrumentationConfig {
return this._config as KoaComposeInstrumentationConfig;
}

protected init() {
return new InstrumentationNodeModuleDefinition<typeof koaCompose>(
'koa-compose',
['^4.0.0'],
moduleExports => {
if (!moduleExports) {
return moduleExports;
}

api.diag.debug('Patching koa-compose');

this.originalCompose = moduleExports;

moduleExports = (middlewares: Middleware[]) => {
middlewares = middlewares.map((middleware, index) => {
if (isLayerIgnored(middleware.name, this._config)) {
return middleware;
}

return (context, next) => {
const span = this.tracer.startSpan(`compose - [${index}] ${middleware.name || 'unnamed'}`);
const newContext = api.trace.setSpan(api.context.active(), span);

return api.context.with(newContext, async () => {
try {
return await middleware(context, next);
} catch (err: unknown) {
span.recordException(err as api.Exception);
throw err;
} finally {
span.end();
}
});
};
});

return this.originalCompose!(middlewares);
};

return moduleExports;
},
moduleExports => {
api.diag.debug('Unpatching koa-compose');

moduleExports = this.originalCompose!;

return moduleExports;
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { InstrumentationConfig } from '@opentelemetry/instrumentation';

export type NameMatcher = string | RegExp | ((name: string) => boolean);

export interface KoaComposeInstrumentationConfig extends InstrumentationConfig {
spanLayers?: NameMatcher[];
}
30 changes: 30 additions & 0 deletions packages/opentelemetry-instrumentation-koa-compose/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { KoaComposeInstrumentationConfig, NameMatcher } from './types';

const satisfiesPattern = (constant: string, pattern: NameMatcher): boolean => {
if (typeof pattern === 'string') {
return pattern === constant;
} else if (pattern instanceof RegExp) {
return pattern.test(constant);
} else if (typeof pattern === 'function') {
return pattern(constant);
}
throw new TypeError('Pattern is in unsupported datatype');
};

export const isLayerIgnored = (name: string, config?: KoaComposeInstrumentationConfig): boolean => {
if (!Array.isArray(config?.spanLayers)) {
return true;
}

try {
for (const pattern of config!.spanLayers) {
if (satisfiesPattern(name, pattern)) {
return false;
}
}
} catch (e) {
// No-op.
}

return true;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERSION = '0.0.0';
14 changes: 14 additions & 0 deletions packages/opentelemetry-instrumentation-koa-compose/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"include": [
"src/**/*.ts",
],
"exclude": [
"node_modules",
"src/**/*.test.ts"
]
}

0 comments on commit ef0c078

Please sign in to comment.