-
Notifications
You must be signed in to change notification settings - Fork 94
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(processing): Support multiple kafka clusters #1101
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ae6856b
wip
untitaker 6db0185
fix broken comment
untitaker 4324dc7
add test
untitaker a8f7118
formatting
untitaker a827708
add changelog
untitaker 888151e
apply some of the review feedback
untitaker bf8f432
refactor to minimize public api of relay-config
untitaker c6fb61f
rename field
untitaker 5e7d5b7
Merge branch 'master' into feat/multiple-kafka-clusters
untitaker b647eb7
Apply suggestions from code review
untitaker 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
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 |
---|---|---|
|
@@ -136,6 +136,9 @@ pub enum ConfigErrorKind { | |
/// compiled without the processing feature. | ||
#[fail(display = "was not compiled with processing, cannot enable processing")] | ||
ProcessingNotAvailable, | ||
/// The user referenced a kafka config name that does not exist. | ||
#[fail(display = "unknown kafka config name")] | ||
UnknownKafkaConfigName, | ||
} | ||
|
||
enum ConfigFormat { | ||
|
@@ -703,30 +706,97 @@ pub enum KafkaTopic { | |
/// Configuration for topics. | ||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(default)] | ||
pub struct TopicNames { | ||
pub struct TopicAssignments { | ||
/// Simple events topic name. | ||
pub events: String, | ||
pub events: TopicAssignment, | ||
/// Events with attachments topic name. | ||
pub attachments: String, | ||
pub attachments: TopicAssignment, | ||
/// Transaction events topic name. | ||
pub transactions: String, | ||
pub transactions: TopicAssignment, | ||
/// Event outcomes topic name. | ||
pub outcomes: String, | ||
pub outcomes: TopicAssignment, | ||
/// Session health topic name. | ||
pub sessions: String, | ||
pub sessions: TopicAssignment, | ||
/// Metrics topic name. | ||
pub metrics: String, | ||
pub metrics: TopicAssignment, | ||
} | ||
|
||
impl Default for TopicNames { | ||
impl TopicAssignments { | ||
/// Get a topic assignment by KafkaTopic value | ||
pub fn get(&self, kafka_topic: KafkaTopic) -> &TopicAssignment { | ||
match kafka_topic { | ||
KafkaTopic::Attachments => &self.attachments, | ||
KafkaTopic::Events => &self.events, | ||
KafkaTopic::Transactions => &self.transactions, | ||
KafkaTopic::Outcomes => &self.outcomes, | ||
KafkaTopic::Sessions => &self.sessions, | ||
KafkaTopic::Metrics => &self.metrics, | ||
} | ||
} | ||
} | ||
|
||
impl Default for TopicAssignments { | ||
fn default() -> Self { | ||
Self { | ||
events: "ingest-events".to_owned(), | ||
attachments: "ingest-attachments".to_owned(), | ||
transactions: "ingest-transactions".to_owned(), | ||
outcomes: "outcomes".to_owned(), | ||
sessions: "ingest-sessions".to_owned(), | ||
metrics: "ingest-metrics".to_owned(), | ||
events: "ingest-events".to_owned().into(), | ||
attachments: "ingest-attachments".to_owned().into(), | ||
transactions: "ingest-transactions".to_owned().into(), | ||
outcomes: "outcomes".to_owned().into(), | ||
sessions: "ingest-sessions".to_owned().into(), | ||
metrics: "ingest-metrics".to_owned().into(), | ||
} | ||
} | ||
} | ||
|
||
/// Configuration for a "logical" topic/datasink that Relay should forward data into. | ||
/// | ||
/// Can be either a string containing the kafka topic name to produce into (using the default | ||
/// `kafka_config`), or an object containing keys `topic_name` and `kafka_config_name` for using a | ||
/// custom kafka cluster. | ||
/// | ||
/// See documentation for `secondary_kafka_configs` for more information. | ||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(untagged)] | ||
pub enum TopicAssignment { | ||
/// String containing the kafka topic name. In this case the default kafka cluster configured | ||
/// in `kafka_config` will be used. | ||
Primary(String), | ||
/// Object containing topic name and string identifier of one of the clusters configured in | ||
/// `secondary_kafka_configs`. In this case that custom kafkaconfig will be used to produce | ||
/// data to the given topic name. | ||
Secondary { | ||
/// The topic name to use. | ||
#[serde(rename = "name")] | ||
topic_name: String, | ||
/// An identifier referencing one of the kafka configurations in `secondary_kafka_configs`. | ||
#[serde(rename = "config")] | ||
kafka_config_name: String, | ||
}, | ||
} | ||
|
||
impl From<String> for TopicAssignment { | ||
fn from(topic_name: String) -> TopicAssignment { | ||
TopicAssignment::Primary(topic_name) | ||
} | ||
} | ||
|
||
impl TopicAssignment { | ||
/// Get the topic name from this topic assignment. | ||
fn topic_name(&self) -> &str { | ||
match *self { | ||
TopicAssignment::Primary(ref s) => s.as_str(), | ||
TopicAssignment::Secondary { ref topic_name, .. } => topic_name.as_str(), | ||
} | ||
} | ||
|
||
/// Get the name of the kafka config to use. `None` means default configuration. | ||
fn kafka_config_name(&self) -> Option<&str> { | ||
match *self { | ||
TopicAssignment::Primary(_) => None, | ||
TopicAssignment::Secondary { | ||
ref kafka_config_name, | ||
.. | ||
} => Some(kafka_config_name.as_str()), | ||
} | ||
} | ||
} | ||
|
@@ -784,9 +854,30 @@ pub struct Processing { | |
pub max_session_secs_in_past: u32, | ||
/// Kafka producer configurations. | ||
pub kafka_config: Vec<KafkaConfigParam>, | ||
/// Additional kafka producer configurations. | ||
/// | ||
/// The `kafka_config` is the default producer configuration used for all topics. A secondary | ||
/// kafka config can be referenced in `topics:` like this: | ||
/// | ||
/// ```yaml | ||
/// secondary_kafka_configs: | ||
/// mycustomcluster: | ||
/// - name: 'bootstrap.servers' | ||
/// value: 'sentry_kafka_metrics:9093' | ||
/// | ||
/// topics: | ||
/// transactions: ingest-transactions | ||
/// metrics: | ||
/// topic_name: ingest-metrics | ||
/// kafka_config_name: mycustomcluster | ||
untitaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ``` | ||
/// | ||
/// Then metrics will be produced to an entirely different Kafka cluster. | ||
#[serde(default)] | ||
pub secondary_kafka_configs: BTreeMap<String, Vec<KafkaConfigParam>>, | ||
/// Kafka topic names. | ||
#[serde(default)] | ||
pub topics: TopicNames, | ||
pub topics: TopicAssignments, | ||
/// Redis hosts to connect to for storing state for rate limits. | ||
#[serde(default)] | ||
pub redis: Option<RedisConfig>, | ||
|
@@ -811,7 +902,8 @@ impl Default for Processing { | |
max_secs_in_past: default_max_secs_in_past(), | ||
max_session_secs_in_past: default_max_session_secs_in_past(), | ||
kafka_config: Vec::new(), | ||
topics: TopicNames::default(), | ||
secondary_kafka_configs: BTreeMap::new(), | ||
topics: TopicAssignments::default(), | ||
redis: None, | ||
attachment_chunk_size: default_chunk_size(), | ||
projectconfig_cache_prefix: default_projectconfig_cache_prefix(), | ||
|
@@ -1592,21 +1684,28 @@ impl Config { | |
self.values.processing.max_session_secs_in_past.into() | ||
} | ||
|
||
/// The list of Kafka configuration parameters. | ||
pub fn kafka_config(&self) -> &[KafkaConfigParam] { | ||
self.values.processing.kafka_config.as_slice() | ||
} | ||
|
||
/// Returns the name of the specified Kafka topic. | ||
/// Topic name and list of Kafka configuration parameters for a given topic. | ||
pub fn kafka_topic_name(&self, topic: KafkaTopic) -> &str { | ||
let topics = &self.values.processing.topics; | ||
match topic { | ||
KafkaTopic::Attachments => topics.attachments.as_str(), | ||
KafkaTopic::Events => topics.events.as_str(), | ||
KafkaTopic::Transactions => topics.transactions.as_str(), | ||
KafkaTopic::Outcomes => topics.outcomes.as_str(), | ||
KafkaTopic::Sessions => topics.sessions.as_str(), | ||
KafkaTopic::Metrics => topics.metrics.as_str(), | ||
self.values.processing.topics.get(topic).topic_name() | ||
} | ||
|
||
/// Topic name and list of Kafka configuration parameters for a given topic. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not return the topic name but rather the configuration name. |
||
pub fn kafka_config( | ||
&self, | ||
topic: KafkaTopic, | ||
) -> Result<(Option<&str>, &[KafkaConfigParam]), ConfigErrorKind> { | ||
if let Some(config_name) = self.values.processing.topics.get(topic).kafka_config_name() { | ||
Ok(( | ||
Some(config_name), | ||
self.values | ||
.processing | ||
.secondary_kafka_configs | ||
.get(config_name) | ||
.ok_or(ConfigErrorKind::UnknownKafkaConfigName)? | ||
.as_slice(), | ||
)) | ||
} else { | ||
Ok((None, self.values.processing.kafka_config.as_slice())) | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -413,7 +413,11 @@ mod processing { | |
pub fn create(config: Arc<Config>) -> Result<Self, ServerError> { | ||
let (future_producer, http_producer) = if config.processing_enabled() { | ||
let mut client_config = ClientConfig::new(); | ||
for config_p in config.kafka_config() { | ||
for config_p in config | ||
.kafka_config(KafkaTopic::Outcomes) | ||
.context(ServerErrorKind::KafkaError)? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Please assign this to a variable rather than wrapping the for header. |
||
.1 | ||
{ | ||
client_config.set(config_p.name.as_str(), config_p.value.as_str()); | ||
} | ||
let future_producer = client_config | ||
|
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.
Kudos for the example!