|
| 1 | +import { MetricsEngine } from '../metrics/MetricsEngine.js'; |
| 2 | +import { BucketStorageFactory, StorageMetrics } from './BucketStorage.js'; |
| 3 | +import { logger } from '@powersync/lib-services-framework'; |
| 4 | + |
| 5 | +export enum StorageMetricType { |
| 6 | + // Size of current replication data stored in PowerSync |
| 7 | + REPLICATION_SIZE_BYTES = 'powersync_replication_storage_size_bytes', |
| 8 | + // Size of operations data stored in PowerSync |
| 9 | + OPERATION_SIZE_BYTES = 'powersync_operation_storage_size_bytes', |
| 10 | + // Size of parameter data stored in PowerSync |
| 11 | + PARAMETER_SIZE_BYTES = 'powersync_parameter_storage_size_bytes' |
| 12 | +} |
| 13 | + |
| 14 | +export function createCoreStorageMetrics(engine: MetricsEngine): void { |
| 15 | + engine.createObservableGauge({ |
| 16 | + name: StorageMetricType.REPLICATION_SIZE_BYTES, |
| 17 | + description: 'Size of current data stored in PowerSync', |
| 18 | + unit: 'bytes' |
| 19 | + }); |
| 20 | + |
| 21 | + engine.createObservableGauge({ |
| 22 | + name: StorageMetricType.OPERATION_SIZE_BYTES, |
| 23 | + description: 'Size of operations stored in PowerSync', |
| 24 | + unit: 'bytes' |
| 25 | + }); |
| 26 | + |
| 27 | + engine.createObservableGauge({ |
| 28 | + name: StorageMetricType.PARAMETER_SIZE_BYTES, |
| 29 | + description: 'Size of parameter data stored in PowerSync', |
| 30 | + unit: 'bytes' |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +export function initializeCoreStorageMetrics(engine: MetricsEngine, storage: BucketStorageFactory): void { |
| 35 | + const replication_storage_size_bytes = engine.getObservableGauge(StorageMetricType.REPLICATION_SIZE_BYTES); |
| 36 | + const operation_storage_size_bytes = engine.getObservableGauge(StorageMetricType.OPERATION_SIZE_BYTES); |
| 37 | + const parameter_storage_size_bytes = engine.getObservableGauge(StorageMetricType.PARAMETER_SIZE_BYTES); |
| 38 | + |
| 39 | + const MINIMUM_INTERVAL = 60_000; |
| 40 | + |
| 41 | + let cachedRequest: Promise<StorageMetrics | null> | undefined = undefined; |
| 42 | + let cacheTimestamp = 0; |
| 43 | + |
| 44 | + const getMetrics = () => { |
| 45 | + if (cachedRequest == null || Date.now() - cacheTimestamp > MINIMUM_INTERVAL) { |
| 46 | + cachedRequest = storage.getStorageMetrics().catch((e) => { |
| 47 | + logger.error(`Failed to get storage metrics`, e); |
| 48 | + return null; |
| 49 | + }); |
| 50 | + cacheTimestamp = Date.now(); |
| 51 | + } |
| 52 | + return cachedRequest; |
| 53 | + }; |
| 54 | + |
| 55 | + replication_storage_size_bytes.setValueProvider(async () => { |
| 56 | + const metrics = await getMetrics(); |
| 57 | + if (metrics) { |
| 58 | + return metrics.replication_size_bytes; |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + operation_storage_size_bytes.setValueProvider(async () => { |
| 63 | + const metrics = await getMetrics(); |
| 64 | + if (metrics) { |
| 65 | + return metrics.operations_size_bytes; |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + parameter_storage_size_bytes.setValueProvider(async () => { |
| 70 | + const metrics = await getMetrics(); |
| 71 | + if (metrics) { |
| 72 | + return metrics.parameters_size_bytes; |
| 73 | + } |
| 74 | + }); |
| 75 | +} |
0 commit comments