Skip to content

Commit

Permalink
adding the ability to inject propagation registry to register custom …
Browse files Browse the repository at this point in the history
…propagators (#29)
  • Loading branch information
mchandramouli authored and ashishagg committed Feb 5, 2019
1 parent c9321b2 commit 8f0ca33
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json

# Runtime data
pids
Expand Down Expand Up @@ -65,4 +66,6 @@ src/proto_idl_codegen/
dist/
.idea/typescript-compiler.xml
*.iml
*.ipr
*.iws
ws2/
21 changes: 15 additions & 6 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

/// first do `npm install haystack-client` and replace '../dist/index' with 'haystack-client'
const initTracer = require('../dist/index').initTracer;
const SpanContext = require('../dist/index').SpanContext;
const PropagationRegistry = require('../dist/index').PropagationRegistry;
const TextMapPropagator = require('../dist/index').TextMapPropagator;
const DefaultCodex = require('../dist/index').DefaultCodex;
const PropagatorOpts = require('../dist/index').PropagatorOpts;

const opentracing = require('opentracing');
const MyLogger = require('./logger');
Expand Down Expand Up @@ -52,8 +55,14 @@ const config = {
logger: logger
};

/// ability to use custom propagation headers if needed
const propagation = PropagationRegistry.default()
.register(opentracing.FORMAT_HTTP_HEADERS,
new TextMapPropagator(new DefaultCodex(),
new PropagatorOpts('X-Trace-ID', 'X-Span-ID', 'X-Parent-ID')));

/// initialize the tracer only once at the time of your service startup
const tracer = initTracer(config);
const tracer = initTracer(config, propagation);

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

Expand Down
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ import { DispatcherConfig } from './dispatchers/dispatcher-config';
import Configuration from './configuration';
import { Logger } from './logger';
import { TracerConfig } from './tracer-config';
import PropagationRegistry from './propagators/propagation_registry';
import { PropagatorOpts } from './propagators/propagator';

import * as opentracing from 'opentracing';
import TextMapPropagator from './propagators/textmap_propagator';
import DefaultCodex from './propagators/default_codex';

export {
Configuration,
Expand All @@ -43,7 +47,11 @@ export {
HttpCollectorDispatcher,
DispatcherConfig,
opentracing,
Logger
Logger,
PropagationRegistry,
PropagatorOpts,
TextMapPropagator,
DefaultCodex
};

module.exports = {
Expand All @@ -56,5 +64,9 @@ module.exports = {
InMemoryDispatcher,
FileDispatcher,
AgentDispatcher,
opentracing
opentracing,
PropagationRegistry,
PropagatorOpts,
TextMapPropagator,
DefaultCodex
};
16 changes: 14 additions & 2 deletions src/propagators/propagation_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,31 @@
*/

import {Propagator} from './propagator';
import TextMapPropagator from './textmap_propagator';
import URLCodex from './url_codex';
import * as opentracing from 'opentracing';
import BinaryPropagator from './binary_propagator';

export default class PropagationRegistry {
_propagators: any;
private _propagators: any;

constructor() {
this._propagators = {};
}

register(format: string, propagator: Propagator): void {
register(format: string, propagator: Propagator): PropagationRegistry {
this._propagators[format] = propagator;
return this;
}

propagator(format): Propagator {
return this._propagators[format];
}

static default(): PropagationRegistry {
return new PropagationRegistry()
.register(opentracing.FORMAT_TEXT_MAP, new TextMapPropagator())
.register(opentracing.FORMAT_BINARY, new BinaryPropagator())
.register(opentracing.FORMAT_HTTP_HEADERS, new TextMapPropagator(new URLCodex()));
}
}
19 changes: 15 additions & 4 deletions src/propagators/propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@
import SpanContext from '../span_context';

export class PropagatorOpts {
_traceIdKey: string;
_spanIdKey: string;
_parentSpanIdKey: string;
_baggageKeyPrefix: string;
private _traceIdKey: string;
private _spanIdKey: string;
private _parentSpanIdKey: string;
private _baggageKeyPrefix: string;

constructor(traceIdKey: string = 'Trace-ID',
spanIdKey: string = 'Span-ID',
parentSpanIdKey: string = 'Parent-ID',
baggageKeyPrefix: string = 'Baggage-') {

this._traceIdKey = traceIdKey;
this._spanIdKey = spanIdKey;
this._parentSpanIdKey = parentSpanIdKey;
this._baggageKeyPrefix = baggageKeyPrefix;
}

traceIdKey(): string {
return this._traceIdKey || 'Trace-ID';
Expand Down
1 change: 1 addition & 0 deletions src/tracer-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export interface TracerConfig {
commonTags?: { [key: string]: any };
dispatcher?: DispatcherConfig;
idGenerator?: Generator;
useDualSpanMode?: boolean;
}
23 changes: 10 additions & 13 deletions src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ import {Dispatcher} from './dispatchers/dispatcher';
import Span from './span';
import SpanContext from './span_context';
import NoopDispatcher from './dispatchers/noop';
import { Logger, NullLogger } from './logger';
import {Logger, NullLogger} from './logger';
import Utils from './utils';
import PropagationRegistry from './propagators/propagation_registry';
import TextMapPropagator from './propagators/textmap_propagator';
import URLCodex from './propagators/url_codex';
import StartSpanFields from './start_span_fields';
import BinaryPropagator from './propagators/binary_propagator';
import { TracerConfig } from './tracer-config';
import { Generator, UUIDGenerator } from './generators';
import {TracerConfig} from './tracer-config';
import {Generator, UUIDGenerator} from './generators';

export default class Tracer extends opentracing.Tracer {
_serviceName: string;
Expand All @@ -45,16 +42,14 @@ export default class Tracer extends opentracing.Tracer {
commonTags: { [key: string]: any } = {},
logger: Logger = new NullLogger(),
idGenerator: Generator = new UUIDGenerator(),
useDualSpanMode: boolean = false) {
useDualSpanMode: boolean = false,
propagationRegistry: PropagationRegistry = PropagationRegistry.default()) {
super();
this._commonTags = commonTags || {};
this._serviceName = serviceName;
this._dispatcher = dispatcher;
this._logger = logger;
this._registry = new PropagationRegistry();
this._registry.register(opentracing.FORMAT_TEXT_MAP, new TextMapPropagator());
this._registry.register(opentracing.FORMAT_BINARY, new BinaryPropagator());
this._registry.register(opentracing.FORMAT_HTTP_HEADERS, new TextMapPropagator(new URLCodex()));
this._registry = propagationRegistry;
this._idGenerator = idGenerator;
this._useDualSpanMode = useDualSpanMode;
}
Expand Down Expand Up @@ -188,7 +183,8 @@ export default class Tracer extends opentracing.Tracer {
return ctx;
}

static initTracer(config: TracerConfig): opentracing.Tracer {
static initTracer(config: TracerConfig,
propagationRegistry: PropagationRegistry = PropagationRegistry.default()): opentracing.Tracer {
if (config.disable) {
return new opentracing.Tracer();
}
Expand All @@ -202,6 +198,7 @@ export default class Tracer extends opentracing.Tracer {
if (config.logger) {
config.logger.info(`Initializing Haystack Tracer with ${dispatcher.name()}`);
}
return new Tracer(config.serviceName, dispatcher, config.commonTags, config.logger, config.idGenerator);
return new Tracer(config.serviceName, dispatcher, config.commonTags, config.logger,
config.idGenerator, config.useDualSpanMode, propagationRegistry);
}
}

0 comments on commit 8f0ca33

Please sign in to comment.