@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
92 lines (91 loc) • 4.26 kB
JavaScript
import { getCurrentScope } from "../currentScopes.js";
import { DEBUG_BUILD } from "../debug-build.js";
import { debug } from "./debug-logger.js";
import { getActiveSpan, spanToStaticSpanJSON } from "./spanUtils.js";
// src/utils/featureFlags.ts
/**
* Max size of the LRU flag buffer stored in Sentry scope and event contexts.
*/
const _INTERNAL_FLAG_BUFFER_SIZE = 100;
/**
* Max number of flag evaluations to record per span.
*/
const _INTERNAL_MAX_FLAGS_PER_SPAN = 10;
const SPAN_FLAG_ATTRIBUTE_PREFIX = "flag.evaluation.";
/**
* Copies feature flags that are in current scope context to the event context
*/
function _INTERNAL_copyFlagsFromScopeToEvent(event) {
if (event.type) return event;
const flagContext = getCurrentScope().getScopeData().contexts.flags;
const flagBuffer = flagContext ? flagContext.values : [];
if (!flagBuffer.length) return event;
if (event.contexts === void 0) event.contexts = {};
event.contexts.flags = { values: [...flagBuffer] };
return event;
}
/**
* Inserts a flag into the current scope's context while maintaining ordered LRU properties.
* Not thread-safe. After inserting:
* - The flag buffer is sorted in order of recency, with the newest evaluation at the end.
* - The names in the buffer are always unique.
* - The length of the buffer never exceeds `maxSize`.
*
* @param name Name of the feature flag to insert.
* @param value Value of the feature flag.
* @param maxSize Max number of flags the buffer should store. Default value should always be used in production.
*/
function _INTERNAL_insertFlagToScope(name, value, maxSize = 100) {
const scopeContexts = getCurrentScope().getScopeData().contexts;
if (!scopeContexts.flags) scopeContexts.flags = { values: [] };
const flags = scopeContexts.flags.values;
_INTERNAL_insertToFlagBuffer(flags, name, value, maxSize);
}
/**
* Exported for tests only. Currently only accepts boolean values (otherwise no-op).
* Inserts a flag into a FeatureFlag array while maintaining the following properties:
* - Flags are sorted in order of recency, with the newest evaluation at the end.
* - The flag names are always unique.
* - The length of the array never exceeds `maxSize`.
*
* @param flags The buffer to insert the flag into.
* @param name Name of the feature flag to insert.
* @param value Value of the feature flag.
* @param maxSize Max number of flags the buffer should store. Default value should always be used in production.
*/
function _INTERNAL_insertToFlagBuffer(flags, name, value, maxSize) {
if (typeof value !== "boolean") return;
if (flags.length > maxSize) {
DEBUG_BUILD && debug.error(`[Feature Flags] insertToFlagBuffer called on a buffer larger than maxSize=${maxSize}`);
return;
}
const index = flags.findIndex((f) => f.flag === name);
if (index !== -1) flags.splice(index, 1);
if (flags.length === maxSize) flags.shift();
flags.push({
flag: name,
result: value
});
}
/**
* Records a feature flag evaluation for the active span. This is a no-op for non-boolean values.
* The flag and its value is stored in span attributes with the `flag.evaluation` prefix. Once the
* unique flags for a span reaches maxFlagsPerSpan, subsequent flags are dropped.
*
* @param name Name of the feature flag.
* @param value Value of the feature flag. Non-boolean values are ignored.
* @param maxFlagsPerSpan Max number of flags a buffer should store. Default value should always be used in production.
*/
function _INTERNAL_addFeatureFlagToActiveSpan(name, value, maxFlagsPerSpan = 10) {
if (typeof value !== "boolean") return;
const span = getActiveSpan();
if (!span) return;
const attributes = spanToStaticSpanJSON(span).data;
if (`${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}` in attributes) {
span.setAttribute(`${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}`, value);
return;
}
if (Object.keys(attributes).filter((key) => key.startsWith(SPAN_FLAG_ATTRIBUTE_PREFIX)).length < maxFlagsPerSpan) span.setAttribute(`${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}`, value);
}
export { _INTERNAL_FLAG_BUFFER_SIZE, _INTERNAL_MAX_FLAGS_PER_SPAN, _INTERNAL_addFeatureFlagToActiveSpan, _INTERNAL_copyFlagsFromScopeToEvent, _INTERNAL_insertFlagToScope, _INTERNAL_insertToFlagBuffer };
//# sourceMappingURL=featureFlags.js.map