@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.
129 lines • 3.89 kB
JavaScript
import { ValueType } from "@opentelemetry/api";
import { hrTimeDuration, hrTimeToMilliseconds } from "@opentelemetry/core";
import { GENKIT_VERSION } from "genkit";
import { logger } from "genkit/logging";
import { toDisplayPath } from "genkit/tracing";
import {
MetricCounter,
MetricHistogram,
internalMetricNamespaceWrap
} from "../metrics.js";
import {
createCommonLogAttributes,
extractErrorName,
extractOuterFeatureNameFromPath,
truncate,
truncatePath
} from "../utils.js";
class ActionTelemetry {
/**
* Wraps the declared metrics in a Genkit-specific, internal namespace.
*/
_N = internalMetricNamespaceWrap.bind(null, "action");
actionCounter = new MetricCounter(this._N("requests"), {
description: "Counts calls to genkit actions.",
valueType: ValueType.INT
});
actionLatencies = new MetricHistogram(this._N("latency"), {
description: "Latencies when calling Genkit actions.",
valueType: 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 = extractOuterFeatureNameFromPath(path);
if (!featureName || featureName === "<unknown>") {
featureName = actionName;
}
const state = attributes["genkit:state"] || "success";
const latencyMs = hrTimeToMilliseconds(
hrTimeDuration(span.startTime, span.endTime)
);
const errorName = extractErrorName(span.events);
if (state === "success") {
this.writeSuccess(actionName, featureName, path, latencyMs);
} else if (state === "error") {
this.writeFailure(actionName, featureName, path, latencyMs, errorName);
} else {
logger.warn(`Unknown action state; ${state}`);
}
if (subtype === "tool" && logInputAndOutput) {
const input = truncate(attributes["genkit:input"]);
const output = 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: 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: 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 = truncatePath(toDisplayPath(qualifiedPath));
const sharedMetadata = {
...createCommonLogAttributes(span, projectId),
path,
qualifiedPath,
featureName,
sessionId,
threadName
};
logger.logStructured(`${tag}[${path}, ${featureName}]`, {
...sharedMetadata,
content
});
}
}
const actionTelemetry = new ActionTelemetry();
export {
actionTelemetry
};
//# sourceMappingURL=action.mjs.map