Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ src/proto_idl_codegen/
.idea/
dist/
.idea/typescript-compiler.xml
*.iml
4 changes: 4 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ const clientChildSpan = tracer.startSpan('downstream-service-call', {
/// add more tags or logs to your spans
clientChildSpan.setTag(opentracing.Tags.ERROR, true);
clientChildSpan.setTag(opentracing.Tags.HTTP_STATUS_CODE, 503);
clientChildSpan.addTags({'child-custom-tag-1': 1, 'child-custom-tag-2': 'someval'});
clientChildSpan.log({
eventCode: 1001
});

serverSpan.setTag(opentracing.Tags.ERROR, true);
serverSpan.setTag('my-custom-tag', 10.5);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"google-protobuf": "^3.0.0",
"grpc": "^1.9.0",
"opentracing": "^0.13.0",
"opentracing": "^0.14.0",
"uuid": "^3.2.1"
},
"devDependencies": {
Expand Down
33 changes: 20 additions & 13 deletions src/dispatchers/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default class RemoteDispatcher implements Dispatcher {
_logger: any;

constructor(agentHost: string, agentPort: number, logger = new NullLogger()) {
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
agentHost = agentHost || 'haystack-agent';
agentPort = agentPort || 35000;
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
this._client = new services.SpanAgentClient(`${agentHost}:${agentPort}`, grpc.credentials.createInsecure());
this._logger = logger;
}
Expand Down Expand Up @@ -63,26 +63,33 @@ export default class RemoteDispatcher implements Dispatcher {
const protoSpan = new messages.Span();
protoSpan.setServicename(span.serviceName());
protoSpan.setOperationname(span.operationName());
protoSpan.setTraceid(span.context().traceId());
protoSpan.setSpanid(span.context().spanId());
protoSpan.setParentspanid(span.context().parentSpanId());
protoSpan.setTraceid(span.context().traceId);
protoSpan.setSpanid(span.context().spanId);
protoSpan.setParentspanid(span.context().parentSpanId);
protoSpan.setStarttime(span.startTime());
protoSpan.setDuration(span.duration());

const protoSpanTags = [];
span.tags().forEach(tag => {
protoSpanTags.push(this._createProtoTag(tag));
});

const tags = span.tags();
for (const k in tags) {
if (tags.hasOwnProperty(k)) {
protoSpanTags.push(RemoteDispatcher._createProtoTag(k, tags[k]));
}
}

protoSpan.setTagsList(protoSpanTags);

const protoSpanLogs = [];
span.logs().forEach(log => {
const protoLog = new messages.Log();
const protoLogTags = [];
log.tags.forEach(tag => {
protoLogTags.push(this._createProtoTag(tag));
});
const kvPairs = log.keyValuePairs;
for (const k in kvPairs) {
if (kvPairs.hasOwnProperty(k)) {
protoLogTags.push(RemoteDispatcher._createProtoTag(k, kvPairs[k]));
}
}
protoLog.setTimestamp(log.timestamp);
protoLog.setFieldsList(protoLogTags);
protoSpanLogs.push(protoLog);
Expand All @@ -92,11 +99,11 @@ export default class RemoteDispatcher implements Dispatcher {
return protoSpan;
}

private _createProtoTag(tag: any): any {
private static _createProtoTag(key: string, value: any): any {
const protoTag = new messages.Tag();
protoTag.setKey(tag.key);
protoTag.setKey(key);

const tagValue = tag.value;
const tagValue = value;
if (typeof tagValue === 'number') {
if (Utils.isFloatType(tagValue)) {
protoTag.setVdouble(tagValue);
Expand Down
25 changes: 25 additions & 0 deletions src/log_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2018 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export default class LogData {
keyValuePairs: { [key: string]: any };
timestamp: number;

constructor(keyValuePairs: { [key: string]: any }, timestamp: number) {
this.keyValuePairs = keyValuePairs;
this.timestamp = timestamp;
}
}
27 changes: 27 additions & 0 deletions src/propagators/binary_propagator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2018 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {Propagator} from './propagator';
import SpanContext from '../span_context';

export default class BinaryPropagator implements Propagator {
inject(spanContext: SpanContext, carrier: any): void {
}

extract(carrier: any): SpanContext {
return null;
}
}
10 changes: 5 additions & 5 deletions src/propagators/textmap_propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export default class TextMapPropagator implements Propagator {
}

inject(spanContext: SpanContext, carrier: any): void {
carrier[this._opts.traceIdKey()] = spanContext.traceId();
carrier[this._opts.spanIdKey()] = spanContext.spanId();
carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId();
carrier[this._opts.traceIdKey()] = spanContext.traceId;
carrier[this._opts.spanIdKey()] = spanContext.spanId;
carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId;

const baggage = spanContext.baggage();
const baggage = spanContext.baggage;
for (const key in baggage) {
if (baggage.hasOwnProperty(key)) {
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage()[key]);
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage[key]);
}
}
}
Expand Down
125 changes: 81 additions & 44 deletions src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,32 @@ import Tracer from './tracer';
import Utils from './utils';

import * as opentracing from 'opentracing';
import LogData from './log_data';

export default class Span {
_tracer: Tracer;
export default class Span extends opentracing.Span {
_tracerImpl: Tracer;
_operationName: string;
_spanContext: SpanContext;
_startTime: number;
_duration: number;
_references: opentracing.Reference[];
_logs: opentracing.LogData[];
_tags: opentracing.Tag[];
_logs: LogData[];
_tags: { [key: string]: any };
_isFinished: boolean;

constructor(tracer: Tracer,
operationName: string,
spanContext: SpanContext,
startTime: number,
references: opentracing.Reference[]) {
this._tracer = tracer;
super();
this._tracerImpl = tracer;
this._operationName = operationName;
this._spanContext = spanContext;
this._startTime = startTime;
this._references = references;
this._logs = [];
this._tags = [];
this._tags = {};
this._isFinished = false;
}

Expand All @@ -51,18 +53,18 @@ export default class Span {
}

serviceName(): string {
return this._tracer._serviceName;
return this._tracerImpl._serviceName;
}

context(): SpanContext {
return this._spanContext;
}

tags(): opentracing.Tag[] {
tags(): { [key: string]: any } {
return this._tags;
}

logs(): opentracing.LogData[] {
logs(): LogData[] {
return this._logs;
}

Expand All @@ -74,68 +76,68 @@ export default class Span {
return this._duration;
}

tracer(): Tracer {
return this._tracer;
tracer(): opentracing.Tracer {
return this._tracer();
}

setOperationName(name): Span {
this._operationName = name;
setOperationName(name): this {
this._setOperationName(name);
return this;
}

isFinished(): boolean {
return this._isFinished;
}
addTags(keyValues: any): Span {
for (const k in keyValues) {
if (keyValues.hasOwnProperty(k)) {
this.setTag(k, keyValues[k]);
}
}

addTags(keyValueMap: { [key: string]: any; }): this {
this._addTags(keyValueMap);
return this;
}

setTag(k: string, v: any): Span {
this._tags.push({ key: k, value: v });
setTag(k: string, v: any): this {
this._tags[k] = v;
return this;
}

setBaggageItem(key: string, value: string): Span {
setBaggageItem(key: string, value: string): this {
const prevBaggageValue = this._spanContext.baggage[key];
this._logFields(key, value, prevBaggageValue);
this._spanContext.addBaggageItem(key, value);
this._spanContext = this._spanContext.addBaggageItem(key, value);
return this;
}

log(keyValuePairs: any, timestamp?: number): void {
const _tags = [];
for (const k in keyValuePairs) {
if (keyValuePairs.hasOwnProperty(k)) {
_tags.push({key: k, value: keyValuePairs[k]});
}
}
this._logs.push({
timestamp: timestamp || Utils.now(),
tags: _tags
});
log(keyValuePairs: { [p: string]: any }, timestamp?: number): this {
this._log(keyValuePairs, timestamp);
return this;
}

logEvent(eventName: string, payLoad: any): void {
return this.log({
logEvent(eventName: string, data: any): void {
this.log({
event: eventName,
payload: payLoad
payload: data
});
}

finish(finishTime?: number, callback?: (error) => void): void {
if (this._isFinished) {
const spanInfo = `operation=${this.operationName},context=${this.context().toString()}`;
throw new Error(`cant finish the same span twice - ${spanInfo}`);
this._finish(finishTime, callback);
}

getBaggageItem(key: string): string | any {
return this._getBaggageItem(key);
}

protected _tracer(): opentracing.Tracer {
return this._tracerImpl;
}

protected _log(keyValuePairs: { [p: string]: any }, timestamp?: number): void {
const kvPairs = {};
for (const k in keyValuePairs) {
if (keyValuePairs.hasOwnProperty(k)) {
kvPairs[k] = keyValuePairs[k];
}
}
const endTime = finishTime || Utils.now();
this._duration = endTime - this._startTime;
this._tracer.dispatcher().dispatch(this, callback);
this._isFinished = true;
this._logs.push(new LogData(kvPairs, timestamp || Utils.now()));
}

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

protected _addTags(keyValuePairs: { [p: string]: any }): void {
for (const k in keyValuePairs) {
if (keyValuePairs.hasOwnProperty(k)) {
this.setTag(k, keyValuePairs[k]);
}
}
}

protected _setOperationName(name: string): void {
this._operationName = name;
}

protected _setBaggageItem(key: string, value: string): void {
this._spanContext = this._spanContext.addBaggageItem(key, value);
}

protected _getBaggageItem(key: string): string | any {
return this._spanContext.baggage[key];
}

protected _context(): SpanContext {
return this._spanContext;
}

protected _finish(finishTime?: number, callback?: (error) => void): void {
if (this._isFinished) {
const spanInfo = `operation=${this.operationName},context=${this.context().toString()}`;
throw new Error(`cant finish the same span twice - ${spanInfo}`);
}
const endTime = finishTime || Utils.now();
this._duration = endTime - this._startTime;
this._tracerImpl.dispatcher().dispatch(this, callback);
this._isFinished = true;
}

toString(): string {
return JSON.stringify(
Utils.merge(this._spanContext, {
Expand Down
Loading