@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
75 lines • 2.74 kB
JavaScript
import { categorizeError, formatErrorMessage, ErrorCategory } from './error-categorizer.js';
import { BArray, BMap } from './bounded-collections.js';
const _runtimeAnalyticsStore = {
timings: new BArray(),
activeTimings: new BMap(),
errors: new BArray(),
retries: new BArray(),
events: new BArray(),
};
export function recordTiming(eventName) {
const now = Date.now();
if (!_runtimeAnalyticsStore.activeTimings.has(eventName)) {
_runtimeAnalyticsStore.activeTimings.set(eventName, now);
recordEvent(`timing:start:${eventName}`);
return;
}
const startTime = _runtimeAnalyticsStore.activeTimings.get(eventName);
if (startTime === undefined)
return;
const duration = now - startTime;
_runtimeAnalyticsStore.timings.push({
event: eventName,
duration,
});
_runtimeAnalyticsStore.activeTimings.delete(eventName);
recordEvent(`timing:end:${eventName}`);
}
export function recordError(error) {
const category = categorizeError(error);
const errorEntry = {
category,
message: (error instanceof Error ? error.message : String(error)).substring(0, 200),
timestamp: Date.now(),
};
if (errorEntry.category === ErrorCategory.Unknown && !errorEntry.message) {
return;
}
_runtimeAnalyticsStore.errors.push(errorEntry);
const normalizedErrorCategory = category.toLowerCase();
const normalizedErrorMessage = formatErrorMessage(error, category);
recordEvent(`error:${normalizedErrorCategory}:${normalizedErrorMessage}`);
}
export function recordRetry(url, operation) {
const existingEntries = _runtimeAnalyticsStore.retries.filter((entry) => entry.url === url && entry.operation === operation);
const attemptCount = existingEntries.length + 1;
_runtimeAnalyticsStore.retries.push({
url,
operation,
attempts: attemptCount,
timestamp: Date.now(),
});
recordEvent(`retry:${operation}:attempt:${attemptCount}`);
}
export function recordEvent(eventName) {
_runtimeAnalyticsStore.events.push({
name: eventName,
timestamp: Date.now(),
});
}
export function compileData() {
return {
timings: _runtimeAnalyticsStore.timings.toArray(),
errors: _runtimeAnalyticsStore.errors.toArray(),
retries: _runtimeAnalyticsStore.retries.toArray(),
events: _runtimeAnalyticsStore.events.toArray(),
};
}
export function reset() {
_runtimeAnalyticsStore.timings.clear();
_runtimeAnalyticsStore.activeTimings.clear();
_runtimeAnalyticsStore.errors.clear();
_runtimeAnalyticsStore.retries.clear();
_runtimeAnalyticsStore.events.clear();
}
//# sourceMappingURL=storage.js.map