@zendesk/react-measure-timing-hooks
Version:
react hooks for measuring time to interactive and time to render of components
87 lines • 3.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.TraceManager = void 0;
const ensureMatcherFn_1 = require("./ensureMatcherFn");
const Tracer_1 = require("./Tracer");
/**
* Class representing the centralized trace performance manager.
*/
class TraceManager {
performanceEntryDeduplicationStrategy;
currentTrace = undefined;
get currentTracerContext() {
if (!this.currentTrace)
return undefined;
return this.currentTrace;
}
constructor(configInput) {
this.utilities = {
// by default noop for warnings
reportWarningFn: () => { },
...configInput,
replaceCurrentTrace: (newTrace, reason) => {
if (this.currentTrace) {
this.currentTrace.interrupt(reason);
}
this.currentTrace = newTrace;
},
onEndTrace: (traceToCleanUp) => {
if (traceToCleanUp === this.currentTrace) {
this.currentTrace = undefined;
}
// warn on miss?
},
getCurrentTrace: () => this.currentTrace,
};
}
utilities;
createTracer(traceDefinition) {
const requiredSpans = (0, ensureMatcherFn_1.convertMatchersToFns)(traceDefinition.requiredSpans);
if (!requiredSpans) {
throw new Error('requiredSpans must be defined, as a trace will never end otherwise');
}
const labelMatching = traceDefinition.labelMatching
? (0, ensureMatcherFn_1.convertLabelMatchersToFns)(traceDefinition.labelMatching)
: undefined;
const debounceOnSpans = (0, ensureMatcherFn_1.convertMatchersToFns)(traceDefinition.debounceOnSpans);
const interruptOnSpans = (0, ensureMatcherFn_1.convertMatchersToFns)(traceDefinition.interruptOnSpans);
const suppressErrorStatusPropagationOnSpans = (0, ensureMatcherFn_1.convertMatchersToFns)(traceDefinition.suppressErrorStatusPropagationOnSpans);
const computedSpanDefinitions = Object.fromEntries(Object.entries(traceDefinition.computedSpanDefinitions ?? {}).map(([name, def]) => [
name,
{
startSpan: typeof def.startSpan === 'string'
? def.startSpan
: (0, ensureMatcherFn_1.ensureMatcherFn)(def.startSpan),
endSpan: typeof def.endSpan === 'string'
? def.endSpan
: (0, ensureMatcherFn_1.ensureMatcherFn)(def.endSpan),
},
]));
const computedValueDefinitionsInputEntries = Object.entries(traceDefinition.computedValueDefinitions ?? {});
const computedValueDefinitions = Object.fromEntries(computedValueDefinitionsInputEntries.map(([name, def]) => [
name,
{
...def,
matches: def.matches.map((m) => (0, ensureMatcherFn_1.ensureMatcherFn)(m)),
computeValueFromMatches: def.computeValueFromMatches,
},
]));
const completeTraceDefinition = {
...traceDefinition,
requiredSpans,
debounceOnSpans,
interruptOnSpans,
suppressErrorStatusPropagationOnSpans,
computedSpanDefinitions,
computedValueDefinitions,
labelMatching,
relationSchema: this.utilities.relationSchemas[traceDefinition.relationSchemaName],
};
return new Tracer_1.Tracer(completeTraceDefinition, this.utilities);
}
processSpan(span) {
return this.currentTrace?.processSpan(span);
}
}
exports.TraceManager = TraceManager;
//# sourceMappingURL=TraceManager.js.map
;