Skip to content

Commit 7088932

Browse files
emmaling27Convex, Inc.
authored and
Convex, Inc.
committed
Add component_path to usage data sent from backend (#28031)
This PR adds `component_path` to usage events sent from backend. GitOrigin-RevId: fa4332926215bd33df427ce91799b5e9f1405a1b
1 parent 61d1a78 commit 7088932

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

crates/common/src/components/function_paths.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ impl CanonicalizedComponentFunctionPath {
9898
pub fn is_system(&self) -> bool {
9999
self.udf_path.is_system()
100100
}
101+
102+
pub fn into_component_and_udf_path(self) -> (ComponentPath, CanonicalizedUdfPath) {
103+
(self.component, self.udf_path)
104+
}
101105
}
102106

103107
impl From<CanonicalizedComponentFunctionPath> for ComponentFunctionPath {

crates/common/src/types/functions.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ pub enum UdfIdentifier {
111111
Cli(String),
112112
}
113113

114+
impl UdfIdentifier {
115+
pub fn into_component_and_udf_path(self) -> (Option<String>, String) {
116+
match self {
117+
UdfIdentifier::Function(path) => {
118+
let (component_path, udf_path) = path.clone().into_component_and_udf_path();
119+
(component_path.serialize(), udf_path.to_string())
120+
},
121+
UdfIdentifier::Http(_) | UdfIdentifier::Cli(_) => (None, self.to_string()),
122+
}
123+
}
124+
}
125+
114126
impl fmt::Display for UdfIdentifier {
115127
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116128
match self {

crates/events/src/testing.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl UsageCounterState {
9191
fn record_event(&mut self, event: UsageEvent) {
9292
match event {
9393
UsageEvent::FunctionCall {
94+
component_path,
9495
udf_id,
9596
tag,
9697
memory_megabytes,
@@ -100,7 +101,14 @@ impl UsageCounterState {
100101
..
101102
} => {
102103
if is_tracked {
103-
*self.recent_calls.entry(udf_id.to_string()).or_default() += 1;
104+
let fn_name = if let Some(mut component) = component_path {
105+
component.push('/');
106+
component.push_str(&udf_id);
107+
component
108+
} else {
109+
udf_id.clone()
110+
};
111+
*self.recent_calls.entry(fn_name).or_default() += 1;
104112
*self.recent_calls_by_tag.entry(tag).or_default() += 1;
105113

106114
// Convert into MB-milliseconds of compute time

crates/events/src/usage.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub enum UsageEvent {
2020
FunctionCall {
2121
// The ExecutionId of a particular UDF
2222
id: String,
23+
/// The path of a component. Uniquely identifies a component in a
24+
/// project.
25+
component_path: Option<String>,
2326
// The path / name of the UDF
2427
udf_id: String,
2528
// The type of the udf identifier (http, function, cli)
@@ -43,6 +46,7 @@ pub enum UsageEvent {
4346
/// invocation.
4447
FunctionStorageCalls {
4548
id: String,
49+
component_path: Option<String>,
4650
udf_id: String,
4751
call: String,
4852
count: u64,
@@ -51,6 +55,7 @@ pub enum UsageEvent {
5155
/// function invocation.
5256
FunctionStorageBandwidth {
5357
id: String,
58+
component_path: Option<String>,
5459
udf_id: String,
5560
ingress: u64,
5661
egress: u64,
@@ -70,13 +75,15 @@ pub enum UsageEvent {
7075
},
7176
DatabaseBandwidth {
7277
id: String,
78+
component_path: Option<String>,
7379
udf_id: String,
7480
table_name: String,
7581
ingress: u64,
7682
egress: u64,
7783
},
7884
VectorBandwidth {
7985
id: String,
86+
component_path: Option<String>,
8087
udf_id: String,
8188
table_name: String,
8289
ingress: u64,

crates/usage_tracking/src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ impl UsageCounter {
118118
UdfIdentifier::Http(_) => (true, "http"),
119119
UdfIdentifier::Cli(_) => (false, "cli"),
120120
};
121+
let (component_path, udf_id) = udf_path.clone().into_component_and_udf_path();
121122
usage_metrics.push(UsageEvent::FunctionCall {
122123
id: execution_id.to_string(),
123-
udf_id: udf_path.to_string(),
124+
component_path,
125+
udf_id,
124126
udf_id_type: udf_id_type.to_string(),
125127
tag: call_type.tag().to_string(),
126128
memory_megabytes: call_type.memory_megabytes(),
@@ -158,25 +160,29 @@ impl UsageCounter {
158160
usage_metrics: &mut Vec<UsageEvent>,
159161
) {
160162
// Merge the storage stats.
163+
let (component_path, udf_id) = udf_path.clone().into_component_and_udf_path();
161164
for (storage_api, function_count) in stats.storage_calls {
162165
usage_metrics.push(UsageEvent::FunctionStorageCalls {
163166
id: execution_id.to_string(),
164-
udf_id: udf_path.to_string(),
167+
component_path: component_path.clone(),
168+
udf_id: udf_id.clone(),
165169
call: storage_api,
166170
count: function_count,
167171
});
168172
}
169173
usage_metrics.push(UsageEvent::FunctionStorageBandwidth {
170174
id: execution_id.to_string(),
171-
udf_id: udf_path.to_string(),
175+
component_path: component_path.clone(),
176+
udf_id: udf_id.clone(),
172177
ingress: stats.storage_ingress_size,
173178
egress: stats.storage_egress_size,
174179
});
175180
// Merge "by table" bandwidth stats.
176181
for (table_name, ingress_size) in stats.database_ingress_size {
177182
usage_metrics.push(UsageEvent::DatabaseBandwidth {
178183
id: execution_id.to_string(),
179-
udf_id: udf_path.to_string(),
184+
component_path: component_path.clone(),
185+
udf_id: udf_id.clone(),
180186
table_name,
181187
ingress: ingress_size,
182188
egress: 0,
@@ -185,7 +191,8 @@ impl UsageCounter {
185191
for (table_name, egress_size) in stats.database_egress_size {
186192
usage_metrics.push(UsageEvent::DatabaseBandwidth {
187193
id: execution_id.to_string(),
188-
udf_id: udf_path.to_string(),
194+
component_path: component_path.clone(),
195+
udf_id: udf_id.clone(),
189196
table_name,
190197
ingress: 0,
191198
egress: egress_size,
@@ -194,7 +201,8 @@ impl UsageCounter {
194201
for (table_name, ingress_size) in stats.vector_ingress_size {
195202
usage_metrics.push(UsageEvent::VectorBandwidth {
196203
id: execution_id.to_string(),
197-
udf_id: udf_path.to_string(),
204+
component_path: component_path.clone(),
205+
udf_id: udf_id.clone(),
198206
table_name,
199207
ingress: ingress_size,
200208
egress: 0,
@@ -203,7 +211,8 @@ impl UsageCounter {
203211
for (table_name, egress_size) in stats.vector_egress_size {
204212
usage_metrics.push(UsageEvent::VectorBandwidth {
205213
id: execution_id.to_string(),
206-
udf_id: udf_path.to_string(),
214+
component_path: component_path.clone(),
215+
udf_id: udf_id.clone(),
207216
table_name,
208217
ingress: 0,
209218
egress: egress_size,

0 commit comments

Comments
 (0)