Skip to content

Commit f8b67aa

Browse files
committedApr 11, 2025
More refactoring/fixing of op name/op sig/stats report key stuff
1 parent 94d1265 commit f8b67aa

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed
 

‎apollo-router/src/apollo_studio_interop/mod.rs

+50-48
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ pub(crate) struct UsageReportingOperationDetails {
175175
referenced_fields_by_type: HashMap<String, ReferencedFieldsForType>,
176176
}
177177

178+
impl UsageReportingOperationDetails {
179+
fn operation_name_or_default(&self) -> String {
180+
self.operation_name.as_deref().unwrap_or("").to_string()
181+
}
182+
183+
fn operation_sig_or_default(&self) -> String {
184+
self.operation_signature.as_deref().unwrap_or("").to_string()
185+
}
186+
187+
fn get_signature_and_operation(&self) -> String {
188+
let op_name = self.operation_name.as_deref().unwrap_or("-").to_string();
189+
let op_sig = self
190+
.operation_signature
191+
.as_deref()
192+
.unwrap_or("")
193+
.to_string();
194+
format!("# {}\n{}", op_name, op_sig)
195+
}
196+
}
197+
178198
/// UsageReporting fields, that will be used to send stats to uplink/studio
179199
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
180200
#[serde(rename_all = "camelCase")]
@@ -203,7 +223,7 @@ impl UsageReporting {
203223
}
204224

