UNPKG

@genkit-ai/google-cloud

Version:

Genkit AI framework plugin for Google Cloud Platform including Firestore trace/state store and deployment helpers for Cloud Functions for Firebase.

143 lines 5.03 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var action_exports = {}; __export(action_exports, { actionTelemetry: () => actionTelemetry }); module.exports = __toCommonJS(action_exports); var import_api = require("@opentelemetry/api"); var import_core = require("@opentelemetry/core"); var import_genkit = require("genkit"); var import_logging = require("genkit/logging"); var import_tracing = require("genkit/tracing"); var import_metrics = require("../metrics.js"); var import_utils = require("../utils.js"); class ActionTelemetry { /** * Wraps the declared metrics in a Genkit-specific, internal namespace. */ _N = import_metrics.internalMetricNamespaceWrap.bind(null, "action"); actionCounter = new import_metrics.MetricCounter(this._N("requests"), { description: "Counts calls to genkit actions.", valueType: import_api.ValueType.INT }); actionLatencies = new import_metrics.MetricHistogram(this._N("latency"), { description: "Latencies when calling Genkit actions.", valueType: import_api.ValueType.DOUBLE, unit: "ms" }); tick(span, paths, logInputAndOutput, projectId) { const attributes = span.attributes; const actionName = attributes["genkit:name"] || "<unknown>"; const subtype = attributes["genkit:metadata:subtype"]; const path = attributes["genkit:path"] || "<unknown>"; let featureName = (0, import_utils.extractOuterFeatureNameFromPath)(path); if (!featureName || featureName === "<unknown>") { featureName = actionName; } const state = attributes["genkit:state"] || "success"; const latencyMs = (0, import_core.hrTimeToMilliseconds)( (0, import_core.hrTimeDuration)(span.startTime, span.endTime) ); const errorName = (0, import_utils.extractErrorName)(span.events); if (state === "success") { this.writeSuccess(actionName, featureName, path, latencyMs); } else if (state === "error") { this.writeFailure(actionName, featureName, path, latencyMs, errorName); } else { import_logging.logger.warn(`Unknown action state; ${state}`); } if (subtype === "tool" && logInputAndOutput) { const input = (0, import_utils.truncate)(attributes["genkit:input"]); const output = (0, import_utils.truncate)(attributes["genkit:output"]); const sessionId = attributes["genkit:sessionId"]; const threadName = attributes["genkit:threadName"]; if (input) { this.writeLog( span, "Input", featureName, path, input, projectId, sessionId, threadName ); } if (output) { this.writeLog( span, "Output", featureName, path, output, projectId, sessionId, threadName ); } } } writeSuccess(actionName, featureName, path, latencyMs) { const dimensions = { name: actionName, featureName, path, status: "success", source: "ts", sourceVersion: import_genkit.GENKIT_VERSION }; this.actionCounter.add(1, dimensions); this.actionLatencies.record(latencyMs, dimensions); } writeFailure(actionName, featureName, path, latencyMs, errorName) { const dimensions = { name: actionName, featureName, path, source: "ts", sourceVersion: import_genkit.GENKIT_VERSION, status: "failure", error: errorName }; this.actionCounter.add(1, dimensions); this.actionLatencies.record(latencyMs, dimensions); } writeLog(span, tag, featureName, qualifiedPath, content, projectId, sessionId, threadName) { const path = (0, import_utils.truncatePath)((0, import_tracing.toDisplayPath)(qualifiedPath)); const sharedMetadata = { ...(0, import_utils.createCommonLogAttributes)(span, projectId), path, qualifiedPath, featureName, sessionId, threadName }; import_logging.logger.logStructured(`${tag}[${path}, ${featureName}]`, { ...sharedMetadata, content }); } } const actionTelemetry = new ActionTelemetry(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { actionTelemetry }); //# sourceMappingURL=action.js.map