-
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
15 changed files
with
1,128 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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. |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
# @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'; | ||
|
||
const sdk = new NodeSDK({ | ||
instrumentations: [ | ||
KoaComposeInstrumentation({ | ||
spanLayers: ['string', /regexp/, name => name === 'function'] | ||
}) | ||
] | ||
}); | ||
|
||
sdk.start(); | ||
``` | ||
|
||
## Tests | ||
|
||
```sh | ||
npm test | ||
``` | ||
|
||
## License | ||
|
||
Licensed under MIT. |
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
{ | ||
"name": "@uphold/opentelemetry-instrumentation-koa-compose", | ||
"version": "0.0.1", | ||
"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" | ||
] | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/instrumentation": "^0.45.1" | ||
}, | ||
"peerDependencies": { | ||
"@opentelemetry/api": ">=1 <2" | ||
}, | ||
"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'; |
185 changes: 185 additions & 0 deletions
185
packages/opentelemetry-instrumentation-koa-compose/src/instrumentation.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,185 @@ | ||
import { InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
import { KoaComposeInstrumentation } from '../src'; | ||
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; | ||
import { context, trace } from '@opentelemetry/api'; | ||
|
||
const plugin = new KoaComposeInstrumentation(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const compose = require('koa-compose'); | ||
|
||
describe('KoaComposeInstrumentation', () => { | ||
const provider = new NodeTracerProvider(); | ||
const memoryExporter = new InMemorySpanExporter(); | ||
const spanProcessor = new SimpleSpanProcessor(memoryExporter); | ||
|
||
provider.addSpanProcessor(spanProcessor); | ||
plugin.setTracerProvider(provider); | ||
const tracer = provider.getTracer('default'); | ||
|
||
beforeAll(() => { | ||
plugin.enable(); | ||
}); | ||
|
||
afterAll(() => { | ||
plugin.disable(); | ||
}); | ||
|
||
beforeEach(() => { | ||
expect(memoryExporter.getFinishedSpans()).toHaveLength(0); | ||
plugin.setConfig({ enabled: true, spanLayers: [() => true] }); | ||
}); | ||
|
||
afterEach(() => { | ||
memoryExporter.reset(); | ||
context.disable(); | ||
}); | ||
|
||
async function simpleResponse(ctx, next) { | ||
ctx.body = 'test'; | ||
await next(); | ||
} | ||
|
||
async function asyncMiddleware(ctx, next) { | ||
const start = Date.now(); | ||
|
||
await next(); | ||
const ms = Date.now() - start; | ||
|
||
ctx.body = `${ctx.method} ${ctx.url} - ${ms}ms`; | ||
} | ||
|
||
function failingMiddleware() { | ||
throw new Error('Foobar'); | ||
} | ||
|
||
describe('Instrumenting koa-compose calls', () => { | ||
it('should create a child span for middlewares', async () => { | ||
const rootSpan = tracer.startSpan('rootSpan'); | ||
const composed = compose([asyncMiddleware, simpleResponse]); | ||
const ctx = {}; | ||
|
||
await context.with(trace.setSpan(context.active(), rootSpan), async () => { | ||
await composed(ctx, async () => {}); | ||
|
||
rootSpan.end(); | ||
|
||
expect(memoryExporter.getFinishedSpans()).toHaveLength(3); | ||
|
||
const composeSpan1 = memoryExporter | ||
.getFinishedSpans() | ||
.find(span => span.name.includes('compose - [1/2] asyncMiddleware')); | ||
const composeSpan2 = memoryExporter | ||
.getFinishedSpans() | ||
.find(span => span.name.includes('compose - [2/2] simpleResponse')); | ||
|
||
expect(composeSpan1).not.toBeUndefined(); | ||
expect(composeSpan2).not.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('should create a child span witha default name for anonymous middlewares', async () => { | ||
const rootSpan = tracer.startSpan('rootSpan'); | ||
const composed = compose([(_, next) => next()]); | ||
const ctx = {}; | ||
|
||
await context.with(trace.setSpan(context.active(), rootSpan), async () => { | ||
await composed(ctx, async () => {}); | ||
|
||
rootSpan.end(); | ||
|
||
expect(memoryExporter.getFinishedSpans()).toHaveLength(2); | ||
|
||
const composeSpan = memoryExporter | ||
.getFinishedSpans() | ||
.find(span => span.name.includes('compose - [1/1] unnamed')); | ||
|
||
expect(composeSpan).not.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('should create a child span only for whitelisted middlewares', async () => { | ||
plugin.setConfig({ spanLayers: ['simpleResponse'] }); | ||
|
||
const rootSpan = tracer.startSpan('rootSpan'); | ||
const composed = compose([asyncMiddleware, simpleResponse]); | ||
const ctx = {}; | ||
|
||
await context.with(trace.setSpan(context.active(), rootSpan), async () => { | ||
await composed(ctx, async () => {}); | ||
|
||
rootSpan.end(); | ||
|
||
expect(memoryExporter.getFinishedSpans()).toHaveLength(2); | ||
|
||
const composeSpan = memoryExporter | ||
.getFinishedSpans() | ||
.find(span => span.name.includes('compose - [2/2] simpleResponse')); | ||
|
||
expect(composeSpan).not.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('should record an event on the span if a middleware throws an error', async () => { | ||
const rootSpan = tracer.startSpan('rootSpan'); | ||
const composed = compose([failingMiddleware, simpleResponse]); | ||
const ctx = {}; | ||
|
||
await context.with(trace.setSpan(context.active(), rootSpan), async () => { | ||
await composed(ctx, async () => {}).catch(() => {}); | ||
|
||
rootSpan.end(); | ||
|
||
expect(memoryExporter.getFinishedSpans()).toHaveLength(2); | ||
|
||
const errorSpan = memoryExporter | ||
.getFinishedSpans() | ||
.find(span => span.name.includes('compose - [1/2] failingMiddleware')); | ||
|
||
expect(errorSpan).not.toBeUndefined(); | ||
|
||
expect(errorSpan?.events[0].name).toBe('exception'); | ||
expect(errorSpan?.events[0].attributes?.['exception.type']).toBe('Error'); | ||
expect(errorSpan?.events[0].attributes?.['exception.message']).toBe('Foobar'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Disabling koa instrumentation', () => { | ||
it('should not create new spans', async () => { | ||
plugin.disable(); | ||
const rootSpan = tracer.startSpan('rootSpan'); | ||
|
||
const composed = compose([simpleResponse]); | ||
|
||
await context.with(trace.setSpan(context.active(), rootSpan), async () => { | ||
await composed({}, async () => {}); | ||
|
||
rootSpan.end(); | ||
|
||
expect(memoryExporter.getFinishedSpans()).toHaveLength(1); | ||
expect(memoryExporter.getFinishedSpans()[0]).not.toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getConfig()', () => { | ||
it('should get the config', () => { | ||
expect(plugin.getConfig()).toEqual({ | ||
enabled: true, | ||
spanLayers: [expect.any(Function)] | ||
}); | ||
}); | ||
}); | ||
|
||
describe('setConfig()', () => { | ||
it('should update the config', () => { | ||
expect(plugin.getConfig().enabled).toBeTruthy(); | ||
|
||
plugin.setConfig({ enabled: false }); | ||
|
||
expect(plugin.getConfig().enabled).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.