diff --git a/.eslintignore b/.eslintignore index 4208fbd3..aadc112d 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,5 +4,6 @@ lib es dist **/test-types/*.js +**/tsr-declarations.js # this file crashes eslint packages/zipkin-instrumentation-request-promise/src/promise.js diff --git a/package.json b/package.json index e4cecf30..01c89bdd 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "lint:ts": "tslint -c tslint.json -e './packages/**/node_modules/**/*.ts' './packages/**/*.ts'", "lint": "npm-run-all --parallel lint:*", "test:es": "npm run lerna-test", - "test:ts": "tsr packages/zipkin/test-types/*.test.ts --noAnnotate --libDeclarations && mocha --opts test/mocha-types.opts", + "test:ts": "tsr packages/*/test-types/*.test.ts --noAnnotate --libDeclarations && mocha --opts test/mocha-types.opts", "test": "npm run test:es && npm run test:ts", "lerna-test": "lerna run test", "lerna-test-browser": "lerna run test-browser", diff --git a/packages/zipkin-transport-http/index.d.ts b/packages/zipkin-transport-http/index.d.ts index 834670ad..57487c0d 100644 --- a/packages/zipkin-transport-http/index.d.ts +++ b/packages/zipkin-transport-http/index.d.ts @@ -1,14 +1,23 @@ +import {Agent as HttpAgent} from 'http'; +import {Agent as HttpsAgent} from 'https'; +import {URL} from 'url'; + import {JsonEncoder, Logger, model} from 'zipkin'; +type Agent = HttpAgent | HttpsAgent; + declare class HttpLogger implements Logger { constructor(options: { endpoint: string, httpInterval?: number, jsonEncoder?: JsonEncoder, - httpTimeout?: number, + timeout?: number, + maxPayloadSize?: number, headers?: { [name: string]: any }, + agent?: Agent | ((url: URL) => Agent), log?: Console }); + logSpan(span: model.Span): void; } export {HttpLogger}; diff --git a/packages/zipkin-transport-http/src/HttpLogger.js b/packages/zipkin-transport-http/src/HttpLogger.js index 3dae65f9..a20186dc 100755 --- a/packages/zipkin-transport-http/src/HttpLogger.js +++ b/packages/zipkin-transport-http/src/HttpLogger.js @@ -13,6 +13,7 @@ class HttpLogger extends EventEmitter { constructor({ endpoint, headers = {}, + agent = null, httpInterval = 1000, jsonEncoder = JSON_V1, timeout = 0, @@ -23,6 +24,7 @@ class HttpLogger extends EventEmitter { super(); // must be before any reference to *this* this.log = log; this.endpoint = endpoint; + this.agent = agent; this.maxPayloadSize = maxPayloadSize; this.queue = []; this.queueBytes = 0; @@ -88,6 +90,7 @@ class HttpLogger extends EventEmitter { body: postBody, headers: self.headers, timeout: self.timeout, + agent: self.agent }).then((response) => { if (response.status !== 202 && response.status !== 200) { const err = 'Unexpected response while sending Zipkin data, status:' diff --git a/packages/zipkin-transport-http/test-types/HttpLogger.test.ts b/packages/zipkin-transport-http/test-types/HttpLogger.test.ts new file mode 100644 index 00000000..5d28c49b --- /dev/null +++ b/packages/zipkin-transport-http/test-types/HttpLogger.test.ts @@ -0,0 +1,41 @@ +import { expect } from 'chai'; + +import {Agent as HttpAgent } from 'http'; +import {Agent as HttpsAgent} from 'https'; +import {jsonEncoder} from 'zipkin'; + +import { HttpLogger } from 'zipkin-transport-http'; + +describe('HttpLogger', () => { + it('should have correct type', () => { + const options = { + endpoint: 'testEndpoint', + httpInterval: 1000, + jsonEncoder: jsonEncoder.JSON_V1, + timeout: 0, + maxPayloadSize: 0, + headers: {}, + agent: new HttpAgent(), + log: console + }; + const httpLogger: HttpLogger = new HttpLogger(options); + + expect(httpLogger.logSpan).to.be.a('function'); + }); + + it('should accept Http(s) Agent or function which returns Agent', () => { + const agents = [new HttpAgent(), new HttpsAgent(), () => new HttpAgent(), () => new HttpsAgent(), + (url: URL) => new HttpAgent(), (url: URL) => new HttpsAgent(), null, undefined]; + + agents.forEach(agent => { + const options = { + endpoint: 'testEndpoint', + agent + }; + + const httpLogger: HttpLogger = new HttpLogger(options); + + expect(httpLogger).to.have.property('agent', agent || null); + }); + }); +});