@opentelemetry/instrumentation-express
Advanced tools
Comparing version 0.30.0 to 0.31.0
@@ -171,2 +171,13 @@ "use strict"; | ||
}); | ||
if (instrumentation.getConfig().requestHook) { | ||
instrumentation_1.safeExecuteInTheMiddle(() => instrumentation.getConfig().requestHook(span, { | ||
request: req, | ||
layerType: type, | ||
route, | ||
}), e => { | ||
if (e) { | ||
api_1.diag.error('express instrumentation: request hook failed', e); | ||
} | ||
}, true); | ||
} | ||
const startTime = core_1.hrTime(); | ||
@@ -173,0 +184,0 @@ let spanHasEnded = false; |
import { kLayerPatched } from './'; | ||
import { Request } from 'express'; | ||
import { SpanAttributes } from '@opentelemetry/api'; | ||
import { Span, SpanAttributes } from '@opentelemetry/api'; | ||
import { InstrumentationConfig } from '@opentelemetry/instrumentation'; | ||
@@ -67,2 +67,11 @@ import { ExpressLayerType } from './enums/ExpressLayerType'; | ||
/** | ||
* Function that can be used to add custom attributes to the current span or the root span on | ||
* a Express request | ||
* @param span - The Express middleware layer span. | ||
* @param info - An instance of ExpressRequestInfo that contains info about the request such as the route, and the layer type. | ||
*/ | ||
export interface ExpressRequestCustomAttributeFunction { | ||
(span: Span, info: ExpressRequestInfo): void; | ||
} | ||
/** | ||
* Options available for the Express Instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-Instrumentation-express#express-Instrumentation-options)) | ||
@@ -76,3 +85,5 @@ */ | ||
spanNameHook?: SpanNameHook; | ||
/** Function for adding custom attributes on Express request */ | ||
requestHook?: ExpressRequestCustomAttributeFunction; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export declare const VERSION = "0.30.0"; | ||
export declare const VERSION = "0.31.0"; | ||
//# sourceMappingURL=version.d.ts.map |
@@ -20,3 +20,3 @@ "use strict"; | ||
// this is autogenerated file, see scripts/version-update.js | ||
exports.VERSION = '0.30.0'; | ||
exports.VERSION = '0.31.0'; | ||
//# sourceMappingURL=version.js.map |
# Changelog | ||
## [0.31.0](https://github.com/open-telemetry/opentelemetry-js-contrib/compare/instrumentation-express-v0.30.0...instrumentation-express-v0.31.0) (2022-09-02) | ||
### Features | ||
* **express:** add requestHook support ([#1091](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1091)) ([bcc048b](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/bcc048b4de1293b0d932ac69dc0b0c056aca13ee)) | ||
* update experimental Otel deps to ^0.31.0 ([#1096](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1096)) ([4c8843b](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/4c8843be14896d1159a622c07eb3a049401ccba1)) | ||
* update experimental Otel deps to ^0.32.0 ([#1143](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1143)) ([6fb1911](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/6fb191139aed2ca763300dcf9adb51121a88f97e)) | ||
### Bug Fixes | ||
* mongodb types fails to compile with latest tsc v4.8 ([#1141](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1141)) ([ec9ee13](https://github.com/open-telemetry/opentelemetry-js-contrib/commit/ec9ee131635dc2db88deea4f2efb887ff6f60577)) | ||
## [0.30.0](https://github.com/open-telemetry/opentelemetry-js-contrib/compare/instrumentation-express-v0.29.0...instrumentation-express-v0.30.0) (2022-06-08) | ||
@@ -4,0 +18,0 @@ |
{ | ||
"name": "@opentelemetry/instrumentation-express", | ||
"version": "0.30.0", | ||
"version": "0.31.0", | ||
"description": "OpenTelemetry express automatic instrumentation package.", | ||
@@ -57,2 +57,3 @@ "main": "build/src/index.js", | ||
"@types/node": "16.11.21", | ||
"@types/sinon": "10.0.9", | ||
"express": "4.17.1", | ||
@@ -63,4 +64,5 @@ "gts": "3.1.0", | ||
"rimraf": "3.0.2", | ||
"sinon": "14.0.0", | ||
"test-all-versions": "5.0.1", | ||
"ts-mocha": "8.0.0", | ||
"ts-mocha": "10.0.0", | ||
"typescript": "4.3.5" | ||
@@ -70,3 +72,3 @@ }, | ||
"@opentelemetry/core": "^1.0.0", | ||
"@opentelemetry/instrumentation": "^0.29.2", | ||
"@opentelemetry/instrumentation": "^0.32.0", | ||
"@opentelemetry/semantic-conventions": "^1.0.0", | ||
@@ -76,3 +78,3 @@ "@types/express": "4.17.13" | ||
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express#readme", | ||
"gitHead": "9633cf49c4099e97dd2a285d0399455e17a593e3" | ||
"gitHead": "4a9442ceea7f8555ad6e5f731f92834f3398d5b9" | ||
} |
@@ -6,6 +6,5 @@ # OpenTelemetry Express Instrumentation for Node.js | ||
This module provides automatic instrumentation for [`express`](https://github.com/expressjs/express). | ||
This module provides automatic instrumentation for the [`express`](https://github.com/expressjs/express) module, which may be loaded using the [`@opentelemetry/sdk-trace-node`](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package and is included in the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle. | ||
For automatic instrumentation see the | ||
[@opentelemetry/sdk-trace-node](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package. | ||
If total installation size is not constrained, it is recommended to use the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle with [@opentelemetry/sdk-node](`https://www.npmjs.com/package/@opentelemetry/sdk-node`) for the most seamless instrumentation experience. | ||
@@ -50,3 +49,3 @@ Compatible with OpenTelemetry JS API and SDK `1.0+`. | ||
See [examples/express](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/examples/express) for a short example. | ||
See [examples](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express/examples) for a short example. | ||
@@ -66,2 +65,3 @@ ### Caveats | ||
| `spanNameHook` | `SpanNameHook` | `() => 'my-span-name'` | Can be used to customize span names by returning a new name from the hook. | | ||
| `requestHook` | `ExpressRequestCustomAttributeFunction (function)` | `(span, info) => {}` | Function for adding custom attributes on Express request. Receives params: `Span, ExpressRequestInfo`. | | ||
@@ -85,2 +85,58 @@ `ignoreLayers` accepts an array of elements of types: | ||
#### Ignore a whole Express route | ||
In order to ignore whole traces that represent a given Express route, use | ||
the `ignoreIncomingRequestHook` option from | ||
`@opentelemetry/instrumentation-http` against the route path. Ideally, this | ||
shouldn't be necessary since spans should a have low cardinality and minimize | ||
interaction between instrumentation libraies but | ||
`@opentelemetry/instrumentation-express` renames the root span from | ||
`@opentelemetry/instrumentation-http` in order to get things in order. | ||
```js | ||
registerInstrumentations({ | ||
instrumentations: [ | ||
// Express instrumentation expects HTTP layer to be instrumented | ||
new HttpInstrumentation({ | ||
ignoreIncomingRequestHook(req) { | ||
// Ignore spans from static assets. | ||
const isStaticAsset = !!req.url.match(/^\/static\/.*$/); | ||
return isStaticAsset; | ||
} | ||
}), | ||
new ExpressInstrumentation(), | ||
], | ||
}); | ||
``` | ||
#### Using `requestHook` | ||
Instrumentation configuration accepts a custom "hook" function which will be called for every instrumented Express layer involved in a request. Custom attributes can be set on the span or run any custom logic per layer. | ||
Here is a simple example that adds to the request handler span some attributes based on the Express request attributes: | ||
```javascript | ||
import { ExpressInstrumentation, ExpressLayerType } from "@opentelemetry/instrumentation-express" | ||
const expressInstrumentation = new ExpressInstrumentation({ | ||
requestHook: function ( | ||
span: Span, | ||
info: ExpressRequestInfo, | ||
) { | ||
if (info.layerType === ExpressLayerType.REQUEST_HANDLER) { | ||
span.setAttribute( | ||
'http.method', | ||
info.request.method | ||
); | ||
span.setAttribute( | ||
'express.base_url', | ||
info.request.baseUrl | ||
); | ||
} | ||
} | ||
}); | ||
``` | ||
## Useful links | ||
@@ -87,0 +143,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64565
660
153
16
+ Added@opentelemetry/api-metrics@0.32.0(transitive)
+ Added@opentelemetry/instrumentation@0.32.0(transitive)
- Removed@opentelemetry/api-metrics@0.29.2(transitive)
- Removed@opentelemetry/instrumentation@0.29.2(transitive)