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

async-nats: Support sample_freq values containing a % #1354

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 13 additions & 3 deletions async-nats/src/jetstream/consumer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub struct Config {
/// What percentage of acknowledgments should be samples for observability, 0-100
#[serde(
rename = "sample_freq",
with = "from_str",
with = "sample_freq_deser",
default,
skip_serializing_if = "is_default"
)]
Expand Down Expand Up @@ -433,15 +433,25 @@ fn is_default<T: Default + Eq>(t: &T) -> bool {
t == &T::default()
}

pub(crate) mod from_str {
pub(crate) mod sample_freq_deser {
pub(crate) fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: std::str::FromStr,
T::Err: std::fmt::Display,
D: serde::Deserializer<'de>,
{
let s = <String as serde::Deserialize>::deserialize(deserializer)?;
T::from_str(&s).map_err(serde::de::Error::custom)

let mut spliterator = s.split('%');
match (spliterator.next(), spliterator.next()) {
// No percentage occurred, parse as number
(Some(number), None) => T::from_str(number).map_err(serde::de::Error::custom),
// A percentage sign occurred right at the end
(Some(number), Some("")) => T::from_str(number).map_err(serde::de::Error::custom),
_ => Err(serde::de::Error::custom(format!(
"Malformed sample frequency: {s}"
))),
}
}

pub(crate) fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
4 changes: 2 additions & 2 deletions async-nats/src/jetstream/consumer/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ pub struct OrderedConfig {
/// What percentage of acknowledgments should be samples for observability, 0-100
#[serde(
rename = "sample_freq",
with = "super::from_str",
with = "super::sample_freq_deser",
default,
skip_serializing_if = "is_default"
)]
Expand Down Expand Up @@ -2051,7 +2051,7 @@ pub struct Config {
/// What percentage of acknowledgments should be samples for observability, 0-100
#[serde(
rename = "sample_freq",
with = "super::from_str",
with = "super::sample_freq_deser",
default,
skip_serializing_if = "is_default"
)]
Expand Down
4 changes: 2 additions & 2 deletions async-nats/src/jetstream/consumer/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub struct Config {
/// What percentage of acknowledgments should be samples for observability, 0-100
#[serde(
rename = "sample_freq",
with = "super::from_str",
with = "super::sample_freq_deser",
default,
skip_serializing_if = "is_default"
)]
Expand Down Expand Up @@ -389,7 +389,7 @@ pub struct OrderedConfig {
/// What percentage of acknowledgments should be samples for observability, 0-100
#[serde(
rename = "sample_freq",
with = "super::from_str",
with = "super::sample_freq_deser",
default,
skip_serializing_if = "is_default"
)]
Expand Down
25 changes: 25 additions & 0 deletions async-nats/tests/jetstream_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,31 @@ mod jetstream {

assert_eq!(100, consumer.cached_info().config.sample_frequency);
}

// `sample_frequency` can contain a %, which occurs in the wild
// when managing NATS resources using Terraform / OpenTofu's NATS JetStream provider.
{
let stream = &stream.cached_info().config.name;
let consumer = serde_json::json!({
"stream_name": stream,
"config": {
"name": "consumer",
"sample_freq": "10%",
},
});

let response: Response<Info> = js
.request(format!("CONSUMER.CREATE.{}", stream), &consumer)
.await
.unwrap();

match response {
Response::Ok(info) => {
assert_eq!(info.config.sample_frequency, 10);
}
Response::Err { error } => panic!("expected ok response, got: {:?}", error),
}
}
}

#[tokio::test]
Expand Down
Loading