Skip to content

Commit 3faff7d

Browse files
Merge pull request #8 from ExpediaDotCom/opentracing-propagators
Adding propagators for textmap and http formats
2 parents ecb9bf4 + 6b000cc commit 3faff7d

12 files changed

Lines changed: 399 additions & 18 deletions

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ 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 tslint
15+
build: check-node-version npm_install idl_codegen tslint compile test
16+
17+
.PHONY: test
18+
test:
19+
./node_modules/mocha/bin/mocha -r ./node_modules/ts-node/register tests/**/*.ts
20+
21+
.PHONY: compile
22+
compile:
1623
rm -rf ./dist/
1724
tsc -p tsconfig.json
1825
cp package.json dist/

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
"dependencies": {
2323
"google-protobuf": "^3.0.0",
2424
"grpc": "^1.9.0",
25-
"uuid": "^3.2.1",
26-
"opentracing": "^0.13.0"
25+
"opentracing": "^0.13.0",
26+
"uuid": "^3.2.1"
2727
},
2828
"devDependencies": {
29-
"@types/node": "^10.3.4",
30-
"typescript": "2.9.2",
29+
"@types/node": "7.0.12",
30+
"@types/chai": "3.4.35",
31+
"@types/mocha": "2.2.40",
32+
"chai": "^3.5.0",
3133
"grpc-tools": "1.6.6",
32-
"tslint": "^5.0.0"
34+
"mocha": "^2.5.3",
35+
"ts-node": "^7.0.0",
36+
"tslint": "^5.0.0",
37+
"typescript": "2.9.2"
3338
}
3439
}

src/dispatchers/in_memory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ export default class InMemoryDispatcher implements Dispatcher {
4040
callback();
4141
}
4242
}
43+
44+
spans(): Span[] {
45+
return this._spans;
46+
}
4347
}

src/dispatchers/remote.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ export default class RemoteDispatcher implements Dispatcher {
108108
} else if (typeof tagValue === 'boolean') {
109109
protoTag.setVbool(tagValue);
110110
protoTag.setType(messages.Tag.TagType.BOOL);
111-
} else {
111+
} else if (typeof tagValue === 'string') {
112112
protoTag.setVstr(tagValue);
113113
protoTag.setType(messages.Tag.TagType.STRING);
114+
} else {
115+
protoTag.setVbytes(tagValue);
116+
protoTag.setType(messages.Tag.TagType.BINARY);
114117
}
115118

116119
return protoTag;

src/propagators/default_codex.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 {Codex} from './propagator';
18+
19+
export default class DefaultCodex implements Codex {
20+
constructor() { }
21+
22+
encode(value: string): string {
23+
return value;
24+
}
25+
26+
decode(value: string): string {
27+
return value;
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 {Propagator} from './propagator';
18+
19+
export default class PropagationRegistry {
20+
_propagators: any;
21+
22+
constructor() {
23+
this._propagators = {};
24+
}
25+
26+
register(format: string, propagator: Propagator): void {
27+
this._propagators[format] = propagator;
28+
}
29+
30+
propagator(format): Propagator {
31+
return this._propagators[format];
32+
}
33+
}

src/propagators/propagator.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 SpanContext from '../span_context';
18+
19+
export class PropagatorOpts {
20+
_traceIdKey: string;
21+
_spanIdKey: string;
22+
_parentSpanIdKey: string;
23+
_baggageKeyPrefix: string;
24+
25+
traceIdKey(): string {
26+
return this._traceIdKey || 'Trace-ID';
27+
}
28+
29+
spanIdKey(): string {
30+
return this._spanIdKey || 'Span-ID';
31+
}
32+
33+
parentSpanIdKey(): string {
34+
return this._parentSpanIdKey || 'Parent-ID';
35+
}
36+
37+
baggageKeyPrefix(): string {
38+
return this._baggageKeyPrefix || 'Baggage-';
39+
}
40+
}
41+
42+
export interface Propagator {
43+
inject(spanContext: SpanContext, carrier: any): void;
44+
extract(carrier: any): SpanContext;
45+
}
46+
47+
export interface Codex {
48+
encode(value: string): string;
49+
decode(value: string): string;
50+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 {Codex, Propagator, PropagatorOpts} from './propagator';
18+
import SpanContext from '../span_context';
19+
import DefaultCodex from './default_codex';
20+
21+
export default class TextMapPropagator implements Propagator {
22+
_opts: PropagatorOpts;
23+
_codex: Codex;
24+
25+
constructor(codex: Codex = new DefaultCodex(), opts: PropagatorOpts = new PropagatorOpts()) {
26+
this._opts = opts;
27+
this._codex = codex;
28+
}
29+
30+
inject(spanContext: SpanContext, carrier: any): void {
31+
carrier[this._opts.traceIdKey()] = spanContext.traceId();
32+
carrier[this._opts.spanIdKey()] = spanContext.spanId();
33+
carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId();
34+
35+
const baggage = spanContext.baggage();
36+
for (const key in baggage) {
37+
if (baggage.hasOwnProperty(key)) {
38+
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage()[key]);
39+
}
40+
}
41+
}
42+
43+
extract(carrier: any): SpanContext {
44+
const baggage = {};
45+
for (const key in carrier) {
46+
if (carrier.hasOwnProperty(key) && key.indexOf(this._opts.baggageKeyPrefix()) === 0) {
47+
const keySansPrefix = key.substring(this._opts.baggageKeyPrefix().length);
48+
baggage[keySansPrefix] = this._codex.decode(carrier[key]);
49+
}
50+
}
51+
return new SpanContext(carrier[this._opts.traceIdKey()], carrier[this._opts.spanIdKey()], carrier[this._opts.parentSpanIdKey()], baggage);
52+
}
53+
}

src/propagators/url_codex.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 {Codex} from './propagator';
18+
19+
export default class URLCodex implements Codex {
20+
encode(value: string): string {
21+
return encodeURI(value);
22+
}
23+
24+
decode(value: string): string {
25+
return decodeURI(value);
26+
}
27+
}

src/start_span_fields.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// startSpanFields is used for type-checking the Trace.startSpan().
18+
import SpanContext from './span_context';
19+
import * as opentracing from 'opentracing';
20+
21+
export default class StartSpanFields {
22+
childOf?: SpanContext;
23+
references?: opentracing.Reference[];
24+
tags?: any;
25+
startTime?: number;
26+
callerSpanContext?: SpanContext;
27+
}

0 commit comments

Comments
 (0)