Skip to content

Commit d9d558f

Browse files
fix(koa)!: use generic config hook types and move dep to dev (#2303)
* fix!(instrumentation-koa): use feneric config hook types and move dep to dev * chore: lint fix --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 16bff40 commit d9d558f

File tree

8 files changed

+143
-61
lines changed

8 files changed

+143
-61
lines changed

package-lock.json

Lines changed: 67 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-koa/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ Note that generator-based middleware are deprecated and won't be instrumented.
6565

6666
Instrumentation configuration accepts a custom "hook" function which will be called for every instrumented Koa middleware layer involved in a request. Custom attributes can be set on the span or run any custom logic per layer.
6767

68-
```javascript
68+
NOTE: `KoaRequestInfo.context` and `KoaRequestInfo.middlewareLayer` are typed as `any`. If you want type support make sure you have `@types/koa` and `@types/koa__router` installed then you can use the following type definitions:
69+
70+
```typescript
6971
import { KoaInstrumentation } from "@opentelemetry/instrumentation-koa"
72+
import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
73+
import type { RouterParamContext } from '@koa/router';
74+
75+
type KoaContext = ParameterizedContext<DefaultState, RouterParamContext>;
76+
type KoaMiddleware = Middleware<DefaultState, KoaContext>;
7077

7178
const koaInstrumentation = new KoaInstrumentation({
72-
requestHook: function (span: Span, info: KoaRequestInfo) {
79+
requestHook: function (span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) {
7380
span.setAttribute(
7481
'http.method',
7582
info.context.request.method

plugins/node/opentelemetry-instrumentation-koa/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"@opentelemetry/instrumentation-http": "^0.52.0",
5454
"@opentelemetry/sdk-trace-base": "^1.8.0",
5555
"@opentelemetry/sdk-trace-node": "^1.8.0",
56+
"@types/koa": "2.15.0",
57+
"@types/koa__router": "12.0.4",
5658
"@types/mocha": "7.0.2",
5759
"@types/node": "18.6.5",
5860
"@types/sinon": "10.0.18",
@@ -68,9 +70,7 @@
6870
"dependencies": {
6971
"@opentelemetry/core": "^1.8.0",
7072
"@opentelemetry/instrumentation": "^0.52.0",
71-
"@opentelemetry/semantic-conventions": "^1.22.0",
72-
"@types/koa": "2.14.0",
73-
"@types/koa__router": "12.0.3"
73+
"@opentelemetry/semantic-conventions": "^1.22.0"
7474
},
7575
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-koa#readme"
7676
}

plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import {
2323
} from '@opentelemetry/instrumentation';
2424

2525
import type * as koa from 'koa';
26-
import { KoaContext, KoaLayerType, KoaInstrumentationConfig } from './types';
26+
import { KoaLayerType, KoaInstrumentationConfig } from './types';
2727
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
2828
import { getMiddlewareMetadata, isLayerIgnored } from './utils';
2929
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
3030
import {
3131
kLayerPatched,
32+
KoaContext,
3233
KoaMiddleware,
3334
KoaPatchedMiddleware,
3435
} from './internal-types';

plugins/node/opentelemetry-instrumentation-koa/src/internal-types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { Middleware, DefaultState } from 'koa';
17-
import { KoaContext } from './types';
16+
import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
1817
import type * as Router from '@koa/router';
1918

19+
export type KoaContext = ParameterizedContext<
20+
DefaultState,
21+
Router.RouterParamContext
22+
>;
2023
export type KoaMiddleware = Middleware<DefaultState, KoaContext> & {
2124
router?: Router;
2225
};

plugins/node/opentelemetry-instrumentation-koa/src/types.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
17-
import type { RouterParamContext } from '@koa/router';
1816
import { Span } from '@opentelemetry/api';
1917
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
2018

@@ -23,11 +21,30 @@ export enum KoaLayerType {
2321
MIDDLEWARE = 'middleware',
2422
}
2523

26-
export type KoaContext = ParameterizedContext<DefaultState, RouterParamContext>;
27-
28-
export type KoaRequestInfo = {
29-
context: KoaContext;
30-
middlewareLayer: Middleware<DefaultState, KoaContext>;
24+
/**
25+
* Information about the current Koa middleware layer
26+
* The middleware layer type is any by default.
27+
* One can install koa types packages `@types/koa` and `@types/koa__router`
28+
* with compatible versions to the koa version used in the project
29+
* to get more specific types for the middleware layer property.
30+
*
31+
* Example use in a custom attribute function:
32+
* ```ts
33+
* import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
34+
* import type { RouterParamContext } from '@koa/router';
35+
*
36+
* type KoaContext = ParameterizedContext<DefaultState, RouterParamContext>;
37+
* type KoaMiddleware = Middleware<DefaultState, KoaContext>;
38+
*
39+
* const koaConfig: KoaInstrumentationConfig<KoaContext, KoaMiddleware> = {
40+
* requestHook: (span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
41+
* // custom typescript code that can access the typed into.middlewareLayer and info.context
42+
* }
43+
*
44+
*/
45+
export type KoaRequestInfo<KoaContextType = any, KoaMiddlewareType = any> = {
46+
context: KoaContextType;
47+
middlewareLayer: KoaMiddlewareType;
3148
layerType: KoaLayerType;
3249
};
3350

@@ -36,16 +53,25 @@ export type KoaRequestInfo = {
3653
* @param span - The Express middleware layer span.
3754
* @param context - The current KoaContext.
3855
*/
39-
export interface KoaRequestCustomAttributeFunction {
40-
(span: Span, info: KoaRequestInfo): void;
56+
export interface KoaRequestCustomAttributeFunction<
57+
KoaContextType = any,
58+
KoaMiddlewareType = any
59+
> {
60+
(span: Span, info: KoaRequestInfo<KoaContextType, KoaMiddlewareType>): void;
4161
}
4262

4363
/**
4464
* Options available for the Koa Instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-Instrumentation-koa#koa-Instrumentation-options))
4565
*/
46-
export interface KoaInstrumentationConfig extends InstrumentationConfig {
66+
export interface KoaInstrumentationConfig<
67+
KoaContextType = any,
68+
KoaMiddlewareType = any
69+
> extends InstrumentationConfig {
4770
/** Ignore specific layers based on their type */
4871
ignoreLayersType?: KoaLayerType[];
4972
/** Function for adding custom attributes to each middleware layer span */
50-
requestHook?: KoaRequestCustomAttributeFunction;
73+
requestHook?: KoaRequestCustomAttributeFunction<
74+
KoaContextType,
75+
KoaMiddlewareType
76+
>;
5177
}

plugins/node/opentelemetry-instrumentation-koa/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { KoaContext, KoaLayerType, KoaInstrumentationConfig } from './types';
17-
import { KoaMiddleware } from './internal-types';
16+
import { KoaLayerType, KoaInstrumentationConfig } from './types';
17+
import { KoaContext, KoaMiddleware } from './internal-types';
1818
import { AttributeNames } from './enums/AttributeNames';
1919
import { Attributes } from '@opentelemetry/api';
2020
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';

plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
18+
import type { RouterParamContext } from '@koa/router';
1719
import * as KoaRouter from '@koa/router';
1820
import { context, trace, Span, SpanKind } from '@opentelemetry/api';
1921
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
@@ -29,6 +31,9 @@ import {
2931
SEMATTRS_HTTP_ROUTE,
3032
} from '@opentelemetry/semantic-conventions';
3133

34+
type KoaContext = ParameterizedContext<DefaultState, RouterParamContext>;
35+
type KoaMiddleware = Middleware<DefaultState, KoaContext>;
36+
3237
import { KoaInstrumentation } from '../src';
3338
const plugin = new KoaInstrumentation();
3439

@@ -594,11 +599,13 @@ describe('Koa Instrumentation', () => {
594599
)
595600
);
596601

597-
const requestHook = sinon.spy((span: Span, info: KoaRequestInfo) => {
598-
span.setAttribute(SEMATTRS_HTTP_METHOD, info.context.request.method);
602+
const requestHook = sinon.spy(
603+
(span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
604+
span.setAttribute(SEMATTRS_HTTP_METHOD, info.context.request.method);
599605

600-
throw Error('error thrown in requestHook');
601-
});
606+
throw Error('error thrown in requestHook');
607+
}
608+
);
602609

603610
plugin.setConfig({
604611
requestHook,
@@ -645,11 +652,13 @@ describe('Koa Instrumentation', () => {
645652
)
646653
);
647654

648-
const requestHook = sinon.spy((span: Span, info: KoaRequestInfo) => {
649-
span.setAttribute('http.method', info.context.request.method);
650-
span.setAttribute('app.env', info.context.app.env);
651-
span.setAttribute('koa.layer', info.layerType);
652-
});
655+
const requestHook = sinon.spy(
656+
(span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
657+
span.setAttribute('http.method', info.context.request.method);
658+
span.setAttribute('app.env', info.context.app.env);
659+
span.setAttribute('koa.layer', info.layerType);
660+
}
661+
);
653662

654663
plugin.setConfig({
655664
requestHook,

0 commit comments

Comments
 (0)