Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dynamic_samping): Adjust sample rate by envelope header's sample_rate [INGEST-1395] #1327

Merged
merged 31 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a462262
wip
untitaker Jun 29, 2022
e59bf09
wip
untitaker Jun 29, 2022
0c007d7
Merge remote-tracking branch 'origin/master' into feat/client-sample-…
untitaker Jun 29, 2022
7eacb08
wip
untitaker Jun 29, 2022
440f71d
wip
untitaker Jun 29, 2022
cdd132f
wip
untitaker Jun 29, 2022
8b4d277
wip
untitaker Jun 29, 2022
a4bdeaf
wip
untitaker Jun 30, 2022
5c246d5
wip
untitaker Jun 30, 2022
505016c
wip
untitaker Jun 30, 2022
9c580bf
add changelog
untitaker Jun 30, 2022
b5fd88e
impl transaction sampling
untitaker Jun 30, 2022
07bfd6e
add more tests
untitaker Jun 30, 2022
c39496c
add warning
untitaker Jun 30, 2022
f0b4db3
fix compilation errors
untitaker Jun 30, 2022
a038fa2
move to serde-with
untitaker Jun 30, 2022
bb5518c
remove unused feature
untitaker Jun 30, 2022
9e900b2
add other attribute
untitaker Jun 30, 2022
bdc6a41
remove JSON support
untitaker Jul 1, 2022
87bd666
rename function
untitaker Jul 1, 2022
e63facf
move to store normalization
untitaker Jul 1, 2022
43d0ecc
Merge remote-tracking branch 'origin/master' into feat/client-sample-…
untitaker Jul 1, 2022
1c2ba7b
remove unused variable
untitaker Jul 1, 2022
b8d1396
Apply suggestions from code review
untitaker Jul 1, 2022
c0988a4
Update relay-sampling/src/lib.rs
untitaker Jul 1, 2022
3ae33d5
rewrite logic to be more readable
untitaker Jul 1, 2022
d07eba1
apply review feedback from iker
untitaker Jul 1, 2022
d7978a1
Update relay-general/src/store/mod.rs
untitaker Jul 4, 2022
f139143
apply review comments
untitaker Jul 4, 2022
0d709ac
insta approve
untitaker Jul 4, 2022
8677996
Merge remote-tracking branch 'origin/master' into feat/client-sample-…
untitaker Jul 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Features**:

- Extract metrics also from trace-sampled transactions. ([#1317](https://github.com/getsentry/relay/pull/1317))
- Adjust sample rate by envelope header's sample_rate. ([#1327](https://github.com/getsentry/relay/pull/1327))

**Bug Fixes**:

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions relay-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ relay-log = { path = "../relay-log" }
sentry-types = "0.20.0"
schemars = { version = "0.8.1", features = ["uuid", "chrono"], optional = true }
serde = { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.55"

[dev-dependencies]
serde_test = "1.0.125"
Expand Down
2 changes: 2 additions & 0 deletions relay-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod constants;
mod glob;
mod project;
mod retry;
mod serde;
mod time;
mod utils;

Expand All @@ -21,6 +22,7 @@ pub use crate::constants::*;
pub use crate::glob::*;
pub use crate::project::*;
pub use crate::retry::*;
pub use crate::serde::*;
pub use crate::time::*;
pub use crate::utils::*;

Expand Down
71 changes: 71 additions & 0 deletions relay-common/src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};

/// A wrapper type that is able to deserialize its inner value from a nested JSON string.
///
/// For example, this struct:
///
/// ```rust
/// # use relay_common::JsonStringifiedValue;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct TraceContext {
/// sample_rate: JsonStringifiedValue<f64>,
/// }
/// ```
///
/// ...deserializes from `{"sample_rate": "1.0"}` and `{"sample_rate": 1.0}`, and serializes to
/// `{"sample_rate": "1.0"}`
///
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct JsonStringifiedValue<T>(pub T);

impl<'de, T> Deserialize<'de> for JsonStringifiedValue<T>
where
for<'de2> T: Deserialize<'de2>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Helper<T> {
Verbatim(T),
// can't change this to borrowed string, otherwise deserialization within an
// ErrorBoundary silently fails for some reason
String(String),
}

let helper = Helper::<T>::deserialize(deserializer)?;

match helper {
Helper::Verbatim(value) => Ok(JsonStringifiedValue(value)),
Helper::String(value) => {
let rv = serde_json::from_str(&value)
.map_err(|e| serde::de::Error::custom(e.to_string()))?;
Ok(JsonStringifiedValue(rv))
}
}
}
}

