@opentelemetry/core
Version:
OpenTelemetry Core provides constants and utilities shared by all OpenTelemetry SDK packages.
82 lines • 2.4 kB
JavaScript
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { diag } from '@opentelemetry/api';
export function sanitizeAttributes(attributes) {
const out = {};
if (typeof attributes !== 'object' || attributes == null) {
return out;
}
for (const [key, val] of Object.entries(attributes)) {
if (!isAttributeKey(key)) {
diag.warn(`Invalid attribute key: ${key}`);
continue;
}
if (!isAttributeValue(val)) {
diag.warn(`Invalid attribute value set for key: ${key}`);
continue;
}
if (Array.isArray(val)) {
out[key] = val.slice();
}
else {
out[key] = val;
}
}
return out;
}
export function isAttributeKey(key) {
return typeof key === 'string' && key.length > 0;
}
export function isAttributeValue(val) {
if (val == null) {
return true;
}
if (Array.isArray(val)) {
return isHomogeneousAttributeValueArray(val);
}
return isValidPrimitiveAttributeValue(val);
}
function isHomogeneousAttributeValueArray(arr) {
let type;
for (const element of arr) {
// null/undefined elements are allowed
if (element == null)
continue;
if (!type) {
if (isValidPrimitiveAttributeValue(element)) {
type = typeof element;
continue;
}
// encountered an invalid primitive
return false;
}
if (typeof element === type) {
continue;
}
return false;
}
return true;
}
function isValidPrimitiveAttributeValue(val) {
switch (typeof val) {
case 'number':
case 'boolean':
case 'string':
return true;
}
return false;
}
//# sourceMappingURL=attributes.js.map