Skip to content

Commit f27bb87

Browse files
atrakhConvex, Inc.
authored andcommitted
add current_storage_usage log streams topic (#42682)
Adds a new log stream topic called `current_storage_usage` to report snapshots of storage usage metrics at the current timestamp GitOrigin-RevId: f15b27d1a8f8fd29ceb8bf5e025ef4380d6a67ec
1 parent bc8a2a0 commit f27bb87

File tree

6 files changed

+181
-4
lines changed

6 files changed

+181
-4
lines changed

crates/common/src/log_streaming.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ pub enum StructuredLogEvent {
159159
ScheduledJobLag {
160160
lag_seconds: Duration,
161161
},
162+
/// Topic for storage usage metrics. These are periodic snapshots of current
163+
/// storage state aggregated across all tables.
164+
CurrentStorageUsage {
165+
total_document_size_bytes: u64,
166+
total_index_size_bytes: u64,
167+
total_vector_storage_bytes: u64,
168+
total_file_storage_bytes: u64,
169+
total_backup_storage_bytes: u64,
170+
},
162171
// User-specified topics -- not yet implemented.
163172
// See here for more details: https://www.notion.so/Log-Streaming-in-Convex-19a1dfadd6924c33b29b2796b0f5b2e2
164173
// User {
@@ -361,6 +370,21 @@ impl LogEvent {
361370
"lag_seconds": lag_seconds.as_secs()
362371
})
363372
},
373+
StructuredLogEvent::CurrentStorageUsage {
374+
total_document_size_bytes,
375+
total_index_size_bytes,
376+
total_vector_storage_bytes,
377+
total_file_storage_bytes,
378+
total_backup_storage_bytes,
379+
} => serialize_map!({
380+
"_timestamp": ms,
381+
"_topic": "_current_storage_usage",
382+
"total_document_size_bytes": total_document_size_bytes,
383+
"total_index_size_bytes": total_index_size_bytes,
384+
"total_vector_storage_bytes": total_vector_storage_bytes,
385+
"total_file_storage_bytes": total_file_storage_bytes,
386+
"total_backup_storage_bytes": total_backup_storage_bytes,
387+
}),
364388
},
365389
LogEventFormatVersion::V2 => match &self.event {
366390
StructuredLogEvent::Verification => {
@@ -498,6 +522,23 @@ impl LogEvent {
498522
"lag_seconds": lag_seconds.as_secs()
499523
})
500524
},
525+
StructuredLogEvent::CurrentStorageUsage {
526+
total_document_size_bytes,
527+
total_index_size_bytes,
528+
total_vector_storage_bytes,
529+
total_file_storage_bytes,
530+
total_backup_storage_bytes,
531+
} => {
532+
serialize_map!({
533+
"timestamp": ms,
534+
"topic": "current_storage_usage",
535+
"total_document_size_bytes": total_document_size_bytes,
536+
"total_index_size_bytes": total_index_size_bytes,
537+
"total_vector_storage_bytes": total_vector_storage_bytes,
538+
"total_file_storage_bytes": total_file_storage_bytes,
539+
"total_backup_storage_bytes": total_backup_storage_bytes,
540+
})
541+
},
501542
},
502543
}
503544
}
@@ -838,6 +879,17 @@ mod tests {
838879
lag_seconds: u64,
839880
}
840881

882+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
883+
#[allow(dead_code)]
884+
struct StorageUsageEvent {
885+
timestamp: u64,
886+
total_document_size_bytes: u64,
887+
total_index_size_bytes: u64,
888+
total_vector_storage_bytes: u64,
889+
total_file_storage_bytes: u64,
890+
total_backup_storage_bytes: u64,
891+
}
892+
841893
// Union type for all log events, discriminated by topic field
842894
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
843895
#[serde(tag = "topic")]
@@ -855,6 +907,8 @@ mod tests {
855907
SchedulerStats(SchedulerStatsEvent),
856908
#[serde(rename = "scheduled_job_lag")]
857909
ScheduledJobLag(ScheduledJobLagEvent),
910+
#[serde(rename = "current_storage_usage")]
911+
CurrentStorageUsage(StorageUsageEvent),
858912
}
859913

