@adonis-agora/telescope 0.26.2 → 0.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/dist/providers/telescope_provider.d.ts +10 -0
- package/dist/providers/telescope_provider.d.ts.map +1 -1
- package/dist/providers/telescope_provider.js +37 -1
- package/dist/providers/telescope_provider.js.map +1 -1
- package/dist/providers/telescope_ui_provider.d.ts.map +1 -1
- package/dist/providers/telescope_ui_provider.js +29 -0
- package/dist/providers/telescope_ui_provider.js.map +1 -1
- package/dist/src/ai/index.d.ts +1 -1
- package/dist/src/ai/index.js +1 -1
- package/dist/src/ai_metrics/agent_events.d.ts +106 -0
- package/dist/src/ai_metrics/agent_events.d.ts.map +1 -0
- package/dist/src/ai_metrics/agent_events.js +170 -0
- package/dist/src/ai_metrics/agent_events.js.map +1 -0
- package/dist/src/ai_metrics/agent_metrics.d.ts +103 -0
- package/dist/src/ai_metrics/agent_metrics.d.ts.map +1 -0
- package/dist/src/ai_metrics/agent_metrics.js +255 -0
- package/dist/src/ai_metrics/agent_metrics.js.map +1 -0
- package/dist/src/ai_metrics/ai_usage.d.ts +123 -0
- package/dist/src/ai_metrics/ai_usage.d.ts.map +1 -0
- package/dist/src/ai_metrics/ai_usage.js +207 -0
- package/dist/src/ai_metrics/ai_usage.js.map +1 -0
- package/dist/src/ai_metrics/index.d.ts +11 -0
- package/dist/src/ai_metrics/index.d.ts.map +1 -0
- package/dist/src/ai_metrics/index.js +11 -0
- package/dist/src/ai_metrics/index.js.map +1 -0
- package/dist/src/ai_metrics/otel_metrics_exporter.d.ts +60 -0
- package/dist/src/ai_metrics/otel_metrics_exporter.d.ts.map +1 -0
- package/dist/src/ai_metrics/otel_metrics_exporter.js +66 -0
- package/dist/src/ai_metrics/otel_metrics_exporter.js.map +1 -0
- package/dist/src/ai_metrics/subscribe.d.ts +17 -0
- package/dist/src/ai_metrics/subscribe.d.ts.map +1 -0
- package/dist/src/ai_metrics/subscribe.js +38 -0
- package/dist/src/ai_metrics/subscribe.js.map +1 -0
- package/dist/src/alerts/index.d.ts +1 -1
- package/dist/src/alerts/index.js +1 -1
- package/dist/src/define_config.d.ts +72 -0
- package/dist/src/define_config.d.ts.map +1 -1
- package/dist/src/define_config.js +24 -4
- package/dist/src/define_config.js.map +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/index.d.ts +1 -1
- package/dist/src/mcp/index.js +1 -1
- package/dist/src/metrics/metrics_service.d.ts +22 -0
- package/dist/src/metrics/metrics_service.d.ts.map +1 -1
- package/dist/src/metrics/metrics_service.js +49 -0
- package/dist/src/metrics/metrics_service.js.map +1 -1
- package/dist/src/profiling/index.d.ts +1 -1
- package/dist/src/profiling/index.js +1 -1
- package/dist/src/pruner.d.ts +5 -1
- package/dist/src/pruner.d.ts.map +1 -1
- package/dist/src/pruner.js +17 -3
- package/dist/src/pruner.js.map +1 -1
- package/dist/src/registry.d.ts +9 -0
- package/dist/src/registry.d.ts.map +1 -1
- package/dist/src/registry.js +6 -0
- package/dist/src/registry.js.map +1 -1
- package/dist/src/stores/factory.d.ts.map +1 -1
- package/dist/src/stores/factory.js +12 -5
- package/dist/src/stores/factory.js.map +1 -1
- package/dist/src/stores/parquet_location.d.ts +1 -0
- package/dist/src/stores/parquet_location.d.ts.map +1 -1
- package/dist/src/stores/parquet_location.js +30 -5
- package/dist/src/stores/parquet_location.js.map +1 -1
- package/dist/src/ui/api.d.ts +6 -0
- package/dist/src/ui/api.d.ts.map +1 -1
- package/dist/src/ui/api.js +23 -0
- package/dist/src/ui/api.js.map +1 -1
- package/dist/src/ui/index.d.ts +1 -1
- package/dist/src/ui/index.js +1 -1
- package/dist/src/watchers/index.d.ts +1 -1
- package/dist/src/watchers/index.js +1 -1
- package/grafana/agora-ai-dashboard.json +813 -0
- package/package.json +17 -5
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { AgentObservation } from './agent_events.js';
|
|
2
|
+
/**
|
|
3
|
+
* The AI-usage and agent-run metrics, as ONE table both exporters read: the dependency-free
|
|
4
|
+
* Prometheus registry below and the OTLP exporter in `otel_metrics_exporter.ts`. Names are
|
|
5
|
+
* stable API — dashboards are built on them.
|
|
6
|
+
*
|
|
7
|
+
* Units live in the NAME (`_usd`, `_ms`, `_seconds`), and the OTLP instruments are created with an
|
|
8
|
+
* EMPTY unit on purpose: Prometheus' OTLP translation appends a unit suffix (`ms` → `_milliseconds`),
|
|
9
|
+
* which would make the same metric read `agora_agent_run_duration_ms_milliseconds` through a
|
|
10
|
+
* Collector and `agora_agent_run_duration_ms` from the scrape. With no unit both paths expose the
|
|
11
|
+
* same series, so one Grafana dashboard serves both.
|
|
12
|
+
*
|
|
13
|
+
* Cardinality: attributes are features, models, providers, agent and tool names only. `actorRef`,
|
|
14
|
+
* `runId` and `threadId` NEVER become attributes — they grow with users and runs.
|
|
15
|
+
*/
|
|
16
|
+
export declare const AGENT_METRICS: {
|
|
17
|
+
readonly costUsd: {
|
|
18
|
+
readonly name: 'agora_ai_cost_usd';
|
|
19
|
+
readonly type: 'counter';
|
|
20
|
+
readonly help: 'AI spend in US dollars, from usage.recorded rows that carried a cost.';
|
|
21
|
+
};
|
|
22
|
+
readonly tokens: {
|
|
23
|
+
readonly name: 'agora_ai_tokens';
|
|
24
|
+
readonly type: 'counter';
|
|
25
|
+
readonly help: 'AI tokens, by direction (in, out, cache_read, cache_write).';
|
|
26
|
+
};
|
|
27
|
+
readonly audioSeconds: {
|
|
28
|
+
readonly name: 'agora_ai_audio_seconds';
|
|
29
|
+
readonly type: 'counter';
|
|
30
|
+
readonly help: 'Seconds of audio processed by AI calls.';
|
|
31
|
+
};
|
|
32
|
+
readonly requests: {
|
|
33
|
+
readonly name: 'agora_ai_requests';
|
|
34
|
+
readonly type: 'counter';
|
|
35
|
+
readonly help: 'AI calls recorded in the usage ledger; cost_source="none" are the unpriced ones.';
|
|
36
|
+
};
|
|
37
|
+
readonly runDurationMs: {
|
|
38
|
+
readonly name: 'agora_agent_run_duration_ms';
|
|
39
|
+
readonly type: 'histogram';
|
|
40
|
+
readonly help: 'Agent run wall-clock duration in milliseconds, turn start to end.';
|
|
41
|
+
};
|
|
42
|
+
readonly runQueueMs: {
|
|
43
|
+
readonly name: 'agora_agent_run_queue_ms';
|
|
44
|
+
readonly type: 'histogram';
|
|
45
|
+
readonly help: 'Agent run time between enqueue and its body starting, in milliseconds.';
|
|
46
|
+
};
|
|
47
|
+
readonly runModelMs: {
|
|
48
|
+
readonly name: 'agora_agent_run_model_ms';
|
|
49
|
+
readonly type: 'histogram';
|
|
50
|
+
readonly help: 'Agent run time spent in model calls, in milliseconds.';
|
|
51
|
+
};
|
|
52
|
+
readonly runToolsMs: {
|
|
53
|
+
readonly name: 'agora_agent_run_tools_ms';
|
|
54
|
+
readonly type: 'histogram';
|
|
55
|
+
readonly help: 'Agent run time spent running tools (parallel calls once), in milliseconds.';
|
|
56
|
+
};
|
|
57
|
+
readonly toolDurationMs: {
|
|
58
|
+
readonly name: 'agora_agent_tool_duration_ms';
|
|
59
|
+
readonly type: 'histogram';
|
|
60
|
+
readonly help: 'One tool call duration, in milliseconds.';
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
export type AgentMetricKey = keyof typeof AGENT_METRICS;
|
|
64
|
+
/** Histogram upper bounds (ms), shared by the scrape and the OTLP instruments. */
|
|
65
|
+
export declare const AGENT_METRIC_BUCKETS_MS: readonly number[];
|
|
66
|
+
/** Attribute values are strings, except `platform`, which OTLP carries as a boolean. */
|
|
67
|
+
export type MetricAttributes = Record<string, string | boolean>;
|
|
68
|
+
/** One value an observation contributes to one metric. */
|
|
69
|
+
export interface MetricPoint {
|
|
70
|
+
metric: AgentMetricKey;
|
|
71
|
+
value: number;
|
|
72
|
+
attributes: MetricAttributes;
|
|
73
|
+
}
|
|
74
|
+
/** Every metric point an observation contributes. Pure. */
|
|
75
|
+
export declare function agentMetricPoints(observation: AgentObservation): MetricPoint[];
|
|
76
|
+
export interface AgentMetricsRegistryOptions {
|
|
77
|
+
/**
|
|
78
|
+
* Max distinct label sets kept per metric. New combinations past it are dropped (existing ones
|
|
79
|
+
* keep counting); every observation dropped that way is tallied in
|
|
80
|
+
* `agora_ai_metrics_dropped_series_total`. Default 2000.
|
|
81
|
+
*/
|
|
82
|
+
maxSeriesPerMetric?: number;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Dependency-free, in-process aggregation of {@link AGENT_METRICS} for the Prometheus scrape
|
|
86
|
+
* (`GET <telescope path>/metrics`). Cumulative since process start, as Prometheus expects.
|
|
87
|
+
*/
|
|
88
|
+
export declare class AgentMetricsRegistry {
|
|
89
|
+
private readonly counters;
|
|
90
|
+
private readonly histograms;
|
|
91
|
+
private readonly dropped;
|
|
92
|
+
private readonly maxSeries;
|
|
93
|
+
constructor(options?: AgentMetricsRegistryOptions);
|
|
94
|
+
observe(observation: AgentObservation): void;
|
|
95
|
+
add(point: MetricPoint): void;
|
|
96
|
+
private drop;
|
|
97
|
+
/**
|
|
98
|
+
* Prometheus text exposition (format 0.0.4). Counters are exposed with the `_total` suffix —
|
|
99
|
+
* the same name Prometheus gives an OTLP monotonic sum — so the shipped dashboard reads either.
|
|
100
|
+
*/
|
|
101
|
+
prometheus(): string;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=agent_metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent_metrics.d.ts","sourceRoot":"","sources":["../../../src/ai_metrics/agent_metrics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAoC,MAAM,mBAAmB,CAAC;AAE5F;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa;aACxB,OAAO;iBACL,IAAI,EAAE,mBAAmB;iBACzB,IAAI,EAAE,SAAS;iBACf,IAAI,EAAE,uEAAu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,MAAM,WAAW,2BAA2B;IAC1C;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED;;;GAGG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyD;IAClF,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2D;IACtF,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA6B;IACrD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IAEnC,YAAY,OAAO,GAAE,2BAAgC,EAEpD;IAED,OAAO,CAAC,WAAW,EAAE,gBAAgB,GAAG,IAAI,CAE3C;IAED,GAAG,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAsC5B;IAED,OAAO,CAAC,IAAI;IAIZ;;;OAGG;IACH,UAAU,IAAI,MAAM,CAyCnB;CACF"}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The AI-usage and agent-run metrics, as ONE table both exporters read: the dependency-free
|
|
3
|
+
* Prometheus registry below and the OTLP exporter in `otel_metrics_exporter.ts`. Names are
|
|
4
|
+
* stable API — dashboards are built on them.
|
|
5
|
+
*
|
|
6
|
+
* Units live in the NAME (`_usd`, `_ms`, `_seconds`), and the OTLP instruments are created with an
|
|
7
|
+
* EMPTY unit on purpose: Prometheus' OTLP translation appends a unit suffix (`ms` → `_milliseconds`),
|
|
8
|
+
* which would make the same metric read `agora_agent_run_duration_ms_milliseconds` through a
|
|
9
|
+
* Collector and `agora_agent_run_duration_ms` from the scrape. With no unit both paths expose the
|
|
10
|
+
* same series, so one Grafana dashboard serves both.
|
|
11
|
+
*
|
|
12
|
+
* Cardinality: attributes are features, models, providers, agent and tool names only. `actorRef`,
|
|
13
|
+
* `runId` and `threadId` NEVER become attributes — they grow with users and runs.
|
|
14
|
+
*/
|
|
15
|
+
export const AGENT_METRICS = {
|
|
16
|
+
costUsd: {
|
|
17
|
+
name: 'agora_ai_cost_usd',
|
|
18
|
+
type: 'counter',
|
|
19
|
+
help: 'AI spend in US dollars, from usage.recorded rows that carried a cost.',
|
|
20
|
+
},
|
|
21
|
+
tokens: {
|
|
22
|
+
name: 'agora_ai_tokens',
|
|
23
|
+
type: 'counter',
|
|
24
|
+
help: 'AI tokens, by direction (in, out, cache_read, cache_write).',
|
|
25
|
+
},
|
|
26
|
+
audioSeconds: {
|
|
27
|
+
name: 'agora_ai_audio_seconds',
|
|
28
|
+
type: 'counter',
|
|
29
|
+
help: 'Seconds of audio processed by AI calls.',
|
|
30
|
+
},
|
|
31
|
+
requests: {
|
|
32
|
+
name: 'agora_ai_requests',
|
|
33
|
+
type: 'counter',
|
|
34
|
+
help: 'AI calls recorded in the usage ledger; cost_source="none" are the unpriced ones.',
|
|
35
|
+
},
|
|
36
|
+
runDurationMs: {
|
|
37
|
+
name: 'agora_agent_run_duration_ms',
|
|
38
|
+
type: 'histogram',
|
|
39
|
+
help: 'Agent run wall-clock duration in milliseconds, turn start to end.',
|
|
40
|
+
},
|
|
41
|
+
runQueueMs: {
|
|
42
|
+
name: 'agora_agent_run_queue_ms',
|
|
43
|
+
type: 'histogram',
|
|
44
|
+
help: 'Agent run time between enqueue and its body starting, in milliseconds.',
|
|
45
|
+
},
|
|
46
|
+
runModelMs: {
|
|
47
|
+
name: 'agora_agent_run_model_ms',
|
|
48
|
+
type: 'histogram',
|
|
49
|
+
help: 'Agent run time spent in model calls, in milliseconds.',
|
|
50
|
+
},
|
|
51
|
+
runToolsMs: {
|
|
52
|
+
name: 'agora_agent_run_tools_ms',
|
|
53
|
+
type: 'histogram',
|
|
54
|
+
help: 'Agent run time spent running tools (parallel calls once), in milliseconds.',
|
|
55
|
+
},
|
|
56
|
+
toolDurationMs: {
|
|
57
|
+
name: 'agora_agent_tool_duration_ms',
|
|
58
|
+
type: 'histogram',
|
|
59
|
+
help: 'One tool call duration, in milliseconds.',
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
/** Histogram upper bounds (ms), shared by the scrape and the OTLP instruments. */
|
|
63
|
+
export const AGENT_METRIC_BUCKETS_MS = [
|
|
64
|
+
10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 20000, 30000, 60000, 120000, 300000, 600000,
|
|
65
|
+
];
|
|
66
|
+
function usagePoints(u) {
|
|
67
|
+
const common = {
|
|
68
|
+
feature: u.feature,
|
|
69
|
+
model: u.model,
|
|
70
|
+
provider: u.provider,
|
|
71
|
+
cost_source: u.costSource,
|
|
72
|
+
platform: u.platform,
|
|
73
|
+
};
|
|
74
|
+
const points = [{ metric: 'requests', value: u.requests, attributes: common }];
|
|
75
|
+
if (u.costUsd !== undefined)
|
|
76
|
+
points.push({ metric: 'costUsd', value: u.costUsd, attributes: common });
|
|
77
|
+
if (u.audioSeconds > 0) {
|
|
78
|
+
points.push({ metric: 'audioSeconds', value: u.audioSeconds, attributes: common });
|
|
79
|
+
}
|
|
80
|
+
const tokens = [
|
|
81
|
+
['in', u.inputTokens],
|
|
82
|
+
['out', u.outputTokens],
|
|
83
|
+
['cache_read', u.cacheReadTokens],
|
|
84
|
+
['cache_write', u.cacheWriteTokens],
|
|
85
|
+
];
|
|
86
|
+
for (const [direction, value] of tokens) {
|
|
87
|
+
if (value > 0) {
|
|
88
|
+
points.push({
|
|
89
|
+
metric: 'tokens',
|
|
90
|
+
value,
|
|
91
|
+
attributes: { direction, feature: u.feature, model: u.model },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return points;
|
|
96
|
+
}
|
|
97
|
+
function runPoints(r) {
|
|
98
|
+
const attributes = { agent: r.agent, status: r.status };
|
|
99
|
+
const points = [];
|
|
100
|
+
const timed = [
|
|
101
|
+
['runDurationMs', r.durationMs],
|
|
102
|
+
['runQueueMs', r.queueMs],
|
|
103
|
+
['runModelMs', r.modelMs],
|
|
104
|
+
['runToolsMs', r.toolsMs],
|
|
105
|
+
];
|
|
106
|
+
for (const [metric, value] of timed) {
|
|
107
|
+
if (value !== undefined && value >= 0)
|
|
108
|
+
points.push({ metric, value, attributes });
|
|
109
|
+
}
|
|
110
|
+
for (const step of r.stepTimings) {
|
|
111
|
+
for (const tool of step.tools) {
|
|
112
|
+
if (tool.ms >= 0) {
|
|
113
|
+
points.push({
|
|
114
|
+
metric: 'toolDurationMs',
|
|
115
|
+
value: tool.ms,
|
|
116
|
+
attributes: { tool: tool.toolName },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return points;
|
|
122
|
+
}
|
|
123
|
+
/** Every metric point an observation contributes. Pure. */
|
|
124
|
+
export function agentMetricPoints(observation) {
|
|
125
|
+
return observation.kind === 'usage' ? usagePoints(observation) : runPoints(observation);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Dependency-free, in-process aggregation of {@link AGENT_METRICS} for the Prometheus scrape
|
|
129
|
+
* (`GET <telescope path>/metrics`). Cumulative since process start, as Prometheus expects.
|
|
130
|
+
*/
|
|
131
|
+
export class AgentMetricsRegistry {
|
|
132
|
+
counters = new Map();
|
|
133
|
+
histograms = new Map();
|
|
134
|
+
dropped = new Map();
|
|
135
|
+
maxSeries;
|
|
136
|
+
constructor(options = {}) {
|
|
137
|
+
this.maxSeries = options.maxSeriesPerMetric ?? 2000;
|
|
138
|
+
}
|
|
139
|
+
observe(observation) {
|
|
140
|
+
for (const point of agentMetricPoints(observation))
|
|
141
|
+
this.add(point);
|
|
142
|
+
}
|
|
143
|
+
add(point) {
|
|
144
|
+
const def = AGENT_METRICS[point.metric];
|
|
145
|
+
const key = labelKey(point.attributes);
|
|
146
|
+
if (def.type === 'counter') {
|
|
147
|
+
const series = getOrCreate(this.counters, point.metric);
|
|
148
|
+
let cell = series.get(key);
|
|
149
|
+
if (cell === undefined) {
|
|
150
|
+
if (series.size >= this.maxSeries) {
|
|
151
|
+
// The exposed name, so the drop joins with the series it describes.
|
|
152
|
+
this.drop(`${def.name}_total`);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
cell = { labels: point.attributes, value: 0 };
|
|
156
|
+
series.set(key, cell);
|
|
157
|
+
}
|
|
158
|
+
cell.value += point.value;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const series = getOrCreate(this.histograms, point.metric);
|
|
162
|
+
let cell = series.get(key);
|
|
163
|
+
if (cell === undefined) {
|
|
164
|
+
if (series.size >= this.maxSeries) {
|
|
165
|
+
this.drop(def.name);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
cell = {
|
|
169
|
+
labels: point.attributes,
|
|
170
|
+
sum: 0,
|
|
171
|
+
count: 0,
|
|
172
|
+
buckets: new Array(AGENT_METRIC_BUCKETS_MS.length).fill(0),
|
|
173
|
+
};
|
|
174
|
+
series.set(key, cell);
|
|
175
|
+
}
|
|
176
|
+
cell.sum += point.value;
|
|
177
|
+
cell.count += 1;
|
|
178
|
+
AGENT_METRIC_BUCKETS_MS.forEach((bound, i) => {
|
|
179
|
+
if (point.value <= bound)
|
|
180
|
+
cell.buckets[i] = (cell.buckets[i] ?? 0) + 1;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
drop(metric) {
|
|
184
|
+
this.dropped.set(metric, (this.dropped.get(metric) ?? 0) + 1);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Prometheus text exposition (format 0.0.4). Counters are exposed with the `_total` suffix —
|
|
188
|
+
* the same name Prometheus gives an OTLP monotonic sum — so the shipped dashboard reads either.
|
|
189
|
+
*/
|
|
190
|
+
prometheus() {
|
|
191
|
+
const lines = [];
|
|
192
|
+
for (const key of Object.keys(AGENT_METRICS)) {
|
|
193
|
+
const def = AGENT_METRICS[key];
|
|
194
|
+
if (def.type === 'counter') {
|
|
195
|
+
const series = this.counters.get(key);
|
|
196
|
+
if (series === undefined || series.size === 0)
|
|
197
|
+
continue;
|
|
198
|
+
const name = `${def.name}_total`;
|
|
199
|
+
lines.push(`# HELP ${name} ${def.help}`, `# TYPE ${name} counter`);
|
|
200
|
+
for (const cell of series.values()) {
|
|
201
|
+
lines.push(`${name}${formatLabels(cell.labels)} ${formatNumber(cell.value)}`);
|
|
202
|
+
}
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const series = this.histograms.get(key);
|
|
206
|
+
if (series === undefined || series.size === 0)
|
|
207
|
+
continue;
|
|
208
|
+
lines.push(`# HELP ${def.name} ${def.help}`, `# TYPE ${def.name} histogram`);
|
|
209
|
+
for (const cell of series.values()) {
|
|
210
|
+
AGENT_METRIC_BUCKETS_MS.forEach((bound, i) => {
|
|
211
|
+
lines.push(`${def.name}_bucket${formatLabels({ ...cell.labels, le: String(bound) })} ${cell.buckets[i] ?? 0}`);
|
|
212
|
+
});
|
|
213
|
+
lines.push(`${def.name}_bucket${formatLabels({ ...cell.labels, le: '+Inf' })} ${cell.count}`);
|
|
214
|
+
lines.push(`${def.name}_sum${formatLabels(cell.labels)} ${formatNumber(cell.sum)}`);
|
|
215
|
+
lines.push(`${def.name}_count${formatLabels(cell.labels)} ${cell.count}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (this.dropped.size > 0) {
|
|
219
|
+
const name = 'agora_ai_metrics_dropped_series_total';
|
|
220
|
+
lines.push(`# HELP ${name} Observations dropped because their label set was new and the metric was at its series cap.`, `# TYPE ${name} counter`);
|
|
221
|
+
for (const [metric, count] of this.dropped) {
|
|
222
|
+
lines.push(`${name}${formatLabels({ metric })} ${count}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return lines.length === 0 ? '' : `${lines.join('\n')}\n`;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function getOrCreate(map, key) {
|
|
229
|
+
let inner = map.get(key);
|
|
230
|
+
if (inner === undefined) {
|
|
231
|
+
inner = new Map();
|
|
232
|
+
map.set(key, inner);
|
|
233
|
+
}
|
|
234
|
+
return inner;
|
|
235
|
+
}
|
|
236
|
+
function labelKey(labels) {
|
|
237
|
+
return Object.keys(labels)
|
|
238
|
+
.sort()
|
|
239
|
+
.map((k) => `${k}=${String(labels[k])}`)
|
|
240
|
+
.join('\u0000');
|
|
241
|
+
}
|
|
242
|
+
function escapeLabel(value) {
|
|
243
|
+
return value.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"');
|
|
244
|
+
}
|
|
245
|
+
function formatLabels(labels) {
|
|
246
|
+
const keys = Object.keys(labels);
|
|
247
|
+
if (keys.length === 0)
|
|
248
|
+
return '';
|
|
249
|
+
return `{${keys.map((k) => `${k}="${escapeLabel(String(labels[k]))}"`).join(',')}}`;
|
|
250
|
+
}
|
|
251
|
+
function formatNumber(value) {
|
|
252
|
+
// Avoid float noise like 0.30000000000000004 in the exposition.
|
|
253
|
+
return Number.isInteger(value) ? String(value) : String(Number(value.toPrecision(12)));
|
|
254
|
+
}
|
|
255
|
+
//# sourceMappingURL=agent_metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent_metrics.js","sourceRoot":"","sources":["../../../src/ai_metrics/agent_metrics.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,OAAO,EAAE;QACP,IAAI,EAAE,mBAAmB;QACzB,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,uEAAuE;KAC9E;IACD,MAAM,EAAE;QACN,IAAI,EAAE,iBAAiB;QACvB,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,6DAA6D;KACpE;IACD,YAAY,EAAE;QACZ,IAAI,EAAE,wBAAwB;QAC9B,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,yCAAyC;KAChD;IACD,QAAQ,EAAE;QACR,IAAI,EAAE,mBAAmB;QACzB,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,kFAAkF;KACzF;IACD,aAAa,EAAE;QACb,IAAI,EAAE,6BAA6B;QACnC,IAAI,EAAE,WAAW;QACjB,IAAI,EAAE,mEAAmE;KAC1E;IACD,UAAU,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,QAAQ,CAAC,MAAwB;IACxC,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;SACvB,IAAI,EAAE;SACN,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SACvC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACpB,CAAC;AAED,SAAS,WAAW,CAAC,KAAa;IAChC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACjF,CAAC;AAED,SAAS,YAAY,CAAC,MAAwB;IAC5C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACjC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACjC,OAAO,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;AACtF,CAAC;AAED,SAAS,YAAY,CAAC,KAAa;IACjC,gEAAgE;IAChE,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACzF,CAAC"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { Entry } from '../entry.js';
|
|
2
|
+
import { type RunStatus, type StepTiming } from './agent_events.js';
|
|
3
|
+
/** One row of a spend breakdown (by feature, model or actor). */
|
|
4
|
+
export interface AiSpendRow {
|
|
5
|
+
key: string;
|
|
6
|
+
costUsd: number;
|
|
7
|
+
calls: number;
|
|
8
|
+
/** Calls in this group that had no cost. */
|
|
9
|
+
unpricedCalls: number;
|
|
10
|
+
inputTokens: number;
|
|
11
|
+
outputTokens: number;
|
|
12
|
+
}
|
|
13
|
+
/** One time bucket of spend. */
|
|
14
|
+
export interface AiSpendBucket {
|
|
15
|
+
/** Bucket start, ISO. */
|
|
16
|
+
start: string;
|
|
17
|
+
costUsd: number;
|
|
18
|
+
platformUsd: number;
|
|
19
|
+
estimatedUsd: number;
|
|
20
|
+
calls: number;
|
|
21
|
+
}
|
|
22
|
+
/** The spend half of the AI screen. */
|
|
23
|
+
export interface AiSpend {
|
|
24
|
+
totalUsd: number;
|
|
25
|
+
/** Paid by the platform (`actorRef === '@platform'`). */
|
|
26
|
+
platformUsd: number;
|
|
27
|
+
/** Priced by an estimate rather than the provider's own figure. */
|
|
28
|
+
estimatedUsd: number;
|
|
29
|
+
calls: number;
|
|
30
|
+
unpricedCalls: number;
|
|
31
|
+
inputTokens: number;
|
|
32
|
+
outputTokens: number;
|
|
33
|
+
cacheReadTokens: number;
|
|
34
|
+
cacheWriteTokens: number;
|
|
35
|
+
audioSeconds: number;
|
|
36
|
+
byFeature: AiSpendRow[];
|
|
37
|
+
byModel: AiSpendRow[];
|
|
38
|
+
/** Top N payers. `@platform` is the platform itself. */
|
|
39
|
+
byActor: AiSpendRow[];
|
|
40
|
+
buckets: AiSpendBucket[];
|
|
41
|
+
}
|
|
42
|
+
/** One agent run, as the runs table shows it. */
|
|
43
|
+
export interface AiRunRow {
|
|
44
|
+
/** The Telescope entry the run's terminal event was recorded as. */
|
|
45
|
+
entryId: string;
|
|
46
|
+
/** The entry's trace — the link to the existing trace/waterfall view; `null` when none. */
|
|
47
|
+
traceId: string | null;
|
|
48
|
+
at: string;
|
|
49
|
+
status: RunStatus;
|
|
50
|
+
agent: string;
|
|
51
|
+
runId: string | null;
|
|
52
|
+
threadId: string | null;
|
|
53
|
+
modelId: string | null;
|
|
54
|
+
durationMs: number | null;
|
|
55
|
+
ttftMs: number | null;
|
|
56
|
+
queueMs: number | null;
|
|
57
|
+
modelMs: number | null;
|
|
58
|
+
toolsMs: number | null;
|
|
59
|
+
steps: number | null;
|
|
60
|
+
modelCalls: number | null;
|
|
61
|
+
toolCalls: number | null;
|
|
62
|
+
inputTokens: number | null;
|
|
63
|
+
outputTokens: number | null;
|
|
64
|
+
costUsd: number | null;
|
|
65
|
+
/** Failure slug on a failed run. */
|
|
66
|
+
code: string | null;
|
|
67
|
+
stepTimings: StepTiming[];
|
|
68
|
+
}
|
|
69
|
+
/** Calls with no cost, grouped. */
|
|
70
|
+
export interface AiUnpricedRow {
|
|
71
|
+
feature: string;
|
|
72
|
+
model: string;
|
|
73
|
+
provider: string;
|
|
74
|
+
calls: number;
|
|
75
|
+
inputTokens: number;
|
|
76
|
+
outputTokens: number;
|
|
77
|
+
audioSeconds: number;
|
|
78
|
+
characters: number;
|
|
79
|
+
lastAt: string;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* What the window held, so the screen can tell "nothing happened" from "this app's agent lib does
|
|
83
|
+
* not publish these events yet".
|
|
84
|
+
*/
|
|
85
|
+
export interface AiCoverage {
|
|
86
|
+
usageEvents: number;
|
|
87
|
+
runEvents: number;
|
|
88
|
+
/** Every agent event seen, of any kind (`run.started`, `tool-call`, …). */
|
|
89
|
+
agentEvents: number;
|
|
90
|
+
/** Agent events exist but none is `usage.recorded`: the agent lib predates the ledger event. */
|
|
91
|
+
legacyAgentLib: boolean;
|
|
92
|
+
/** Runs whose terminal event carried no timing breakdown (queue/model/tools). */
|
|
93
|
+
runsWithoutTimings: number;
|
|
94
|
+
}
|
|
95
|
+
export interface AiUsageSummary {
|
|
96
|
+
windowStart: string;
|
|
97
|
+
windowEnd: string;
|
|
98
|
+
spend: AiSpend;
|
|
99
|
+
/** Most recent first. */
|
|
100
|
+
runs: AiRunRow[];
|
|
101
|
+
slowest: AiRunRow[];
|
|
102
|
+
mostExpensive: AiRunRow[];
|
|
103
|
+
unpriced: AiUnpricedRow[];
|
|
104
|
+
coverage: AiCoverage;
|
|
105
|
+
}
|
|
106
|
+
export interface SummarizeAiUsageOptions {
|
|
107
|
+
windowStart: Date;
|
|
108
|
+
windowEnd: Date;
|
|
109
|
+
/** Spend time buckets. Default 24. */
|
|
110
|
+
buckets?: number;
|
|
111
|
+
/** Rows per breakdown and per runs table. Default 10 for breakdowns, 50 for runs. */
|
|
112
|
+
top?: number;
|
|
113
|
+
runsLimit?: number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Summarize the agent entries of a window into the AI screen: spend (total, platform and estimated
|
|
117
|
+
* shares, by feature / model / actor, over time), runs (recent, slowest, most expensive, each with
|
|
118
|
+
* its step breakdown) and the unpriced calls, from the `diagnostic` entries the generic watcher
|
|
119
|
+
* records for `agora:agent:*` (see {@link agentEventOfEntry}). Entries outside the window are
|
|
120
|
+
* ignored. Pure.
|
|
121
|
+
*/
|
|
122
|
+
export declare function summarizeAiUsage(entries: readonly Entry[], options: SummarizeAiUsageOptions): AiUsageSummary;
|
|
123
|
+
//# sourceMappingURL=ai_usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai_usage.d.ts","sourceRoot":"","sources":["../../../src/ai_metrics/ai_usage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAGL,KAAK,SAAS,EAEd,KAAK,UAAU,EAEhB,MAAM,mBAAmB,CAAC;AAE3B,iEAAiE;AACjE,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,4CAA4C;IAC5C,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC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