From 86a21d7b4ce289dc986925ad73ffd6f0618bb5c7 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 7 Dec 2023 01:19:41 +0100 Subject: [PATCH] fix(instrumentation-express)!: remove `@types/express` from dependencies (#1804) Closes: #1787 --- package-lock.json | 4 ++-- .../node/opentelemetry-instrumentation-express/README.md | 7 +++++++ .../opentelemetry-instrumentation-express/package.json | 4 ++-- .../src/instrumentation.ts | 4 ++-- .../src/internal-types.ts | 4 ++-- .../opentelemetry-instrumentation-express/src/types.ts | 8 ++++---- .../opentelemetry-instrumentation-express/src/utils.ts | 4 ++-- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 393b5258c4..1d850d42a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34634,14 +34634,14 @@ "dependencies": { "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.45.1", - "@opentelemetry/semantic-conventions": "^1.0.0", - "@types/express": "4.17.18" + "@opentelemetry/semantic-conventions": "^1.0.0" }, "devDependencies": { "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", + "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.6.5", "@types/sinon": "10.0.18", diff --git a/plugins/node/opentelemetry-instrumentation-express/README.md b/plugins/node/opentelemetry-instrumentation-express/README.md index 6eba2b6c67..3bcb67989b 100644 --- a/plugins/node/opentelemetry-instrumentation-express/README.md +++ b/plugins/node/opentelemetry-instrumentation-express/README.md @@ -79,6 +79,13 @@ Express instrumentation has few options available to choose from. You can set th - `info: ExpressRequestInfo` containing the incoming Express.js request, the current route handler creating a span and `ExpressLayerType` - the type of the handling layer. - `defaultName: string` - original name proposed by the instrumentation. +`requestHook` is invoked with 2 arguments: + +- `span: Span` - the span associated with the express request. +- `info: ExpressRequestInfo` containing the incoming Express.js request, the current route handler creating a span and `ExpressLayerType` - the type of the handling layer. + +NOTE: `ExpressRequestInfo.request` is typed as `any`. If you want type support make sure you have `@types/express` installed then you can use `ExpressRequestInfo` + #### Ignore a whole Express route In order to ignore whole traces that represent a given Express route, use diff --git a/plugins/node/opentelemetry-instrumentation-express/package.json b/plugins/node/opentelemetry-instrumentation-express/package.json index 9106594094..fd1d0163c4 100644 --- a/plugins/node/opentelemetry-instrumentation-express/package.json +++ b/plugins/node/opentelemetry-instrumentation-express/package.json @@ -49,6 +49,7 @@ "@opentelemetry/context-async-hooks": "^1.8.0", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", + "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.6.5", "@types/sinon": "10.0.18", @@ -64,8 +65,7 @@ "dependencies": { "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.45.1", - "@opentelemetry/semantic-conventions": "^1.0.0", - "@types/express": "4.17.18" + "@opentelemetry/semantic-conventions": "^1.0.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express#readme" } diff --git a/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts index 61d91b55e6..46fd4cc0ff 100644 --- a/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts @@ -19,7 +19,7 @@ import { trace, context, diag, - SpanAttributes, + Attributes, SpanStatusCode, } from '@opentelemetry/api'; import type * as express from 'express'; @@ -199,7 +199,7 @@ export class ExpressInstrumentation extends InstrumentationBase< .filter(path => path !== '/' && path !== '/*') .join(''); - const attributes: SpanAttributes = { + const attributes: Attributes = { [SemanticAttributes.HTTP_ROUTE]: route.length > 0 ? route : '/', }; const metadata = getLayerMetadata(layer, layerPath); diff --git a/plugins/node/opentelemetry-instrumentation-express/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-express/src/internal-types.ts index ab53fc37e7..bae07da361 100644 --- a/plugins/node/opentelemetry-instrumentation-express/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-express/src/internal-types.ts @@ -15,7 +15,7 @@ */ import type { Request } from 'express'; -import { SpanAttributes } from '@opentelemetry/api'; +import { Attributes } from '@opentelemetry/api'; /** * This symbol is used to mark express layer as being already instrumented @@ -67,6 +67,6 @@ export type ExpressLayer = { }; export type LayerMetadata = { - attributes: SpanAttributes; + attributes: Attributes; name: string; }; diff --git a/plugins/node/opentelemetry-instrumentation-express/src/types.ts b/plugins/node/opentelemetry-instrumentation-express/src/types.ts index a6d376face..50d76df514 100644 --- a/plugins/node/opentelemetry-instrumentation-express/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-express/src/types.ts @@ -14,15 +14,15 @@ * limitations under the License. */ -import type { Request } from 'express'; import { Span } from '@opentelemetry/api'; import { InstrumentationConfig } from '@opentelemetry/instrumentation'; import { ExpressLayerType } from './enums/ExpressLayerType'; export type IgnoreMatcher = string | RegExp | ((name: string) => boolean); -export type ExpressRequestInfo = { - request: Request; +export type ExpressRequestInfo = { + /** An express request object */ + request: T; route: string; layerType: ExpressLayerType; }; @@ -47,7 +47,7 @@ export interface ExpressRequestCustomAttributeFunction { } /** - * Options available for the Express Instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-Instrumentation-express#express-Instrumentation-options)) + * Options available for the Express Instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express#express-instrumentation-options)) */ export interface ExpressInstrumentationConfig extends InstrumentationConfig { /** Ignore specific based on their name */ diff --git a/plugins/node/opentelemetry-instrumentation-express/src/utils.ts b/plugins/node/opentelemetry-instrumentation-express/src/utils.ts index f543104e49..7e51b84124 100644 --- a/plugins/node/opentelemetry-instrumentation-express/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-express/src/utils.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { SpanAttributes } from '@opentelemetry/api'; +import { Attributes } from '@opentelemetry/api'; import { IgnoreMatcher, ExpressInstrumentationConfig } from './types'; import { ExpressLayerType } from './enums/ExpressLayerType'; import { AttributeNames } from './enums/AttributeNames'; @@ -49,7 +49,7 @@ export const getLayerMetadata = ( layer: ExpressLayer, layerPath?: string ): { - attributes: SpanAttributes; + attributes: Attributes; name: string; } => { if (layer.name === 'router') {