860914
#[test]

crates/database/src/table_usage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use itertools::{
99
use value::TableName;
1010

1111
/// Counts the amount of storage used by documents and indexes in a table.
12-
#[derive(Debug)]
12+
#[derive(Debug, Clone)]
1313
pub struct TableUsage {
1414
/// Bytes used by documents in this table
1515
pub document_size: u64,
@@ -23,7 +23,7 @@ pub struct TableUsage {
2323

2424
/// `TableUsage` for all tables in a database. `T` is the fully qualified name
2525
/// of a table.
26-
#[derive(Debug)]
26+
#[derive(Debug, Clone)]
2727
pub struct TablesUsage<T>(pub BTreeMap<T, TableUsage>);
2828

2929
#[derive(Debug, PartialEq)]

crates/log_streaming/src/sinks/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub fn default_log_filter(event: &LogEvent) -> bool {
2525
| StructuredLogEvent::FunctionExecution { .. }
2626
| StructuredLogEvent::DeploymentAuditLog { .. }
2727
| StructuredLogEvent::SchedulerStats { .. }
28-
| StructuredLogEvent::ScheduledJobLag { .. } => true,
28+
| StructuredLogEvent::ScheduledJobLag { .. }
29+
| StructuredLogEvent::CurrentStorageUsage { .. } => true,
2930
StructuredLogEvent::Exception { .. } => false,
3031
}
3132
}

npm-packages/@convex-dev/platform/log-stream-openapi.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,27 @@
430430
}
431431
}
432432
]
433+
},
434+
{
435+
"allOf": [
436+
{
437+
"$ref": "#/components/schemas/StorageUsageEvent"
438+
},
439+
{
440+
"type": "object",
441+
"required": [
442+
"topic"
443+
],
444+
"properties": {
445+
"topic": {
446+
"type": "string",
447+
"enum": [
448+
"current_storage_usage"
449+
]
450+
}
451+
}
452+
}
453+
]
433454
}
434455
]
435456
},
@@ -477,6 +498,49 @@
477498
}
478499
}
479500
},
501+
"StorageUsageEvent": {
502+
"type": "object",
503+
"required": [
504+
"timestamp",
505+
"total_document_size_bytes",
506+
"total_index_size_bytes",
507+
"total_vector_storage_bytes",
508+
"total_file_storage_bytes",
509+
"total_backup_storage_bytes"
510+
],
511+
"properties": {
512+
"timestamp": {
513+
"type": "integer",
514+
"format": "int64",
515+
"minimum": 0
516+
},
517+
"total_backup_storage_bytes": {
518+
"type": "integer",
519+
"format": "int64",
520+
"minimum": 0
521+
},
522+
"total_document_size_bytes": {
523+
"type": "integer",
524+
"format": "int64",
525+
"minimum": 0
526+
},
527+
"total_file_storage_bytes": {
528+
"type": "integer",
529+
"format": "int64",
530+
"minimum": 0
531+
},
532+
"total_index_size_bytes": {
533+
"type": "integer",
534+
"format": "int64",
535+
"minimum": 0
536+
},
537+
"total_vector_storage_bytes": {
538+
"type": "integer",
539+
"format": "int64",
540+
"minimum": 0
541+
}
542+
}
543+
},
480544
"VerificationEvent": {
481545
"type": "object",
482546
"required": [

npm-packages/@convex-dev/platform/src/generatedLogStreamApi.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ export interface components {
9393
}) | (components["schemas"]["ScheduledJobLagEvent"] & {
9494
/** @enum {string} */
9595
topic: "scheduled_job_lag";
96+
}) | (components["schemas"]["StorageUsageEvent"] & {
97+
/** @enum {string} */
98+
topic: "current_storage_usage";
9699
});
97100
ScheduledJobLagEvent: {
98101
/** Format: int64 */
@@ -108,6 +111,20 @@ export interface components {
108111
/** Format: int64 */
109112
timestamp: number;
110113
};
114+
StorageUsageEvent: {
115+
/** Format: int64 */
116+
timestamp: number;
117+
/** Format: int64 */
118+
total_backup_storage_bytes: number;
119+
/** Format: int64 */
120+
total_document_size_bytes: number;
121+
/** Format: int64 */
122+
total_file_storage_bytes: number;
123+
/** Format: int64 */
124+
total_index_size_bytes: number;
125+
/** Format: int64 */
126+
total_vector_storage_bytes: number;
127+
};
111128
VerificationEvent: {
112129
message: string;
113130
/** Format: int64 */
@@ -126,6 +143,7 @@ export type FunctionExecutionEvent = components['schemas']['FunctionExecutionEve
126143
export type LogStreamEvent = components['schemas']['LogStreamEvent'];
127144
export type ScheduledJobLagEvent = components['schemas']['ScheduledJobLagEvent'];
128145
export type SchedulerStatsEvent = components['schemas']['SchedulerStatsEvent'];
146+
export type StorageUsageEvent = components['schemas']['StorageUsageEvent'];
129147
export type VerificationEvent = components['schemas']['VerificationEvent'];
130148
export type $defs = Record<string, never>;
131149
export type operations = Record<string, never>;

npm-packages/docs/docs/production/integrations/log-streams/log-streams.mdx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type-safe pipelines ingesting log events.
8888
All events will have the following three fields:
8989

9090
- `topic`: string, categorizes a log event, one of
91-
`["verification", "console", "function_execution", "audit_log"]`
91+
`["verification", "console", "function_execution", "audit_log", "scheduler_stats", "current_storage_usage"]`
9292
- `timestamp`: number, Unix epoch timestamp in milliseconds as an integer
9393
- `convex`: An object containing metadata related to your Convex deployment,
9494
including `deployment_name`, `deployment_type`, `project_name`, and
@@ -245,6 +245,46 @@ Schema:
245245
of the oldest overdue scheduled job, in seconds.
246246
- `num_running_jobs`: number, the number of scheduled jobs currently running
247247

248+
### `current_storage_usage` events
249+
250+
These events are periodically sent with snapshots of the current storage usage
251+
across your deployment. They provide aggregated totals for all storage types.
252+
253+
These events are not currently sent for self-hosted deployments.
254+
255+
For calculating billing costs:
256+
257+
- Database Storage Bytes: `total_document_size_bytes + total_index_size_bytes`
258+
- File Storage: `total_file_storage_bytes + total_backup_storage_bytes`
259+
- Vector Storage: `total_vector_storage_bytes`
260+
261+
Schema:
262+
263+
- `topic`: `"current_storage_usage"`
264+
- `timestamp`: Unix epoch timestamp in milliseconds
265+
- `total_document_size_bytes`: number, total size in bytes of all documents
266+
stored in database tables
267+
- `total_index_size_bytes`: number, total size in bytes of all database indexes
268+
- `total_vector_storage_bytes`: number, total size in bytes of vector index
269+
storage
270+
- `total_file_storage_bytes`: number, total size in bytes of file storage
271+
- `total_backup_storage_bytes`: number, total size in bytes of snapshot/backup
272+
storage
273+
274+
Example event:
275+
276+
```json
277+
{
278+
"topic": "current_storage_usage",
279+
"timestamp": 1715973841548,
280+
"total_document_size_bytes": 104857600,
281+
"total_index_size_bytes": 10485760,
282+
"total_vector_storage_bytes": 5242880,
283+
"total_file_storage_bytes": 52428800,
284+
"total_backup_storage_bytes": 209715200
285+
}
286+
```
287+
248288
### `audit_log` events
249289

250290
These events represent changes to your deployment, which also show up in the

0 commit comments

Comments
 (0)