Skip to content

Commit bd6a173

Browse files
nipunn1313Convex, Inc.
authored and
Convex, Inc.
committed
Log content type to event streams (#28999)
Gets ContentType and StorageId in the logging information. Ideally it would include `Id<_storage>`, but it's a bit annoying to get, so just doing StorageUuid for now which is 1-1 equivalent for the time being. This makes changes to both usher/backend - which I ensure compatibility of via the `optional` field. GitOrigin-RevId: a9d40d4a790348c25850d22893b8cbe9b2361348
1 parent c4d2cd5 commit bd6a173

File tree

14 files changed

+146
-96
lines changed

14 files changed

+146
-96
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/application/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ keybroker = { path = "../keybroker" }
3838
lru = { workspace = true }
3939
maplit = { workspace = true }
4040
metrics = { path = "../metrics" }
41-
mime = { workspace = true }
4241
mime2ext = { workspace = true }
4342
minitrace = { workspace = true }
4443
model = { path = "../model" }

crates/application/src/export_worker.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,18 @@ impl<RT: Runtime> ExportWorker<RT> {
593593
file_storage_entry.storage_key,
594594
)
595595
})?;
596+
597+
let content_type = file_storage_entry
598+
.content_type
599+
.as_ref()
600+
.map(|ct| ct.parse())
601+
.transpose()?;
596602
usage
597-
.track_storage_call("snapshot_export")
603+
.track_storage_call(
604+
"snapshot_export",
605+
Some(file_storage_entry.storage_id.clone()),
606+
content_type,
607+
)
598608
.track_storage_egress_size(file_stream.content_length as u64);
599609
zip_snapshot_upload
600610
.stream_full_file(path, file_stream.stream)

crates/application/src/snapshot_import.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use common::{
5757
FieldName,
5858
MemberId,
5959
ObjectKey,
60+
StorageUuid,
6061
TableName,
6162
UdfIdentifier,
6263
},
@@ -112,7 +113,6 @@ use model::{
112113
DeploymentAuditLogModel,
113114
},
114115
file_storage::{
115-
types::StorageUuid,
116116
FILE_STORAGE_TABLE,
117117
FILE_STORAGE_VIRTUAL_TABLE,
118118
},
@@ -1902,7 +1902,7 @@ async fn import_storage_table<RT: Runtime>(
19021902
let content_length = metadata.size.map(|size| ContentLength(size as u64));
19031903
let content_type = metadata
19041904
.content_type
1905-
.map(|content_type| anyhow::Ok(ContentType::from(mime::Mime::from_str(&content_type)?)))
1905+
.map(|content_type| anyhow::Ok(ContentType::from_str(&content_type)?))
19061906
.transpose()
19071907
.map_err(|e| ImportError::InvalidConvexValue(lineno, e))?;
19081908
let sha256 = metadata
@@ -1912,7 +1912,9 @@ async fn import_storage_table<RT: Runtime>(
19121912
.map_err(|e| ImportError::InvalidConvexValue(lineno, e))?;
19131913
let storage_id = metadata
19141914
.internal_id
1915-
.map(|storage_id| StorageUuid::from_str(&storage_id))
1915+
.map(|storage_id| {
1916+
StorageUuid::from_str(&storage_id).context("Couldn't parse storage_id")
1917+
})
19161918
.transpose()
19171919
.map_err(|e| ImportError::InvalidConvexValue(lineno, e))?;
19181920
let creation_time = metadata
@@ -2003,8 +2005,13 @@ async fn import_storage_table<RT: Runtime>(
20032005
},
20042006
)
20052007
.await?;
2008+
let content_type = entry
2009+
.content_type
2010+
.as_ref()
2011+
.map(|ct| ct.parse())
2012+
.transpose()?;
20062013
usage
2007-
.track_storage_call("snapshot_import")
2014+
.track_storage_call("snapshot_import", Some(entry.storage_id), content_type)
20082015
.track_storage_ingress_size(file_size);
20092016
num_files += 1;
20102017
if let Some(import_id) = import_id {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use uuid::Uuid;
2+
use value::ConvexValue;
3+
4+
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
5+
#[derive(
6+
Clone, Debug, Eq, PartialEq, Ord, PartialOrd, derive_more::Display, derive_more::FromStr,
7+
)]
8+
pub struct StorageUuid(
9+
#[cfg_attr(
10+
any(test, feature = "testing"),
11+
proptest(strategy = "Uuid::new_v4 as fn() -> Uuid")
12+
)]
13+
Uuid,
14+
);
15+
16+
impl From<Uuid> for StorageUuid {
17+
fn from(u: Uuid) -> Self {
18+
Self(u)
19+
}
20+
}
21+
22+
impl TryFrom<StorageUuid> for ConvexValue {
23+
type Error = anyhow::Error;
24+
25+
fn try_from(s: StorageUuid) -> anyhow::Result<Self> {
26+
s.to_string().try_into()
27+
}
28+
}
29+
30+
impl TryFrom<ConvexValue> for StorageUuid {
31+
type Error = anyhow::Error;
32+
33+
fn try_from(v: ConvexValue) -> anyhow::Result<Self> {
34+
match v {
35+
ConvexValue::String(s) => Ok(StorageUuid(Uuid::try_parse(&s)?)),
36+
_ => anyhow::bail!("Can only convert Value::String to StorageUuid"),
37+
}
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use proptest::prelude::*;
44+
use value::ConvexValue;
45+
46+
use crate::{
47+
testing::assert_roundtrips,
48+
types::StorageUuid,
49+
};
50+
51+
proptest! {
52+
#![proptest_config(
53+
ProptestConfig { failure_persistence: None, ..ProptestConfig::default() }
54+
)]
55+
56+
#[test]
57+
fn test_storage_roundtrip(v in any::<StorageUuid>()) {
58+
assert_roundtrips::<StorageUuid, ConvexValue>(v);
59+
}
60+
61+
}
62+
}

