veryfront 0.1.912 → 0.1.914
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/esm/deno.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/src/metrics/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,yBAAyB,CAAC;AAcjC,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAChF,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC;AAEpE,MAAM,WAAW,uBAAuB;IACtC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/src/metrics/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,yBAAyB,CAAC;AAcjC,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAChF,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC;AAEpE,MAAM,WAAW,uBAAuB;IACtC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AA6XD,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,KAAK,SAAI,EACT,UAAU,CAAC,EAAE,gBAAgB,EAC7B,OAAO,CAAC,EAAE,uBAAuB,GAChC,IAAI,CAON;AAED,wBAAgB,SAAS,CACvB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,UAAU,CAAC,EAAE,gBAAgB,EAC7B,OAAO,CAAC,EAAE,uBAAuB,GAChC,IAAI,CAON;AAED,wBAAgB,KAAK,CACnB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,UAAU,CAAC,EAAE,gBAAgB,EAC7B,OAAO,CAAC,EAAE,uBAAuB,GAChC,IAAI,CAaN;AAED,eAAO,MAAM,OAAO;;;;uBAIO,OAAO,CAAC,IAAI,CAAC;uBAGnB,IAAI;CAYxB,CAAC"}
|
package/esm/src/metrics/index.js
CHANGED
|
@@ -21,6 +21,7 @@ const histograms = new Map();
|
|
|
21
21
|
const gauges = new Map();
|
|
22
22
|
const directQueue = [];
|
|
23
23
|
const directCounterTotals = new Map();
|
|
24
|
+
const directHistogramTotals = new Map();
|
|
24
25
|
let directFlushTimer = null;
|
|
25
26
|
const DIRECT_FLUSH_DELAY_MS = 1_000;
|
|
26
27
|
const DIRECT_MAX_BATCH_SIZE = 100;
|
|
@@ -113,6 +114,20 @@ function resolveOtlpMetricsUrl() {
|
|
|
113
114
|
const trimmed = endpoint.replace(/\/$/, "");
|
|
114
115
|
return trimmed.endsWith("/v1/metrics") ? trimmed : `${trimmed}/v1/metrics`;
|
|
115
116
|
}
|
|
117
|
+
function resolveInternalMetricsUrl() {
|
|
118
|
+
if (readEnv("OTEL_METRICS_ENABLED") !== "true")
|
|
119
|
+
return null;
|
|
120
|
+
const apiBaseUrl = readEnv("VERYFRONT_API_BASE_URL") ?? readEnv("VERYFRONT_API_URL");
|
|
121
|
+
const username = readEnv("VERYFRONT_API_INTERNAL_USER");
|
|
122
|
+
const password = readEnv("VERYFRONT_API_INTERNAL_PASS");
|
|
123
|
+
if (!apiBaseUrl || !username || !password)
|
|
124
|
+
return null;
|
|
125
|
+
return `${apiBaseUrl.replace(/\/$/, "")}/internal/metrics/otlp/v1/metrics`;
|
|
126
|
+
}
|
|
127
|
+
function buildBasicAuth(username, password) {
|
|
128
|
+
const credentials = `${username}:${password}`;
|
|
129
|
+
return `Basic ${globalThis.btoa(credentials)}`;
|
|
130
|
+
}
|
|
116
131
|
function parseHeaders(headerInput) {
|
|
117
132
|
if (!headerInput)
|
|
118
133
|
return {};
|
|
@@ -130,6 +145,24 @@ function parseHeaders(headerInput) {
|
|
|
130
145
|
}
|
|
131
146
|
return result;
|
|
132
147
|
}
|
|
148
|
+
function resolveDirectMetricsTarget() {
|
|
149
|
+
const internalUrl = resolveInternalMetricsUrl();
|
|
150
|
+
if (internalUrl) {
|
|
151
|
+
return {
|
|
152
|
+
url: internalUrl,
|
|
153
|
+
headers: {
|
|
154
|
+
Authorization: buildBasicAuth(readEnv("VERYFRONT_API_INTERNAL_USER") ?? "", readEnv("VERYFRONT_API_INTERNAL_PASS") ?? ""),
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const otlpUrl = resolveOtlpMetricsUrl();
|
|
159
|
+
if (!otlpUrl)
|
|
160
|
+
return null;
|
|
161
|
+
return {
|
|
162
|
+
url: otlpUrl,
|
|
163
|
+
headers: parseHeaders(readEnv("OTEL_EXPORTER_OTLP_HEADERS")),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
133
166
|
function toOtlpValue(value) {
|
|
134
167
|
if (typeof value === "boolean")
|
|
135
168
|
return { boolValue: value };
|
|
@@ -177,19 +210,31 @@ function buildDirectMetric(sample) {
|
|
|
177
210
|
};
|
|
178
211
|
}
|
|
179
212
|
if (sample.kind === "histogram") {
|
|
213
|
+
const key = `${sample.name}:${attributesKey(sample.attributes)}`;
|
|
214
|
+
const total = directHistogramTotals.get(key) ?? {
|
|
215
|
+
count: 0,
|
|
216
|
+
sum: 0,
|
|
217
|
+
bucketCounts: new Array(HISTOGRAM_BOUNDS.length + 1).fill(0),
|
|
218
|
+
startTimeUnixNano: sample.timestampUnixNano,
|
|
219
|
+
};
|
|
220
|
+
const sampleBuckets = buildHistogramBuckets(sample.value);
|
|
221
|
+
total.count += 1;
|
|
222
|
+
total.sum += sample.value;
|
|
223
|
+
total.bucketCounts = total.bucketCounts.map((count, index) => count + sampleBuckets[index]);
|
|
224
|
+
directHistogramTotals.set(key, total);
|
|
180
225
|
return {
|
|
181
226
|
name: sample.name,
|
|
182
227
|
histogram: {
|
|
183
228
|
dataPoints: [{
|
|
184
229
|
attributes,
|
|
185
|
-
startTimeUnixNano:
|
|
230
|
+
startTimeUnixNano: total.startTimeUnixNano,
|
|
186
231
|
timeUnixNano: sample.timestampUnixNano,
|
|
187
|
-
count:
|
|
188
|
-
sum:
|
|
232
|
+
count: total.count,
|
|
233
|
+
sum: total.sum,
|
|
189
234
|
explicitBounds: HISTOGRAM_BOUNDS,
|
|
190
|
-
bucketCounts:
|
|
235
|
+
bucketCounts: total.bucketCounts,
|
|
191
236
|
}],
|
|
192
|
-
aggregationTemporality:
|
|
237
|
+
aggregationTemporality: 2,
|
|
193
238
|
},
|
|
194
239
|
};
|
|
195
240
|
}
|
|
@@ -234,17 +279,17 @@ async function flushDirectMetrics() {
|
|
|
234
279
|
clearTimeout(directFlushTimer);
|
|
235
280
|
directFlushTimer = null;
|
|
236
281
|
}
|
|
237
|
-
const
|
|
238
|
-
if (!
|
|
282
|
+
const target = resolveDirectMetricsTarget();
|
|
283
|
+
if (!target || directQueue.length === 0) {
|
|
239
284
|
directQueue.length = 0;
|
|
240
285
|
return;
|
|
241
286
|
}
|
|
242
287
|
const batch = directQueue.splice(0, DIRECT_MAX_BATCH_SIZE);
|
|
243
288
|
try {
|
|
244
|
-
const response = await fetch(url, {
|
|
289
|
+
const response = await fetch(target.url, {
|
|
245
290
|
method: "POST",
|
|
246
291
|
headers: {
|
|
247
|
-
...
|
|
292
|
+
...target.headers,
|
|
248
293
|
"Content-Type": "application/json",
|
|
249
294
|
},
|
|
250
295
|
body: JSON.stringify(buildDirectOtlpBody(batch)),
|
|
@@ -261,7 +306,7 @@ async function flushDirectMetrics() {
|
|
|
261
306
|
}
|
|
262
307
|
}
|
|
263
308
|
function scheduleDirectFlush() {
|
|
264
|
-
if (directFlushTimer ||
|
|
309
|
+
if (directFlushTimer || resolveDirectMetricsTarget() === null)
|
|
265
310
|
return;
|
|
266
311
|
directFlushTimer = dntShim.setTimeout(() => {
|
|
267
312
|
void flushDirectMetrics();
|
|
@@ -279,7 +324,7 @@ function scheduleDirectFlush() {
|
|
|
279
324
|
}
|
|
280
325
|
}
|
|
281
326
|
function enqueueDirectMetric(kind, name, value, attributes) {
|
|
282
|
-
if (
|
|
327
|
+
if (resolveDirectMetricsTarget() === null)
|
|
283
328
|
return;
|
|
284
329
|
directQueue.push({
|
|
285
330
|
kind,
|
|
@@ -296,7 +341,7 @@ function enqueueDirectMetric(kind, name, value, attributes) {
|
|
|
296
341
|
}
|
|
297
342
|
export function counter(name, value = 1, attributes, options) {
|
|
298
343
|
const normalizedAttributes = normalizeAttributes(attributes);
|
|
299
|
-
if (
|
|
344
|
+
if (resolveDirectMetricsTarget() === null) {
|
|
300
345
|
getCounter(name, options)?.add(value, normalizedAttributes);
|
|
301
346
|
return;
|
|
302
347
|
}
|
|
@@ -304,7 +349,7 @@ export function counter(name, value = 1, attributes, options) {
|
|
|
304
349
|
}
|
|
305
350
|
export function histogram(name, value, attributes, options) {
|
|
306
351
|
const normalizedAttributes = normalizeAttributes(attributes);
|
|
307
|
-
if (
|
|
352
|
+
if (resolveDirectMetricsTarget() === null) {
|
|
308
353
|
getHistogram(name, options)?.record(value, normalizedAttributes);
|
|
309
354
|
return;
|
|
310
355
|
}
|
|
@@ -312,7 +357,7 @@ export function histogram(name, value, attributes, options) {
|
|
|
312
357
|
}
|
|
313
358
|
export function gauge(name, value, attributes, options) {
|
|
314
359
|
const normalizedAttributes = normalizeAttributes(attributes);
|
|
315
|
-
if (
|
|
360
|
+
if (resolveDirectMetricsTarget() !== null) {
|
|
316
361
|
enqueueDirectMetric("gauge", name, value, normalizedAttributes);
|
|
317
362
|
return;
|
|
318
363
|
}
|
|
@@ -337,6 +382,7 @@ export const metrics = {
|
|
|
337
382
|
gauges.clear();
|
|
338
383
|
directQueue.length = 0;
|
|
339
384
|
directCounterTotals.clear();
|
|
385
|
+
directHistogramTotals.clear();
|
|
340
386
|
if (directFlushTimer) {
|
|
341
387
|
clearTimeout(directFlushTimer);
|
|
342
388
|
directFlushTimer = null;
|