Skip to content
Open
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
29 changes: 17 additions & 12 deletions packages/opentelemetry-core/src/common/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ export function sanitizeAttributes(attributes: unknown): Attributes {
return out;
}

for (const [key, val] of Object.entries(attributes)) {
if (!isAttributeKey(key)) {
for (const key in attributes) {
if (
!Object.prototype.hasOwnProperty.call(attributes, key) ||
!isAttributeKey(key)
) {
diag.warn(`Invalid attribute key: ${key}`);
continue;
}
const val = (attributes as Record<string, unknown>)[key];
if (!isAttributeValue(val)) {
diag.warn(`Invalid attribute value set for key: ${key}`);
continue;
Expand All @@ -43,7 +47,7 @@ export function sanitizeAttributes(attributes: unknown): Attributes {
}

export function isAttributeKey(key: unknown): key is string {
return typeof key === 'string' && key.length > 0;
return typeof key === 'string' && key !== '';
}

export function isAttributeValue(val: unknown): val is AttributeValue {
Expand All @@ -55,7 +59,7 @@ export function isAttributeValue(val: unknown): val is AttributeValue {
return isHomogeneousAttributeValueArray(val);
}

return isValidPrimitiveAttributeValue(val);
return isValidPrimitiveAttributeValueType(typeof val);
}

function isHomogeneousAttributeValueArray(arr: unknown[]): boolean {
Expand All @@ -64,28 +68,29 @@ function isHomogeneousAttributeValueArray(arr: unknown[]): boolean {
for (const element of arr) {
// null/undefined elements are allowed
if (element == null) continue;
const elementType = typeof element;

if (elementType === type) {
continue;
}

if (!type) {
if (isValidPrimitiveAttributeValue(element)) {
type = typeof element;
if (isValidPrimitiveAttributeValueType(elementType)) {
type = elementType;
continue;
}
// encountered an invalid primitive
return false;
}

if (typeof element === type) {
continue;
}

return false;
}

return true;
}

function isValidPrimitiveAttributeValue(val: unknown): boolean {
switch (typeof val) {
function isValidPrimitiveAttributeValueType(valType: string): boolean {
switch (valType) {
case 'number':
case 'boolean':
case 'string':
Expand Down
Loading