Skip to content

Commit 5dd42cf

Browse files
authored
Merge pull request #6 from ExpediaDotCom/example-improvement
Example improvement
2 parents ca78b22 + 3d1d89f commit 5dd42cf

8 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class Configuration {
3333
case 'in_memory':
3434
return new InMemoryDispatcher();
3535
default:
36-
throw new Error(`reporter of type ${dispatcher} is not unknown`);
36+
throw new Error(`dispatcher of type ${dispatcher} is not unknown`);
3737
}
3838
}
3939
return new NoopDispatcher();

src/dispatchers/dispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ import Span from '../span';
1818

1919
export interface Dispatcher {
2020
name(): string;
21-
dispatch(span: Span): void;
21+
dispatch(span: Span, callback: (error) => void): void;
2222
close(callback: () => void): void;
2323
}

src/dispatchers/file.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ export default class FileDispatcher implements Dispatcher {
3232
return 'FileDispatcher';
3333
}
3434

35-
dispatch(span: Span): void {
35+
dispatch(span: Span, callback: (error) => void): void {
3636
this._spanFileStream.write(span.toString());
3737
this._spanFileStream.write('\n');
38+
if (callback) {
39+
callback(null);
40+
}
3841
}
3942

4043
close(callback: () => void): void {

src/dispatchers/in_memory.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ export default class InMemoryDispatcher implements Dispatcher {
2828
return 'InMemoryDispatcher';
2929
}
3030

31-
dispatch(span: Span): void {
31+
dispatch(span: Span, callback: (error) => void): void {
3232
this._spans.push(span);
33+
if (callback) {
34+
callback(null);
35+
}
3336
}
3437

3538
close(callback: () => void): void {

src/dispatchers/noop.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export default class NoopDispatcher implements Dispatcher {
2424
return 'NoopDispatcher';
2525
}
2626

27-
dispatch(span: Span): void { }
27+
dispatch(span: Span, callback: (error) => void): void {
28+
if (callback) {
29+
callback(null);
30+
}
31+
}
2832

2933
close(callback: () => void): void {
3034
if (callback) {

src/dispatchers/remote.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@ export default class RemoteDispatcher implements Dispatcher {
3636
return 'RemoteDispatcher';
3737
}
3838

39-
dispatch(span: Span): void {
39+
dispatch(span: Span, callback: (error) => void): void {
4040
const proto = this._convertToProtoSpan(span);
4141
this._client.dispatch(proto, (err, response) => {
4242
if (this._logger) {
4343
if (err) {
4444
this._logger.error(`Fail to dispatch span to haystack-agent ${err.toString()}`);
45+
if (callback) {
46+
callback(new Error(err));
47+
}
4548
} else {
4649
this._logger.debug(`grpc response code from haystack-agent - ${response.getCode()}`);
50+
if (callback) {
51+
callback(null);
52+
}
4753
}
4854
}
4955
});

src/span.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ export default class Span {
127127
});
128128
}
129129

130-
finish(finishTime?: number): void {
130+
finish(finishTime?: number, callback?: (error) => void): void {
131131
if (this._isFinished) {
132132
const spanInfo = `operation=${this.operationName},context=${this.context().toString()}`;
133133
throw new Error(`cant finish the same span twice - ${spanInfo}`);
134134
}
135135
const endTime = finishTime || Utils.now();
136136
this._duration = endTime - this._startTime;
137-
this._tracer.dispatcher().dispatch(this);
137+
this._tracer.dispatcher().dispatch(this, callback);
138138
this._isFinished = true;
139139
}
140140

src/tracer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ export default class Tracer {
112112
return this._dispatcher;
113113
}
114114

115-
close(): void {
115+
close(callback: () => void): void {
116116
this._dispatcher.close(() => {
117117
if (this._logger) {
118118
this._logger.info('Tracer has been closed now.');
119119
}
120+
if (callback) {
121+
callback();
122+
}
120123
});
121124
}
122125

0 commit comments

Comments
 (0)