Skip to content

Commit

Permalink
chore: lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir committed Jun 26, 2024
1 parent 7286e31 commit faa5cdd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
import type * as Router from '@koa/router';

export type KoaContext = ParameterizedContext<DefaultState, Router.RouterParamContext>;
export type KoaContext = ParameterizedContext<
DefaultState,
Router.RouterParamContext
>;
export type KoaMiddleware = Middleware<DefaultState, KoaContext> & {
router?: Router;
};
Expand Down
25 changes: 17 additions & 8 deletions plugins/node/opentelemetry-instrumentation-koa/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ export enum KoaLayerType {
/**
* Information about the current Koa middleware layer
* The middleware layer type is any by default.
* One can install koa types packages `@types/koa` and `@types/koa__router`
* One can install koa types packages `@types/koa` and `@types/koa__router`
* with compatible versions to the koa version used in the project
* to get more specific types for the middleware layer property.
*
*
* Example use in a custom attribute function:
* ```ts
* import type { Middleware, ParameterizedContext, DefaultState } from 'koa';
* import type { RouterParamContext } from '@koa/router';
*
*
* type KoaContext = ParameterizedContext<DefaultState, RouterParamContext>;
* type KoaMiddleware = Middleware<DefaultState, KoaContext>;
*
*
* const koaConfig: KoaInstrumentationConfig<KoaContext, KoaMiddleware> = {
* requestHook: (span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
* // custom typescript code that can access the typed into.middlewareLayer and info.context
* }
*
*
*/
export type KoaRequestInfo<KoaContextType = any, KoaMiddlewareType = any> = {
context: KoaContextType;
Expand All @@ -53,16 +53,25 @@ export type KoaRequestInfo<KoaContextType = any, KoaMiddlewareType = any> = {
* @param span - The Express middleware layer span.
* @param context - The current KoaContext.
*/
export interface KoaRequestCustomAttributeFunction<KoaContextType = any, KoaMiddlewareType = any> {
export interface KoaRequestCustomAttributeFunction<
KoaContextType = any,
KoaMiddlewareType = any
> {
(span: Span, info: KoaRequestInfo<KoaContextType, KoaMiddlewareType>): void;
}

/**
* Options available for the Koa Instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-Instrumentation-koa#koa-Instrumentation-options))
*/
export interface KoaInstrumentationConfig<KoaContextType = any, KoaMiddlewareType = any> extends InstrumentationConfig {
export interface KoaInstrumentationConfig<
KoaContextType = any,
KoaMiddlewareType = any
> extends InstrumentationConfig {
/** Ignore specific layers based on their type */
ignoreLayersType?: KoaLayerType[];
/** Function for adding custom attributes to each middleware layer span */
requestHook?: KoaRequestCustomAttributeFunction<KoaContextType, KoaMiddlewareType>;
requestHook?: KoaRequestCustomAttributeFunction<
KoaContextType,
KoaMiddlewareType
>;
}
22 changes: 13 additions & 9 deletions plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,13 @@ describe('Koa Instrumentation', () => {
)
);

const requestHook = sinon.spy((span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
span.setAttribute(SEMATTRS_HTTP_METHOD, info.context.request.method);
const requestHook = sinon.spy(
(span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
span.setAttribute(SEMATTRS_HTTP_METHOD, info.context.request.method);

throw Error('error thrown in requestHook');
});
throw Error('error thrown in requestHook');
}
);

plugin.setConfig({
requestHook,
Expand Down Expand Up @@ -650,11 +652,13 @@ describe('Koa Instrumentation', () => {
)
);

const requestHook = sinon.spy((span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
span.setAttribute('http.method', info.context.request.method);
span.setAttribute('app.env', info.context.app.env);
span.setAttribute('koa.layer', info.layerType);
});
const requestHook = sinon.spy(
(span: Span, info: KoaRequestInfo<KoaContext, KoaMiddleware>) => {
span.setAttribute('http.method', info.context.request.method);
span.setAttribute('app.env', info.context.app.env);
span.setAttribute('koa.layer', info.layerType);
}
);

plugin.setConfig({
requestHook,
Expand Down

0 comments on commit faa5cdd

Please sign in to comment.