-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: sanitize attributes inputs #2881
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,17 +13,19 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { SpanAttributeValue, SpanAttributes } from '@opentelemetry/api'; | ||
import { AttributeValue, Attributes } from '@opentelemetry/api'; | ||
|
||
export function sanitizeAttributes(attributes: unknown): SpanAttributes { | ||
const out: SpanAttributes = {}; | ||
export function sanitizeAttributes(attributes: unknown): Attributes { | ||
const out: Attributes = {}; | ||
|
||
if (attributes == null || typeof attributes !== 'object') { | ||
if (typeof attributes !== 'object' || attributes == null) { | ||
return out; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
for (const [k, v] of Object.entries(attributes!)) { | ||
for (const [k, v] of Object.entries(attributes)) { | ||
vmarchaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!isAttributeKey(k)) { | ||
continue; | ||
} | ||
if (isAttributeValue(v)) { | ||
if (Array.isArray(v)) { | ||
out[k] = v.slice(); | ||
|
@@ -36,7 +38,11 @@ export function sanitizeAttributes(attributes: unknown): SpanAttributes { | |
return out; | ||
} | ||
|
||
export function isAttributeValue(val: unknown): val is SpanAttributeValue { | ||
export function isAttributeKey(key: unknown): key is string { | ||
return typeof key === 'string' && key.length > 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor perf and minification improvements by reordering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't find there is any big perf difference between the two flavors: https://jsbench.me/30l1ntj53a/1 TBO, I think checking the length is more explicit and easier to read. |
||
} | ||
|
||
export function isAttributeValue(val: unknown): val is AttributeValue { | ||
if (val == null) { | ||
return true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
githuc :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should consider a less elaborate changelog format?
This might be good enough? Maybe name/handle is even optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree the current form is kinda verbose :) I'd be happy to shorten the entries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dyladan updated :)