crates/common/src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod actions;
1818
mod admin_key;
1919
mod backend_state;
2020
mod environment_variables;
21+
mod file_storage;
2122
mod functions;
2223
mod index;
2324
mod maybe_value;
@@ -52,6 +53,7 @@ pub use environment_variables::{
5253
EnvironmentVariable,
5354
ENV_VAR_LIMIT,
5455
};
56+
pub use file_storage::StorageUuid;
5557
pub use functions::{
5658
AllowedVisibility,
5759
FunctionCaller,

crates/events/src/usage.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ pub enum UsageEvent {
6464
/// snapshot import/export)
6565
StorageCall {
6666
id: String,
67+
storage_id: Option<String>,
6768
call: String,
69+
content_type: Option<String>,
6870
},
6971
/// Bandwidth from a storage call outside of a user function (e.g. snapshot
7072
/// import/export).

crates/file_storage/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ headers = { workspace = true }
1919
keybroker = { path = "../keybroker" }
2020
maplit = { workspace = true }
2121
metrics = { path = "../metrics" }
22-
mime = { workspace = true }
2322
model = { path = "../model" }
2423
storage = { path = "../storage" }
2524
tracing = { workspace = true }

crates/file_storage/src/core.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use common::{
1313
UnixTimestamp,
1414
},
1515
sha256::Sha256Digest,
16-
types::ConvexOrigin,
16+
types::{
17+
ConvexOrigin,
18+
StorageUuid,
19+
},
1720
};
1821
use database::Transaction;
1922
use errors::ErrorMetadata;
@@ -35,12 +38,8 @@ use keybroker::{
3538
KeyBroker,
3639
};
3740
use maplit::btreemap;
38-
use mime::Mime;
3941
use model::file_storage::{
40-
types::{
41-
FileStorageEntry,
42-
StorageUuid,
43-
},
42+
types::FileStorageEntry,
4443
BatchKey,
4544
FileStorageId,
4645
FileStorageModel,
@@ -223,17 +222,14 @@ impl<RT: Runtime> TransactionalFileStorage<RT> {
223222
get_file_type: GetFileType,
224223
) -> anyhow::Result<FileRangeStream> {
225224
let FileStorageEntry {
226-
storage_id: _,
225+
storage_id,
227226
storage_key,
228227
sha256: _,
229228
size,
230229
content_type,
231230
} = file;
232231

233-
let content_type = match content_type {
234-
None => None,
235-
Some(ct) => Some(ct.parse::<Mime>()?.into()),
236-
};
232+
let content_type = content_type.as_ref().map(|ct| ct.parse()).transpose()?;
237233

238234
let storage_get_stream = self
239235
.storage
@@ -244,7 +240,8 @@ impl<RT: Runtime> TransactionalFileStorage<RT> {
244240
let stream = storage_get_stream.stream;
245241
let content_length = ContentLength(storage_get_stream.content_length as u64);
246242

247-
let call_tracker = usage_tracker.track_storage_call("get range");
243+
let call_tracker =
244+
usage_tracker.track_storage_call("get range", Some(storage_id), content_type.clone());
248245

249246
Ok(FileRangeStream {
250247
content_length,
@@ -402,9 +399,16 @@ impl<RT: Runtime> FileStorage<RT> {
402399
entry: FileStorageEntry,
403400
usage_tracker: &dyn StorageUsageTracker,
404401
) -> anyhow::Result<DeveloperDocumentId> {
402+
let storage_id = entry.storage_id.clone();
403+
let size = entry.size;
404+
let content_type = entry
405+
.content_type
406+
.as_ref()
407+
.map(|ct| ct.parse())
408+
.transpose()?;
409+
405410
// Start/Complete transaction after the slow upload process
406411
// to avoid OCC risk.
407-
let size = entry.size;
408412
let mut tx = self.database.begin(Identity::system()).await?;
409413
let virtual_id = self
410414
.transactional_file_storage
@@ -415,7 +419,7 @@ impl<RT: Runtime> FileStorage<RT> {
415419
.await?;
416420

417421
usage_tracker
418-
.track_storage_call("store")
422+
.track_storage_call("store", Some(storage_id), content_type)
419423
.track_storage_ingress_size(size as u64);
420424
Ok(virtual_id)
421425
}

crates/isolate/src/environment/action/storage.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ impl<RT: Runtime> TaskExecutor<RT> {
6969

7070
let entry = self
7171
.file_storage
72-
.upload_file(content_length, content_type, body_stream, digest)
72+
.upload_file(content_length, content_type.clone(), body_stream, digest)
7373
.await?;
74+
let storage_id = entry.storage_id.clone();
7475
let size = entry.size;
75-
let storage_id = self
76+
let storage_doc_id = self
7677
.action_callbacks
7778
.storage_store_file_entry(self.identity.clone(), self.component_id(), entry)
7879
.await?;
7980

8081
self.usage_tracker
81-
.track_storage_call("store")
82+
.track_storage_call("store", Some(storage_id), content_type)
8283
.track_storage_ingress_size(size as u64);
8384

84-
Ok(storage_id)
85+
Ok(storage_doc_id)
8586
}
8687

8788
#[convex_macro::instrument_future]

crates/model/src/file_storage/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use common::{
2323
types::{
2424
GenericIndexName,
2525
IndexName,
26+
StorageUuid,
2627
},
2728
virtual_system_mapping::VirtualSystemDocMapper,
2829
};
@@ -56,10 +57,7 @@ use value::{
5657

5758
use self::virtual_table::FileStorageDocMapper;
5859
use crate::{
59-
file_storage::types::{
60-
FileStorageEntry,
61-
StorageUuid,
62-
},
60+
file_storage::types::FileStorageEntry,
6361
SystemIndex,
6462
SystemTable,
6563
};

0 commit comments

Comments
 (0)