205225
/// The `stats_report_key` is a unique identifier derived from schema and query.
206-
/// Metric data sent to Studio must be aggregated via grouped key of
226+
/// Metric data sent to Studio must be aggregated via grouped key of
207227
/// (`client_name`, `client_version`, `stats_report_key`).
208228
/// For errors, the report key is of the form "## <error name>\n".
209229
/// For operations not requested by PQ, the report key is of the form "# <op name>\n<op sig>".
@@ -215,62 +235,38 @@ impl UsageReporting {
215235
/// the "operation signature".
216236
pub(crate) fn get_stats_report_key(&self) -> String {
217237
match self {
218-
UsageReporting::Operation { .. } | UsageReporting::Error { .. } => {
219-
self.get_signature_and_operation()
220-
},
238+
UsageReporting::Operation(operation_details) => {
239+
operation_details.get_signature_and_operation()
240+
}
241+
UsageReporting::Error(error_key) => {
242+
format!("## {}\n", error_key)
243+
}
221244
UsageReporting::PersistedQuery {
245+
operation_details,
222246
persisted_query_id, ..
223247
} => {
224248
let string_to_hash = format!(
225-
"{}\n{}",
249+
"{}\n{}\n{}",
226250
persisted_query_id,
227-
self.get_operation_signature()
251+
operation_details.operation_name_or_default(),
252+
operation_details.operation_sig_or_default()
228253
);
229254
format!("pq# {}", Self::hash_string(&string_to_hash))
230255
}
231256
}
232257
}
233258

234-
pub(crate) fn get_operation_signature(&self) -> String {
235-
match self {
259+
pub(crate) fn get_operation_id(&self) -> String {
260+
let string_to_hash = match self {
236261
UsageReporting::Operation(operation_details)
237262
| UsageReporting::PersistedQuery {
238263
operation_details, ..
239-
} => operation_details
240-
.operation_signature
241-
.clone()
242-
.unwrap_or("".to_string()),
243-
UsageReporting::Error { .. } => "".to_string(),
244-
}
245-
}
246-
247-
fn get_signature_and_operation(&self) -> String {
248-
match self {
249-
UsageReporting::Operation(operation_details) | UsageReporting::PersistedQuery { operation_details, .. } => {
250-
let op_name = operation_details
251-
.operation_name
252-
.as_deref()
253-
.unwrap_or("-")
254-
.to_string();
255-
format!(
256-
"# {}\n{}",
257-
op_name,
258-
self.get_operation_signature(),
259-
)
260-
},
261-
UsageReporting::Error(error_key) => format!("## {}\n", error_key),
262-
}
263-
}
264-
265-
pub(crate) fn get_operation_id(&self) -> String {
266-
Self::hash_string(&self.get_signature_and_operation())
267-
}
268-
269-
fn hash_string(string_to_hash: &String) -> String {
270-
let mut hasher = sha1::Sha1::new();
271-
hasher.update(string_to_hash.as_bytes());
272-
let result = hasher.finalize();
273-
hex::encode(result)
264+
} => operation_details.get_signature_and_operation(),
265+
UsageReporting::Error(error_key) => {
266+
format!("# # {}\n", error_key)
267+
}
268+
};
269+
Self::hash_string(&string_to_hash)
274270
}
275271

276272
pub(crate) fn get_operation_name(&self) -> String {
@@ -279,9 +275,7 @@ impl UsageReporting {
279275
| UsageReporting::PersistedQuery {
280276
operation_details, ..
281277
} => operation_details
282-
.operation_name
283-
.clone()
284-
.unwrap_or("".to_string()),
278+
.operation_name_or_default(),
285279
UsageReporting::Error(error_key) => format!("# {}", error_key),
286280
}
287281
}
@@ -299,16 +293,24 @@ impl UsageReporting {
299293
pub(crate) fn get_query_metadata(&self) -> Option<QueryMetadata> {
300294
match self {
301295
UsageReporting::PersistedQuery {
296+
operation_details,
302297
persisted_query_id, ..
303298
} => Some(QueryMetadata {
304-
name: self.get_operation_name(),
305-
signature: self.get_operation_signature(),
299+
name: operation_details.operation_name_or_default(),
300+
signature: operation_details.operation_sig_or_default(),
306301
pq_id: persisted_query_id.clone(),
307302
}),
308303
// For now we only want to populate query metadata for PQ operations
309304
UsageReporting::Operation { .. } | UsageReporting::Error { .. } => None,
310305
}
311306
}
307+
308+
fn hash_string(string_to_hash: &String) -> String {
309+
let mut hasher = sha1::Sha1::new();
310+
hasher.update(string_to_hash.as_bytes());
311+
let result = hasher.finalize();
312+
hex::encode(result)
313+
}
312314
}
313315

314316
/// A list of fields that will be resolved for a given type

‎apollo-router/src/apollo_studio_interop/tests.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,12 @@ fn test_get_stats_report_key_and_metadata() {
351351
persisted_query_id: "SomePqId".into(),
352352
};
353353
assert_eq!(
354-
"pq# SomePqId",
355-
usage_reporting_for_pq.get_stats_report_key()
354+
"pq# ",
355+
usage_reporting_for_pq
356+
.get_stats_report_key()
357+
.chars()
358+
.take(4)
359+
.collect::<String>()
356360
);
357361
assert_eq!(
358362
Some(QueryMetadata {
@@ -420,6 +424,14 @@ fn test_get_stats_report_key_uses_distinct_keys_for_pq_operations() {
420424
usage_reporting_op_1_pq_1_again.get_stats_report_key()
421425
);
422426

427+
let usage_reporting_op_1_pq_1_different_name = UsageReporting::PersistedQuery {
428+
operation_details: UsageReportingOperationDetails {
429+
operation_name: Some("DifferentName".into()),
430+
operation_signature: Some("query SomeQuery1{thing{id}}".into()),
431+
referenced_fields_by_type: HashMap::new(),
432+
},
433+
persisted_query_id: "SomePqId1".into(),
434+
};
423435
let usage_reporting_op_2_pq_1 = UsageReporting::PersistedQuery {
424436
operation_details: UsageReportingOperationDetails {
425437
operation_name: Some("SomeQuery2".into()),
@@ -457,6 +469,7 @@ fn test_get_stats_report_key_uses_distinct_keys_for_pq_operations() {
457469

458470
let stats_report_keys = [
459471
usage_reporting_op_1_pq_1,
472+
usage_reporting_op_1_pq_1_different_name,
460473
usage_reporting_op_2_pq_1,
461474
usage_reporting_op_1_pq_2,
462475
usage_reporting_op_2_pq_2,

0 commit comments

Comments
 (0)
Failed to load comments.