Skip to content

Commit

Permalink
fix(plugin-http): typings
Browse files Browse the repository at this point in the history
closes #541

Signed-off-by: Olivier Albertini <[email protected]>
  • Loading branch information
OlivierAlbertini committed Nov 16, 2019
1 parent d80cf56 commit cf5bed1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 21 deletions.
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
10 changes: 7 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,12 @@ 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
4 changes: 2 additions & 2 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 @@ -34,7 +34,7 @@ export const httpRequest = {
headers: {
'user-agent': 'http-plugin-test',
},
})
}) as RequestOptions
: options;
return new Promise((resolve, reject) => {
const req = http.get(_options, (resp: http.IncomingMessage) => {
Expand Down

0 comments on commit cf5bed1

Please sign in to comment.