Skip to content
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

fix(plugin-http): typings #545

Closed
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
16 changes: 8 additions & 8 deletions packages/opentelemetry-plugin-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@
"@opentelemetry/node": "^0.2.0",
"@opentelemetry/scope-base": "^0.2.0",
"@opentelemetry/tracing": "^0.2.0",
"@types/got": "^9.6.7",
"@types/got": "^9.6.9",
"@types/mocha": "^5.2.7",
"@types/nock": "^11.1.0",
"@types/node": "^12.7.9",
"@types/node": "^12.12.8",
"@types/request-promise-native": "^1.0.17",
"@types/semver": "^6.0.2",
"@types/semver": "^6.2.0",
"@types/shimmer": "^1.0.1",
"@types/sinon": "^7.0.13",
"@types/superagent": "^4.1.3",
"@types/sinon": "^7.5.0",
"@types/superagent": "^4.1.4",
"axios": "^0.19.0",
"codecov": "^3.6.1",
"got": "^9.6.0",
"gts": "^1.1.0",
"mocha": "^6.2.1",
"nock": "^11.3.5",
"mocha": "^6.2.2",
"nock": "^11.7.0",
"nyc": "^14.1.1",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"request-promise-native": "^1.0.8",
"rimraf": "^3.0.0",
"sinon": "^7.5.0",
"superagent": "5.1.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ export class HttpPlugin extends BasePlugin<Http> {
this._logger.debug('makeRequestTrace by injecting context into header');

const host = options.hostname || options.host || 'localhost';
const method = options.method ? options.method.toUpperCase() : 'GET';
const headers = options.headers || {};
const method = (options as RequestOptions).method
? (options as RequestOptions).method!.toUpperCase()
: 'GET';
const headers = (options as RequestOptions).headers || {};
const userAgent = headers['user-agent'];

span.setAttributes({
Expand Down Expand Up @@ -409,7 +411,7 @@ export class HttpPlugin extends BasePlugin<Http> {
: undefined
);

options = optionsParsed;
options = optionsParsed as RequestOptions;

if (
utils.isOpenTelemetryRequest(options) ||
Expand Down
11 changes: 8 additions & 3 deletions packages/opentelemetry-plugin-http/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
get,
} from 'http';
import * as http from 'http';
import { ParsedUrlQuery } from 'querystring';

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

export type HttpRequestArgs = Array<HttpCallbackOptional | RequestSignature>;

export type ParsedRequestOptions =
| (http.RequestOptions & Partial<url.UrlWithParsedQuery>)
| http.RequestOptions;
export type ParsedRequestOptions = (
| http.RequestOptions
| Omit<url.UrlWithParsedQuery, 'query'>
) & {
protocol?: string | null;
query?: string | ParsedUrlQuery | null;
};
export type Http = typeof http;
/* tslint:disable-next-line:no-any */
export type Func<T> = (...args: any[]) => T;
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-plugin-http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const parseResponseStatus = (
* Returns whether the Expect header is on the given options object.
* @param options Options for http.request.
*/
export const hasExpectHeader = (options: RequestOptions | url.URL): boolean => {
export const hasExpectHeader = (options: ParsedRequestOptions): boolean => {
if (typeof (options as RequestOptions).headers !== 'object') {
return false;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ export const setSpanWithError = (
* @param [extraOptions] additional options for the request
*/
export const getRequestInfo = (
options: RequestOptions | string,
options: string | ParsedRequestOptions,
extraOptions?: RequestOptions
) => {
let pathname = '/';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Utility', () => {
const urlParsed = url.parse(webUrl);
const urlParsedWithoutPathname = {
...urlParsed,
pathname: undefined,
pathname: null,
};
for (const param of [webUrl, urlParsed, urlParsedWithoutPathname]) {
const result = utils.getRequestInfo(param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
SimpleSpanProcessor,
} from '@opentelemetry/tracing';
import { HttpPluginConfig } from '../../src/types';
import { RequestOptions } from 'http';

const serverPort = 32345;
const hostname = 'localhost';
Expand Down Expand Up @@ -139,7 +140,7 @@ describe('HttpPlugin Integration tests', () => {
const options = Object.assign(
{ headers: { Expect: '100-continue' } },
url.parse('http://google.fr/')
);
) as RequestOptions;

const result = await httpRequest.get(options);
spans = memoryExporter.getFinishedSpans();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const assertSpan = (
hostname: string;
pathname: string;
reqHeaders?: http.OutgoingHttpHeaders;
path?: string;
path?: string | null;
forceStatus?: Status;
component: string;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-plugin-http/test/utils/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { RequestOptions } from 'https';

export const httpRequest = {
get: (
options: string | RequestOptions
options: string | RequestOptions | url.URL
): Promise<{
data: string;
statusCode: number | undefined;
Expand All @@ -30,11 +30,11 @@ export const httpRequest = {
}> => {
const _options =
typeof options === 'string'
? Object.assign(url.parse(options), {
? (Object.assign(url.parse(options), {
headers: {
'user-agent': 'http-plugin-test',
},
})
}) as RequestOptions)
: options;
return new Promise((resolve, reject) => {
const req = http.get(_options, (resp: http.IncomingMessage) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { assertSpan } from '../utils/assertSpan';
import { DummyPropagation } from '../utils/DummyPropagation';
import { httpsRequest } from '../utils/httpsRequest';
import * as utils from '../utils/utils';
import { RequestOptions } from 'https';

const serverPort = 42345;
const hostname = 'localhost';
Expand Down Expand Up @@ -140,7 +141,7 @@ describe('HttpsPlugin Integration tests', () => {
const options = Object.assign(
{ headers: { Expect: '100-continue' } },
url.parse('https://google.fr/')
);
) as RequestOptions;

const result = await httpsRequest.get(options);
spans = memoryExporter.getFinishedSpans();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const assertSpan = (
hostname: string;
pathname: string;
reqHeaders?: http.OutgoingHttpHeaders;
path?: string;
path?: string | null;
component: string;
}
) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

export const httpsRequest = {
get: (
options: string | RequestOptions
options: string | RequestOptions | url.URL
): Promise<{
data: string;
statusCode: number | undefined;
Expand All @@ -33,11 +33,11 @@ export const httpsRequest = {
}> => {
const _options =
typeof options === 'string'
? Object.assign(url.parse(options), {
? (Object.assign(url.parse(options), {
headers: {
'user-agent': 'https-plugin-test',
},
})
}) as RequestOptions)
: options;
return new Promise((resolve, reject) => {
const req = https.get(_options, (resp: http.IncomingMessage) => {
Expand Down