Skip to content

Commit a0d7204

Browse files
authored
Bug fixes (#3)
* bug fixes for handling tag values of float-type * minor improvement in the code comments * adding tslint * adding caller's span context option to startSpan
1 parent e914b7b commit a0d7204

19 files changed

Lines changed: 261 additions & 232 deletions

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ check-node-version:
1212
@$(MIN_NODE_VER_FOUND) || echo Build requires minimum Node $(MIN_NODE_VER).x
1313

1414
.PHONY: build
15-
build: check-node-version npm_install idl_codegen
15+
build: check-node-version npm_install idl_codegen tslint
1616
rm -rf ./dist/
1717
tsc -p tsconfig.json
1818
cp package.json dist/
1919

20+
.PHONY: tslnt
21+
tslint:
22+
$(shell ./node_modules/tslint/bin/tslint -t msbuild -c tslint.json 'src/**/*.ts')
23+
2024
.PHONY: idl_codegen
2125
idl_codegen:
2226
rm -rf src/proto_idl_codegen
@@ -33,5 +37,5 @@ npm_install:
3337
example: build
3438
mkdir -p logs
3539
rm -rf logs/spans
36-
node dist/examples/index.js
40+
node examples/index.js
3741
cat logs/spans

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is Haystack's client library for Nodejs that implements [OpenTracing API 1.
88

99
## How to use the library?
1010

11-
Check our detailed [example](src/examples/) on how to initialize tracer, start a span and send it to one of the dispatchers.
11+
Check our detailed [example](examples/index.js) on how to initialize tracer, start a span and send it to one of the dispatchers.
1212

1313

1414
## How to build this library?
Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
*/
1616

1717

18-
/// first do `npm install haystack-client`
19-
const initTracer = require('../index').initTracer;
20-
const opentracing = require('opentracing');
21-
import MyLogger from "./logger";
18+
"use strict";
2219

20+
/// first do `npm install haystack-client` and replace '../dist/index' with 'haystack-client'
21+
const initTracer = require('../dist/index').initTracer;
22+
const opentracing = require('opentracing');
23+
const MyLogger = require('./logger');
2324

2425
/// setup a logger. if you skip providing logger to config, the library will not spit any errors, warning or info
2526
/// you can provide the logger object already configured in your service, provided it has 4 methods defined:
2627
// debug(msg), info(msg), error(msg), warn(msg)
27-
const logger = new MyLogger();
28+
const logger = new MyLogger.default();
2829

2930

3031
/// setup the config object required for initializing Tracer
@@ -42,7 +43,7 @@ const config = {
4243
// or
4344

4445
// type: 'haystack_agent',
45-
// agentHost: 'haystack-agent',
46+
// agentHost: '192.168.99.100',
4647
// agentPort: '35000'
4748
},
4849
logger: logger
@@ -53,26 +54,34 @@ const tracer = initTracer(config);
5354

5455
/// now create a span, for e.g. at the time of incoming REST call.
5556
/// Make sure to add SPAN_KIND tag. Possible values are 'server' or 'client'.
57+
/// Important: if you are receiving TraceId, SpanId, ParentSpanId in the http headers or message payload of your incoming REST call,
58+
/// then set it in the callerSpanContext
59+
5660
const serverSpan = tracer
57-
.startSpan('dummy-operation')
61+
.startSpan('dummy-operation', {
62+
callerSpanContext: {
63+
_traceId: '1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
64+
_spanId: '7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
65+
_parentSpanId: 'e96de653-ad6e-4ad5-b437-e81fd9d2d61d'
66+
}
67+
})
5868
.setTag(opentracing.Tags.SPAN_KIND, 'server')
5969
.setTag(opentracing.Tags.HTTP_METHOD, 'GET');
6070

71+
/// Or if you are the root service, skip callerSpanContext and use the following
72+
73+
// const serverSpan = tracer
74+
// .startSpan('dummy-operation')
75+
// .setTag(opentracing.Tags.SPAN_KIND, 'server')
76+
// .setTag(opentracing.Tags.HTTP_METHOD, 'GET');
6177

6278

63-
/// Important:
64-
// if you are receiving TraceId, SpanId, ParentSpanId in the http headers or message payload of your incoming REST call,
65-
/// then update the IDs in the span context, else tracer will use the unique TraceId and SpanId.
66-
/*
67-
serverSpan.context().setTraceId(<TRACE ID> );
68-
serverSpan.context().setSpanId(<SPAN ID>);
69-
serverSpan.context().setParentSpanId(<PARENT SPAN ID>);
70-
*/
7179

7280

7381
/// now say service is calling downstream service, then you start another span - a client span
7482
/// since this span is a child of the main serverSpan, so pass it along as `childOf` attribute.
7583
/// library will setup the traceId, spanId and parentSpanId by itself.
84+
/// You dont need to set the callerSpanContext if you are setting childOf
7685
const clientChildSpan = tracer.startSpan('downstream-service-call', {
7786
childOf: serverSpan,
7887
tags: {
@@ -82,9 +91,11 @@ const clientChildSpan = tracer.startSpan('downstream-service-call', {
8291

8392

8493
/// add more tags or logs to your spans
85-
clientChildSpan.setTag(opentracing.Tags.ERROR, false);
86-
serverSpan.setTag(opentracing.Tags.ERROR, false);
94+
clientChildSpan.setTag(opentracing.Tags.ERROR, true);
95+
clientChildSpan.setTag(opentracing.Tags.HTTP_STATUS_CODE, 503);
8796

97+
serverSpan.setTag(opentracing.Tags.ERROR, true);
98+
serverSpan.setTag('my-custom-tag', 10.5);
8899

89100
/// finish the downstream call span. This will publish the span to either file or haystack-agent
90101
clientChildSpan.finish();
@@ -93,4 +104,6 @@ clientChildSpan.finish();
93104
serverSpan.finish();
94105

95106
/// close the tracer at the time of service shutdown
96-
tracer.close();
107+
setTimeout(() => {
108+
tracer.close();
109+
}, 3000);
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
export default class MyLogger {
18-
constructor() {}
17+
"use strict";
1918

20-
debug(message) {
21-
console.log(message);
19+
const MyLogger = (function () {
20+
function MyLogger() {
2221
}
23-
24-
info(message) {
22+
MyLogger.prototype.debug = function (message) {
2523
console.log(message);
26-
}
27-
28-
warn(message) {
24+
};
25+
MyLogger.prototype.info = function (message) {
2926
console.log(message);
30-
}
31-
32-
error(message) {
27+
};
28+
MyLogger.prototype.warn = function (message) {
3329
console.log(message);
34-
}
35-
}
30+
};
31+
MyLogger.prototype.error = function (message) {
32+
console.log(message);
33+
};
34+
return MyLogger;
35+
}());
36+
37+
exports.default = MyLogger;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"type": "git",
1414
"url": "git://github.com/ExpediaDotCom/haystack-client-node.git"
1515
},
16+
"scripts": {
17+
"lint": "tslint -t msbuild -c tslint.json 'src/**/*.ts'"
18+
},
1619
"contributors": [
1720
"Haystack <haystack@expedia.com>"
1821
],
@@ -25,6 +28,7 @@
2528
"devDependencies": {
2629
"@types/node": "^10.3.4",
2730
"typescript": "2.9.2",
28-
"grpc-tools": "1.6.6"
31+
"grpc-tools": "1.6.6",
32+
"tslint": "^5.0.0"
2933
}
3034
}

src/configuration.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
import FileDispatcher from "./dispatchers/file";
18-
import RemoteDispatcher from "./dispatchers/remote";
19-
import {Dispatcher} from "./dispatchers/dispatcher";
20-
import InMemoryDispatcher from "./dispatchers/in_memory";
21-
import NoopDispatcher from "./dispatchers/noop";
17+
import FileDispatcher from './dispatchers/file';
18+
import RemoteDispatcher from './dispatchers/remote';
19+
import {Dispatcher} from './dispatchers/dispatcher';
20+
import InMemoryDispatcher from './dispatchers/in_memory';
21+
import NoopDispatcher from './dispatchers/noop';
2222

2323
export default class Configuration {
2424
static _getDispatcher(config): Dispatcher {
2525

26-
let dispatcher = config.dispatcher;
26+
const dispatcher = config.dispatcher;
2727
if (dispatcher) {
2828
switch (dispatcher.type) {
2929
case 'file':

src/dispatchers/composite_dispatchers.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/dispatchers/dispatcher.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import Span from "../span";
17+
import Span from '../span';
1818

1919
export interface Dispatcher {
20-
name(): string
21-
dispatch(span: Span): void
22-
close(callback: () => void): void
23-
}
20+
name(): string;
21+
dispatch(span: Span): void;
22+
close(callback: () => void): void;
23+
}

src/dispatchers/file.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,33 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as fs from "fs";
18-
import Span from "../span";
19-
import {Dispatcher} from "./dispatcher";
17+
import * as fs from 'fs';
18+
import Span from '../span';
19+
import {Dispatcher} from './dispatcher';
2020

2121
export default class FileDispatcher implements Dispatcher {
2222
_spanFileStream: fs.WriteStream;
2323

2424
constructor(_filePath: string) {
25-
if(!_filePath) {
25+
if (!_filePath) {
2626
throw new Error(`Fail to create file dispatcher without valid 'filePath' in the config`);
2727
}
28-
this._spanFileStream = fs.createWriteStream(_filePath, { 'flags': 'a' });
28+
this._spanFileStream = fs.createWriteStream(_filePath, { flags: 'a' });
2929
}
3030

31-
name() {
32-
return "FileDispatcher";
31+
name(): string {
32+
return 'FileDispatcher';
3333
}
3434

35-
dispatch(span: Span) {
35+
dispatch(span: Span): void {
3636
this._spanFileStream.write(span.toString());
37-
this._spanFileStream.write("\n");
37+
this._spanFileStream.write('\n');
3838
}
3939

40-
close(callback: () => void) {
41-
this._spanFileStream.end("\n");
40+
close(callback: () => void): void {
41+
this._spanFileStream.end('\n');
4242
if (callback) {
4343
callback();
4444
}
4545
}
4646
}
47-

src/dispatchers/in_memory.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17-
import Span from "../span";
18-
import {Dispatcher} from "./dispatcher";
17+
import Span from '../span';
18+
import {Dispatcher} from './dispatcher';
1919

2020
export default class InMemoryDispatcher implements Dispatcher {
21-
_spans: Array<Span>;
21+
_spans: Span[];
2222

2323
constructor() {
2424
this._spans = [];
2525
}
2626

27-
name() {
28-
return "InMemoryDispatcher";
27+
name(): string {
28+
return 'InMemoryDispatcher';
2929
}
3030

31-
dispatch(span: Span) {
31+
dispatch(span: Span): void {
3232
this._spans.push(span);
3333
}
3434

35-
close(callback: () => void) {
35+
close(callback: () => void): void {
3636
if (callback) {
3737
callback();
3838
}

0 commit comments

Comments
 (0)