impl<T: Serialize> Serialize for JsonStringifiedValue<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let string =
serde_json::to_string(&self.0).map_err(|e| serde::ser::Error::custom(e.to_string()))?;
string.serialize(serializer)
}
}

#[test]
fn test_basic() {
let value = serde_json::from_str::<JsonStringifiedValue<f32>>("42.0").unwrap();
assert_eq!(value.0, 42.0);
let value = serde_json::from_str::<JsonStringifiedValue<f32>>("\"42.0\"").unwrap();
assert_eq!(value.0, 42.0);
}
2 changes: 1 addition & 1 deletion relay-general/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ relay-general-derive = { path = "derive" }
schemars = { version = "0.8.1", features = ["uuid", "chrono"], optional = true }
sentry-release-parser = { version = "1.3.1" }
serde = { version = "1.0.114", features = ["derive"] }
serde_json = "1.0.55"
serde_json = { version = "1.0.55", features = ["raw_value"] }
serde_urlencoded = "0.5.5"
sha-1 = "0.8.1"
smallvec = { version = "1.4.0", features = ["serde"] }
Expand Down
15 changes: 15 additions & 0 deletions relay-general/src/protocol/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ pub struct TraceContext {
/// excluding its immediate child spans.
pub exclusive_time: Annotated<f64>,

/// The SDK-side sample rate as reported in the envelope's `trace.sample_rate` header.
///
/// The server takes this field from envelope headers and writes it back into the event. SDKs
/// should not ever send this value.
pub client_sample_rate: Annotated<f64>,

/// Additional arbitrary fields for forwards compatibility.
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
pub other: Object<Value>,
Expand Down Expand Up @@ -697,6 +703,13 @@ impl Contexts {
.value_mut()
.get_or_insert_with(|| ContextInner(context_builder()))
}

pub fn get_context_mut<S>(&mut self, key: S) -> Option<&mut Context>
where
S: AsRef<str>,
{
Some(&mut self.get_mut(key.as_ref())?.value_mut().as_mut()?.0)
}
}

impl std::ops::Deref for Contexts {
Expand Down Expand Up @@ -933,6 +946,7 @@ fn test_trace_context_roundtrip() {
"op": "http",
"status": "ok",
"exclusive_time": 0.0,
"client_sample_rate": 0.5,
"other": "value",
"type": "trace"
}"#;
Expand All @@ -943,6 +957,7 @@ fn test_trace_context_roundtrip() {
op: Annotated::new("http".into()),
status: Annotated::new(SpanStatus::Ok),
exclusive_time: Annotated::new(0.0),
client_sample_rate: Annotated::new(0.5),
other: {
let mut map = Object::new();
map.insert(
Expand Down
10 changes: 10 additions & 0 deletions relay-general/tests/snapshots/test_fixtures__event_schema.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: relay-general/tests/test_fixtures.rs
assertion_line: 106
expression: event_json_schema()
---
{
Expand Down Expand Up @@ -2727,6 +2728,15 @@ expression: event_json_schema()
"trace_id"
],
"properties": {
"client_sample_rate": {
"description": " The SDK-side sample rate as reported in the envelope's `trace.sample_rate` header.\n\n The server takes this field from envelope headers and writes it back into the event. SDKs\n should not ever send this value.",
"default": null,
"type": [
"number",
"null"
],
"format": "double"
},
"exclusive_time": {
"description": " The amount of time in milliseconds spent in this transaction span,\n excluding its immediate child spans.",
"default": null,
Expand Down
Loading