Skip to content

Commit

Permalink
Use a configuration builder to skip defaults on the configuration object
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed Dec 15, 2022
1 parent b0ba15a commit 9f25fbc
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 24 deletions.
106 changes: 106 additions & 0 deletions glean-core/rlb/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,109 @@ pub struct Configuration {
/// _before_ init, you shouldn't use this.
pub trim_data_to_registered_pings: bool,
}

/// Configuration builder.
///
/// Let's you build a configuration from the required fields
/// and let you set optional fields individually.
#[derive(Debug)]
pub struct Builder {
/// Required: Whether upload should be enabled.
pub upload_enabled: bool,
/// Required: Path to a directory to store all data in.
pub data_path: PathBuf,
/// Required: The application ID (will be sanitized during initialization).
pub application_id: String,
/// Optional: The maximum number of events to store before sending a ping containing events.
/// Default: `None`
pub max_events: Option<usize>,
/// Optional: Whether Glean should delay persistence of data from metrics with ping lifetime.
/// Default: `false`
pub delay_ping_lifetime_io: bool,
/// Optional: The server pings are sent to.
/// Default: `None`
pub server_endpoint: Option<String>,
/// Optional: The instance of the uploader used to send pings.
/// Default: `None`
pub uploader: Option<Box<dyn PingUploader + 'static>>,
/// Optional: Whether Glean should schedule "metrics" pings for you.
/// Default: `true`
pub use_core_mps: bool,
/// Optional: Whether Glean should limit its storage to only that of registered pings.
/// Unless you know that all your and your libraries' pings are appropriately registered
/// _before_ init, you shouldn't use this.
/// Default: `false`
pub trim_data_to_registered_pings: bool,
}

impl Builder {
/// A new configuration builder.
pub fn new<P: Into<PathBuf>, S: Into<String>>(
upload_enabled: bool,
data_path: P,
application_id: S,
) -> Self {
Self {
upload_enabled,
data_path: data_path.into(),
application_id: application_id.into(),
max_events: None,
delay_ping_lifetime_io: false,
server_endpoint: None,
uploader: None,
use_core_mps: true,
trim_data_to_registered_pings: false,
}
}

/// Generate the full configuration.
pub fn build(self) -> Configuration {
Configuration {
upload_enabled: self.upload_enabled,
data_path: self.data_path,
application_id: self.application_id,
max_events: self.max_events,
delay_ping_lifetime_io: self.delay_ping_lifetime_io,
server_endpoint: self.server_endpoint,
uploader: self.uploader,
use_core_mps: self.use_core_mps,
trim_data_to_registered_pings: self.trim_data_to_registered_pings,
}
}

/// Set the maximum number of events to store before sending a ping containing events.
pub fn with_max_events(mut self, max_events: usize) -> Self {
self.max_events = Some(max_events);
self
}

/// Set whether Glean should delay persistence of data from metrics with ping lifetime.
pub fn with_delay_ping_lifetime_io(mut self, value: bool) -> Self {
self.delay_ping_lifetime_io = value;
self
}

/// Set the server pings are sent to.
pub fn with_server_endpoint<S: Into<String>>(mut self, server_endpoint: S) -> Self {
self.server_endpoint = Some(server_endpoint.into());
self
}

/// Set the instance of the uploader used to send pings.
pub fn with_uploader<U: PingUploader + 'static>(mut self, uploader: U) -> Self {
self.uploader = Some(Box::new(uploader));
self
}

/// Set whether Glean should schedule "metrics" pings for you.
pub fn with_use_core_mps(mut self, value: bool) -> Self {
self.use_core_mps = value;
self
}

/// Set whether Glean should limit its storage to only that of registered pings.
pub fn with_trim_data_to_registered_pings(mut self, value: bool) -> Self {
self.trim_data_to_registered_pings = value;
self
}
}
2 changes: 1 addition & 1 deletion glean-core/rlb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
use std::collections::HashMap;
use std::path::Path;

pub use configuration::Configuration;
use configuration::DEFAULT_GLEAN_ENDPOINT;
pub use configuration::{Builder as ConfigurationBuilder, Configuration};
pub use core_metrics::ClientInfoMetrics;
pub use glean_core::{
metrics::{Datetime, DistributionData, MemoryUnit, Rate, RecordedEvent, TimeUnit, TimerId},
Expand Down
31 changes: 8 additions & 23 deletions glean-core/rlb/tests/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_json::Value;

//use glean::private::{DenominatorMetric, NumeratorMetric, RateMetric};
use glean::net::UploadResult;
use glean::{ClientInfoMetrics, Configuration};
use glean::{ClientInfoMetrics, Configuration, ConfigurationBuilder};

const SCHEMA_JSON: &str = include_str!("../../../glean.1.schema.json");

Expand All @@ -28,17 +28,9 @@ fn new_glean(configuration: Option<Configuration>) -> tempfile::TempDir {

let cfg = match configuration {
Some(c) => c,
None => Configuration {
data_path: tmpname,
application_id: GLOBAL_APPLICATION_ID.into(),
upload_enabled: true,
max_events: None,
delay_ping_lifetime_io: false,
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
trim_data_to_registered_pings: false,
},
None => ConfigurationBuilder::new(true, tmpname, GLOBAL_APPLICATION_ID)
.with_server_endpoint("invalid-test-host")
.build(),
};

let client_info = ClientInfoMetrics {
Expand Down Expand Up @@ -80,17 +72,10 @@ fn validate_against_schema() {
let dir = tempfile::tempdir().unwrap();
let tmpname = dir.path().to_path_buf();

let cfg = Configuration {
data_path: tmpname,
application_id: GLOBAL_APPLICATION_ID.into(),
upload_enabled: true,
max_events: None,
delay_ping_lifetime_io: false,
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(ValidatingUploader { sender: s })),
use_core_mps: false,
trim_data_to_registered_pings: false,
};
let cfg = ConfigurationBuilder::new(true, tmpname, GLOBAL_APPLICATION_ID)
.with_server_endpoint("invalid-test-host")
.with_uploader(ValidatingUploader { sender: s })
.build();
let _ = new_glean(Some(cfg));

const PING_NAME: &str = "test-ping";
Expand Down

0 comments on commit 9f25fbc

Please sign in to comment.