UNPKG

@genkit-ai/core

Version:

Genkit AI framework core libraries.

221 lines 6.53 kB
import { context, SpanKind } from "@opentelemetry/api"; import { ExportResultCode, hrTimeToMilliseconds, suppressTracing } from "@opentelemetry/core"; import { logger } from "../logging.mjs"; import { deleteUndefinedProps } from "../utils.mjs"; let telemetryServerUrl; function setTelemetryServerUrl(url) { telemetryServerUrl = url; } class TraceServerExporter { /** * Export spans. * @param spans * @param resultCallback */ export(spans, resultCallback) { this._sendSpans(spans, resultCallback); } /** * Shutdown the exporter. */ shutdown() { this._sendSpans([]); return this.forceFlush(); } /** * Converts span info into trace store format. * @param span */ _exportInfo(span) { const spanData = { spanId: span.spanContext().spanId, traceId: span.spanContext().traceId, startTime: transformTime(span.startTime), endTime: transformTime(span.endTime), attributes: { ...span.attributes }, displayName: span.name, links: span.links, spanKind: SpanKind[span.kind], parentSpanId: span.parentSpanId, sameProcessAsParentSpan: { value: !span.spanContext().isRemote }, status: span.status, timeEvents: { timeEvent: span.events.map((e) => ({ time: transformTime(e.time), annotation: { attributes: e.attributes ?? {}, description: e.name } })) } }; if (span.instrumentationLibrary !== void 0) { spanData.instrumentationLibrary = { name: span.instrumentationLibrary.name }; if (span.instrumentationLibrary.schemaUrl !== void 0) { spanData.instrumentationLibrary.schemaUrl = span.instrumentationLibrary.schemaUrl; } if (span.instrumentationLibrary.version !== void 0) { spanData.instrumentationLibrary.version = span.instrumentationLibrary.version; } } deleteUndefinedProps(spanData); return spanData; } /** * Exports any pending spans in exporter */ forceFlush() { return Promise.resolve(); } async _sendSpans(spans, done) { const traces = {}; for (const span of spans) { if (!traces[span.spanContext().traceId]) { traces[span.spanContext().traceId] = []; } traces[span.spanContext().traceId].push(span); } let error = false; for (const traceId of Object.keys(traces)) { try { await this.save(traceId, traces[traceId]); } catch (e) { error = true; logger.error(`Failed to save trace ${traceId}`, e); } if (done) { return done({ code: error ? ExportResultCode.FAILED : ExportResultCode.SUCCESS }); } } } async save(traceId, spans) { if (!telemetryServerUrl) { logger.debug( `Telemetry server is not configured, trace ${traceId} not saved!` ); return; } const data = { traceId, spans: {} }; for (const span of spans) { const convertedSpan = this._exportInfo(span); data.spans[convertedSpan.spanId] = convertedSpan; if (!convertedSpan.parentSpanId) { data.displayName = convertedSpan.displayName; data.startTime = convertedSpan.startTime; data.endTime = convertedSpan.endTime; } } await context.with( suppressTracing(context.active()), () => fetch(`${telemetryServerUrl}/api/traces`, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" }, body: JSON.stringify(data) }) ); } } function transformTime(time) { return hrTimeToMilliseconds(time); } class LogServerExporter { export(logs, resultCallback) { this._sendLogs(logs, resultCallback); } shutdown() { return this.forceFlush(); } forceFlush() { return Promise.resolve(); } async _sendLogs(logs, done) { if (!telemetryServerUrl) { if (done) done({ code: ExportResultCode.SUCCESS }); return; } try { const scopeLogsMap = /* @__PURE__ */ new Map(); for (const log of logs) { const scopeName = log.instrumentationScope.name || "unknown"; if (!scopeLogsMap.has(scopeName)) { scopeLogsMap.set(scopeName, { scope: { name: scopeName, version: log.instrumentationScope.version || "" }, logRecords: [] }); } const attributes = []; for (const [k, v] of Object.entries(log.attributes)) { if (typeof v === "string") attributes.push({ key: k, value: { stringValue: v } }); else if (typeof v === "number") attributes.push({ key: k, value: { intValue: v } }); else if (typeof v === "boolean") attributes.push({ key: k, value: { boolValue: v } }); } let bodyValue; if (typeof log.body === "string") bodyValue = { stringValue: log.body }; else if (typeof log.body === "number") bodyValue = { intValue: log.body }; else if (typeof log.body === "boolean") bodyValue = { boolValue: log.body }; else bodyValue = { stringValue: JSON.stringify(log.body) }; scopeLogsMap.get(scopeName).logRecords.push({ timeUnixNano: (hrTimeToMilliseconds(log.hrTime) * 1e6).toString(), severityNumber: log.severityNumber, severityText: log.severityText, body: bodyValue, attributes, traceId: log.spanContext?.traceId, spanId: log.spanContext?.spanId }); } const payload = { resourceLogs: [ { resource: { attributes: [], droppedAttributesCount: 0 }, scopeLogs: Array.from(scopeLogsMap.values()) } ] }; await context.with( suppressTracing(context.active()), () => fetch(`${telemetryServerUrl}/api/otlp`, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" }, body: JSON.stringify(payload) }) ); if (done) done({ code: ExportResultCode.SUCCESS }); } catch (e) { logger.error("Failed to export logs", e); if (done) done({ code: ExportResultCode.FAILED }); } } } export { LogServerExporter, TraceServerExporter, setTelemetryServerUrl, telemetryServerUrl }; //# sourceMappingURL=exporter.mjs.map