@opentelemetry/instrumentation-express
Advanced tools
Comparing version 0.27.1 to 0.28.0
@@ -1,3 +0,3 @@ | ||
import * as express from 'express'; | ||
import { ExpressInstrumentationConfig } from './types'; | ||
import type * as express from 'express'; | ||
import { ExpressInstrumentationConfig, ExpressRequestInfo } from './types'; | ||
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'; | ||
@@ -12,2 +12,4 @@ /** | ||
constructor(config?: ExpressInstrumentationConfig); | ||
setConfig(config?: ExpressInstrumentationConfig): void; | ||
getConfig(): ExpressInstrumentationConfig; | ||
init(): InstrumentationNodeModuleDefinition<typeof express>[]; | ||
@@ -28,3 +30,4 @@ /** | ||
private _applyPatch; | ||
_getSpanName(info: ExpressRequestInfo, defaultName: string): string; | ||
} | ||
//# sourceMappingURL=instrumentation.d.ts.map |
@@ -38,2 +38,8 @@ "use strict"; | ||
} | ||
setConfig(config = {}) { | ||
this._config = Object.assign({}, config); | ||
} | ||
getConfig() { | ||
return this._config; | ||
} | ||
init() { | ||
@@ -141,3 +147,7 @@ return [ | ||
(rpcMetadata === null || rpcMetadata === void 0 ? void 0 : rpcMetadata.type) === core_1.RPCType.HTTP) { | ||
rpcMetadata.span.updateName(`${req.method} ${route.length > 0 ? route : '/'}`); | ||
const name = instrumentation._getSpanName({ | ||
request: req, | ||
route, | ||
}, `${req.method} ${route.length > 0 ? route : '/'}`); | ||
rpcMetadata.span.updateName(name); | ||
} | ||
@@ -154,3 +164,8 @@ // verify against the config if the layer should be ignored | ||
} | ||
const span = instrumentation.tracer.startSpan(metadata.name, { | ||
const spanName = instrumentation._getSpanName({ | ||
request: req, | ||
layerType: type, | ||
route, | ||
}, metadata.name); | ||
const span = instrumentation.tracer.startSpan(spanName, { | ||
attributes: Object.assign(attributes, metadata.attributes), | ||
@@ -209,4 +224,18 @@ }); | ||
} | ||
_getSpanName(info, defaultName) { | ||
var _a; | ||
const hook = this.getConfig().spanNameHook; | ||
if (!(hook instanceof Function)) { | ||
return defaultName; | ||
} | ||
try { | ||
return (_a = hook(info, defaultName)) !== null && _a !== void 0 ? _a : defaultName; | ||
} | ||
catch (err) { | ||
api_1.diag.error('express instrumentation: error calling span name rewrite hook', err); | ||
return defaultName; | ||
} | ||
} | ||
} | ||
exports.ExpressInstrumentation = ExpressInstrumentation; | ||
//# sourceMappingURL=instrumentation.js.map |
@@ -52,3 +52,17 @@ import { kLayerPatched } from './'; | ||
export declare type IgnoreMatcher = string | RegExp | ((name: string) => boolean); | ||
export declare type ExpressRequestInfo = { | ||
request: Request; | ||
route: string; | ||
/** | ||
* If layerType is undefined, SpanNameHook is being invoked to rename the original root HTTP span. | ||
*/ | ||
layerType?: ExpressLayerType; | ||
}; | ||
export declare type SpanNameHook = (info: ExpressRequestInfo, | ||
/** | ||
* If no decision is taken based on RequestInfo, the default name | ||
* supplied by the instrumentation can be used instead. | ||
*/ | ||
defaultName: string) => string; | ||
/** | ||
* Options available for the Express Instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-Instrumentation-express#express-Instrumentation-options)) | ||
@@ -61,3 +75,4 @@ */ | ||
ignoreLayersType?: ExpressLayerType[]; | ||
spanNameHook?: SpanNameHook; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export declare const VERSION = "0.27.1"; | ||
export declare const VERSION = "0.28.0"; | ||
//# sourceMappingURL=version.d.ts.map |
@@ -20,3 +20,3 @@ "use strict"; | ||
// this is autogenerated file, see scripts/version-update.js | ||
exports.VERSION = '0.27.1'; | ||
exports.VERSION = '0.28.0'; | ||
//# sourceMappingURL=version.js.map |
# Changelog | ||
## [0.28.0](https://www.github.com/open-telemetry/opentelemetry-js-contrib/compare/instrumentation-express-v0.27.1...instrumentation-express-v0.28.0) (2022-02-06) | ||
### Features | ||
* **express:** allow rewriting span names ([#463](https://www.github.com/open-telemetry/opentelemetry-js-contrib/issues/463)) ([7510757](https://www.github.com/open-telemetry/opentelemetry-js-contrib/commit/7510757aeeee47a7f0c4bb31de45be3a71bb673e)) | ||
### [0.27.1](https://www.github.com/open-telemetry/opentelemetry-js-contrib/compare/instrumentation-express-v0.27.0...instrumentation-express-v0.27.1) (2022-01-24) | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "@opentelemetry/instrumentation-express", | ||
"version": "0.27.1", | ||
"version": "0.28.0", | ||
"description": "OpenTelemetry express automatic instrumentation package.", | ||
@@ -9,3 +9,4 @@ "main": "build/src/index.js", | ||
"scripts": { | ||
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.ts'", | ||
"test-all-versions": "tav", | ||
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'", | ||
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", | ||
@@ -56,3 +57,3 @@ "tdd": "yarn test -- --watch-extensions ts --watch", | ||
"@types/mocha": "7.0.2", | ||
"@types/node": "14.17.9", | ||
"@types/node": "16.11.21", | ||
"codecov": "3.8.3", | ||
@@ -64,2 +65,3 @@ "express": "4.17.1", | ||
"rimraf": "3.0.2", | ||
"test-all-versions": "^5.0.1", | ||
"ts-mocha": "8.0.0", | ||
@@ -74,3 +76,3 @@ "typescript": "4.3.5" | ||
}, | ||
"gitHead": "edfc04cbe3e7faaa365724ddc26bf702b68cc008" | ||
"gitHead": "ecd0307e98c4d4ecd2a111764bd77371a1390653" | ||
} |
@@ -65,2 +65,3 @@ # OpenTelemetry Express Instrumentation for Node.js | ||
| `ignoreLayersType`| `ExpressLayerType[]` | `['request_handler']` | Ignore layers of specified type. | | ||
| `spanNameHook` | `SpanNameHook` | `() => 'my-span-name'` | Can be used to customize span names by returning a new name from the hook. | | ||
@@ -79,2 +80,7 @@ `ignoreLayers` accepts an array of elements of types: | ||
`spanNameHook` is invoked with 2 arguments: | ||
- `info: ExpressRequestInfo` containing the incoming Express.js request, the current route handler creating a span and `ExpressLayerType` - the type of the handling layer or undefined when renaming the root HTTP instrumentation span. | ||
- `defaultName: string` - original name proposed by the instrumentation. | ||
## Useful links | ||
@@ -81,0 +87,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
58826
637
103
15