-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
881 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/opentelemetry-instrumentation-koa-compose/.eslintignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
2 changes: 2 additions & 0 deletions
2
packages/opentelemetry-instrumentation-koa-compose/.eslintrc.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
packages/opentelemetry-instrumentation-koa-compose/LICENSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
packages/opentelemetry-instrumentation-koa-compose/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
packages/opentelemetry-instrumentation-koa-compose/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/opentelemetry-instrumentation-koa-compose/src/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
packages/opentelemetry-instrumentation-koa-compose/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { KoaComposeInstrumentation } from './instrumentation'; | ||
export { KoaComposeInstrumentationConfig } from './types'; |
74 changes: 74 additions & 0 deletions
74
packages/opentelemetry-instrumentation-koa-compose/src/instrumentation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/opentelemetry-instrumentation-koa-compose/src/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
packages/opentelemetry-instrumentation-koa-compose/src/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/opentelemetry-instrumentation-koa-compose/src/version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
packages/opentelemetry-instrumentation-koa-compose/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |