@vtex/api 7.5.0 → 7.5.1-beta.0

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.
@@ -1,5 +1,5 @@
1
1
  import { CacheLayer } from './CacheLayer';
2
- import { DiskStats } from './typings';
2
+ import { CumulativeStats, DiskStats } from './typings';
3
3
  import { outputJSON, readJSON } from 'fs-extra';
4
4
  export declare class DiskCache<V> implements CacheLayer<string, V> {
5
5
  private cachePath;
@@ -8,9 +8,11 @@ export declare class DiskCache<V> implements CacheLayer<string, V> {
8
8
  private hits;
9
9
  private total;
10
10
  private lock;
11
+ private reported;
11
12
  constructor(cachePath: string, readFile?: typeof readJSON, writeFile?: typeof outputJSON);
12
13
  has: (key: string) => boolean;
13
14
  getStats: (name?: string) => DiskStats;
15
+ getCumulativeStats: () => CumulativeStats;
14
16
  get: (key: string) => Promise<V | void>;
15
17
  set: (key: string, value: V) => Promise<boolean>;
16
18
  private getPathKey;
@@ -14,20 +14,24 @@ class DiskCache {
14
14
  this.writeFile = writeFile;
15
15
  this.hits = 0;
16
16
  this.total = 0;
17
+ this.reported = { hits: 0, total: 0 };
17
18
  this.has = (key) => {
18
19
  const pathKey = this.getPathKey(key);
19
20
  return (0, fs_extra_1.pathExistsSync)(pathKey);
20
21
  };
21
22
  this.getStats = (name = 'disk-cache') => {
22
23
  const stats = {
23
- hits: this.hits,
24
+ hits: this.hits - this.reported.hits,
24
25
  name,
25
- total: this.total,
26
+ total: this.total - this.reported.total,
26
27
  };
27
- this.hits = 0;
28
- this.total = 0;
28
+ this.reported = { hits: this.hits, total: this.total };
29
29
  return stats;
30
30
  };
31
+ this.getCumulativeStats = () => ({
32
+ hits: this.hits,
33
+ total: this.total,
34
+ });
31
35
  this.get = async (key) => {
32
36
  const pathKey = this.getPathKey(key);
33
37
  this.total += 1;
@@ -1,16 +1,18 @@
1
1
  import LRU from 'lru-cache';
2
2
  import { CacheLayer } from './CacheLayer';
3
- import { FetchResult, LRUStats } from './typings';
3
+ import { CumulativeStats, FetchResult, LRUStats } from './typings';
4
4
  export declare class LRUCache<K, V> implements CacheLayer<K, V> {
5
5
  private multilayer;
6
6
  private storage;
7
7
  private hits;
8
8
  private total;
9
9
  private disposed;
10
+ private reported;
10
11
  constructor(options: LRU.Options<K, V>);
11
12
  get: (key: K) => V | void;
12
13
  getOrSet: (key: K, fetcher?: () => Promise<FetchResult<V>>) => Promise<V | void>;
13
14
  set: (key: K, value: V, maxAge?: number) => boolean;
14
15
  has: (key: K) => boolean;
15
16
  getStats: (name?: string) => LRUStats;
17
+ getCumulativeStats: () => CumulativeStats;
16
18
  }
@@ -20,24 +20,33 @@ class LRUCache {
20
20
  this.set = (key, value, maxAge) => this.storage.set(key, value, maxAge);
21
21
  this.has = (key) => this.storage.has(key);
22
22
  this.getStats = (name = 'lru-cache') => {
23
+ const hits = this.hits - this.reported.hits;
24
+ const total = this.total - this.reported.total;
23
25
  const stats = {
24
- disposedItems: this.disposed,
25
- hitRate: this.total > 0 ? this.hits / this.total : undefined,
26
- hits: this.hits,
26
+ disposedItems: this.disposed - this.reported.disposed,
27
+ hitRate: total > 0 ? hits / total : undefined,
28
+ hits,
27
29
  itemCount: this.storage.itemCount,
28
30
  length: this.storage.length,
29
31
  max: this.storage.max,
30
32
  name,
31
- total: this.total,
33
+ total,
32
34
  };
33
- this.hits = 0;
34
- this.total = 0;
35
- this.disposed = 0;
35
+ this.reported = { disposed: this.disposed, hits: this.hits, total: this.total };
36
36
  return stats;
37
37
  };
38
+ this.getCumulativeStats = () => ({
39
+ disposedItems: this.disposed,
40
+ hits: this.hits,
41
+ itemCount: this.storage.itemCount,
42
+ length: this.storage.length,
43
+ max: this.storage.max,
44
+ total: this.total,
45
+ });
38
46
  this.hits = 0;
39
47
  this.total = 0;
40
48
  this.disposed = 0;
49
+ this.reported = { disposed: 0, hits: 0, total: 0 };
41
50
  this.storage = new lru_cache_1.default({
42
51
  ...options,
43
52
  dispose: () => this.disposed += 1,
@@ -1,5 +1,5 @@
1
1
  import { CacheLayer } from './CacheLayer';
2
- import { LRUDiskCacheOptions, LRUStats } from './typings';
2
+ import { CumulativeStats, LRUDiskCacheOptions, LRUStats } from './typings';
3
3
  import { outputJSON, readJSON } from 'fs-extra';
4
4
  export declare class LRUDiskCache<V> implements CacheLayer<string, V> {
5
5
  private cachePath;
@@ -11,9 +11,11 @@ export declare class LRUDiskCache<V> implements CacheLayer<string, V> {
11
11
  private total;
12
12
  private lruStorage;
13
13
  private keyToBeDeleted;
14
+ private reported;
14
15
  constructor(cachePath: string, options: LRUDiskCacheOptions, readFile?: typeof readJSON, writeFile?: typeof outputJSON);
15
16
  has: (key: string) => boolean;
16
17
  getStats: (name?: string) => LRUStats;
18
+ getCumulativeStats: () => CumulativeStats;
17
19
  get: (key: string) => Promise<V | void>;
18
20
  set: (key: string, value: V, maxAge?: number) => Promise<boolean>;
19
21
  private getPathKey;
@@ -15,23 +15,32 @@ class LRUDiskCache {
15
15
  this.writeFile = writeFile;
16
16
  this.hits = 0;
17
17
  this.total = 0;
18
+ this.reported = { disposed: 0, hits: 0, total: 0 };
18
19
  this.has = (key) => this.lruStorage.has(key);
19
20
  this.getStats = (name = 'disk-lru-cache') => {
21
+ const hits = this.hits - this.reported.hits;
22
+ const total = this.total - this.reported.total;
20
23
  const stats = {
21
- disposedItems: this.disposed,
22
- hitRate: this.total > 0 ? this.hits / this.total : undefined,
23
- hits: this.hits,
24
+ disposedItems: this.disposed - this.reported.disposed,
25
+ hitRate: total > 0 ? hits / total : undefined,
26
+ hits,
24
27
  itemCount: this.lruStorage.itemCount,
25
28
  length: this.lruStorage.length,
26
29
  max: this.lruStorage.max,
27
30
  name,
28
- total: this.total,
31
+ total,
29
32
  };
30
- this.hits = 0;
31
- this.total = 0;
32
- this.disposed = 0;
33
+ this.reported = { disposed: this.disposed, hits: this.hits, total: this.total };
33
34
  return stats;
34
35
  };
36
+ this.getCumulativeStats = () => ({
37
+ disposedItems: this.disposed,
38
+ hits: this.hits,
39
+ itemCount: this.lruStorage.itemCount,
40
+ length: this.lruStorage.length,
41
+ max: this.lruStorage.max,
42
+ total: this.total,
43
+ });
35
44
  this.get = async (key) => {
36
45
  const timeOfDeath = this.lruStorage.get(key);
37
46
  this.total += 1;
@@ -1,14 +1,15 @@
1
1
  import { CacheLayer } from './CacheLayer';
2
- import { FetchResult, MultilayerStats } from './typings';
2
+ import { CumulativeStats, FetchResult, MultilayerStats } from './typings';
3
3
  export declare class MultilayeredCache<K, V> implements CacheLayer<K, V> {
4
4
  private caches;
5
5
  private hits;
6
6
  private total;
7
+ private reported;
7
8
  constructor(caches: Array<CacheLayer<K, V>>);
8
9
  get: (key: K, fetcher?: () => Promise<FetchResult<V>>) => Promise<V | void>;
9
10
  set: (key: K, value: V, maxAge?: number) => Promise<boolean>;
10
11
  has: (key: K) => Promise<boolean>;
11
12
  getStats: (name?: string) => MultilayerStats;
13
+ getCumulativeStats: () => CumulativeStats;
12
14
  private findIndex;
13
- private resetCounters;
14
15
  }
@@ -7,6 +7,7 @@ class MultilayeredCache {
7
7
  this.caches = caches;
8
8
  this.hits = 0;
9
9
  this.total = 0;
10
+ this.reported = { hits: 0, total: 0 };
10
11
  this.get = async (key, fetcher) => {
11
12
  let value = undefined;
12
13
  let maxAge;
@@ -40,15 +41,21 @@ class MultilayeredCache {
40
41
  return (0, ramda_1.any)(item => item, hasList);
41
42
  };
42
43
  this.getStats = (name = 'multilayred-cache') => {
44
+ const hits = this.hits - this.reported.hits;
45
+ const total = this.total - this.reported.total;
43
46
  const multilayerStats = {
44
- hitRate: this.total > 0 ? this.hits / this.total : undefined,
45
- hits: this.hits,
47
+ hitRate: total > 0 ? hits / total : undefined,
48
+ hits,
46
49
  name,
47
- total: this.total,
50
+ total,
48
51
  };
49
- this.resetCounters();
52
+ this.reported = { hits: this.hits, total: this.total };
50
53
  return multilayerStats;
51
54
  };
55
+ this.getCumulativeStats = () => ({
56
+ hits: this.hits,
57
+ total: this.total,
58
+ });
52
59
  this.findIndex = async (func, array) => {
53
60
  this.total += 1;
54
61
  for (let index = 0; index < array.length; index++) {
@@ -61,9 +68,5 @@ class MultilayeredCache {
61
68
  return -1;
62
69
  };
63
70
  }
64
- resetCounters() {
65
- this.hits = 0;
66
- this.total = 0;
67
- }
68
71
  }
69
72
  exports.MultilayeredCache = MultilayeredCache;
@@ -17,6 +17,14 @@ export type LRUStats = {
17
17
  name: string;
18
18
  total: number;
19
19
  };
20
+ export type CumulativeStats = {
21
+ hits: number;
22
+ total: number;
23
+ disposedItems?: number;
24
+ itemCount?: number;
25
+ length?: number;
26
+ max?: number;
27
+ };
20
28
  export type MultilayerStats = {
21
29
  hitRate: number | undefined;
22
30
  hits: number;
@@ -1,4 +1,8 @@
1
- import { Attributes } from '@opentelemetry/api';
1
+ import { Attributes, MetricOptions, ObservableCallback } from '@opentelemetry/api';
2
+ import { CumulativeStats } from '../caches/typings';
3
+ export interface TrackedCache {
4
+ getCumulativeStats(): CumulativeStats;
5
+ }
2
6
  /**
3
7
  * DiagnosticsMetrics provides a high-level API for recording metrics using
4
8
  * the @vtex/diagnostics-nodejs library. It completely abstracts instrument
@@ -56,8 +60,12 @@ export declare class DiagnosticsMetrics {
56
60
  private metricsClient;
57
61
  private clientInitPromise;
58
62
  private latencyHistogram;
59
- private counters;
60
- private gauges;
63
+ private readonly counters;
64
+ private readonly gauges;
65
+ private readonly observableRegistrations;
66
+ private readonly observableInstruments;
67
+ private readonly cacheRegistry;
68
+ private cacheInstruments;
61
69
  constructor();
62
70
  /**
63
71
  * Initialize the metrics client with timeout handling.
@@ -186,4 +194,12 @@ export declare class DiagnosticsMetrics {
186
194
  * ```
187
195
  */
188
196
  setGauge(name: string, value: number, attributes?: Attributes): void;
197
+ registerObservableGauge(name: string, observe: ObservableCallback, options?: MetricOptions): () => void;
198
+ registerObservableCounter(name: string, observe: ObservableCallback, options?: MetricOptions): () => void;
199
+ private registerObservable;
200
+ private syncObservables;
201
+ private flushPendingObservables;
202
+ trackCache(name: string, cacheInstance: TrackedCache): () => void;
203
+ private ensureCacheInstruments;
204
+ private observeCaches;
189
205
  }
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DiagnosticsMetrics = void 0;
4
4
  const api_1 = require("@opentelemetry/api");
5
- const client_1 = require("../service/metrics/client");
6
5
  const constants_1 = require("../constants");
6
+ const client_1 = require("../service/metrics/client");
7
7
  /**
8
8
  * Maximum number of custom attributes allowed per metric call to control cardinality.
9
9
  * This limit applies only to custom attributes provided by callers (VTEX IO Apps).
@@ -17,6 +17,11 @@ const MAX_CUSTOM_ATTRIBUTES = 7;
17
17
  * These attributes are automatically merged with custom attributes in all metric methods.
18
18
  */
19
19
  const BASE_ATTRIBUTES_KEY = (0, api_1.createContextKey)('vtex.metrics.baseAttributes');
20
+ // trackCache() metric names — one shared instrument set, differentiated by `cache`.
21
+ const CACHE_OPERATIONS_METRIC = 'io_app_cache_operations_total';
22
+ const CACHE_ITEMS_METRIC = 'io_app_cache_items_current';
23
+ const CACHE_CAPACITY_METRIC = 'io_app_cache_capacity';
24
+ const CACHE_DISPOSED_METRIC = 'io_app_cache_disposed_total';
20
25
  /**
21
26
  * Converts an hrtime tuple [seconds, nanoseconds] to milliseconds.
22
27
  */
@@ -46,6 +51,16 @@ function limitCustomAttributes(customAttributes) {
46
51
  }
47
52
  return Object.fromEntries(entries.slice(0, MAX_CUSTOM_ATTRIBUTES));
48
53
  }
54
+ /**
55
+ * Applies the same cardinality limit the push-based methods use to whatever an
56
+ * observable callback reports. Observables run outside any request, so there are no
57
+ * base attributes to merge — every attribute here is a custom one.
58
+ */
59
+ function limitObservableResult(result) {
60
+ return {
61
+ observe: (value, attributes) => result.observe(value, limitCustomAttributes(attributes)),
62
+ };
63
+ }
49
64
  /**
50
65
  * DiagnosticsMetrics provides a high-level API for recording metrics using
51
66
  * the @vtex/diagnostics-nodejs library. It completely abstracts instrument
@@ -103,6 +118,9 @@ class DiagnosticsMetrics {
103
118
  constructor() {
104
119
  this.counters = new Map();
105
120
  this.gauges = new Map();
121
+ this.observableRegistrations = { gauge: new Map(), counter: new Map() };
122
+ this.observableInstruments = { gauge: new Map(), counter: new Map() };
123
+ this.cacheRegistry = new Map();
106
124
  this.initMetricClient();
107
125
  }
108
126
  /**
@@ -124,6 +142,9 @@ class DiagnosticsMetrics {
124
142
  ]);
125
143
  // Create the single latency histogram after client is ready
126
144
  this.createLatencyHistogram();
145
+ // Attach any observable registrations made before the client was ready.
146
+ // No-op if there are none.
147
+ this.flushPendingObservables();
127
148
  return this.metricsClient;
128
149
  }
129
150
  catch (error) {
@@ -338,5 +359,156 @@ class DiagnosticsMetrics {
338
359
  // Set the gauge value
339
360
  this.gauges.get(name).set(value, mergedAttributes);
340
361
  }
362
+ // Registers (or replaces) a pull-based gauge: OTel calls `observe` on its own
363
+ // schedule, with `result.observe(value, attributes?)`. No base-attribute merging —
364
+ // there's no request in progress when this runs — but the attributes reported are
365
+ // held to the same MAX_CUSTOM_ATTRIBUTES limit as the push-based methods.
366
+ registerObservableGauge(name, observe, options) {
367
+ return this.registerObservable('gauge', name, observe, options);
368
+ }
369
+ // Same as registerObservableGauge, but `observe` reports the cumulative total —
370
+ // the SDK derives the delta itself.
371
+ registerObservableCounter(name, observe, options) {
372
+ return this.registerObservable('counter', name, observe, options);
373
+ }
374
+ registerObservable(kind, name, observe, options) {
375
+ // The same name registered as both kinds produces two same-named streams of
376
+ // different types in one meter, which the SDK accepts silently and the collector
377
+ // then rejects. Refuse the second one instead of publishing a broken metric.
378
+ const otherKind = kind === 'gauge' ? 'counter' : 'gauge';
379
+ if (this.observableRegistrations[otherKind].has(name)) {
380
+ console.error(`DiagnosticsMetrics: '${name}' is already registered as an observable ${otherKind}; ` +
381
+ `ignoring the ${kind} registration. Pick a distinct metric name.`);
382
+ return () => undefined;
383
+ }
384
+ this.observableRegistrations[kind].set(name, { observe, options });
385
+ this.syncObservables(kind);
386
+ return () => {
387
+ var _a, _b;
388
+ // A later registration for the same name owns the instrument now; disposing
389
+ // this one must not tear that down.
390
+ if (((_a = this.observableRegistrations[kind].get(name)) === null || _a === void 0 ? void 0 : _a.observe) !== observe) {
391
+ return;
392
+ }
393
+ this.observableRegistrations[kind].delete(name);
394
+ (_b = this.observableInstruments[kind].get(name)) === null || _b === void 0 ? void 0 : _b.remove();
395
+ this.observableInstruments[kind].delete(name);
396
+ };
397
+ }
398
+ // Attaches every `kind` registration to its instrument. A no-op if there's nothing
399
+ // registered or the client isn't ready — safe to call anytime, including from
400
+ // flushPendingObservables() once the client becomes ready.
401
+ //
402
+ // One instrument per name, carrying one callback for its whole life: the callback
403
+ // is a trampoline that reads whatever registration is current, so replacing the
404
+ // app's function is a map update and never touches the SDK. Re-creating the
405
+ // instrument instead would leave the meter holding a stream the collector sees as
406
+ // a duplicate type for the same name.
407
+ syncObservables(kind) {
408
+ if (this.observableRegistrations[kind].size === 0) {
409
+ return; // skip the meter entirely if this kind is unused
410
+ }
411
+ const client = this.metricsClient;
412
+ if (!client) {
413
+ return;
414
+ }
415
+ for (const [name, { options }] of this.observableRegistrations[kind]) {
416
+ if (this.observableInstruments[kind].has(name)) {
417
+ continue;
418
+ }
419
+ const attached = (result) => {
420
+ var _a;
421
+ (_a = this.observableRegistrations[kind].get(name)) === null || _a === void 0 ? void 0 : _a.observe(limitObservableResult(result));
422
+ };
423
+ const instrument = kind === 'gauge'
424
+ ? client.createObservableGauge(name, attached, options)
425
+ : client.createObservableCounter(name, attached, options);
426
+ this.observableInstruments[kind].set(name, instrument);
427
+ }
428
+ }
429
+ // Runs once the client is ready. No-op for an app that never calls
430
+ // registerObservableGauge/Counter/trackCache.
431
+ flushPendingObservables() {
432
+ this.syncObservables('gauge');
433
+ this.syncObservables('counter');
434
+ if (this.cacheRegistry.size > 0) {
435
+ this.ensureCacheInstruments();
436
+ }
437
+ }
438
+ // Replacement for the legacy MetricsAccumulator.trackCache() — same cache instances,
439
+ // see METRICS_CATALOG.md for the metrics emitted. Reads getCumulativeStats(), which
440
+ // has no side effects, so this can run alongside the legacy trackCache().
441
+ trackCache(name, cacheInstance) {
442
+ this.cacheRegistry.set(name, cacheInstance);
443
+ this.ensureCacheInstruments();
444
+ return () => {
445
+ this.cacheRegistry.delete(name);
446
+ };
447
+ }
448
+ ensureCacheInstruments() {
449
+ if (this.cacheInstruments) {
450
+ return;
451
+ }
452
+ const client = this.metricsClient;
453
+ if (!client) {
454
+ return;
455
+ }
456
+ const operations = client.createObservableCounter(CACHE_OPERATIONS_METRIC, undefined, {
457
+ description: 'Hit/miss operations for a VTEX IO app in-memory cache',
458
+ unit: '1',
459
+ });
460
+ const items = client.createObservableGauge(CACHE_ITEMS_METRIC, undefined, {
461
+ description: 'Current number of items held by a VTEX IO app cache',
462
+ unit: '1',
463
+ });
464
+ const capacity = client.createObservableGauge(CACHE_CAPACITY_METRIC, undefined, {
465
+ description: 'Capacity of a VTEX IO app cache, in the units that cache uses (item count, unless the LRU was built with a length function)',
466
+ unit: '1',
467
+ });
468
+ const disposed = client.createObservableCounter(CACHE_DISPOSED_METRIC, undefined, {
469
+ description: 'Items disposed (evicted) from a VTEX IO app cache',
470
+ unit: '1',
471
+ });
472
+ client.addBatchObservableCallback((result) => this.observeCaches(result), [operations.instrument, items.instrument, capacity.instrument, disposed.instrument]);
473
+ // These live for the process, so only the handles are kept: the wrappers exist
474
+ // to attach the batch callback above.
475
+ this.cacheInstruments = {
476
+ operations: operations.instrument,
477
+ items: items.instrument,
478
+ capacity: capacity.instrument,
479
+ disposed: disposed.instrument,
480
+ };
481
+ }
482
+ // Reads each registered cache's cumulative counters straight into the instruments.
483
+ observeCaches(result) {
484
+ if (!this.cacheInstruments) {
485
+ return;
486
+ }
487
+ const { operations, items, capacity, disposed } = this.cacheInstruments;
488
+ for (const [name, cache] of this.cacheRegistry) {
489
+ let stats;
490
+ try {
491
+ stats = cache.getCumulativeStats();
492
+ }
493
+ catch (error) {
494
+ console.error('DiagnosticsMetrics: failed to read stats for cache', name, error);
495
+ continue;
496
+ }
497
+ const attributes = { cache: name };
498
+ if (typeof stats.hits === 'number' && typeof stats.total === 'number') {
499
+ result.observe(operations, stats.hits, { ...attributes, cache_state: 'hit' });
500
+ result.observe(operations, Math.max(stats.total - stats.hits, 0), { ...attributes, cache_state: 'miss' });
501
+ }
502
+ if (typeof stats.itemCount === 'number') {
503
+ result.observe(items, stats.itemCount, attributes);
504
+ }
505
+ if (typeof stats.max === 'number') {
506
+ result.observe(capacity, stats.max, attributes);
507
+ }
508
+ if (typeof stats.disposedItems === 'number') {
509
+ result.observe(disposed, stats.disposedItems, attributes);
510
+ }
511
+ }
512
+ }
341
513
  }
342
514
  exports.DiagnosticsMetrics = DiagnosticsMetrics;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtex/api",
3
- "version": "7.5.0",
3
+ "version": "7.5.1-beta.0",
4
4
  "description": "VTEX I/O API client",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -53,7 +53,7 @@
53
53
  "@opentelemetry/instrumentation-koa": "0.47.1",
54
54
  "@types/koa": "^2.11.0",
55
55
  "@types/koa-compose": "^3.2.3",
56
- "@vtex/diagnostics-nodejs": "0.1.8-io",
56
+ "@vtex/diagnostics-nodejs": "0.1.8-io-beta.4",
57
57
  "@vtex/diagnostics-semconv": "5.5.2",
58
58
  "@vtex/node-error-report": "^0.0.3",
59
59
  "@wry/equality": "^0.1.9",
@@ -95,6 +95,8 @@
95
95
  "xss": "^1.0.6"
96
96
  },
97
97
  "devDependencies": {
98
+ "@opentelemetry/context-async-hooks": "^1.30.1",
99
+ "@opentelemetry/sdk-metrics": "^1.30.1",
98
100
  "@tiagonapoli/opentracing-alternate-mock": "^0.0.3",
99
101
  "@types/archiver": "^2.0.1",
100
102
  "@types/bluebird": "^3.5.27",