@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
103 lines (102 loc) • 4.03 kB
JavaScript
import { isPrimitive } from "./utils/is.js";
// src/attributes.ts
/**
* Type-guard: The attribute object has the shape the official attribute object (value, type, unit).
* https://develop.sentry.dev/sdk/telemetry/scopes/#setting-attributes
*/
function isAttributeObject(maybeObj) {
return typeof maybeObj === "object" && maybeObj != null && !Array.isArray(maybeObj) && Object.keys(maybeObj).includes("value");
}
/**
* Converts an attribute value to a typed attribute value.
*
* For now, we support primitive values and arrays, either raw or inside attribute objects.
* If @param useFallback is true, we stringify other non-primitive values to a string attribute
* value. Otherwise we return `undefined` for unsupported values.
*
* @param value - The value of the passed attribute.
* @param useFallback - If true, unsupported values will be stringified to a string attribute value.
* Defaults to false. In this case, `undefined` is returned for unsupported values.
* @returns The typed attribute.
*/
function attributeValueToTypedAttributeValue(rawValue, useFallback) {
const { value, unit } = isAttributeObject(rawValue) ? rawValue : {
value: rawValue,
unit: void 0
};
const attributeValue = getTypedAttributeValue(value);
const checkedUnit = unit && typeof unit === "string" ? { unit } : {};
if (attributeValue) return {
...attributeValue,
...checkedUnit
};
if (!useFallback || useFallback === "skip-undefined" && value === void 0) return;
let stringValue = "";
try {
stringValue = JSON.stringify(value) ?? "";
} catch {}
return {
value: stringValue,
type: "string",
...checkedUnit
};
}
/**
* Serializes raw attributes to typed attributes as expected in our envelopes.
*
* @param attributes The raw attributes to serialize.
* @param fallback If true, unsupported values will be stringified to a string attribute value.
* Defaults to false. In this case, `undefined` is returned for unsupported values.
*
* @returns The serialized attributes.
*/
function serializeAttributes(attributes, fallback = false) {
const serializedAttributes = {};
for (const [key, value] of Object.entries(attributes ?? {})) {
const typedValue = attributeValueToTypedAttributeValue(value, fallback);
if (typedValue) serializedAttributes[key] = typedValue;
}
return serializedAttributes;
}
/**
* Estimates the serialized byte size of {@link Attributes},
* with a couple of heuristics for performance.
*/
function estimateTypedAttributesSizeInBytes(attributes) {
if (!attributes) return 0;
let weight = 0;
for (const [key, attr] of Object.entries(attributes)) {
weight += key.length * 2;
weight += attr.type.length * 2;
weight += (attr.unit?.length ?? 0) * 2;
const val = attr.value;
if (Array.isArray(val)) weight += estimatePrimitiveSizeInBytes(val[0]) * val.length;
else if (isPrimitive(val)) weight += estimatePrimitiveSizeInBytes(val);
else weight += 100;
}
return weight;
}
function estimatePrimitiveSizeInBytes(value) {
if (typeof value === "string") return value.length * 2;
else if (typeof value === "boolean") return 4;
else if (typeof value === "number") return 8;
return 0;
}
/**
* NOTE: We return typed attributes for primitives and arrays:
* - Relay currently only supports arrays consisting of primitive values. Attributes with non-conforming arrays are dropped by Relay, so runtime type validation in the SDK is unnecessary.
* - Objects are not supported yet and product support is still TBD.
*/
function getTypedAttributeValue(value) {
if (Array.isArray(value)) return {
value,
type: "array"
};
const primitiveType = typeof value === "string" ? "string" : typeof value === "boolean" ? "boolean" : typeof value === "number" && !Number.isNaN(value) ? Number.isInteger(value) ? "integer" : "double" : null;
if (primitiveType) return {
value,
type: primitiveType
};
}
export { attributeValueToTypedAttributeValue, estimateTypedAttributesSizeInBytes, isAttributeObject, serializeAttributes };
//# sourceMappingURL=attributes.js.map