Skip to content

fix(opentelemetry-instrumentation-http): Support http headers as array #5693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion experimental/examples/events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@opentelemetry/sdk-logs": "0.201.0"
},
"devDependencies": {
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"ts-node": "^10.9.1"
}
}
2 changes: 1 addition & 1 deletion experimental/examples/logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@opentelemetry/sdk-logs": "0.201.0"
},
"devDependencies": {
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"ts-node": "^10.9.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@opentelemetry/api-logs": "0.201.0",
"@opentelemetry/resources": "2.0.1",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@opentelemetry/api": "1.9.0",
"@opentelemetry/resources": "2.0.1",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"@types/webpack-env": "1.16.3",
"babel-loader": "10.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@babel/preset-env": "7.26.9",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"babel-plugin-istanbul": "7.0.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@grpc/proto-loader": "^0.7.10",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@babel/preset-env": "7.26.9",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"@types/webpack-env": "1.16.3",
"babel-loader": "10.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@babel/preset-env": "7.26.9",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"babel-plugin-istanbul": "7.0.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@babel/preset-env": "7.26.9",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"babel-plugin-istanbul": "7.0.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@grpc/proto-loader": "^0.7.10",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@babel/preset-env": "7.26.9",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"@types/webpack-env": "1.16.3",
"babel-loader": "10.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"devDependencies": {
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@opentelemetry/api": "1.9.0",
"@opentelemetry/semantic-conventions": "^1.29.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@opentelemetry/propagator-b3": "2.0.1",
"@opentelemetry/sdk-trace-base": "2.0.1",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"@types/webpack-env": "1.16.3",
"babel-loader": "10.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@protobuf-ts/runtime": "2.9.6",
"@protobuf-ts/runtime-rpc": "2.9.6",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "18.19.100",
"@types/sinon": "17.0.4",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@opentelemetry/sdk-trace-base": "2.0.1",
"@opentelemetry/sdk-trace-node": "2.0.1",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/node": "^18.19.100",
"@types/request-promise-native": "1.0.21",
"@types/sinon": "17.0.4",
"@types/superagent": "8.1.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
request,
response
),
() => {},
() => { },
true
);
}
Expand Down Expand Up @@ -735,7 +735,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
}
const extraOptions =
typeof args[0] === 'object' &&
(typeof options === 'string' || options instanceof url.URL)
(typeof options === 'string' || options instanceof url.URL)
? (args.shift() as http.RequestOptions)
: undefined;
const { method, invalidUrl, optionsParsed } = getRequestInfo(
Expand Down Expand Up @@ -817,9 +817,24 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
} else {
// Make a copy of the headers object to avoid mutating an object the
// caller might have a reference to.
optionsParsed.headers = Object.assign({}, optionsParsed.headers);
if (optionsParsed.headers instanceof Array) {
optionsParsed.headers = [...optionsParsed.headers];
} else {
optionsParsed.headers = Object.assign({}, optionsParsed.headers);
}
}

if (optionsParsed.headers instanceof Array) {
debugger;
propagation.inject(requestContext, optionsParsed.headers, {
set(carrier, key, value) {
// We copied the array earlier so we are not actually modifying the original array
(carrier as string[]).push(key, value);
},
});
} else {
propagation.inject(requestContext, optionsParsed.headers);
}
propagation.inject(requestContext, optionsParsed.headers);

return context.with(requestContext, () => {
/*
Expand Down Expand Up @@ -916,7 +931,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
request,
response
),
() => {},
() => { },
true
);
}
Expand Down Expand Up @@ -1013,7 +1028,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
) {
safeExecuteInTheMiddle(
() => this.getConfig().responseHook!(span, response),
() => {},
() => { },
true
);
}
Expand All @@ -1024,7 +1039,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
) {
safeExecuteInTheMiddle(
() => this.getConfig().requestHook!(span, request),
() => {},
() => { },
true
);
}
Expand All @@ -1036,7 +1051,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
if (typeof hookFunc === 'function') {
return safeExecuteInTheMiddle(
() => hookFunc(request),
() => {},
() => { },
true
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,28 @@ export const extractHostnameAndPort = (
return { hostname, port };
};

export const normalizeHeaders = (headers: ParsedRequestOptions['headers']): OutgoingHttpHeaders => {
if (headers === undefined) {
return {};
}

if (!(headers instanceof Array)) {
return headers;
}

const result: Record<string, string> = {};
for (let i = 0; i < headers.length; i += 2) {
const key = headers[i];
const value = headers[i + 1];

if (key !== undefined && value !== undefined) {
result[key] = value;
}
}

return result;
};

/**
* Returns outgoing request attributes scoped to the options passed to the request
* @param {ParsedRequestOptions} requestOptions the same options used to make the request
Expand All @@ -449,7 +471,7 @@ export const getOutgoingRequestAttributes = (
const port = options.port;
const method = requestOptions.method ?? 'GET';
const normalizedMethod = normalizeMethod(method);
const headers = requestOptions.headers || {};
const headers = normalizeHeaders(requestOptions.headers);
const userAgent = headers['user-agent'];
const urlFull = getAbsoluteUrl(
requestOptions,
Expand Down Expand Up @@ -807,7 +829,7 @@ function getInfoFromIncomingMessage(
return {
pathname: unsafeParsedUrl.pathname,
search: unsafeParsedUrl.search,
toString: function () {
toString: function() {
// we cannot use the result of unsafeParsedUrl.toString as it's potentially wrong.
return unsafeParsedUrl.pathname + unsafeParsedUrl.search;
},
Expand Down
Loading