-
Notifications
You must be signed in to change notification settings - Fork 262
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: add a NATS "dead letter queue" stream for failing messages #5035
Merged
Merged
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,14 @@ | ||
load("@prelude-si//:macros.bzl", "rust_library") | ||
|
||
rust_library( | ||
name = "nats-dead-letter-queue", | ||
deps = [ | ||
"//lib/si-data-nats:si-data-nats", | ||
"//lib/telemetry-rs:telemetry", | ||
"//third-party/rust:remain", | ||
"//third-party/rust:thiserror", | ||
], | ||
srcs = glob([ | ||
"src/**/*.rs", | ||
]), | ||
) |
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,15 @@ | ||
[package] | ||
name = "nats-dead-letter-queue" | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
publish.workspace = true | ||
|
||
[dependencies] | ||
remain = { workspace = true } | ||
si-data-nats = { path = "../../lib/si-data-nats" } | ||
telemetry-nats = { path = "../../lib/telemetry-nats-rs" } | ||
thiserror = { workspace = true } |
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,59 @@ | ||
use si_data_nats::{ | ||
async_nats::jetstream::{ | ||
context::CreateStreamError, | ||
stream::{Config, RetentionPolicy}, | ||
}, | ||
jetstream::Context, | ||
}; | ||
use thiserror::Error; | ||
|
||
const STREAM_NAME: &str = "DEAD_LETTER_QUEUES"; | ||
const STREAM_DESCRIPTION: &str = "Dead Letter Queues"; | ||
// Subscribe to *all* stream and consumer max deliveries events. This subject is of the form: | ||
// `$JS.EVENT.ADVISORY.CONSUMER.MAX_DELIVERIES.<STREAM>.<CONSUMER>` | ||
// | ||
// See: https://docs.nats.io/running-a-nats-service/nats_admin/monitoring/monitoring_jetstream | ||
const STREAM_SUBJECTS: &str = "$JS.EVENT.ADVISORY.CONSUMER.MAX_DELIVERIES.*.*"; | ||
|
||
#[allow(missing_docs)] | ||
#[remain::sorted] | ||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("create stream error: {0}")] | ||
CreateStream(#[from] CreateStreamError), | ||
} | ||
|
||
pub type NatsDeadLetterQueueError = Error; | ||
|
||
type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
/// Ensures that the "dead letter queue" stream is created | ||
pub async fn create_stream(context: &Context) -> Result<()> { | ||
let prefix = context.metadata().subject_prefix(); | ||
|
||
context | ||
.get_or_create_stream(Config { | ||
name: prefixed_stream_name(prefix, STREAM_NAME), | ||
description: Some(STREAM_DESCRIPTION.to_string()), | ||
retention: RetentionPolicy::Limits, | ||
subjects: vec![prefixed_subject(prefix, STREAM_SUBJECTS)], | ||
..Default::default() | ||
}) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn prefixed_stream_name(prefix: Option<&str>, stream_name: &str) -> String { | ||
match prefix { | ||
Some(prefix) => format!("{prefix}_{stream_name}"), | ||
None => stream_name.to_owned(), | ||
} | ||
} | ||
|
||
fn prefixed_subject(prefix: Option<&str>, subject: &str) -> String { | ||
match prefix { | ||
Some(prefix) => format!("{prefix}.{subject}"), | ||
None => subject.to_owned(), | ||
} | ||
} |
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.
Excellent!