-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add mezmo_analytics source #85
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,141 @@ | ||
| //! In-process delivery for Mezmo analytics records. | ||
|
|
||
| use std::sync::OnceLock; | ||
|
|
||
| use futures::{Stream, StreamExt, future::ready}; | ||
| use tokio::sync::broadcast::{self, Receiver, Sender}; | ||
| use tokio_stream::wrappers::BroadcastStream; | ||
| use tracing::warn; | ||
|
|
||
| use crate::event::LogEvent; | ||
|
|
||
| const ANALYTICS_CHANNEL_CAPACITY: usize = 1_000; | ||
| const ANALYTICS_OUTPUT_COUNT: usize = 5; | ||
|
|
||
| type AnalyticsSenders = [Sender<AnalyticsEventBatch>; ANALYTICS_OUTPUT_COUNT]; | ||
|
|
||
| static ANALYTICS: OnceLock<AnalyticsSenders> = OnceLock::new(); | ||
|
|
||
| /// A named output exposed by the `mezmo_analytics` source. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum AnalyticsOutput { | ||
| UsageMetrics, | ||
| UsageMetricsByAnnotations, | ||
| LogClusters, | ||
| LogClusterSamples, | ||
| LogClusterUsage, | ||
| } | ||
|
|
||
| impl AnalyticsOutput { | ||
| /// Returns the source output name. | ||
| pub const fn as_str(self) -> &'static str { | ||
| match self { | ||
| Self::UsageMetrics => "usage_metrics", | ||
| Self::UsageMetricsByAnnotations => "usage_metrics_by_annotations", | ||
| Self::LogClusters => "log_clusters", | ||
| Self::LogClusterSamples => "log_cluster_samples", | ||
| Self::LogClusterUsage => "log_cluster_usage", | ||
| } | ||
| } | ||
|
|
||
| /// Returns every named output in declaration order. | ||
| pub const fn all() -> [Self; 5] { | ||
|
biblicalph marked this conversation as resolved.
Outdated
|
||
| [ | ||
| Self::UsageMetrics, | ||
| Self::UsageMetricsByAnnotations, | ||
| Self::LogClusters, | ||
| Self::LogClusterSamples, | ||
| Self::LogClusterUsage, | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| /// A batch of analytics records destined for one named source output. | ||
| #[derive(Clone, Debug)] | ||
| pub struct AnalyticsEventBatch { | ||
| output: AnalyticsOutput, | ||
| events: Vec<LogEvent>, | ||
| } | ||
|
|
||
| impl AnalyticsEventBatch { | ||
| /// Creates a batch for one named output. | ||
| pub fn new(output: AnalyticsOutput, events: Vec<LogEvent>) -> Self { | ||
| Self { output, events } | ||
| } | ||
|
|
||
| /// Returns the named output associated with this batch. | ||
| pub const fn output(&self) -> AnalyticsOutput { | ||
| self.output | ||
| } | ||
|
|
||
| /// Returns the records in this batch. | ||
| pub fn events(&self) -> &[LogEvent] { | ||
| &self.events | ||
| } | ||
|
|
||
| /// Splits the batch into its named output and records. | ||
| pub fn into_parts(self) -> (AnalyticsOutput, Vec<LogEvent>) { | ||
| (self.output, self.events) | ||
| } | ||
| } | ||
|
|
||
| fn senders() -> &'static AnalyticsSenders { | ||
| ANALYTICS | ||
| .get_or_init(|| std::array::from_fn(|_| broadcast::channel(ANALYTICS_CHANNEL_CAPACITY).0)) | ||
| } | ||
|
|
||
| /// Builds and publishes batches if a `mezmo_analytics` source is subscribed. | ||
| pub fn publish<F, B>(build_batches: F) | ||
| where | ||
| F: FnOnce() -> B, | ||
| B: IntoIterator<Item = AnalyticsEventBatch>, | ||
| { | ||
| let Some(senders) = ANALYTICS.get() else { | ||
| return; | ||
| }; | ||
| if senders.iter().all(|sender| sender.receiver_count() == 0) { | ||
| return; | ||
| } | ||
|
|
||
| for batch in build_batches() { | ||
| let sender = &senders[batch.output() as usize]; | ||
| if sender.receiver_count() > 0 { | ||
| let _ = sender.send(batch); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A subscription to analytics batches produced inside Vector. | ||
| pub struct AnalyticsSubscription { | ||
| receiver: Receiver<AnalyticsEventBatch>, | ||
| } | ||
|
|
||
| impl AnalyticsSubscription { | ||
| /// Subscribes to analytics batches for one output produced after this call. | ||
| pub fn subscribe(output: AnalyticsOutput) -> Self { | ||
| Self { | ||
| receiver: senders()[output as usize].subscribe(), | ||
| } | ||
| } | ||
|
|
||
| /// Subscribes to analytics batches for every output. | ||
| pub fn subscribe_all() -> Vec<Self> { | ||
| AnalyticsOutput::all() | ||
| .into_iter() | ||
| .map(Self::subscribe) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Converts this subscription into its analytics batch stream. | ||
| pub fn into_stream(self) -> impl Stream<Item = AnalyticsEventBatch> + Unpin { | ||
| BroadcastStream::new(self.receiver).filter_map(|received| { | ||
| ready(match received { | ||
| Ok(batch) => Some(batch), | ||
| Err(error) => { | ||
| warn!(message = "Mezmo analytics source lagged behind.", %error); | ||
| None | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| }) | ||
|
biblicalph marked this conversation as resolved.
|
||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod analytics; | ||
| pub mod postgres; |
This file contains hidden or 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.
tokio-stream is no longer optional