Skip to content

Commit 6f171e2

Browse files
author
Kapil Rastogi
authored
Merge pull request #18 from ExpediaDotCom/upgrade-0.14-opentracing
upgrade to opentracing v0.14
2 parents 04b9989 + d5c352c commit 6f171e2

13 files changed

Lines changed: 262 additions & 125 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ src/proto_idl_codegen/
6464
.idea/
6565
dist/
6666
.idea/typescript-compiler.xml
67+
*.iml

examples/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ const clientChildSpan = tracer.startSpan('downstream-service-call', {
9191
/// add more tags or logs to your spans
9292
clientChildSpan.setTag(opentracing.Tags.ERROR, true);
9393
clientChildSpan.setTag(opentracing.Tags.HTTP_STATUS_CODE, 503);
94+
clientChildSpan.addTags({'child-custom-tag-1': 1, 'child-custom-tag-2': 'someval'});
95+
clientChildSpan.log({
96+
eventCode: 1001
97+
});
9498

9599
serverSpan.setTag(opentracing.Tags.ERROR, true);
96100
serverSpan.setTag('my-custom-tag', 10.5);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dependencies": {
2323
"google-protobuf": "^3.0.0",
2424
"grpc": "^1.9.0",
25-
"opentracing": "^0.13.0",
25+
"opentracing": "^0.14.0",
2626
"uuid": "^3.2.1"
2727
},
2828
"devDependencies": {

src/dispatchers/remote.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export default class RemoteDispatcher implements Dispatcher {
2727
_logger: any;
2828

2929
constructor(agentHost: string, agentPort: number, logger = new NullLogger()) {
30-
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
3130
agentHost = agentHost || 'haystack-agent';
3231
agentPort = agentPort || 35000;
32+
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
3333
this._client = new services.SpanAgentClient(`${agentHost}:${agentPort}`, grpc.credentials.createInsecure());
3434
this._logger = logger;
3535
}
@@ -63,26 +63,33 @@ export default class RemoteDispatcher implements Dispatcher {
6363
const protoSpan = new messages.Span();
6464
protoSpan.setServicename(span.serviceName());
6565
protoSpan.setOperationname(span.operationName());
66-
protoSpan.setTraceid(span.context().traceId());
67-
protoSpan.setSpanid(span.context().spanId());
68-
protoSpan.setParentspanid(span.context().parentSpanId());
66+
protoSpan.setTraceid(span.context().traceId);
67+
protoSpan.setSpanid(span.context().spanId);
68+
protoSpan.setParentspanid(span.context().parentSpanId);
6969
protoSpan.setStarttime(span.startTime());
7070
protoSpan.setDuration(span.duration());
7171

7272
const protoSpanTags = [];
73-
span.tags().forEach(tag => {
74-
protoSpanTags.push(this._createProtoTag(tag));
75-
});
73+
74+
const tags = span.tags();
75+
for (const k in tags) {
76+
if (tags.hasOwnProperty(k)) {
77+
protoSpanTags.push(RemoteDispatcher._createProtoTag(k, tags[k]));
78+
}
79+
}
7680

7781
protoSpan.setTagsList(protoSpanTags);
7882

7983
const protoSpanLogs = [];
8084
span.logs().forEach(log => {
8185
const protoLog = new messages.Log();
8286
const protoLogTags = [];
83-
log.tags.forEach(tag => {
84-
protoLogTags.push(this._createProtoTag(tag));
85-
});
87+
const kvPairs = log.keyValuePairs;
88+
for (const k in kvPairs) {
89+
if (kvPairs.hasOwnProperty(k)) {
90+
protoLogTags.push(RemoteDispatcher._createProtoTag(k, kvPairs[k]));
91+
}
92+
}
8693
protoLog.setTimestamp(log.timestamp);
8794
protoLog.setFieldsList(protoLogTags);
8895
protoSpanLogs.push(protoLog);
@@ -92,11 +99,11 @@ export default class RemoteDispatcher implements Dispatcher {
9299
return protoSpan;
93100
}
94101

95-
private _createProtoTag(tag: any): any {
102+
private static _createProtoTag(key: string, value: any): any {
96103
const protoTag = new messages.Tag();
97-
protoTag.setKey(tag.key);
104+
protoTag.setKey(key);
98105

99-
const tagValue = tag.value;
106+
const tagValue = value;
100107
if (typeof tagValue === 'number') {
101108
if (Utils.isFloatType(tagValue)) {
102109
protoTag.setVdouble(tagValue);

src/log_data.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 default class LogData {
18+
keyValuePairs: { [key: string]: any };
19+
timestamp: number;
20+
21+
constructor(keyValuePairs: { [key: string]: any }, timestamp: number) {
22+
this.keyValuePairs = keyValuePairs;
23+
this.timestamp = timestamp;
24+
}
25+
}
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 {Propagator} from './propagator';
18+
import SpanContext from '../span_context';
19+
20+
export default class BinaryPropagator implements Propagator {
21+
inject(spanContext: SpanContext, carrier: any): void {
22+
}
23+
24+
extract(carrier: any): SpanContext {
25+
return null;
26+
}
27+
}

src/propagators/textmap_propagator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export default class TextMapPropagator implements Propagator {
2828
}
2929

3030
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();
31+
carrier[this._opts.traceIdKey()] = spanContext.traceId;
32+
carrier[this._opts.spanIdKey()] = spanContext.spanId;
33+
carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId;
3434

35-
const baggage = spanContext.baggage();
35+
const baggage = spanContext.baggage;
3636
for (const key in baggage) {
3737
if (baggage.hasOwnProperty(key)) {
38-
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage()[key]);
38+
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage[key]);
3939
}
4040
}
4141
}

src/span.ts

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,32 @@ import Tracer from './tracer';
1919
import Utils from './utils';
2020

2121
import * as opentracing from 'opentracing';
22+
import LogData from './log_data';
2223

23-
export default class Span {
24-
_tracer: Tracer;
24+
export default class Span extends opentracing.Span {
25+
_tracerImpl: Tracer;
2526
_operationName: string;
2627
_spanContext: SpanContext;
2728
_startTime: number;
2829
_duration: number;
2930
_references: opentracing.Reference[];
30-
_logs: opentracing.LogData[];
31-
_tags: opentracing.Tag[];
31+
_logs: LogData[];
32+
_tags: { [key: string]: any };
3233
_isFinished: boolean;
3334

3435
constructor(tracer: Tracer,
3536
operationName: string,
3637
spanContext: SpanContext,
3738
startTime: number,
3839
references: opentracing.Reference[]) {
39-
this._tracer = tracer;
40+
super();
41+
this._tracerImpl = tracer;
4042
this._operationName = operationName;
4143
this._spanContext = spanContext;
4244
this._startTime = startTime;
4345
this._references = references;
4446
this._logs = [];
45-
this._tags = [];
47+
this._tags = {};
4648
this._isFinished = false;
4749
}
4850

@@ -51,18 +53,18 @@ export default class Span {
5153
}
5254

5355
serviceName(): string {
54-
return this._tracer._serviceName;
56+
return this._tracerImpl._serviceName;
5557
}
5658

5759
context(): SpanContext {
5860
return this._spanContext;
5961
}
6062

61-
tags(): opentracing.Tag[] {
63+
tags(): { [key: string]: any } {
6264
return this._tags;
6365
}
6466

65-
logs(): opentracing.LogData[] {
67+
logs(): LogData[] {
6668
return this._logs;
6769
}
6870

@@ -74,68 +76,68 @@ export default class Span {
7476
return this._duration;
7577
}
7678

77-
tracer(): Tracer {
78-
return this._tracer;
79+
tracer(): opentracing.Tracer {
80+
return this._tracer();
7981
}
8082

81-
setOperationName(name): Span {
82-
this._operationName = name;
83+
setOperationName(name): this {
84+
this._setOperationName(name);
8385
return this;
8486
}
8587

8688
isFinished(): boolean {
8789
return this._isFinished;
8890
}
89-
addTags(keyValues: any): Span {
90-
for (const k in keyValues) {
91-
if (keyValues.hasOwnProperty(k)) {
92-
this.setTag(k, keyValues[k]);
93-
}
94-
}
91+
92+
addTags(keyValueMap: { [key: string]: any; }): this {
93+
this._addTags(keyValueMap);
9594
return this;
9695
}
9796

98-
setTag(k: string, v: any): Span {
99-
this._tags.push({ key: k, value: v });
97+
setTag(k: string, v: any): this {
98+
this._tags[k] = v;
10099
return this;
101100
}
102101

103-
setBaggageItem(key: string, value: string): Span {
102+
setBaggageItem(key: string, value: string): this {
104103
const prevBaggageValue = this._spanContext.baggage[key];
105104
this._logFields(key, value, prevBaggageValue);
106-
this._spanContext.addBaggageItem(key, value);
105+
this._spanContext = this._spanContext.addBaggageItem(key, value);
107106
return this;
108107
}
109108

110-
log(keyValuePairs: any, timestamp?: number): void {
111-
const _tags = [];
112-
for (const k in keyValuePairs) {
113-
if (keyValuePairs.hasOwnProperty(k)) {
114-
_tags.push({key: k, value: keyValuePairs[k]});
115-
}
116-
}
117-
this._logs.push({
118-
timestamp: timestamp || Utils.now(),
119-
tags: _tags
120-
});
109+
log(keyValuePairs: { [p: string]: any }, timestamp?: number): this {
110+
this._log(keyValuePairs, timestamp);
111+
return this;
121112
}
122113

123-
logEvent(eventName: string, payLoad: any): void {
124-
return this.log({
114+
logEvent(eventName: string, data: any): void {
115+
this.log({
125116
event: eventName,
126-
payload: payLoad
117+
payload: data
127118
});
128119
}
129120

130121
finish(finishTime?: number, callback?: (error) => void): void {
131-
if (this._isFinished) {
132-
const spanInfo = `operation=${this.operationName},context=${this.context().toString()}`;
133-
throw new Error(`cant finish the same span twice - ${spanInfo}`);
122+
this._finish(finishTime, callback);
123+
}
124+
125+
getBaggageItem(key: string): string | any {
126+
return this._getBaggageItem(key);
127+
}
128+
129+
protected _tracer(): opentracing.Tracer {
130+
return this._tracerImpl;
131+
}
132+
133+
protected _log(keyValuePairs: { [p: string]: any }, timestamp?: number): void {
134+
const kvPairs = {};
135+
for (const k in keyValuePairs) {
136+
if (keyValuePairs.hasOwnProperty(k)) {
137+
kvPairs[k] = keyValuePairs[k];
138+
}
134139
}
135-
const endTime = finishTime || Utils.now();
136-
this._duration = endTime - this._startTime;
137-
this._tracer.dispatcher().dispatch(this, callback);
138-
this._isFinished = true;
140+
this._logs.push(new LogData(kvPairs, timestamp || Utils.now()));
139141
}
140142

141143
private _logFields(k: string, v: string, prevBaggageValue: string): void {
@@ -150,6 +152,41 @@ export default class Span {
150152
this.log(fields);
151153
}
152154

155+
protected _addTags(keyValuePairs: { [p: string]: any }): void {
156+
for (const k in keyValuePairs) {
157+
if (keyValuePairs.hasOwnProperty(k)) {
158+
this.setTag(k, keyValuePairs[k]);
159+
}
160+
}
161+
}
162+
163+
protected _setOperationName(name: string): void {
164+
this._operationName = name;
165+
}
166+
167+
protected _setBaggageItem(key: string, value: string): void {
168+
this._spanContext = this._spanContext.addBaggageItem(key, value);
169+
}
170+
171+
protected _getBaggageItem(key: string): string | any {
172+
return this._spanContext.baggage[key];
173+
}
174+
175+
protected _context(): SpanContext {
176+
return this._spanContext;
177+
}
178+
179+
protected _finish(finishTime?: number, callback?: (error) => void): void {
180+
if (this._isFinished) {
181+
const spanInfo = `operation=${this.operationName},context=${this.context().toString()}`;
182+
throw new Error(`cant finish the same span twice - ${spanInfo}`);
183+
}
184+
const endTime = finishTime || Utils.now();
185+
this._duration = endTime - this._startTime;
186+
this._tracerImpl.dispatcher().dispatch(this, callback);
187+
this._isFinished = true;
188+
}
189+
153190
toString(): string {
154191
return JSON.stringify(
155192
Utils.merge(this._spanContext, {

0 commit comments

Comments
 (0)