UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

273 lines (272 loc) • 8.67 kB
/** * Abstract base class for all observability exporters * Follows NeuroLink's factory pattern with a unified exporter interface */ import { logger } from "../../utils/logger.js"; // What each exporter delivered and dropped is kept here, not on the instance: // a subclass written before these counts existed may declare a member of any // name, and a plain-JavaScript exporter has no counts at all. const deliveryTallies = new WeakMap(); const deliveryReaders = new WeakMap(); const NO_DELIVERIES = { delivered: 0, failed: 0, lastError: undefined, }; /** * Backoff of `withRetry` unless its caller passes another: one second, doubled * for every attempt that failed. */ export function exponentialRetryDelayMs(_error, attempt) { return 1000 * 2 ** attempt; } /** * Lets an exporter that counts its own deliveries, instead of through * `createSuccessResult` and `createFailureResult`, say what it counted. */ export function reportDeliveriesWith(exporter, read) { deliveryReaders.set(exporter, read); } /** * Snapshot of a registered exporter for status reporting, without a network * call. It reads only what every exporter has had since the first release * (`getName`, `isInitialized`, `getPendingCount`, `getLastExportTime`), so an * exporter written in plain JavaScript against that set is reported too. */ export function exporterStatusOf(exporter) { const { delivered, failed, lastError } = (deliveryReaders.get(exporter) ?? (() => deliveryTallies.get(exporter) ?? NO_DELIVERIES))(); const initialized = exporter.isInitialized(); const lastExportTime = typeof exporter.getLastExportTime === "function" ? exporter.getLastExportTime() : 0; return { name: exporter.getName(), enabled: initialized, healthy: initialized && lastError === undefined, pendingSpans: exporter.getPendingCount(), deliveredSpans: delivered, failedSpans: failed, ...(lastExportTime > 0 && { lastExportTime: new Date(lastExportTime).toISOString(), }), ...(lastError !== undefined && { errors: [lastError] }), }; } /** * Abstract base class for all observability exporters * Provides common functionality: buffering, flush intervals, health checks, retry logic */ export class BaseExporter { name; config; initialized = false; buffer = []; maxBufferSize; retries; flushInterval = null; lastExportTime = 0; constructor(name, config) { this.name = name; this.config = config; this.maxBufferSize = config.maxBufferSize ?? 100; this.retries = config.retries ?? 3; } /** * Ping the exporter's backend to verify connectivity * Override this in subclasses to provide backend-specific health check */ async ping() { // Default implementation does nothing // Subclasses should override this to make an actual API call } /** * Buffer a span for batch export * Triggers flush if buffer is full */ bufferSpan(span) { this.buffer.push(span); if (this.buffer.length >= this.maxBufferSize) { // Use void to explicitly ignore the promise void this.flush(); } } /** * Start automatic flush interval * @param intervalMs - Interval in milliseconds between flushes */ startFlushInterval(intervalMs) { if (this.flushInterval) { clearInterval(this.flushInterval); } this.flushInterval = setInterval(() => { this.flush().catch((error) => { logger.warn(`[${this.name}] Periodic flush failed`, { error: error instanceof Error ? error.message : String(error), }); }); }, intervalMs); } /** * Stop the automatic flush interval */ stopFlushInterval() { if (this.flushInterval) { clearInterval(this.flushInterval); this.flushInterval = null; } } /** * Get exporter name */ getName() { return this.name; } /** * Check if exporter is initialized */ isInitialized() { return this.initialized; } /** * Get number of pending spans in buffer */ getPendingCount() { return this.buffer.length; } /** * Get last export timestamp */ getLastExportTime() { return this.lastExportTime; } /** * Create a standard export result for success */ createSuccessResult(exportedCount, durationMs) { this.lastExportTime = Date.now(); const tally = deliveryTallies.get(this) ?? NO_DELIVERIES; deliveryTallies.set(this, { delivered: tally.delivered + exportedCount, failed: tally.failed, lastError: undefined, }); return { success: true, exportedCount, failedCount: 0, durationMs, }; } /** * Create a standard export result for failure */ createFailureResult(spanIds, error, durationMs, retryable = true) { const tally = deliveryTallies.get(this) ?? NO_DELIVERIES; deliveryTallies.set(this, { delivered: tally.delivered, failed: tally.failed + spanIds.length, lastError: error, }); return { success: false, exportedCount: 0, failedCount: spanIds.length, errors: spanIds.map((spanId) => ({ spanId, error, retryable, })), durationMs, }; } /** * Create a standard health status */ createHealthStatus(healthy, errors) { return { healthy, name: this.name, pendingSpans: this.buffer.length, lastExportTime: this.lastExportTime || undefined, errors, }; } /** * Execute an operation with exponential backoff retry * @param operation - The async operation to execute * @param operationName - Name for logging purposes * @param signal - Stops further attempts and cuts a backoff wait short when aborted * @param delayMs - Milliseconds to wait before the next attempt, given why the attempt failed and its zero-based number; one second doubled per attempt by default * @returns The result of the operation * @throws The last error if all retries fail, or the abort reason when the signal fires */ async withRetry(operation, operationName, signal, delayMs = exponentialRetryDelayMs) { const maxRetries = this.retries; let lastError; for (let attempt = 0; attempt <= maxRetries; attempt++) { signal?.throwIfAborted(); try { return await operation(); } catch (error) { lastError = error; if (signal?.aborted) { break; } if (attempt < maxRetries) { const delay = delayMs(lastError, attempt); logger.warn(`[${this.name}] ${operationName} failed, retrying in ${delay}ms`, { attempt: attempt + 1, maxRetries, error: lastError.message, }); await sleepUnlessAborted(delay, signal); } } } throw lastError; } } function sleepUnlessAborted(delayMs, signal) { return new Promise((resolve) => { if (signal?.aborted) { resolve(); return; } const finish = () => { clearTimeout(timer); signal?.removeEventListener("abort", finish); resolve(); }; const timer = setTimeout(finish, delayMs); signal?.addEventListener("abort", finish, { once: true }); }); } /** * No-op exporter for when observability is disabled * Provides zero-overhead behavior */ export class NoOpExporter extends BaseExporter { constructor() { super("noop", { enabled: false }); } async initialize() { this.initialized = true; } async exportSpan(_span) { return this.createSuccessResult(0, 0); } async exportBatch(_spans) { return this.createSuccessResult(0, 0); } async flush() { // No-op } async shutdown() { this.initialized = false; } async healthCheck() { return this.createHealthStatus(true); } }