This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added span_context and associated tests
- added configuation files and package.json that will be needed - added flow declarations as well.
- Loading branch information
Onwukike Ibe
committed
Aug 16, 2016
1 parent
fed975b
commit cfa9140
Showing
13 changed files
with
510 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"presets": ["es2015"] | ||
"presets": ["es2015"], | ||
"plugins": ["transform-flow-strip-types", "transform-class-properties"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[include] | ||
./node_modules/ | ||
|
||
[libs] | ||
./test/decls/ | ||
|
||
[options] | ||
unsafe.enable_getters_and_setters=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
coverage/ | ||
dist/ | ||
node_modules/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// @flow | ||
// Copyright (c) 2016 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import Int64 from 'node-int64'; | ||
import Utils from './util.js'; | ||
|
||
let SAMPLED_MASK = 0x1; | ||
let DEBUG_MASK = 0x2; | ||
|
||
export default class SpanContext { | ||
_traceId: any; | ||
_spanId: any; | ||
_parentId: any; | ||
_flags: number; | ||
_baggage: any; | ||
_baggageHeaderCache: any; | ||
|
||
constructor(traceId: any, | ||
spanId: any, | ||
parentId: any, | ||
flags: number, | ||
baggage: any = {}) { | ||
this._traceId = traceId; | ||
this._spanId = spanId; | ||
this._parentId = parentId; | ||
this._flags = flags; | ||
this._baggage = baggage; | ||
} | ||
|
||
get traceId(): any { | ||
return this._traceId; | ||
} | ||
|
||
get spanId(): any { | ||
return this._spanId; | ||
} | ||
|
||
get parentId(): any { | ||
return this._parentId; | ||
} | ||
|
||
get flags(): number { | ||
return this._flags; | ||
} | ||
|
||
get baggage(): any { | ||
return this._baggage; | ||
} | ||
|
||
static getBaggageHeaderCache() { | ||
if (!this._baggageHeaderCache) { | ||
this._baggageHeaderCache = {}; | ||
} | ||
|
||
return this._baggageHeaderCache; | ||
} | ||
|
||
/** | ||
* Returns a normalize key. | ||
* | ||
* @param {string} key - The key to be normalized for a particular baggage value. | ||
* @return {string} - The normalized key (lower cased and underscores replaced, along with dashes.) | ||
**/ | ||
// todo(oibe) moved to span in newest opentracing version | ||
_normalizeBaggageKey(key: string) { | ||
let baggageHeaderCache = this.constructor.getBaggageHeaderCache(); | ||
if (key in baggageHeaderCache) { | ||
return baggageHeaderCache[key]; | ||
} | ||
|
||
// This stackoverflow claims this approach is slower than regex | ||
// http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript | ||
let normalizedKey: string = key.toLowerCase().split('_').join('-'); | ||
|
||
if (Object.keys(baggageHeaderCache).length < 100) { | ||
baggageHeaderCache[key] = normalizedKey; | ||
} | ||
|
||
return normalizedKey; | ||
} | ||
|
||
/** | ||
* Sets a baggage value with an associated key. | ||
* | ||
* @param {string} key - The baggage key. | ||
* @param {string} value - The baggage value. | ||
**/ | ||
// todo(oibe) moved to span in newest opentracing version | ||
setBaggageItem(key: string, value: string): void { | ||
let normalizedKey = this._normalizeBaggageKey(key); | ||
this._baggage[normalizedKey] = value; | ||
} | ||
|
||
/** | ||
* Gets a baggage value with an associated key. | ||
* | ||
* @param {string} key - The baggage key. | ||
* @return {string} value - The baggage value. | ||
**/ | ||
// todo(oibe) moved to span in newest opentracing version | ||
getBaggageItem(key: string): string { | ||
let normalizedKey = this._normalizeBaggageKey(key); | ||
return this._baggage[normalizedKey]; | ||
} | ||
|
||
/** | ||
* @return {boolean} - returns whether or not this span context was sampled. | ||
**/ | ||
IsSampled(): boolean { | ||
return (this.flags & SAMPLED_MASK) === SAMPLED_MASK; | ||
} | ||
|
||
/** | ||
* @return {boolean} - returns whether or not this span context has a debug flag set. | ||
**/ | ||
IsDebug(): boolean { | ||
return (this.flags & DEBUG_MASK) === DEBUG_MASK; | ||
} | ||
|
||
/** | ||
* @return {string} - returns a string version of this span context. | ||
**/ | ||
toString(): string { | ||
var parentId = this._parentId ? this._parentId.toString('hex') : '0'; | ||
|
||
return [ | ||
Utils.removeLeadingZeros(this._traceId.toString('hex')), | ||
Utils.removeLeadingZeros(this._spanId.toString('hex')), | ||
Utils.removeLeadingZeros(parentId), | ||
this._flags.toString(16) | ||
].join(':'); | ||
} | ||
|
||
/** | ||
* @param {string} serializedString - a serialized span context. | ||
* @return {SpanContext} - returns a span context represented by the serializedString. | ||
**/ | ||
static fromString(serializedString: string): any { | ||
let headers: any = serializedString.split(':'); | ||
if (headers.length !== 4) { | ||
return null; | ||
} | ||
|
||
// todo(oibe) is it better to throw an exception here, or return null? | ||
let traceId: any = Utils.encodeInt64(headers[0]); | ||
let spanId: any = Utils.encodeInt64(headers[1]); | ||
let parentId: any = headers[2]; | ||
let flags: number = parseInt(headers[3], 16); | ||
|
||
let convertedParentId: any = null; | ||
if (!(parentId === '0')) { | ||
convertedParentId = Utils.encodeInt64(parentId); | ||
} | ||
|
||
return new SpanContext( | ||
traceId, | ||
spanId, | ||
convertedParentId, | ||
flags | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @flow | ||
// Copyright (c) 2016 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
export const CLIENT_SEND = 'cs'; | ||
export const CLIENT_RECV = 'cr'; | ||
export const SERVER_SEND = 'ss'; | ||
export const SERVER_RECV = 'sr'; | ||
export const LOCAL_COMPONENT = 'lc'; | ||
export const CLIENT_ADDR = 'ca'; | ||
export const SERVER_ADDR = 'sa'; | ||
export const annotationType = { | ||
BOOL: 'BOOL', | ||
BYTES: 'BYTES', | ||
I16: 'I16', | ||
I32: 'I32', | ||
I64: 'I64', | ||
DOUBLE: 'DOUBLE', | ||
STRING: 'STRING' | ||
}; | ||
|
Oops, something went wrong.