Skip to content

Commit 2c18f9f

Browse files
author
Kapil Rastogi
authored
Merge pull request #23 from ExpediaDotCom/allow-idgenerate-func
allow id generation to be client driven
2 parents 61dc131 + 613be6c commit 2c18f9f

6 files changed

Lines changed: 76 additions & 9 deletions

File tree

src/generators/id-generator.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export interface Generator {
18+
generate(): string;
19+
}

src/generators/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export * from './id-generator';
18+
export * from './uuid-generator';

src/generators/uuid-generator.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Generator } from './id-generator';
18+
import Utils from '../utils';
19+
20+
export class UUIDGenerator implements Generator {
21+
generate(): string {
22+
return Utils.randomUUID();
23+
}
24+
}

src/tracer-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { Logger } from './logger';
18+
import { Generator } from './generators';
1819
import { DispatcherConfig } from './dispatchers/dispatcher-config';
1920

2021
export interface TracerConfig {
@@ -23,4 +24,5 @@ export interface TracerConfig {
2324
logger?: Logger;
2425
commonTags?: { [key: string]: any };
2526
dispatcher?: DispatcherConfig;
27+
idGenerator?: Generator;
2628
}

src/tracer.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ import URLCodex from './propagators/url_codex';
2929
import StartSpanFields from './start_span_fields';
3030
import BinaryPropagator from './propagators/binary_propagator';
3131
import { TracerConfig } from './tracer-config';
32+
import { Generator, UUIDGenerator } from './generators';
3233

3334
export default class Tracer extends opentracing.Tracer {
3435
_serviceName: string;
3536
_dispatcher: Dispatcher;
37+
_idGenerator: Generator;
3638
_commonTags: { [key: string]: any };
3739
_logger: any;
3840
_registry: PropagationRegistry;
3941

4042
constructor(serviceName: string,
4143
dispatcher: Dispatcher = new NoopDispatcher(),
4244
commonTags: { [key: string]: any } = {},
43-
logger: Logger = new NullLogger()) {
45+
logger: Logger = new NullLogger(),
46+
idGenerator: Generator = new UUIDGenerator()) {
4447
super();
4548
this._commonTags = commonTags || {};
4649
this._serviceName = serviceName;
@@ -50,6 +53,7 @@ export default class Tracer extends opentracing.Tracer {
5053
this._registry.register(opentracing.FORMAT_TEXT_MAP, new TextMapPropagator());
5154
this._registry.register(opentracing.FORMAT_BINARY, new BinaryPropagator());
5255
this._registry.register(opentracing.FORMAT_HTTP_HEADERS, new TextMapPropagator(new URLCodex()));
56+
this._idGenerator = idGenerator;
5357
}
5458

5559
startSpan(operationName: string, fields?: StartSpanFields): Span {
@@ -83,7 +87,7 @@ export default class Tracer extends opentracing.Tracer {
8387
}
8488
}
8589

86-
const ctx = Tracer._createSpanContext(parent, fields.callerSpanContext);
90+
const ctx = this._createSpanContext(parent, fields.callerSpanContext);
8791
return this._spanStart(operationName, ctx, startTime, references, spanTags);
8892
}
8993

@@ -98,16 +102,16 @@ export default class Tracer extends opentracing.Tracer {
98102
return span;
99103
}
100104

101-
static _createSpanContext(parent: SpanContext, callerContext: SpanContext): SpanContext {
105+
_createSpanContext(parent: SpanContext, callerContext: SpanContext): SpanContext {
102106
if (!parent || !parent.isValid) {
103107
if (callerContext) {
104108
return new SpanContext(callerContext.traceId, callerContext.spanId, callerContext.parentSpanId, callerContext.baggage);
105109
} else {
106110
const parentBaggage = parent && parent.baggage;
107-
return new SpanContext(Utils.randomUUID(), Utils.randomUUID(), parentBaggage);
111+
return new SpanContext(this._idGenerator.generate(), this._idGenerator.generate(), parentBaggage);
108112
}
109113
} else {
110-
return new SpanContext(parent.traceId, Utils.randomUUID(), parent.spanId, parent.baggage);
114+
return new SpanContext(parent.traceId, this._idGenerator.generate(), parent.spanId, parent.baggage);
111115
}
112116
}
113117

@@ -178,7 +182,6 @@ export default class Tracer extends opentracing.Tracer {
178182
if (config.logger) {
179183
config.logger.info(`Initializing Haystack Tracer with ${dispatcher.name()}`);
180184
}
181-
182-
return new Tracer(config.serviceName, dispatcher, config.commonTags, config.logger);
185+
return new Tracer(config.serviceName, dispatcher, config.commonTags, config.logger, config.idGenerator);
183186
}
184187
}

tests/integration/agent-integration.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import * as opentracing from 'opentracing';
1919
import SpanContext from '../../src/span_context';
2020
import * as kafka from 'kafka-node';
2121
import { expect } from 'chai';
22+
import { Logger } from '../../src/logger'
2223

23-
class ConsoleLogger {
24+
class ConsoleLogger implements Logger {
2425
log(msg) { console.log(msg); }
2526
debug(msg: string): void { this.log(msg); }
2627
info(msg: string): void { this.log(msg); }
@@ -42,7 +43,7 @@ describe('Haystack Integration Tests', () => {
4243
type: 'haystack_agent',
4344
agentHost: 'haystack_agent'
4445
},
45-
logger: new ConsoleLogger()
46+
logger: new ConsoleLogger(),
4647
});
4748

4849
const serverSpan = tracer.startSpan('my-operation', {

0 commit comments

Comments
 (0)