Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Attributes.ts

48 lines1.4 KB

1import { Cause, Exit } from "effect";
2​
3export type MetricAttributeValue = string;
4export type MetricAttributes = Readonly<Record<string, MetricAttributeValue>>;
5export type ObservabilityOutcome = "success" | "failure" | "interrupt";
6​
7export function compactMetricAttributes(
8 attributes: Readonly<Record<string, unknown>>,
9): MetricAttributes {
10 return Object.fromEntries(
11 Object.entries(attributes).flatMap(([key, value]) => {
12 if (value === undefined || value === null) {
13 return [];
14 }
15 if (typeof value === "string") {
16 return [[key, value]];
17 }
18 if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
19 return [[key, String(value)]];
20 }
21 return [];
22 }),
23 );
25​
26export function outcomeFromExit(exit: Exit.Exit<unknown, unknown>): ObservabilityOutcome {
27 if (Exit.isSuccess(exit)) {
28 return "success";
29 }
30 return Cause.hasInterruptsOnly(exit.cause) ? "interrupt" : "failure";
32​
33export function normalizeModelMetricLabel(model: string | null | undefined): string | undefined {
34 const normalized = model?.trim().toLowerCase();
35 if (!normalized) {
36 return undefined;
37 }
38 if (normalized.includes("gpt")) {
39 return "gpt";
40 }
41 if (normalized.includes("claude")) {
42 return "claude";
43 }
44 if (normalized.includes("gemini")) {
45 return "gemini";
46 }
47 return "other";