@embrace-io/react-native-navigation
Version:
A React Native wrapper for the OTel Navigation instrumentation library that creates telemetry data representing Navigation changes
70 lines (69 loc) • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ATTRIBUTES = exports.spanEnd = exports.spanStart = exports.spanCreatorAppState = exports.spanCreator = void 0;
const ATTRIBUTES = {
initialView: "view.launch",
finalView: "view.unmount",
viewName: "view.name",
appState: "view.state.end",
};
exports.ATTRIBUTES = ATTRIBUTES;
const spanStart = (tracer, span, currentRouteName, customAttributes, isLaunch) => {
if (!tracer.current || span.current !== null) {
// do nothing in case for some reason the tracer is not initialized or there is already an active span
return;
}
const attributes = Object.assign({
// it should create the first span knowing there is not a previous view
[ATTRIBUTES.initialView]: !!isLaunch,
// it should set the view name in case it's useful to have this as an attr
[ATTRIBUTES.viewName]: currentRouteName }, customAttributes);
// Starting the span
span.current = tracer.current.startSpan(currentRouteName, { attributes });
};
exports.spanStart = spanStart;
const spanEnd = (span, appState, isUnmount) => {
if (span.current) {
span.current.setAttribute(ATTRIBUTES.appState, appState !== null && appState !== void 0 ? appState : "active");
if (isUnmount) {
span.current.setAttribute(ATTRIBUTES.finalView, true);
}
span.current.end();
// make sure we destroy any existing span
span.current = null;
}
};
exports.spanEnd = spanEnd;
const spanCreator = (tracer, span, view, console, customAttributes) => (currentRouteName) => {
if (!tracer.current) {
// do nothing in case for some reason the tracer is not initialized
console.log("[Embrace] no tracer available, not creating a span");
return;
}
const isInitialView = view.current === null;
const shouldEndCurrentSpan = view.current !== null && view.current !== currentRouteName;
// it means the view has changed and we are ending the previous span
if (shouldEndCurrentSpan) {
console.log("[Embrace] ending the current view span");
spanEnd(span);
}
console.log(`[Embrace] starting a span for ${currentRouteName}`);
spanStart(tracer, span, currentRouteName, customAttributes, isInitialView);
// last step before it changes the view
view.current = currentRouteName;
};
exports.spanCreator = spanCreator;
const spanCreatorAppState = (tracer, span, customAttributes) => (currentRouteName, currentState) => {
if (currentState === null || currentState === undefined) {
return;
}
if (currentState === "active") {
console.log(`[Embrace] moving into the foreground and starting span for ${currentRouteName}`);
spanStart(tracer, span, currentRouteName, customAttributes);
}
else {
console.log("[Embrace] moving into the background");
spanEnd(span, currentState);
}
};
exports.spanCreatorAppState = spanCreatorAppState;