-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtoken-persistence.js
More file actions
305 lines (282 loc) · 9.91 KB
/
Copy pathtoken-persistence.js
File metadata and controls
305 lines (282 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Token usage persistence layer for AWF API Proxy.
*
* Manages the NDJSON token-usage log file: stream lifecycle, record
* validation, and disk writes. Also owns the optional diagnostics log
* (AWF_DEBUG_TOKENS=1).
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { logRequest } = require('./logging');
// Token usage log file path (inside the mounted log volume)
const TOKEN_LOG_DIR = process.env.AWF_TOKEN_LOG_DIR || '/var/log/api-proxy';
const TOKEN_LOG_FILE = path.join(TOKEN_LOG_DIR, 'token-usage.jsonl');
const DIAG_LOG_FILE = path.join(TOKEN_LOG_DIR, 'token-diag.jsonl');
const AUDIT_LOG_FILE = path.join(TOKEN_LOG_DIR, 'token-tracker-audit.jsonl');
const DIAG_ENABLED = process.env.AWF_DEBUG_TOKENS === '1';
// AWF version used to identify schema version in JSONL records.
// Set to the container image version at build time via ARG AWF_VERSION in the Dockerfile
// (baked in by the release workflow with --build-arg AWF_VERSION=<version>).
// Falls back to "0.0.0-dev" for local/un-versioned builds.
const AWF_VERSION = process.env.AWF_VERSION;
if (!AWF_VERSION) {
// Log a warning (to stderr to avoid polluting stdout) when running without the env var.
// This can happen during local development or tests outside the container.
process.stderr.write('{"level":"warn","event":"awf_version_missing","message":"AWF_VERSION env var not set; _schema will use 0.0.0-dev"}\n');
}
const TOKEN_USAGE_SCHEMA = `token-usage/v${AWF_VERSION || '0.0.0-dev'}`;
const TOKEN_DIAG_SCHEMA = `token-diag/v${AWF_VERSION || '0.0.0-dev'}`;
let logStream = null;
let diagStream = null;
let auditStream = null;
/**
* Write a diagnostic line to the diagnostics log file.
* Only active when AWF_DEBUG_TOKENS=1 environment variable is set.
* Does not persist request/response payload data.
*/
function validateTokenDiagRecord(record) {
if (!record || typeof record !== 'object' || Array.isArray(record)) return false;
if (typeof record._schema !== 'string') return false;
if (!/^token-diag\/v\d+\.\d+\.\d+(-\w+)?$/.test(record._schema)) return false;
if (typeof record.timestamp !== 'string') return false;
if (typeof record.event !== 'string') return false;
if (record.data !== undefined && (typeof record.data !== 'object' || record.data === null || Array.isArray(record.data))) return false;
return true;
}
function buildTokenDiagRecord(event, data) {
return {
_schema: TOKEN_DIAG_SCHEMA,
timestamp: new Date().toISOString(),
event: typeof event === 'string' ? event : 'DIAG',
...(data && typeof data === 'object' && !Array.isArray(data) ? { data } : {}),
};
}
function diag(msg, data) {
if (!DIAG_ENABLED) return;
try {
if (!diagStream) {
fs.mkdirSync(TOKEN_LOG_DIR, { recursive: true });
diagStream = fs.createWriteStream(DIAG_LOG_FILE, { flags: 'a', mode: 0o644 });
diagStream.on('error', () => { diagStream = null; });
}
const record = buildTokenDiagRecord(msg, data);
if (!validateTokenDiagRecord(record)) return;
diagStream.write(JSON.stringify(record) + '\n');
} catch { /* best-effort */ }
}
/**
* Always-on audit trail for token tracking lifecycle.
*
* Writes minimal structured events to token-tracker-audit.jsonl so that CI
* failures can be diagnosed without AWF_DEBUG_TOKENS=1. Each tracked
* request emits exactly two lines: TRACK_START and TRACK_END (with result).
*/
function auditTrack(event, data) {
try {
if (!auditStream) {
fs.mkdirSync(TOKEN_LOG_DIR, { recursive: true });
auditStream = fs.createWriteStream(AUDIT_LOG_FILE, { flags: 'a', mode: 0o644 });
auditStream.on('error', () => { auditStream = null; });
}
const line = { ts: Date.now(), event, ...data };
auditStream.write(JSON.stringify(line) + '\n');
} catch { /* best-effort */ }
}
/**
* Get or create the JSONL append stream for token usage logs.
* Uses a lazy singleton — created on first write.
*/
function getLogStream() {
if (logStream) return logStream;
try {
// Ensure directory exists
fs.mkdirSync(TOKEN_LOG_DIR, { recursive: true });
logStream = fs.createWriteStream(TOKEN_LOG_FILE, { flags: 'a', mode: 0o644 });
logStream.on('error', (err) => {
logRequest('warn', 'token_log_error', { error: err.message });
logStream = null;
});
return logStream;
} catch (err) {
logRequest('warn', 'token_log_init_error', { error: err.message });
return null;
}
}
/**
* Validate a token usage record against the token-usage schema contract.
*
* Checks that all required fields are present and have the expected types.
* Logs a warning and returns false if the record is non-conformant; does
* not throw, so a bad record is dropped rather than crashing the proxy.
*
* @param {object} record - The record to validate
* @returns {boolean} true if the record is valid, false otherwise
*/
function validateTokenUsageRecord(record) {
if (!record || typeof record !== 'object') {
logRequest('warn', 'token_record_schema_violation', {
field: 'record',
expected: 'object',
actual: record === null ? 'null' : typeof record,
});
return false;
}
const required = [
['_schema', 'string'],
['timestamp', 'string'],
['event', 'string'],
['request_id', 'string'],
['provider', 'string'],
['model', 'string'],
['path', 'string'],
['status', 'number'],
['streaming', 'boolean'],
['input_tokens', 'number'],
['output_tokens', 'number'],
['cache_read_tokens', 'number'],
['cache_write_tokens', 'number'],
['duration_ms', 'number'],
];
for (const [field, expectedType] of required) {
// eslint-disable-next-line valid-typeof
if (typeof record[field] !== expectedType) {
logRequest('warn', 'token_record_schema_violation', {
request_id: record.request_id,
field,
expected: expectedType,
actual: typeof record[field],
});
return false;
}
}
if (!/^token-usage\/v\d+\.\d+\.\d+(-\w+)?$/.test(record._schema)) {
logRequest('warn', 'token_record_schema_violation', {
request_id: record.request_id,
field: '_schema',
expected: 'token-usage/v<semver>',
actual: record._schema,
});
return false;
}
if (record.event !== 'token_usage') {
logRequest('warn', 'token_record_schema_violation', {
request_id: record.request_id,
field: 'event',
expected: 'token_usage',
actual: record.event,
});
return false;
}
return true;
}
/**
* Build a token usage record for JSONL persistence.
*
* @param {object} normalized - Normalized usage object from normalizeUsage()
* @param {object} opts
* @param {string} opts.requestId
* @param {string} opts.provider
* @param {string|null} opts.model
* @param {string} opts.reqPath
* @param {number} opts.status
* @param {boolean} opts.streaming
* @param {number} opts.duration
* @param {number} opts.responseBytes
* @returns {object}
*/
function buildTokenUsageRecord(normalized, opts) {
const { requestId, provider, model, reqPath, status, streaming, duration, responseBytes } = opts;
return {
_schema: TOKEN_USAGE_SCHEMA,
timestamp: new Date().toISOString(),
event: 'token_usage',
request_id: requestId,
provider,
model: model || 'unknown',
path: reqPath,
status,
streaming,
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
duration_ms: duration,
response_bytes: responseBytes,
};
}
/**
* Increment token usage metrics when a metrics sink is available.
*
* @param {object|null} metricsRef
* @param {string} provider
* @param {object} normalized
*/
function incrementTokenMetrics(metricsRef, provider, normalized) {
if (!metricsRef) return;
metricsRef.increment('input_tokens_total', { provider }, normalized.input_tokens);
metricsRef.increment('output_tokens_total', { provider }, normalized.output_tokens);
}
/**
* Write a token usage record to the JSONL log file.
* Validates the record against the token-usage schema before writing.
* Handles backpressure by dropping writes when the stream buffer is full.
*/
function writeTokenUsage(record) {
if (!validateTokenUsageRecord(record)) return;
const stream = getLogStream();
if (stream && !stream.writableEnded) {
const ok = stream.write(JSON.stringify(record) + '\n');
if (!ok) {
// Backpressure — stream buffer full. Drop this write rather than
// accumulating unbounded memory. The 'drain' event will unblock
// future writes naturally.
logRequest('warn', 'token_log_backpressure', { request_id: record.request_id });
}
}
}
/**
* Close the log stream (for graceful shutdown).
* Returns a Promise that resolves once the stream has flushed.
*/
function closeLogStream() {
// Also close the blocked-request diagnostics stream if the module is loaded.
let closeBlockedRequestDiagStream = () => Promise.resolve();
try {
({ closeBlockedRequestDiagStream } = require('./blocked-request-diagnostics'));
} catch { /* optional module — no-op when absent */ }
return Promise.all([
new Promise((resolve) => {
let pending = 0;
const check = () => { if (pending === 0) resolve(); };
if (logStream) {
pending++;
logStream.end(() => { logStream = null; pending--; check(); });
}
if (diagStream) {
pending++;
diagStream.end(() => { diagStream = null; pending--; check(); });
}
if (auditStream) {
pending++;
auditStream.end(() => { auditStream = null; pending--; check(); });
}
if (pending === 0) resolve();
}),
closeBlockedRequestDiagStream(),
]).then(() => {});
}
module.exports = {
TOKEN_LOG_FILE,
TOKEN_USAGE_SCHEMA,
TOKEN_DIAG_SCHEMA,
diag,
auditTrack,
buildTokenDiagRecord,
buildTokenUsageRecord,
incrementTokenMetrics,
validateTokenDiagRecord,
validateTokenUsageRecord,
writeTokenUsage,
closeLogStream,
};