-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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(sink: amqp): add priority property #22243
Open
aramperes
wants to merge
5
commits into
vectordotdev:master
Choose a base branch
from
aramperes:sink-amqp-priority
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26047bd
feat(amqp sink): add priority property
aramperes dbc0af8
feat(sink: amqp): support templating for priority
aramperes cbe70c3
feat(sink: amqp): improve doc
aramperes cf3fea5
fix: allow config Template to be a constant number (u64, i64, f64)
aramperes 30f0cde
update tests: amqp::sink, crate::template
aramperes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Support was added for `priority` on the `amqp` sink, to set a priority on messages sent. | ||
The priority value can be templated to an integer between 0 and 255 (inclusive). | ||
|
||
authors: aramperes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
use crate::{amqp::AmqpConfig, sinks::prelude::*}; | ||
use lapin::{types::ShortString, BasicProperties}; | ||
use std::sync::Arc; | ||
use vector_lib::codecs::TextSerializerConfig; | ||
use vector_lib::{ | ||
codecs::TextSerializerConfig, | ||
internal_event::{error_stage, error_type}, | ||
}; | ||
|
||
use super::sink::AmqpSink; | ||
|
||
|
@@ -19,10 +22,13 @@ pub struct AmqpPropertiesConfig { | |
|
||
/// Expiration for AMQP messages (in milliseconds) | ||
pub(crate) expiration_ms: Option<u64>, | ||
|
||
/// Priority for AMQP messages. | ||
pub(crate) priority: Option<Template>, | ||
} | ||
|
||
impl AmqpPropertiesConfig { | ||
pub(super) fn build(&self) -> BasicProperties { | ||
pub(super) fn build(&self, event: &Event) -> Option<BasicProperties> { | ||
let mut prop = BasicProperties::default(); | ||
if let Some(content_type) = &self.content_type { | ||
prop = prop.with_content_type(ShortString::from(content_type.clone())); | ||
|
@@ -33,7 +39,36 @@ impl AmqpPropertiesConfig { | |
if let Some(expiration_ms) = &self.expiration_ms { | ||
prop = prop.with_expiration(ShortString::from(expiration_ms.to_string())); | ||
} | ||
prop | ||
if let Some(priority_template) = &self.priority { | ||
let priority_string = priority_template | ||
.render_string(event) | ||
.map_err(|error| { | ||
emit!(TemplateRenderingError { | ||
error, | ||
field: Some("properties.priority"), | ||
drop_event: true, | ||
}) | ||
}) | ||
.ok()?; | ||
|
||
// Valid template but invalid priorty type (not numeric) does not throw an error; instead warn. | ||
// Fall back to no priority in those cases (equivalent to 0). | ||
match priority_string.parse::<u8>() { | ||
Ok(priority) => { | ||
prop = prop.with_priority(priority); | ||
} | ||
Err(error) => { | ||
warn!( | ||
message = "Failed to convert to numeric value for \"properties.priority\"", | ||
error = %error, | ||
error_type = error_type::CONVERSION_FAILED, | ||
stage = error_stage::PROCESSING, | ||
internal_log_rate_limit = true, | ||
); | ||
} | ||
} | ||
} | ||
Some(prop) | ||
} | ||
} | ||
|
||
|
@@ -127,7 +162,126 @@ pub(super) async fn healthcheck(channel: Arc<lapin::Channel>) -> crate::Result<( | |
Ok(()) | ||
} | ||
|
||
#[test] | ||
pub fn generate_config() { | ||
crate::test_util::test_generate_config::<AmqpSinkConfig>(); | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::config::format::{deserialize, Format}; | ||
|
||
#[test] | ||
pub fn generate_config() { | ||
crate::test_util::test_generate_config::<AmqpSinkConfig>(); | ||
} | ||
|
||
fn assert_config_priority_eq(config: AmqpSinkConfig, event: &LogEvent, priority: u8) { | ||
assert_eq!( | ||
config | ||
.properties | ||
.unwrap() | ||
.priority | ||
.unwrap() | ||
.render_string(event) | ||
.unwrap(), | ||
priority.to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn parse_config_priority_static() { | ||
for (format, config) in [ | ||
( | ||
Format::Yaml, | ||
r#" | ||
exchange: "test" | ||
routing_key: "user_id" | ||
encoding: | ||
codec: "json" | ||
connection_string: "amqp://user:[email protected]:5672/" | ||
properties: | ||
priority: 1 | ||
"#, | ||
), | ||
( | ||
Format::Toml, | ||
r#" | ||
exchange = "test" | ||
routing_key = "user_id" | ||
encoding.codec = "json" | ||
connection_string = "amqp://user:[email protected]:5672/" | ||
properties = { priority = 1 } | ||
"#, | ||
), | ||
( | ||
Format::Json, | ||
r#" | ||
{ | ||
"exchange": "test", | ||
"routing_key": "user_id", | ||
"encoding": { | ||
"codec": "json" | ||
}, | ||
"connection_string": "amqp://user:[email protected]:5672/", | ||
"properties": { | ||
"priority": 1 | ||
} | ||
} | ||
"#, | ||
), | ||
] { | ||
let config: AmqpSinkConfig = deserialize(&config, format).unwrap(); | ||
let event = LogEvent::from_str_legacy("message"); | ||
assert_config_priority_eq(config, &event, 1); | ||
} | ||
} | ||
|
||
#[test] | ||
pub fn parse_config_priority_templated() { | ||
for (format, config) in [ | ||
( | ||
Format::Yaml, | ||
r#" | ||
exchange: "test" | ||
routing_key: "user_id" | ||
encoding: | ||
codec: "json" | ||
connection_string: "amqp://user:[email protected]:5672/" | ||
properties: | ||
priority: "{{ .priority }}" | ||
"#, | ||
), | ||
( | ||
Format::Toml, | ||
r#" | ||
exchange = "test" | ||
routing_key = "user_id" | ||
encoding.codec = "json" | ||
connection_string = "amqp://user:[email protected]:5672/" | ||
properties = { priority = "{{ .priority }}" } | ||
"#, | ||
), | ||
( | ||
Format::Json, | ||
r#" | ||
{ | ||
"exchange": "test", | ||
"routing_key": "user_id", | ||
"encoding": { | ||
"codec": "json" | ||
}, | ||
"connection_string": "amqp://user:[email protected]:5672/", | ||
"properties": { | ||
"priority": "{{ .priority }}" | ||
} | ||
} | ||
"#, | ||
), | ||
] { | ||
let config: AmqpSinkConfig = deserialize(&config, format).unwrap(); | ||
let event = { | ||
let mut event = LogEvent::from_str_legacy("message"); | ||
event.insert("priority", 2); | ||
event | ||
}; | ||
assert_config_priority_eq(config, &event, 2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I wonder if this is 'too spammy' because we can potentially have one per event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is why
internal_log_rate_limit
is set to true? Unless that doesn't throttle the logsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good, yes 👍
We should probably make
internal_log_rate_limit
true by default.