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 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BaggageRestrictionManager and BaggageSetter (#142)
- Loading branch information
1 parent
f29839b
commit 5bd0763
Showing
12 changed files
with
399 additions
and
46 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
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,32 @@ | ||
// @flow | ||
// Copyright (c) 2017 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 Restriction from '../baggage/restriction.js' | ||
|
||
/** | ||
* BaggageRestrictionManager is an interface for a class that manages baggage | ||
* restrictions for baggage keys. The manager will return a Restriction | ||
* for a specific baggage key which will determine whether the baggage key is | ||
* allowed and any other applicable restrictions on the baggage value. | ||
*/ | ||
declare interface BaggageRestrictionManager { | ||
getRestriction(service: string, key: string): Restriction; | ||
} |
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,89 @@ | ||
// @flow | ||
// Copyright (c) 2017 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 Span from '../span.js'; | ||
import SpanContext from '../span_context.js'; | ||
import Metrics from '../metrics/metrics.js' | ||
|
||
/** | ||
* BaggageSetter is a class that sets a baggage key:value and the associated | ||
* logs on a Span. | ||
*/ | ||
export default class BaggageSetter { | ||
_restrictionManager: BaggageRestrictionManager; | ||
_metrics: Metrics; | ||
|
||
constructor(restrictionManager: BaggageRestrictionManager, metrics: Metrics) { | ||
this._restrictionManager = restrictionManager; | ||
this._metrics = metrics; | ||
} | ||
|
||
/** | ||
* Sets the baggage key:value on the span and the corresponding logs. | ||
* A SpanContext is returned with the new baggage key:value set. | ||
* | ||
* @param {Span} span - The span to set the baggage on. | ||
* @param {string} key - The baggage key to set. | ||
* @param {string} baggageValue - The baggage value to set. | ||
* @return {SpanContext} - The SpanContext with the baggage set if applicable. | ||
*/ | ||
setBaggage(span: Span, key: string, baggageValue: string): SpanContext { | ||
let value = baggageValue; | ||
let truncated = false; | ||
let prevItem = ''; | ||
let restriction = this._restrictionManager.getRestriction(span.serviceName, key); | ||
if (!restriction.keyAllowed) { | ||
this._logFields(span, key, value, prevItem, truncated, restriction.keyAllowed); | ||
this._metrics.baggageUpdateFailure.increment(1); | ||
return span.context(); | ||
} | ||
if (value.length > restriction.maxValueLength) { | ||
truncated = true; | ||
value = value.substring(0, restriction.maxValueLength); | ||
this._metrics.baggageTruncate.increment(1); | ||
} | ||
prevItem = span.getBaggageItem(key); | ||
this._logFields(span, key, value, prevItem, truncated, restriction.keyAllowed); | ||
this._metrics.baggageUpdateSuccess.increment(1); | ||
return span.context().withBaggageItem(key, value); | ||
} | ||
|
||
_logFields(span: Span, key: string, value: string, prevItem: string, truncated: boolean, valid: boolean) { | ||
if (!span.context().isSampled()) { | ||
return | ||
} | ||
let fields: { [key: string]: string } = { | ||
event: 'baggage', | ||
key: key, | ||
value: value, | ||
}; | ||
if (prevItem) { | ||
fields.override = 'true'; | ||
} | ||
if (truncated) { | ||
fields.truncated = 'true'; | ||
} | ||
if (!valid) { | ||
fields.invalid = 'true'; | ||
} | ||
span.log(fields); | ||
} | ||
} |
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,40 @@ | ||
// @flow | ||
// Copyright (c) 2017 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 Restriction from './restriction.js' | ||
|
||
export const DEFAULT_MAX_VALUE_LENGTH = 2048; | ||
|
||
/** | ||
* Creates a BaggageRestrictionManager that allows any baggage key. | ||
*/ | ||
export default class DefaultBaggageRestrictionManager { | ||
_restriction: Restriction; | ||
|
||
constructor(maxValueLength: ?number) { | ||
let length = maxValueLength || DEFAULT_MAX_VALUE_LENGTH; | ||
this._restriction = new Restriction(true, length); | ||
} | ||
|
||
getRestriction(service: string, key: string): Restriction { | ||
return this._restriction; | ||
} | ||
} |
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,42 @@ | ||
// @flow | ||
// Copyright (c) 2017 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. | ||
|
||
/** | ||
* Restriction determines whether a baggage key is allowed and contains any | ||
* restrictions on the baggage value. | ||
*/ | ||
export default class Restriction { | ||
_keyAllowed: boolean; | ||
_maxValueLength: number; | ||
|
||
constructor(keyAllowed: boolean, maxValueLength: number) { | ||
this._keyAllowed = keyAllowed; | ||
this._maxValueLength = maxValueLength; | ||
} | ||
|
||
get keyAllowed(): boolean { | ||
return this._keyAllowed; | ||
} | ||
|
||
get maxValueLength(): number { | ||
return this._maxValueLength; | ||
} | ||
} |
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
Oops, something went wrong.