@opentelemetry/instrumentation-http
Advanced tools
Comparing version 0.34.0 to 0.35.0
/// <reference types="node" /> | ||
import { MeterProvider } from '@opentelemetry/api'; | ||
import type * as http from 'http'; | ||
@@ -18,7 +17,9 @@ import * as url from 'url'; | ||
constructor(config?: HttpInstrumentationConfig); | ||
setMeterProvider(meterProvider: MeterProvider): void; | ||
private _updateMetricInstruments; | ||
protected _updateMetricInstruments(): void; | ||
private _getConfig; | ||
setConfig(config?: HttpInstrumentationConfig): void; | ||
init(): [InstrumentationNodeModuleDefinition<Https>, InstrumentationNodeModuleDefinition<Http>]; | ||
init(): [ | ||
InstrumentationNodeModuleDefinition<Https>, | ||
InstrumentationNodeModuleDefinition<Http> | ||
]; | ||
private _getHttpInstrumentation; | ||
@@ -53,2 +54,4 @@ private _getHttpsInstrumentation; | ||
private _outgoingRequestFunction; | ||
private _onServerResponseFinish; | ||
private _onServerResponseError; | ||
private _startHttpSpan; | ||
@@ -55,0 +58,0 @@ private _closeHttpSpan; |
@@ -27,2 +27,3 @@ "use strict"; | ||
const core_2 = require("@opentelemetry/core"); | ||
const events_1 = require("events"); | ||
/** | ||
@@ -38,8 +39,3 @@ * Http instrumentation instrumentation for Opentelemetry | ||
this._headerCapture = this._createHeaderCapture(); | ||
this._updateMetricInstruments(); | ||
} | ||
setMeterProvider(meterProvider) { | ||
super.setMeterProvider(meterProvider); | ||
this._updateMetricInstruments(); | ||
} | ||
_updateMetricInstruments() { | ||
@@ -49,3 +45,3 @@ this._httpServerDurationHistogram = this.meter.createHistogram('http.server.duration', { | ||
unit: 'ms', | ||
valueType: api_1.ValueType.DOUBLE | ||
valueType: api_1.ValueType.DOUBLE, | ||
}); | ||
@@ -55,3 +51,3 @@ this._httpClientDurationHistogram = this.meter.createHistogram('http.client.duration', { | ||
unit: 'ms', | ||
valueType: api_1.ValueType.DOUBLE | ||
valueType: api_1.ValueType.DOUBLE, | ||
}); | ||
@@ -225,3 +221,5 @@ } | ||
else { | ||
status = { code: utils.parseResponseStatus(api_1.SpanKind.CLIENT, response.statusCode) }; | ||
status = { | ||
code: utils.parseResponseStatus(api_1.SpanKind.CLIENT, response.statusCode), | ||
}; | ||
} | ||
@@ -234,3 +232,3 @@ span.setStatus(status); | ||
}); | ||
response.on('error', (error) => { | ||
response.on(events_1.errorMonitor, (error) => { | ||
this._diag.debug('outgoingRequest on error()', error); | ||
@@ -249,3 +247,3 @@ utils.setSpanWithError(span, error); | ||
}); | ||
request.on('error', (error) => { | ||
request.on(events_1.errorMonitor, (error) => { | ||
this._diag.debug('outgoingRequest on request error()', error); | ||
@@ -295,3 +293,3 @@ utils.setSpanWithError(span, error); | ||
const startTime = (0, core_1.hrTime)(); | ||
let metricAttributes = utils.getIncomingRequestMetricAttributes(spanAttributes); | ||
const metricAttributes = utils.getIncomingRequestMetricAttributes(spanAttributes); | ||
const ctx = api_1.propagation.extract(api_1.ROOT_CONTEXT, headers); | ||
@@ -313,27 +311,14 @@ const span = instrumentation._startHttpSpan(`${component.toLocaleUpperCase()} ${method}`, spanOptions, ctx); | ||
instrumentation._headerCapture.server.captureRequestHeaders(span, header => request.headers[header]); | ||
// Wraps end (inspired by: | ||
// https://github.com/GoogleCloudPlatform/cloud-trace-nodejs/blob/master/src/instrumentations/instrumentation-connect.ts#L75) | ||
const originalEnd = response.end; | ||
response.end = function (..._args) { | ||
response.end = originalEnd; | ||
// Cannot pass args of type ResponseEndArgs, | ||
const returned = (0, instrumentation_1.safeExecuteInTheMiddle)(() => response.end.apply(this, arguments), error => { | ||
if (error) { | ||
utils.setSpanWithError(span, error); | ||
instrumentation._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes); | ||
throw error; | ||
} | ||
}); | ||
const attributes = utils.getIncomingRequestAttributesOnResponse(request, response); | ||
metricAttributes = Object.assign(metricAttributes, utils.getIncomingRequestMetricAttributesOnResponse(attributes)); | ||
instrumentation._headerCapture.server.captureResponseHeaders(span, header => response.getHeader(header)); | ||
span | ||
.setAttributes(attributes) | ||
.setStatus({ code: utils.parseResponseStatus(api_1.SpanKind.SERVER, response.statusCode) }); | ||
if (instrumentation._getConfig().applyCustomAttributesOnSpan) { | ||
(0, instrumentation_1.safeExecuteInTheMiddle)(() => instrumentation._getConfig().applyCustomAttributesOnSpan(span, request, response), () => { }, true); | ||
// After 'error', no further events other than 'close' should be emitted. | ||
let hasError = false; | ||
response.on('close', () => { | ||
if (hasError) { | ||
return; | ||
} | ||
instrumentation._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes); | ||
return returned; | ||
}; | ||
instrumentation._onServerResponseFinish(request, response, span, metricAttributes, startTime); | ||
}); | ||
response.on(events_1.errorMonitor, (err) => { | ||
hasError = true; | ||
instrumentation._onServerResponseError(span, metricAttributes, startTime, err); | ||
}); | ||
return (0, instrumentation_1.safeExecuteInTheMiddle)(() => original.apply(this, [event, ...args]), error => { | ||
@@ -371,3 +356,7 @@ if (error) { | ||
if (utils.isIgnored(origin + pathname, instrumentation._getConfig().ignoreOutgoingUrls, (e) => instrumentation._diag.error('caught ignoreOutgoingUrls error: ', e)) || | ||
(0, instrumentation_1.safeExecuteInTheMiddle)(() => { var _a, _b; return (_b = (_a = instrumentation._getConfig()).ignoreOutgoingRequestHook) === null || _b === void 0 ? void 0 : _b.call(_a, optionsParsed); }, (e) => { | ||
(0, instrumentation_1.safeExecuteInTheMiddle)(() => { | ||
var _a, _b; | ||
return (_b = (_a = instrumentation | ||
._getConfig()).ignoreOutgoingRequestHook) === null || _b === void 0 ? void 0 : _b.call(_a, optionsParsed); | ||
}, (e) => { | ||
if (e != null) { | ||
@@ -422,2 +411,18 @@ instrumentation._diag.error('caught ignoreOutgoingRequestHook error: ', e); | ||
} | ||
_onServerResponseFinish(request, response, span, metricAttributes, startTime) { | ||
const attributes = utils.getIncomingRequestAttributesOnResponse(request, response); | ||
metricAttributes = Object.assign(metricAttributes, utils.getIncomingRequestMetricAttributesOnResponse(attributes)); | ||
this._headerCapture.server.captureResponseHeaders(span, header => response.getHeader(header)); | ||
span.setAttributes(attributes).setStatus({ | ||
code: utils.parseResponseStatus(api_1.SpanKind.SERVER, response.statusCode), | ||
}); | ||
if (this._getConfig().applyCustomAttributesOnSpan) { | ||
(0, instrumentation_1.safeExecuteInTheMiddle)(() => this._getConfig().applyCustomAttributesOnSpan(span, request, response), () => { }, true); | ||
} | ||
this._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes); | ||
} | ||
_onServerResponseError(span, metricAttributes, startTime, error) { | ||
utils.setSpanWithError(span, error); | ||
this._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes); | ||
} | ||
_startHttpSpan(name, options, ctx = api_1.context.active()) { | ||
@@ -477,3 +482,3 @@ /* | ||
captureRequestHeaders: utils.headerCapture('request', (_c = (_b = (_a = config.headersToSpanAttributes) === null || _a === void 0 ? void 0 : _a.client) === null || _b === void 0 ? void 0 : _b.requestHeaders) !== null && _c !== void 0 ? _c : []), | ||
captureResponseHeaders: utils.headerCapture('response', (_f = (_e = (_d = config.headersToSpanAttributes) === null || _d === void 0 ? void 0 : _d.client) === null || _e === void 0 ? void 0 : _e.responseHeaders) !== null && _f !== void 0 ? _f : []) | ||
captureResponseHeaders: utils.headerCapture('response', (_f = (_e = (_d = config.headersToSpanAttributes) === null || _d === void 0 ? void 0 : _d.client) === null || _e === void 0 ? void 0 : _e.responseHeaders) !== null && _f !== void 0 ? _f : []), | ||
}, | ||
@@ -483,3 +488,3 @@ server: { | ||
captureResponseHeaders: utils.headerCapture('response', (_m = (_l = (_k = config.headersToSpanAttributes) === null || _k === void 0 ? void 0 : _k.server) === null || _l === void 0 ? void 0 : _l.responseHeaders) !== null && _m !== void 0 ? _m : []), | ||
} | ||
}, | ||
}; | ||
@@ -486,0 +491,0 @@ } |
@@ -19,3 +19,2 @@ /// <reference types="node" /> | ||
export declare type Func<T> = (...args: any[]) => T; | ||
export declare type ResponseEndArgs = [((() => void) | undefined)?] | [unknown, ((() => void) | undefined)?] | [unknown, string, ((() => void) | undefined)?]; | ||
export interface HttpCustomAttributeFunction { | ||
@@ -22,0 +21,0 @@ (span: Span, request: ClientRequest | IncomingMessage, response: IncomingMessage | ServerResponse): void; |
@@ -135,3 +135,4 @@ "use strict"; | ||
else { | ||
attributes[semantic_conventions_1.SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = length; | ||
attributes[semantic_conventions_1.SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = | ||
length; | ||
} | ||
@@ -153,3 +154,4 @@ }; | ||
else { | ||
attributes[semantic_conventions_1.SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = length; | ||
attributes[semantic_conventions_1.SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = | ||
length; | ||
} | ||
@@ -217,3 +219,6 @@ }; | ||
} | ||
const hostname = optionsParsed.host || (optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname); | ||
const hostname = optionsParsed.host || | ||
(optionsParsed.port != null | ||
? `${optionsParsed.hostname}${optionsParsed.port}` | ||
: optionsParsed.hostname); | ||
origin = `${optionsParsed.protocol || 'http:'}//${hostname}`; | ||
@@ -231,3 +236,3 @@ } | ||
: 'GET'; | ||
return { origin, pathname, method, optionsParsed, }; | ||
return { origin, pathname, method, optionsParsed }; | ||
}; | ||
@@ -299,4 +304,6 @@ exports.getRequestInfo = getRequestInfo; | ||
const metricAttributes = {}; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME]; | ||
//TODO: http.url attribute, it should susbtitute any parameters to avoid high cardinality. | ||
@@ -351,5 +358,8 @@ return metricAttributes; | ||
const metricAttributes = {}; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR]; | ||
return metricAttributes; | ||
@@ -406,6 +416,10 @@ }; | ||
const metricAttributes = {}; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_NAME] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_NAME]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_NAME] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_NAME]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR]; | ||
//TODO: http.target attribute, it should susbtitute any parameters to avoid high cardinality. | ||
@@ -446,4 +460,6 @@ return metricAttributes; | ||
const metricAttributes = {}; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_PORT] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_PORT]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE]; | ||
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_PORT] = | ||
spanAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_PORT]; | ||
return metricAttributes; | ||
@@ -453,3 +469,6 @@ }; | ||
function headerCapture(type, headers) { | ||
const normalizedHeaders = new Map(headers.map(header => [header.toLowerCase(), header.toLowerCase().replace(/-/g, '_')])); | ||
const normalizedHeaders = new Map(headers.map(header => [ | ||
header.toLowerCase(), | ||
header.toLowerCase().replace(/-/g, '_'), | ||
])); | ||
return (span, getHeader) => { | ||
@@ -456,0 +475,0 @@ for (const [capturedHeader, normalizedHeader] of normalizedHeaders) { |
@@ -1,2 +0,2 @@ | ||
export declare const VERSION = "0.34.0"; | ||
export declare const VERSION = "0.35.0"; | ||
//# sourceMappingURL=version.d.ts.map |
@@ -20,3 +20,3 @@ "use strict"; | ||
// this is autogenerated file, see scripts/version-update.js | ||
exports.VERSION = '0.34.0'; | ||
exports.VERSION = '0.35.0'; | ||
//# sourceMappingURL=version.js.map |
{ | ||
"name": "@opentelemetry/instrumentation-http", | ||
"version": "0.34.0", | ||
"version": "0.35.0", | ||
"description": "OpenTelemetry http/https automatic instrumentation package.", | ||
@@ -49,6 +49,6 @@ "main": "build/src/index.js", | ||
"@opentelemetry/api": "^1.3.0", | ||
"@opentelemetry/context-async-hooks": "1.8.0", | ||
"@opentelemetry/sdk-metrics": "1.8.0", | ||
"@opentelemetry/sdk-trace-base": "1.8.0", | ||
"@opentelemetry/sdk-trace-node": "1.8.0", | ||
"@opentelemetry/context-async-hooks": "1.9.0", | ||
"@opentelemetry/sdk-metrics": "1.9.0", | ||
"@opentelemetry/sdk-trace-base": "1.9.0", | ||
"@opentelemetry/sdk-trace-node": "1.9.0", | ||
"@types/mocha": "10.0.0", | ||
@@ -68,3 +68,3 @@ "@types/node": "18.6.5", | ||
"rimraf": "3.0.2", | ||
"sinon": "14.0.0", | ||
"sinon": "15.0.0", | ||
"superagent": "8.0.0", | ||
@@ -78,5 +78,5 @@ "ts-mocha": "10.0.0", | ||
"dependencies": { | ||
"@opentelemetry/core": "1.8.0", | ||
"@opentelemetry/instrumentation": "0.34.0", | ||
"@opentelemetry/semantic-conventions": "1.8.0", | ||
"@opentelemetry/core": "1.9.0", | ||
"@opentelemetry/instrumentation": "0.35.0", | ||
"@opentelemetry/semantic-conventions": "1.9.0", | ||
"semver": "^7.3.5" | ||
@@ -86,3 +86,3 @@ }, | ||
"sideEffects": false, | ||
"gitHead": "7972edf6659fb6e0d5928a5cf7a35f26683e168f" | ||
"gitHead": "08f597f3a3d71a4852b0afbba120af15ca038121" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
173040
1336
+ Added@opentelemetry/api@1.4.1(transitive)
+ Added@opentelemetry/core@1.9.0(transitive)
+ Added@opentelemetry/instrumentation@0.35.0(transitive)
+ Added@opentelemetry/semantic-conventions@1.9.0(transitive)
- Removed@opentelemetry/api@1.3.0(transitive)
- Removed@opentelemetry/core@1.8.0(transitive)
- Removed@opentelemetry/instrumentation@0.34.0(transitive)
- Removed@opentelemetry/semantic-conventions@1.8.0(transitive)
Updated@opentelemetry/core@1.9.0