Skip to content

Commit 8f0ca33

Browse files
mchandramouliashishagg
authored andcommitted
adding the ability to inject propagation registry to register custom propagators (#29)
1 parent c9321b2 commit 8f0ca33

File tree

7 files changed

+72
-27
lines changed

7 files changed

+72
-27
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ logs
44
npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
7+
package-lock.json
78

89
# Runtime data
910
pids
@@ -65,4 +66,6 @@ src/proto_idl_codegen/
6566
dist/
6667
.idea/typescript-compiler.xml
6768
*.iml
69+
*.ipr
70+
*.iws
6871
ws2/

examples/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
/// first do `npm install haystack-client` and replace '../dist/index' with 'haystack-client'
2121
const initTracer = require('../dist/index').initTracer;
22-
const SpanContext = require('../dist/index').SpanContext;
22+
const PropagationRegistry = require('../dist/index').PropagationRegistry;
23+
const TextMapPropagator = require('../dist/index').TextMapPropagator;
24+
const DefaultCodex = require('../dist/index').DefaultCodex;
25+
const PropagatorOpts = require('../dist/index').PropagatorOpts;
2326

2427
const opentracing = require('opentracing');
2528
const MyLogger = require('./logger');
@@ -52,8 +55,14 @@ const config = {
5255
logger: logger
5356
};
5457

58+
/// ability to use custom propagation headers if needed
59+
const propagation = PropagationRegistry.default()
60+
.register(opentracing.FORMAT_HTTP_HEADERS,
61+
new TextMapPropagator(new DefaultCodex(),
62+
new PropagatorOpts('X-Trace-ID', 'X-Span-ID', 'X-Parent-ID')));
63+
5564
/// initialize the tracer only once at the time of your service startup
56-
const tracer = initTracer(config);
65+
const tracer = initTracer(config, propagation);
5766

5867
/// now create a span, for e.g. at the time of incoming REST call.
5968
/// Make sure to add SPAN_KIND tag. Possible values are 'server' or 'client'.
@@ -65,15 +74,15 @@ const tracer = initTracer(config);
6574
const serverSpan = tracer
6675
.startSpan('/foo', {
6776
childOf: tracer.extract(opentracing.FORMAT_HTTP_HEADERS, {
68-
'Trace-ID': '1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
69-
'Span-ID': '7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
70-
'Parent-ID': 'e96de653-ad6e-4ad5-b437-e81fd9d2d61d'
77+
'X-Trace-ID': '1848fadd-fa16-4b3e-8ad1-6d73339bbee7',
78+
'X-Span-ID': '7a7cc5bf-796e-4527-9b42-13ae5766c6fd',
79+
'X-Parent-ID': 'e96de653-ad6e-4ad5-b437-e81fd9d2d61d'
7180
}),
7281
tags: {
7382
'span.kind': 'server',
7483
'http.method': 'GET'
7584
}
76-
})
85+
});
7786
// you can add more tags on server span later in the workflow
7887
serverSpan.setTag(opentracing.Tags.ERROR, true);
7988

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ import { DispatcherConfig } from './dispatchers/dispatcher-config';
2727
import Configuration from './configuration';
2828
import { Logger } from './logger';
2929
import { TracerConfig } from './tracer-config';
30+
import PropagationRegistry from './propagators/propagation_registry';
31+
import { PropagatorOpts } from './propagators/propagator';
3032

3133
import * as opentracing from 'opentracing';
34+
import TextMapPropagator from './propagators/textmap_propagator';
35+
import DefaultCodex from './propagators/default_codex';
3236

