Skip to content

Commit

Permalink
Adds agent option to http transport (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAWeber authored and adriancole committed Aug 19, 2019
1 parent 0db501d commit 98b3a68
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion packages/zipkin-transport-http/index.d.ts
Original file line number Diff line number Diff line change
@@ -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};
3 changes: 3 additions & 0 deletions packages/zipkin-transport-http/src/HttpLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class HttpLogger extends EventEmitter {
constructor({
endpoint,
headers = {},
agent = null,
httpInterval = 1000,
jsonEncoder = JSON_V1,
timeout = 0,
Expand All @@ -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;
Expand Down Expand Up @@ -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:'
Expand Down
41 changes: 41 additions & 0 deletions packages/zipkin-transport-http/test-types/HttpLogger.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 98b3a68

Please sign in to comment.