Skip to content

Commit e914b7b

Browse files
authored
Merge pull request #2 from ExpediaDotCom/code-refactoring
Code refactoring
2 parents 7fac287 + 38e4a8f commit e914b7b

17 files changed

Lines changed: 50 additions & 628 deletions

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ idl_codegen:
2929
.PHONY: npm_install
3030
npm_install:
3131
npm install
32+
33+
example: build
34+
mkdir -p logs
35+
rm -rf logs/spans
36+
node dist/examples/index.js
37+
cat logs/spans

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Haystack bindings for Nodejs OpenTracing API.
55

6-
This is Haystack's client library for Nodejs that implements [OpenTracing API 1.0]((https://github.com/opentracing/opentracing-javascript/).
6+
This is Haystack's client library for Nodejs that implements [OpenTracing API 1.0](https://github.com/opentracing/opentracing-javascript/).
77

88

99
## How to use the library?
@@ -15,7 +15,7 @@ Check our detailed [example](src/examples/) on how to initialize tracer, start a
1515

1616
`make build`
1717

18-
This will create dist/ folder with the compiled js files.
18+
This library has been written in typescript, so we first compile them into js files under dist/ folder
1919

2020
## How to run the example code
2121
```bash

src/configuration.js

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

src/dispatchers/composite_dispatchers.js

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

src/dispatchers/file.js

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

src/dispatchers/in_memory.js

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

src/dispatchers/noop.js

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

src/dispatchers/remote.js

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

src/examples/index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,21 @@
1616

1717

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

2223

23-
/// setup a logger, you can skip this but note that library wont spit out any errors, warning or info
24-
var MyLogger = (function () {
25-
function MyLogger() {
26-
}
27-
MyLogger.prototype.debug = function (msg) { console.log(msg); };
28-
MyLogger.prototype.info = function (msg) { console.log(msg); };
29-
MyLogger.prototype.warn = function (msg) { console.log(msg); };
30-
MyLogger.prototype.error = function (msg) { console.log(msg); };
31-
return MyLogger;
32-
}());
33-
var _logger = new MyLogger();
34-
24+
/// setup a logger. if you skip providing logger to config, the library will not spit any errors, warning or info
25+
/// you can provide the logger object already configured in your service, provided it has 4 methods defined:
26+
// debug(msg), info(msg), error(msg), warn(msg)
27+
const logger = new MyLogger();
3528

3629

3730
/// setup the config object required for initializing Tracer
38-
/// Use `file` dispatcher for local development else you haystack_agent for other environment like prod
39-
/// commonTags are the tags that are injected in every span emitted by your app.
40-
var config = {
31+
/// Use `file` dispatcher for local development else use haystack_agent for environments like prod
32+
/// commonTags are the tags that are injected in every span emitted by your service.
33+
const config = {
4134
serviceName: 'dummy-service',
4235
commonTags: {
4336
'dummy-service-version': '0.1.0'
@@ -52,15 +45,15 @@ var config = {
5245
// agentHost: 'haystack-agent',
5346
// agentPort: '35000'
5447
},
55-
logger: _logger
48+
logger: logger
5649
};
5750

5851
/// initialize the tracer only once at the time of your service startup
59-
var tracer = initTracer(config);
52+
const tracer = initTracer(config);
6053

6154
/// now create a span, for e.g. at the time of incoming REST call.
6255
/// Make sure to add SPAN_KIND tag. Possible values are 'server' or 'client'.
63-
var serverSpan = tracer
56+
const serverSpan = tracer
6457
.startSpan('dummy-operation')
6558
.setTag(opentracing.Tags.SPAN_KIND, 'server')
6659
.setTag(opentracing.Tags.HTTP_METHOD, 'GET');
@@ -77,10 +70,10 @@ var serverSpan = tracer
7770
*/
7871

7972

80-
/// now say service is calling downstream app, then you start another span - a client span
73+
/// now say service is calling downstream service, then you start another span - a client span
8174
/// since this span is a child of the main serverSpan, so pass it along as `childOf` attribute.
8275
/// library will setup the traceId, spanId and parentSpanId by itself.
83-
var clientChildSpan = tracer.startSpan('downstream-service-call', {
76+
const clientChildSpan = tracer.startSpan('downstream-service-call', {
8477
childOf: serverSpan,
8578
tags: {
8679
'span.kind': 'client' // Note `span.kind` is now `client`
@@ -96,5 +89,8 @@ serverSpan.setTag(opentracing.Tags.ERROR, false);
9689
/// finish the downstream call span. This will publish the span to either file or haystack-agent
9790
clientChildSpan.finish();
9891

99-
/// finish the server span at the time it sending the response back to the client
92+
/// finish the server span when your service is ready to send the response back to upstream
10093
serverSpan.finish();
94+
95+
/// close the tracer at the time of service shutdown
96+
tracer.close();
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
"use strict";
17-
Object.defineProperty(exports, "__esModule", { value: true });
16+
17+
export default class MyLogger {
18+
constructor() {}
19+
20+
debug(message) {
21+
console.log(message);
22+
}
23+
24+
info(message) {
25+
console.log(message);
26+
}
27+
28+
warn(message) {
29+
console.log(message);
30+
}
31+
32+
error(message) {
33+
console.log(message);
34+
}
35+
}

0 commit comments

Comments
 (0)