Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* test(shim-opentracing): add comparison thresholds in flaky assertions [#5974](https://github.com/open-telemetry/opentelemetry-js/pull/5974) @cjihrig
* test(exporter-jaeger): clean up OTEL_EXPORTER_JAEGER_AGENT_PORT between tests [#6003](https://github.com/open-telemetry/opentelemetry-js/pull/6003) @cjihrig
* test(sdk-trace-base): ensure environment variables are cleaned up between tests [#6011](https://github.com/open-telemetry/opentelemetry-js/pull/6011) @cjihrig
* perf(opentelemetry-core): optimize attribute serialization [#5866](https://github.com/open-telemetry/opentelemetry-js/pull/5866) @43081j

## 2.1.0

Expand Down
27 changes: 16 additions & 11 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)) {
for (const key in attributes) {
if (!Object.prototype.hasOwnProperty.call(attributes, key)) {
continue;
}
if (!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