Skip to content

Commit 0f4307d

Browse files
Merge pull request #24 from ExpediaDotCom/http-collector-dispatcher
adding support for http collector dispatcher
2 parents 2c18f9f + aeec7fb commit 0f4307d

File tree

6 files changed

+80
-7
lines changed

6 files changed

+80
-7
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
"google-protobuf": "^3.0.0",
2121
"grpc": "^1.9.0",
2222
"opentracing": "^0.14.0",
23-
"uuid": "^3.2.1"
23+
"uuid": "^3.2.1",
24+
"request": "^2.88.0"
2425
},
2526
"devDependencies": {
2627
"@types/node": "7.0.12",
2728
"@types/chai": "3.4.35",
2829
"@types/mocha": "2.2.40",
2930
"@types/kafka-node": "2.0.7",
31+
"@types/request": "2.48.0",
3032
"kafka-node": "2.6.1",
3133
"chai": "^3.5.0",
3234
"grpc-tools": "1.6.6",

src/configuration.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616

1717
import FileDispatcher from './dispatchers/file';
18-
import RemoteDispatcher from './dispatchers/remote';
19-
import {Dispatcher} from './dispatchers/dispatcher';
18+
import RemoteGrpcAgentDispatcher from './dispatchers/grpc_agent';
19+
import { Dispatcher } from './dispatchers/dispatcher';
2020
import InMemoryDispatcher from './dispatchers/in_memory';
2121
import NoopDispatcher from './dispatchers/noop';
22+
import HttpCollectorDispatcher from './dispatchers/http_collector';
2223
import { DispatcherConfig } from './dispatchers/dispatcher-config';
2324
import { TracerConfig } from './tracer-config';
2425

@@ -31,7 +32,9 @@ export default class Configuration {
3132
case 'file':
3233
return new FileDispatcher(dispatcher.filePath);
3334
case 'haystack_agent':
34-
return new RemoteDispatcher(dispatcher.agentHost, dispatcher.agentPort, config.logger);
35+
return new RemoteGrpcAgentDispatcher(dispatcher.agentHost, dispatcher.agentPort, config.logger);
36+
case 'http_collector':
37+
return new HttpCollectorDispatcher(dispatcher.collectorUrl, dispatcher.collectorHttpHeaders, config.logger);
3538
case 'in_memory':
3639
return new InMemoryDispatcher();
3740
default:

src/dispatchers/dispatcher-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ export interface DispatcherConfig {
1919
filePath?: string;
2020
agentHost?: string;
2121
agentPort?: number;
22+
collectorUrl?: string;
23+
collectorHttpHeaders?: { [key: string]: any };
2224
}

src/dispatchers/remote.ts renamed to src/dispatchers/grpc_agent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ export default class RemoteDispatcher implements Dispatcher {
2626
_logger: any;
2727

2828
constructor(agentHost: string = 'haystack-agent', agentPort: number = 35000, logger: Logger = new NullLogger()) {
29-
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
29+
logger.info(`Initializing the remote grpc agent dispatcher, connecting at ${agentHost}:${agentPort}`);
3030
this._client = new services.SpanAgentClient(`${agentHost}:${agentPort}`, grpc.credentials.createInsecure());
3131
this._logger = logger;
3232
}
3333

3434
name(): string {
35-
return 'RemoteDispatcher';
35+
return 'GrpcAgentDispatcher';
3636
}
3737

3838
dispatch(span: Span, callback: (error) => void): void {

src/dispatchers/http_collector.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/*
3+
* Copyright 2018 Expedia, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {Dispatcher} from './dispatcher';
19+
import Span from '../span';
20+
import { Logger, NullLogger } from '../logger';
21+
import Utils from '../utils';
22+
import {CoreOptions, Response} from 'request';
23+
import * as request from 'request';
24+
25+
export default class HttpCollectorDispatcher implements Dispatcher {
26+
_collectorUrl: string;
27+
_headers: { [key: string]: any };
28+
_logger: Logger;
29+
30+
constructor(_collectorUrl: string = 'http://localhost:8080/span', headers: { [key: string]: any } = {}, logger: Logger = new NullLogger()) {
31+
this._collectorUrl = _collectorUrl;
32+
this._headers = headers;
33+
this._logger = logger;
34+
this._logger.info(`Initializing the http collector dispatcher, connecting at ${_collectorUrl}`);
35+
}
36+
37+
name(): string {
38+
return 'HttpCollectorDispatcher';
39+
}
40+
41+
dispatch(span: Span, callback: (error) => void): void {
42+
const serializedSpanBuffer = Utils.convertToProtoSpan(span).serializeBinary();
43+
const options: CoreOptions = {};
44+
options.headers = this._headers;
45+
options.body = serializedSpanBuffer;
46+
request.post(this._collectorUrl, options, (error: any, response: Response, body: any) => {
47+
if (error) {
48+
this._logger.error(`Fail to dipatch to http collector with error ${error}`);
49+
return;
50+
}
51+
if (response.statusCode < 200 || response.statusCode >= 300) {
52+
this._logger.error(`Fail to dispatch to http collector with statusCode ${response.statusCode} and response ${body}`);
53+
return;
54+
}
55+
this._logger.debug(`successfully submitted the span to http collector with http response: ${body}`);
56+
});
57+
}
58+
59+
close(callback: () => void): void {
60+
if (callback) {
61+
callback();
62+
}
63+
}
64+
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import Tracer from './tracer';
2121
import NoopDispatcher from './dispatchers/noop';
2222
import InMemoryDispatcher from './dispatchers/in_memory';
2323
import FileDispatcher from './dispatchers/file';
24-
import AgentDispatcher from './dispatchers/remote';
24+
import AgentDispatcher from './dispatchers/grpc_agent';
25+
import HttpCollectorDispatcher from './dispatchers/http_collector';
2526
import { DispatcherConfig } from './dispatchers/dispatcher-config';
2627
import Configuration from './configuration';
2728
import { Logger } from './logger';
@@ -39,6 +40,7 @@ export {
3940
InMemoryDispatcher,
4041
FileDispatcher,
4142
AgentDispatcher,
43+
HttpCollectorDispatcher,
4244
DispatcherConfig,
4345
opentracing,
4446
Logger

0 commit comments

Comments
 (0)