Skip to content

Commit 4d4a96e

Browse files
fix(plugin-http): typings
closes #541 Signed-off-by: Olivier Albertini <[email protected]>
1 parent d80cf56 commit 4d4a96e

File tree

8 files changed

+28
-21
lines changed

8 files changed

+28
-21
lines changed

packages/opentelemetry-plugin-http/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@
4343
"@opentelemetry/node": "^0.2.0",
4444
"@opentelemetry/scope-base": "^0.2.0",
4545
"@opentelemetry/tracing": "^0.2.0",
46-
"@types/got": "^9.6.7",
46+
"@types/got": "^9.6.9",
4747
"@types/mocha": "^5.2.7",
4848
"@types/nock": "^11.1.0",
49-
"@types/node": "^12.7.9",
49+
"@types/node": "^12.12.8",
5050
"@types/request-promise-native": "^1.0.17",
51-
"@types/semver": "^6.0.2",
51+
"@types/semver": "^6.2.0",
5252
"@types/shimmer": "^1.0.1",
53-
"@types/sinon": "^7.0.13",
54-
"@types/superagent": "^4.1.3",
53+
"@types/sinon": "^7.5.0",
54+
"@types/superagent": "^4.1.4",
5555
"axios": "^0.19.0",
5656
"codecov": "^3.6.1",
5757
"got": "^9.6.0",
5858
"gts": "^1.1.0",
59-
"mocha": "^6.2.1",
60-
"nock": "^11.3.5",
59+
"mocha": "^6.2.2",
60+
"nock": "^11.7.0",
6161
"nyc": "^14.1.1",
6262
"request": "^2.88.0",
63-
"request-promise-native": "^1.0.7",
63+
"request-promise-native": "^1.0.8",
6464
"rimraf": "^3.0.0",
6565
"sinon": "^7.5.0",
6666
"superagent": "5.1.0",

packages/opentelemetry-plugin-http/src/http.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,10 @@ export class HttpPlugin extends BasePlugin<Http> {
184184
this._logger.debug('makeRequestTrace by injecting context into header');
185185

186186
const host = options.hostname || options.host || 'localhost';
187-
const method = options.method ? options.method.toUpperCase() : 'GET';
188-
const headers = options.headers || {};
187+
const method = (options as RequestOptions).method
188+
? (options as RequestOptions).method!.toUpperCase()
189+
: 'GET';
190+
const headers = (options as RequestOptions).headers || {};
189191
const userAgent = headers['user-agent'];
190192

191193
span.setAttributes({
@@ -409,7 +411,7 @@ export class HttpPlugin extends BasePlugin<Http> {
409411
: undefined
410412
);
411413

412-
options = optionsParsed;
414+
options = optionsParsed as RequestOptions;
413415

414416
if (
415417
utils.isOpenTelemetryRequest(options) ||

packages/opentelemetry-plugin-http/src/types.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
get,
2525
} from 'http';
2626
import * as http from 'http';
27+
import { ParsedUrlQuery } from 'querystring';
2728

2829
export type IgnoreMatcher = string | RegExp | ((url: string) => boolean);
2930
export type HttpCallback = (res: IncomingMessage) => void;
@@ -38,9 +39,12 @@ export type RequestSignature = [http.RequestOptions, HttpCallbackOptional] &
3839

3940
export type HttpRequestArgs = Array<HttpCallbackOptional | RequestSignature>;
4041

41-
export type ParsedRequestOptions =
42-
| (http.RequestOptions & Partial<url.UrlWithParsedQuery>)
43-
| http.RequestOptions;
42+
export type ParsedRequestOptions = (
43+
| http.RequestOptions
44+
| Omit<url.UrlWithParsedQuery, 'query'>) & {
45+
protocol?: string | null | undefined;
46+
query?: string | ParsedUrlQuery | null;
47+
};
4448
export type Http = typeof http;
4549
/* tslint:disable-next-line:no-any */
4650
export type Func<T> = (...args: any[]) => T;

packages/opentelemetry-plugin-http/src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const parseResponseStatus = (
9393
* Returns whether the Expect header is on the given options object.
9494
* @param options Options for http.request.
9595
*/
96-
export const hasExpectHeader = (options: RequestOptions | url.URL): boolean => {
96+
export const hasExpectHeader = (options: ParsedRequestOptions): boolean => {
9797
if (typeof (options as RequestOptions).headers !== 'object') {
9898
return false;
9999
}
@@ -200,7 +200,7 @@ export const setSpanWithError = (
200200
* @param [extraOptions] additional options for the request
201201
*/
202202
export const getRequestInfo = (
203-
options: RequestOptions | string,
203+
options: string | ParsedRequestOptions,
204204
extraOptions?: RequestOptions
205205
) => {
206206
let pathname = '/';

packages/opentelemetry-plugin-http/test/functionals/utils.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('Utility', () => {
7878
const urlParsed = url.parse(webUrl);
7979
const urlParsedWithoutPathname = {
8080
...urlParsed,
81-
pathname: undefined,
81+
pathname: null,
8282
};
8383
for (const param of [webUrl, urlParsed, urlParsedWithoutPathname]) {
8484
const result = utils.getRequestInfo(param);

packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
SimpleSpanProcessor,
3131
} from '@opentelemetry/tracing';
3232
import { HttpPluginConfig } from '../../src/types';
33+
import { RequestOptions } from 'https';
3334

3435
const serverPort = 32345;
3536
const hostname = 'localhost';
@@ -139,7 +140,7 @@ describe('HttpPlugin Integration tests', () => {
139140
const options = Object.assign(
140141
{ headers: { Expect: '100-continue' } },
141142
url.parse('http://google.fr/')
142-
);
143+
) as RequestOptions;
143144

144145
const result = await httpRequest.get(options);
145146
spans = memoryExporter.getFinishedSpans();

packages/opentelemetry-plugin-http/test/utils/assertSpan.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const assertSpan = (
3333
hostname: string;
3434
pathname: string;
3535
reqHeaders?: http.OutgoingHttpHeaders;
36-
path?: string;
36+
path?: string | null;
3737
forceStatus?: Status;
3838
component: string;
3939
}

packages/opentelemetry-plugin-http/test/utils/httpRequest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { RequestOptions } from 'https';
2020

2121
export const httpRequest = {
2222
get: (
23-
options: string | RequestOptions
23+
options: string | RequestOptions | url.URL
2424
): Promise<{
2525
data: string;
2626
statusCode: number | undefined;
@@ -34,7 +34,7 @@ export const httpRequest = {
3434
headers: {
3535
'user-agent': 'http-plugin-test',
3636
},
37-
})
37+
}) as RequestOptions
3838
: options;
3939
return new Promise((resolve, reject) => {
4040
const req = http.get(_options, (resp: http.IncomingMessage) => {

0 commit comments

Comments
 (0)