-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMetrics.ts
200 lines (168 loc) · 6.99 KB
/
Metrics.ts
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
import { Attributes, Counter, ObservableGauge, UpDownCounter, ValueType } from '@opentelemetry/api';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import * as jpgwire from '@powersync/service-jpgwire';
import * as storage from '../storage/storage-index.js';
import { CorePowerSyncSystem } from '../system/CorePowerSyncSystem.js';
import { logger } from '@powersync/lib-services-framework';
export interface MetricsOptions {
disable_telemetry_sharing: boolean;
powersync_instance_id: string;
internal_metrics_endpoint: string;
}
export class Metrics {
private prometheusExporter: PrometheusExporter;
private meterProvider: MeterProvider;
// Metrics
// 1. Data processing / month
// 1a. Postgres -> PowerSync
// Record on replication pod
public data_replicated_bytes: Counter<Attributes>;
// 1b. PowerSync -> clients
// Record on API pod
public data_synced_bytes: Counter<Attributes>;
// Unused for pricing
// Record on replication pod
public rows_replicated_total: Counter<Attributes>;
// Unused for pricing
// Record on replication pod
public transactions_replicated_total: Counter<Attributes>;
// Unused for pricing
// Record on replication pod
public chunks_replicated_total: Counter<Attributes>;
// 2. Sync operations / month
// Record on API pod
public operations_synced_total: Counter<Attributes>;
// 3. Data hosted on PowerSync sync service
// Record on replication pod
// 3a. Replication storage -> raw data as received from Postgres.
public replication_storage_size_bytes: ObservableGauge<Attributes>;
// 3b. Operations storage -> transformed history, as will be synced to clients
public operation_storage_size_bytes: ObservableGauge<Attributes>;
// 3c. Parameter storage -> used for parameter queries
public parameter_storage_size_bytes: ObservableGauge<Attributes>;
// 4. Peak concurrent connections
// Record on API pod
public concurrent_connections: UpDownCounter<Attributes>;
constructor(meterProvider: MeterProvider, prometheusExporter: PrometheusExporter) {
this.meterProvider = meterProvider;
this.prometheusExporter = prometheusExporter;
const meter = meterProvider.getMeter('powersync');
this.data_replicated_bytes = meter.createCounter('powersync_data_replicated_bytes_total', {
description: 'Uncompressed size of replicated data',
unit: 'bytes',
valueType: ValueType.INT
});
this.data_synced_bytes = meter.createCounter('powersync_data_synced_bytes_total', {
description: 'Uncompressed size of synced data',
unit: 'bytes',
valueType: ValueType.INT
});
this.rows_replicated_total = meter.createCounter('powersync_rows_replicated_total', {
description: 'Total number of replicated rows',
valueType: ValueType.INT
});
this.transactions_replicated_total = meter.createCounter('powersync_transactions_replicated_total', {
description: 'Total number of replicated transactions',
valueType: ValueType.INT
});
this.chunks_replicated_total = meter.createCounter('powersync_chunks_replicated_total', {
description: 'Total number of replication chunks',
valueType: ValueType.INT
});
this.operations_synced_total = meter.createCounter('powersync_operations_synced_total', {
description: 'Number of operations synced',
valueType: ValueType.INT
});
this.replication_storage_size_bytes = meter.createObservableGauge('powersync_replication_storage_size_bytes', {
description: 'Size of current data stored in PowerSync',
unit: 'bytes',
valueType: ValueType.INT
});
this.operation_storage_size_bytes = meter.createObservableGauge('powersync_operation_storage_size_bytes', {
description: 'Size of operations stored in PowerSync',
unit: 'bytes',
valueType: ValueType.INT
});
this.parameter_storage_size_bytes = meter.createObservableGauge('powersync_parameter_storage_size_bytes', {
description: 'Size of parameter data stored in PowerSync',
unit: 'bytes',
valueType: ValueType.INT
});
this.concurrent_connections = meter.createUpDownCounter('powersync_concurrent_connections', {
description: 'Number of concurrent sync connections',
valueType: ValueType.INT
});
}
// Generally only useful for tests. Note: gauges are ignored here.
resetCounters() {
this.data_replicated_bytes.add(0);
this.data_synced_bytes.add(0);
this.rows_replicated_total.add(0);
this.transactions_replicated_total.add(0);
this.chunks_replicated_total.add(0);
this.operations_synced_total.add(0);
this.concurrent_connections.add(0);
}
public async shutdown(): Promise<void> {
await this.meterProvider.shutdown();
}
public configureApiMetrics() {
// Initialize the metric, so that it reports a value before connections
// have been opened.
this.concurrent_connections.add(0);
}
public configureReplicationMetrics(system: CorePowerSyncSystem) {
// Rate limit collection of these stats, since it may be an expensive query
const MINIMUM_INTERVAL = 60_000;
let cachedRequest: Promise<storage.StorageMetrics | null> | undefined = undefined;
let cacheTimestamp = 0;
function getMetrics() {
if (cachedRequest == null || Date.now() - cacheTimestamp > MINIMUM_INTERVAL) {
cachedRequest = system.storage.getStorageMetrics().catch((e) => {
logger.error(`Failed to get storage metrics`, e);
return null;
});
cacheTimestamp = Date.now();
}
return cachedRequest;
}
this.operation_storage_size_bytes.addCallback(async (result) => {
const metrics = await getMetrics();
if (metrics) {
result.observe(metrics.operations_size_bytes);
}
});
this.parameter_storage_size_bytes.addCallback(async (result) => {
const metrics = await getMetrics();
if (metrics) {
result.observe(metrics.parameters_size_bytes);
}
});
this.replication_storage_size_bytes.addCallback(async (result) => {
const metrics = await getMetrics();
if (metrics) {
result.observe(metrics.replication_size_bytes);
}
});
const class_scoped_data_replicated_bytes = this.data_replicated_bytes;
// Record replicated bytes using global jpgwire metrics.
jpgwire.setMetricsRecorder({
addBytesRead(bytes) {
class_scoped_data_replicated_bytes.add(bytes);
}
});
}
public async getMetricValueForTests(name: string): Promise<number | undefined> {
const metrics = await this.prometheusExporter.collect();
const scoped = metrics.resourceMetrics.scopeMetrics[0].metrics;
const metric = scoped.find((metric) => metric.descriptor.name == name);
if (metric == null) {
throw new Error(
`Cannot find metric ${name}. Options: ${scoped.map((metric) => metric.descriptor.name).join(',')}`
);
}
const point = metric.dataPoints[metric.dataPoints.length - 1];
return point?.value as number;
}
}