Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
- added span_context and associated tests
Browse files Browse the repository at this point in the history
- 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
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .babelrc
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"]
}
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@

// Disabled as the "exceptions" clause does not seem to currently work
"spaced-comment": [0],
},
"plugins": {
"flowtype"
}
}
8 changes: 8 additions & 0 deletions .flowconfig
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage/
dist/
node_modules/
npm-debug.log
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ services:

env:
global:
- DOCKER_VERSION=1.11.1-0~trusty
- DOCKER_VERSION=1.12.0-0~trusty
- DOCKER_COMPOSE_VERSION=1.7.0
- COMMIT=${TRAVIS_COMMIT::8}

before_install:
- apt-cache madison docker-engine
- sudo apt-get -o Dpkg::Options::="--force-confnew" install -y docker-engine=${DOCKER_VERSION}
- docker version
- sudo rm /usr/local/bin/docker-compose
- sudo rm -rf /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname
-s`-`uname -m` > docker-compose
- chmod +x docker-compose
Expand Down
27 changes: 22 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,44 @@
"name": "oibe"
}
],
"dependencies": {},
"dependencies": {
"assert": "^1.4.1",
"my-local-ip": "^1.0.0",
"node-int64": "^0.4.0",
"xorshift": "^0.2.0"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-istanbul": "^0.11.0",
"babel-node-debug": "^2.0.0",
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.13.2",
"babel-register": "^6.11.6",
"buffer-equal": "^1.0.0",
"chai": "^3.5.0",
"eslint": "^2.4.0",
"eslint-config-airbnb": "^6.2.0",
"eslint-plugin-flowtype": "^2.4.0",
"eslint-plugin-react": "^4.3.0",
"faucet": "0.0.1",
"flow-bin": "^0.30.0",
"mocha": "^3.0.1",
"nyc": "2.4.0",
"sinon": "^1.17.5"
"opentracing": "^0.11.1",
"sinon": "^1.17.5",
"uber-licence": "^2.0.2"
},
"scripts": {
"add-license": "./node_modules/.bin/uber-licence",
"check-license": "./node_modules/.bin/uber-licence --dry",
"check-ls": "npm ls --loglevel=http --parseable 1>/dev/null && echo '# npm is in a good state'",
"test": "npm run lint && ./node_modules/mocha/bin/mocha --compilers js:babel-core/register",
"compile": "babel --presets es2015 -d dist/ src/",
"lint": "eslint $(ls src/ | grep '.js$') && echo '# linter passed'",
"compile": "babel --presets es2015 --plugins transform-class-properties -d dist/ src/",
"cover": "./node_modules/.bin/babel-node ./node_modules/.bin/babel-istanbul cover ./node_modules/.bin/_mocha -- test/",
"flow": "flow; test $? -eq 0 -o $? -eq 2",
"lint": "eslint $(ls src/ | grep '.js$') && echo '# linter passed'",
"report-cover": "nyc report --reporter html --reporter text",
"test": "npm run lint && ./node_modules/mocha/bin/mocha --compilers js:babel-core/register",
"check-cover": "nyc check-coverage --branches=10 --lines=10 --functions=10",
"view-cover": "opn ./coverage/index.html"
}
Expand Down
20 changes: 20 additions & 0 deletions src/leaky_bucket_rate_limiter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// 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 default class RateLimiter {

constructor(creditsPerSecond) {
Expand Down
180 changes: 180 additions & 0 deletions src/span_context.js
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
);
}
}
38 changes: 38 additions & 0 deletions src/thrift.js
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'
};

Loading

0 comments on commit cfa9140

Please sign in to comment.