From 26cb6d355f216615d4910ee3b0fa95a41e60f9f3 Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki Date: Tue, 2 Apr 2024 17:39:25 -0400 Subject: [PATCH 1/7] extract cache hit and cache size --- .../src/normalize/span/tag_extraction.rs | 114 ++++++++++++++++++ relay-event-schema/src/protocol/span.rs | 12 +- ...t__tests__extract_span_metrics_mobile.snap | 4 + 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index 2fbcfa374f..c7ecc2b0ed 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -66,6 +66,8 @@ pub enum SpanTagKey { /// The start type of the application when the span occurred. AppStartType, ReplayId, + CacheHit, + CacheItemSize, } impl SpanTagKey { @@ -104,6 +106,8 @@ impl SpanTagKey { SpanTagKey::TimeToInitialDisplay => "ttid", SpanTagKey::FileExtension => "file_extension", SpanTagKey::MainThread => "main_thread", + SpanTagKey::CacheHit => "cache.hit", + SpanTagKey::CacheItemSize => "cache.item_size", SpanTagKey::OsName => "os.name", SpanTagKey::AppStartType => "app_start_type", SpanTagKey::ReplayId => "replay_id", @@ -406,6 +410,18 @@ pub fn extract_tags( } } + if span_op.starts_with("cache.") { + if let Some(cache_hit) = span.data.value().and_then(|data| data.cache_hit.value()) { + match cache_hit { + Value::Bool(true) => span_tags.insert(SpanTagKey::CacheHit, "true".to_owned()), + Value::Bool(false) => { + span_tags.insert(SpanTagKey::CacheHit, "false".to_owned()) + } + _ => None, + }; + } + } + if let Some(scrubbed_desc) = scrubbed_description { // Truncating the span description's tag value is, for now, // a temporary solution to not get large descriptions dropped. The @@ -559,6 +575,27 @@ pub fn extract_measurements(span: &mut Span) { return; }; + if span_op.starts_with("cache.") { + if let Some(data) = span.data.value() { + if let Some(value) = match &data.cache_item_size.value() { + Some(Value::F64(f)) => Some(*f), + Some(Value::I64(i)) => Some(*i as f64), + Some(Value::U64(u)) => Some(*u as f64), + _ => None, + } { + let measurements = span.measurements.get_or_insert_with(Default::default); + measurements.insert( + SpanTagKey::CacheItemSize.sentry_tag_key().into(), + Measurement { + value: value.into(), + unit: MetricUnit::Information(InformationUnit::Byte).into(), + } + .into(), + ); + } + } + } + if span_op.starts_with("resource.") { if let Some(data) = span.data.value() { for (field, key) in [ @@ -1283,6 +1320,83 @@ LIMIT 1 assert!(!tags_3.contains_key("raw_domain")); } + #[test] + fn test_cache_extraction() { + let json = r#" + { + "spans": [ + { + "timestamp": 1694732408.3145, + "start_timestamp": 1694732407.8367, + "exclusive_time": 477.800131, + "description": "get my_key", + "op": "cache.get_item", + "span_id": "97c0ef9770a02f9d", + "parent_span_id": "9756d8d7b2b364ff", + "trace_id": "77aeb1c16bb544a4a39b8d42944947a3", + "data": { + "cache.hit": true, + "cache.item_size": 8, + "thread.id": "6286962688", + "thread.name": "Thread-4 (process_request_thread)" + + }, + "hash": "e2fae740cccd3781" + }, + { + "timestamp": 1694732409.3145, + "start_timestamp": 1694732408.8367, + "exclusive_time": 477.800131, + "description": "get my_key", + "op": "cache.get_item", + "span_id": "97c0ef9770a02f9d", + "parent_span_id": "9756d8d7b2b364ff", + "trace_id": "77aeb1c16bb544a4a39b8d42944947a3", + "data": { + "cache.hit": false, + "cache.item_size": 8, + "thread.id": "6286962688", + "thread.name": "Thread-4 (process_request_thread)" + + }, + "hash": "e2fae740cccd3781" + } + ] + } + "#; + + let mut event = Annotated::::from_json(json) + .unwrap() + .into_value() + .unwrap(); + + extract_span_tags_from_event(&mut event, 200); + + let span_1 = &event.spans.value().unwrap()[0]; + let span_2 = &event.spans.value().unwrap()[1]; + + let tags_1 = get_value!(span_1.sentry_tags).unwrap(); + let tags_2 = get_value!(span_2.sentry_tags).unwrap(); + let measurements_1 = span_1.value().unwrap().measurements.value().unwrap(); + println!("MEASUREMENTS!"); + println!("{:?}", measurements_1); + + assert_eq!(tags_1.get("cache.hit").unwrap().as_str(), Some("true")); + assert_eq!(tags_2.get("cache.hit").unwrap().as_str(), Some("false")); + assert_debug_snapshot!(measurements_1, @r###" + Measurements( + { + "cache.item_size": Measurement { + value: 8.0, + unit: Information( + Byte, + ), + }, + }, + ) + "###); + } + #[test] fn test_mobile_specific_tags() { let json = r#" diff --git a/relay-event-schema/src/protocol/span.rs b/relay-event-schema/src/protocol/span.rs index 9241b7bd2b..61679b6d89 100644 --- a/relay-event-schema/src/protocol/span.rs +++ b/relay-event-schema/src/protocol/span.rs @@ -262,10 +262,18 @@ pub struct SpanData { #[metastructure(field = "resource.render_blocking_status")] pub resource_render_blocking_status: Annotated, - /// Name of the database host. + /// Name of the web server host. #[metastructure(field = "server.address")] pub server_address: Annotated, + /// Wether cache was hit or miss on a read operation. + #[metastructure(field = "cache.hit")] + pub cache_hit: Annotated, + + /// The size of the cache item. + #[metastructure(field = "cache.item_size")] + pub cache_item_size: Annotated, + /// The status HTTP response. #[metastructure(field = "http.response.status_code", legacy_alias = "status_code")] pub http_response_status_code: Annotated, @@ -625,6 +633,8 @@ mod tests { http_response_transfer_size: ~, resource_render_blocking_status: ~, server_address: ~, + cache_hit: ~, + cache_item_size: ~, http_response_status_code: ~, ai_input_messages: ~, ai_completion_tokens_used: ~, diff --git a/relay-server/src/metrics_extraction/snapshots/relay_server__metrics_extraction__event__tests__extract_span_metrics_mobile.snap b/relay-server/src/metrics_extraction/snapshots/relay_server__metrics_extraction__event__tests__extract_span_metrics_mobile.snap index 01d216f029..830d3a1512 100644 --- a/relay-server/src/metrics_extraction/snapshots/relay_server__metrics_extraction__event__tests__extract_span_metrics_mobile.snap +++ b/relay-server/src/metrics_extraction/snapshots/relay_server__metrics_extraction__event__tests__extract_span_metrics_mobile.snap @@ -324,6 +324,8 @@ expression: "(&event.value().unwrap().spans, metrics)" http_response_transfer_size: ~, resource_render_blocking_status: ~, server_address: ~, + cache_hit: ~, + cache_item_size: ~, http_response_status_code: ~, ai_input_messages: ~, ai_completion_tokens_used: ~, @@ -403,6 +405,8 @@ expression: "(&event.value().unwrap().spans, metrics)" http_response_transfer_size: ~, resource_render_blocking_status: ~, server_address: ~, + cache_hit: ~, + cache_item_size: ~, http_response_status_code: ~, ai_input_messages: ~, ai_completion_tokens_used: ~, From 83e0322a6f5a5e6bb2983b14323c754422dd74d8 Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki <44422760+DominikB2014@users.noreply.github.com> Date: Wed, 3 Apr 2024 09:34:28 -0400 Subject: [PATCH 2/7] Update tag_extraction.rs Co-authored-by: Joris Bayer --- .../src/normalize/span/tag_extraction.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index c7ecc2b0ed..ee70408832 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -411,14 +411,9 @@ pub fn extract_tags( } if span_op.starts_with("cache.") { - if let Some(cache_hit) = span.data.value().and_then(|data| data.cache_hit.value()) { - match cache_hit { - Value::Bool(true) => span_tags.insert(SpanTagKey::CacheHit, "true".to_owned()), - Value::Bool(false) => { - span_tags.insert(SpanTagKey::CacheHit, "false".to_owned()) - } - _ => None, - }; + if let Some(Value::Bool(cache_hit)) = span.data.value().and_then(|data| data.cache_hit.value()) { + let tag_value = if cache_hit { "true" } else { "false" }; + span_tags.insert(SpanTagKey::CacheHit, tag_value.to_owned()); } } From 296efee7ef721ac5298d164a8ea44c5cb342c8fe Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki <44422760+DominikB2014@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:34:18 -0400 Subject: [PATCH 3/7] Apply suggestions from code review Co-authored-by: Joris Bayer Co-authored-by: Iker Barriocanal <32816711+iker-barriocanal@users.noreply.github.com> --- .../src/normalize/span/tag_extraction.rs | 5 +---- relay-event-schema/src/protocol/span.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index ee70408832..1dc271a97a 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -107,7 +107,6 @@ impl SpanTagKey { SpanTagKey::FileExtension => "file_extension", SpanTagKey::MainThread => "main_thread", SpanTagKey::CacheHit => "cache.hit", - SpanTagKey::CacheItemSize => "cache.item_size", SpanTagKey::OsName => "os.name", SpanTagKey::AppStartType => "app_start_type", SpanTagKey::ReplayId => "replay_id", @@ -580,7 +579,7 @@ pub fn extract_measurements(span: &mut Span) { } { let measurements = span.measurements.get_or_insert_with(Default::default); measurements.insert( - SpanTagKey::CacheItemSize.sentry_tag_key().into(), + "cache.item_size".to_owned(), Measurement { value: value.into(), unit: MetricUnit::Information(InformationUnit::Byte).into(), @@ -1373,8 +1372,6 @@ LIMIT 1 let tags_1 = get_value!(span_1.sentry_tags).unwrap(); let tags_2 = get_value!(span_2.sentry_tags).unwrap(); let measurements_1 = span_1.value().unwrap().measurements.value().unwrap(); - println!("MEASUREMENTS!"); - println!("{:?}", measurements_1); assert_eq!(tags_1.get("cache.hit").unwrap().as_str(), Some("true")); assert_eq!(tags_2.get("cache.hit").unwrap().as_str(), Some("false")); diff --git a/relay-event-schema/src/protocol/span.rs b/relay-event-schema/src/protocol/span.rs index 61679b6d89..9ecb663753 100644 --- a/relay-event-schema/src/protocol/span.rs +++ b/relay-event-schema/src/protocol/span.rs @@ -266,7 +266,7 @@ pub struct SpanData { #[metastructure(field = "server.address")] pub server_address: Annotated, - /// Wether cache was hit or miss on a read operation. + /// Whether cache was hit or miss on a read operation. #[metastructure(field = "cache.hit")] pub cache_hit: Annotated, From 9cd769a79ed926431e196aa2f47347ed728b929f Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki Date: Wed, 3 Apr 2024 11:49:19 -0400 Subject: [PATCH 4/7] code review --- CHANGELOG.md | 1 + .../src/normalize/span/tag_extraction.rs | 68 +++++++++---------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fd3e32602..78fb1c919b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ **Internal**: +- Extract `cache.item_size` and `cache.hit` data into span indexed ([#3367]https://github.com/getsentry/relay/pull/3367) - Enable `db.redis` span metrics extraction. ([#3283](https://github.com/getsentry/relay/pull/3283)) - Add data categories for continuous profiling. ([#3284](https://github.com/getsentry/relay/pull/3284), [#3303](https://github.com/getsentry/relay/pull/3303)) - Apply rate limits to span metrics. ([#3255](https://github.com/getsentry/relay/pull/3255)) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index c7ecc2b0ed..66b0f9bbc8 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -1325,43 +1325,43 @@ LIMIT 1 let json = r#" { "spans": [ - { - "timestamp": 1694732408.3145, - "start_timestamp": 1694732407.8367, - "exclusive_time": 477.800131, - "description": "get my_key", - "op": "cache.get_item", - "span_id": "97c0ef9770a02f9d", - "parent_span_id": "9756d8d7b2b364ff", - "trace_id": "77aeb1c16bb544a4a39b8d42944947a3", - "data": { - "cache.hit": true, - "cache.item_size": 8, - "thread.id": "6286962688", - "thread.name": "Thread-4 (process_request_thread)" + { + "timestamp": 1694732408.3145, + "start_timestamp": 1694732407.8367, + "exclusive_time": 477.800131, + "description": "get my_key", + "op": "cache.get_item", + "span_id": "97c0ef9770a02f9d", + "parent_span_id": "9756d8d7b2b364ff", + "trace_id": "77aeb1c16bb544a4a39b8d42944947a3", + "data": { + "cache.hit": true, + "cache.item_size": 8, + "thread.id": "6286962688", + "thread.name": "Thread-4 (process_request_thread)" + }, + "hash": "e2fae740cccd3781" }, - "hash": "e2fae740cccd3781" - }, - { - "timestamp": 1694732409.3145, - "start_timestamp": 1694732408.8367, - "exclusive_time": 477.800131, - "description": "get my_key", - "op": "cache.get_item", - "span_id": "97c0ef9770a02f9d", - "parent_span_id": "9756d8d7b2b364ff", - "trace_id": "77aeb1c16bb544a4a39b8d42944947a3", - "data": { - "cache.hit": false, - "cache.item_size": 8, - "thread.id": "6286962688", - "thread.name": "Thread-4 (process_request_thread)" + { + "timestamp": 1694732409.3145, + "start_timestamp": 1694732408.8367, + "exclusive_time": 477.800131, + "description": "get my_key", + "op": "cache.get_item", + "span_id": "97c0ef9770a02f9d", + "parent_span_id": "9756d8d7b2b364ff", + "trace_id": "77aeb1c16bb544a4a39b8d42944947a3", + "data": { + "cache.hit": false, + "cache.item_size": 8, + "thread.id": "6286962688", + "thread.name": "Thread-4 (process_request_thread)" - }, - "hash": "e2fae740cccd3781" - } - ] + }, + "hash": "e2fae740cccd3781" + } + ] } "#; From e05426164176b9964ef97886dd406ff81a3ba3c7 Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki Date: Wed, 3 Apr 2024 11:53:08 -0400 Subject: [PATCH 5/7] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78fb1c919b..858b13cafc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,10 @@ - Drop `event_id` and `remote_addr` from all outcomes. ([#3319](https://github.com/getsentry/relay/pull/3319)) - Support for AI token metrics ([#3250](https://github.com/getsentry/relay/pull/3250)) - Accept integers in `event.user.username`. ([#3328](https://github.com/getsentry/relay/pull/3328)) +- Extract `cache.item_size` and `cache.hit` data into span indexed ([#3367]https://github.com/getsentry/relay/pull/3367) **Internal**: -- Extract `cache.item_size` and `cache.hit` data into span indexed ([#3367]https://github.com/getsentry/relay/pull/3367) - Enable `db.redis` span metrics extraction. ([#3283](https://github.com/getsentry/relay/pull/3283)) - Add data categories for continuous profiling. ([#3284](https://github.com/getsentry/relay/pull/3284), [#3303](https://github.com/getsentry/relay/pull/3303)) - Apply rate limits to span metrics. ([#3255](https://github.com/getsentry/relay/pull/3255)) From bf447c1c5ae25599c022cc80b67ea1911e26bebd Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki Date: Wed, 3 Apr 2024 11:54:03 -0400 Subject: [PATCH 6/7] formatting --- .../src/normalize/span/tag_extraction.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index c8084008bf..ce46494e38 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -410,8 +410,10 @@ pub fn extract_tags( } if span_op.starts_with("cache.") { - if let Some(Value::Bool(cache_hit)) = span.data.value().and_then(|data| data.cache_hit.value()) { - let tag_value = if cache_hit { "true" } else { "false" }; + if let Some(Value::Bool(cache_hit)) = + span.data.value().and_then(|data| data.cache_hit.value()) + { + let tag_value = if *cache_hit { "true" } else { "false" }; span_tags.insert(SpanTagKey::CacheHit, tag_value.to_owned()); } } From 004719c99d4e84302562a69c63775d7716e1b354 Mon Sep 17 00:00:00 2001 From: Dominik Buszowiecki Date: Wed, 3 Apr 2024 11:55:29 -0400 Subject: [PATCH 7/7] fix test --- relay-event-normalization/src/normalize/span/tag_extraction.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index ce46494e38..797ef3ec44 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -67,7 +67,6 @@ pub enum SpanTagKey { AppStartType, ReplayId, CacheHit, - CacheItemSize, } impl SpanTagKey {