@embrace-io/web-sdk
Version:
70 lines (69 loc) • 3.07 kB
JavaScript
import { isSessionPartSpan } from "../../utils/spanPredicates.js";
import { diag } from "@opentelemetry/api";
import { BindOnceFuture, ExportResultCode, internal } from "@opentelemetry/core";
//#region src/processors/EmbraceSessionPartBatchedSpanProcessor/EmbraceSessionPartBatchedSpanProcessor.ts
const exportFailureAttributeKey = (reason, session) => `emb.${session === "current" ? "export_failed" : "previous_export_failed"}.${reason}`;
var EmbraceSessionPartBatchedSpanProcessor = class {
_shutdownOnce;
_pendingSpans = [];
_exporter;
_limitManager;
_userSessionManager;
_diag;
constructor({ exporter, limitManager, userSessionManager, diag: diagParam = diag.createComponentLogger({ namespace: "EmbraceSessionPartBatchedSpanProcessor" }) }) {
this._diag = diagParam;
this._exporter = exporter;
this._shutdownOnce = new BindOnceFuture(this._shutdown, this);
this._limitManager = limitManager;
this._userSessionManager = userSessionManager;
}
forceFlush() {
this._diag.debug("forceFlush called for EmbraceSessionPartBatchedSpanProcessor. This is a no op");
return Promise.resolve(void 0);
}
onEnd(span) {
if (this._shutdownOnce.isCalled) {
this._diag.debug("span ended after processor shutdown. Ignoring span.");
return;
}
if (!isSessionPartSpan(span)) {
this._diag.debug("non-session-part span ended. Adding to pending spans queue.");
if (this._limitManager.dropReadableSpan(span)) return;
this._pendingSpans.push(span);
} else {
this._diag.debug("session part span ended. Exporting all pending spans.");
this._exportSpans([span, ...this._pendingSpans]);
this._pendingSpans = [];
}
}
_exportSpans(spans) {
internal._export(this._exporter, spans).then((result) => {
if (result.code === ExportResultCode.FAILED) {
const errorMessage = result.error?.message || "unknown error";
let failureReason = "unknown";
if (errorMessage === "Concurrent export limit reached") failureReason = "concurrent_limit";
else if (errorMessage === "Fetch request errored") failureReason = "fetch_error";
this._userSessionManager.incrSessionPartCountForKey(exportFailureAttributeKey(failureReason, "current"));
this._userSessionManager.incrNextSessionPartCountForKey(exportFailureAttributeKey(failureReason, "previous"));
this._diag.error(`spans failed to export: ${errorMessage}`);
}
}).catch((reason) => {
let msg = "unknown error";
if (reason && reason instanceof Error) msg = reason.message;
else if (typeof reason === "string") msg = reason;
this._userSessionManager.incrSessionPartCountForKey(exportFailureAttributeKey("unknown", "current"));
this._userSessionManager.incrNextSessionPartCountForKey(exportFailureAttributeKey("unknown", "previous"));
this._diag.error(`spans failed to export: ${msg}`);
});
}
onStart() {}
shutdown() {
return this._shutdownOnce.call();
}
_shutdown = () => {
return this._exporter.shutdown();
};
};
//#endregion
export { EmbraceSessionPartBatchedSpanProcessor };
//# sourceMappingURL=EmbraceSessionPartBatchedSpanProcessor.js.map