3337
export {
3438
Configuration,
@@ -43,7 +47,11 @@ export {
4347
HttpCollectorDispatcher,
4448
DispatcherConfig,
4549
opentracing,
46-
Logger
50+
Logger,
51+
PropagationRegistry,
52+
PropagatorOpts,
53+
TextMapPropagator,
54+
DefaultCodex
4755
};
4856

4957
module.exports = {
@@ -56,5 +64,9 @@ module.exports = {
5664
InMemoryDispatcher,
5765
FileDispatcher,
5866
AgentDispatcher,
59-
opentracing
67+
opentracing,
68+
PropagationRegistry,
69+
PropagatorOpts,
70+
TextMapPropagator,
71+
DefaultCodex
6072
};

src/propagators/propagation_registry.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@
1515
*/
1616

1717
import {Propagator} from './propagator';
18+
import TextMapPropagator from './textmap_propagator';
19+
import URLCodex from './url_codex';
20+
import * as opentracing from 'opentracing';
21+
import BinaryPropagator from './binary_propagator';
1822

1923
export default class PropagationRegistry {
20-
_propagators: any;
24+
private _propagators: any;
2125

2226
constructor() {
2327
this._propagators = {};
2428
}
2529

26-
register(format: string, propagator: Propagator): void {
30+
register(format: string, propagator: Propagator): PropagationRegistry {
2731
this._propagators[format] = propagator;
32+
return this;
2833
}
2934

3035
propagator(format): Propagator {
3136
return this._propagators[format];
3237
}
38+
39+
static default(): PropagationRegistry {
40+
return new PropagationRegistry()
41+
.register(opentracing.FORMAT_TEXT_MAP, new TextMapPropagator())
42+
.register(opentracing.FORMAT_BINARY, new BinaryPropagator())
43+
.register(opentracing.FORMAT_HTTP_HEADERS, new TextMapPropagator(new URLCodex()));
44+
}
3345
}

src/propagators/propagator.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@
1717
import SpanContext from '../span_context';
1818

1919
export class PropagatorOpts {
20-
_traceIdKey: string;
21-
_spanIdKey: string;
22-
_parentSpanIdKey: string;
23-
_baggageKeyPrefix: string;
20+
private _traceIdKey: string;
21+
private _spanIdKey: string;
22+
private _parentSpanIdKey: string;
23+
private _baggageKeyPrefix: string;
24+
25+
constructor(traceIdKey: string = 'Trace-ID',
26+
spanIdKey: string = 'Span-ID',
27+
parentSpanIdKey: string = 'Parent-ID',
28+
baggageKeyPrefix: string = 'Baggage-') {
29+
30+
this._traceIdKey = traceIdKey;
31+
this._spanIdKey = spanIdKey;
32+
this._parentSpanIdKey = parentSpanIdKey;
33+
this._baggageKeyPrefix = baggageKeyPrefix;
34+
}
2435

2536
traceIdKey(): string {
2637
return this._traceIdKey || 'Trace-ID';

src/tracer-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export interface TracerConfig {
2525
commonTags?: { [key: string]: any };
2626
dispatcher?: DispatcherConfig;
2727
idGenerator?: Generator;
28+
useDualSpanMode?: boolean;
2829
}

src/tracer.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ import {Dispatcher} from './dispatchers/dispatcher';
2121
import Span from './span';
2222
import SpanContext from './span_context';
2323
import NoopDispatcher from './dispatchers/noop';
24-
import { Logger, NullLogger } from './logger';
24+
import {Logger, NullLogger} from './logger';
2525
import Utils from './utils';
2626
import PropagationRegistry from './propagators/propagation_registry';
27-
import TextMapPropagator from './propagators/textmap_propagator';
28-
import URLCodex from './propagators/url_codex';
2927
import StartSpanFields from './start_span_fields';
30-
import BinaryPropagator from './propagators/binary_propagator';
31-
import { TracerConfig } from './tracer-config';
32-
import { Generator, UUIDGenerator } from './generators';
28+
import {TracerConfig} from './tracer-config';
29+
import {Generator, UUIDGenerator} from './generators';
3330

3431
export default class Tracer extends opentracing.Tracer {
3532
_serviceName: string;
@@ -45,16 +42,14 @@ export default class Tracer extends opentracing.Tracer {
4542
commonTags: { [key: string]: any } = {},
4643
logger: Logger = new NullLogger(),
4744
idGenerator: Generator = new UUIDGenerator(),
48-
useDualSpanMode: boolean = false) {
45+
useDualSpanMode: boolean = false,
46+
propagationRegistry: PropagationRegistry = PropagationRegistry.default()) {
4947
super();
5048
this._commonTags = commonTags || {};
5149
this._serviceName = serviceName;
5250
this._dispatcher = dispatcher;
5351
this._logger = logger;
54-
this._registry = new PropagationRegistry();
55-
this._registry.register(opentracing.FORMAT_TEXT_MAP, new TextMapPropagator());
56-
this._registry.register(opentracing.FORMAT_BINARY, new BinaryPropagator());
57-
this._registry.register(opentracing.FORMAT_HTTP_HEADERS, new TextMapPropagator(new URLCodex()));
52+
this._registry = propagationRegistry;
5853
this._idGenerator = idGenerator;
5954
this._useDualSpanMode = useDualSpanMode;
6055
}
@@ -188,7 +183,8 @@ export default class Tracer extends opentracing.Tracer {
188183
return ctx;
189184
}
190185

191-
static initTracer(config: TracerConfig): opentracing.Tracer {
186+
static initTracer(config: TracerConfig,
187+
propagationRegistry: PropagationRegistry = PropagationRegistry.default()): opentracing.Tracer {
192188
if (config.disable) {
193189
return new opentracing.Tracer();
194190
}
@@ -202,6 +198,7 @@ export default class Tracer extends opentracing.Tracer {
202198
if (config.logger) {
203199
config.logger.info(`Initializing Haystack Tracer with ${dispatcher.name()}`);
204200
}
205-
return new Tracer(config.serviceName, dispatcher, config.commonTags, config.logger, config.idGenerator);
201+
return new Tracer(config.serviceName, dispatcher, config.commonTags, config.logger,
202+
config.idGenerator, config.useDualSpanMode, propagationRegistry);
206203
}
207204
}

0 commit comments

Comments
 (0)