Struct glean_core::Glean
source · pub struct Glean { /* private fields */ }
Expand description
The object holding meta information about a Glean instance.
Example
Create a new Glean instance, register a ping, record a simple counter and then send the final ping.
let cfg = InternalConfiguration {
data_path: "/tmp/glean".into(),
application_id: "glean.sample.app".into(),
language_binding_name: "Rust".into(),
upload_enabled: true,
max_events: None,
delay_ping_lifetime_io: false,
app_build: "".into(),
use_core_mps: false,
trim_data_to_registered_pings: false,
log_level: None,
rate_limit: None,
enable_event_timestamps: true,
experimentation_id: None,
enable_internal_pings: true,
ping_schedule: Default::default(),
ping_lifetime_threshold: 1000,
ping_lifetime_max_time: 2000,
};
let mut glean = Glean::new(cfg).unwrap();
let ping = PingType::new("sample", true, false, true, true, true, vec![], vec![], true);
glean.register_ping_type(&ping);
let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
name: "calls".into(),
category: "local".into(),
send_in_pings: vec!["sample".into()],
..Default::default()
});
call_counter.add_sync(&glean, 1);
ping.submit_sync(&glean, None);
Note
In specific language bindings, this is usually wrapped in a singleton and all metric recording goes to a single instance of this object. In the Rust core, it is possible to create multiple instances, which is used in testing.
Implementations§
source§impl Glean
impl Glean
sourcepub fn new_for_subprocess(
cfg: &InternalConfiguration,
scan_directories: bool
) -> Result<Self>
pub fn new_for_subprocess( cfg: &InternalConfiguration, scan_directories: bool ) -> Result<Self>
Creates and initializes a new Glean object for use in a subprocess.
Importantly, this will not send any pings at startup, since that sort of management should only happen in the main process.
sourcepub fn new(cfg: InternalConfiguration) -> Result<Self>
pub fn new(cfg: InternalConfiguration) -> Result<Self>
Creates and initializes a new Glean object.
This will create the necessary directories and files in
cfg.data_path
. This will also initialize
the core metrics.
sourcepub fn destroy_db(&mut self)
pub fn destroy_db(&mut self)
Destroys the database.
After this Glean needs to be reinitialized.
sourcepub fn on_ready_to_submit_pings(
&mut self,
trim_data_to_registered_pings: bool
) -> bool
pub fn on_ready_to_submit_pings( &mut self, trim_data_to_registered_pings: bool ) -> bool
Signals that the environment is ready to submit pings.
Should be called when Glean is initialized to the point where it can correctly assemble pings. Usually called from the language binding after all of the core metrics have been set and the ping types have been registered.
Arguments
trim_data_to_registered_pings
- Whether we should limit to storing data only for data belonging to pings previously registered viaregister_ping_type
.
Returns
Whether the “events” ping was submitted.
sourcepub fn set_upload_enabled(&mut self, flag: bool) -> bool
pub fn set_upload_enabled(&mut self, flag: bool) -> bool
Sets whether upload is enabled or not.
When uploading is disabled, metrics aren’t recorded at all and no data is uploaded.
When disabling, all pending metrics, events and queued pings are cleared.
When enabling, the core Glean metrics are recreated.
If the value of this flag is not actually changed, this is a no-op.
Arguments
flag
- When true, enable metric collection.
Returns
Whether the flag was different from the current value, and actual work was done to clear or reinstate metrics.
sourcepub fn is_upload_enabled(&self) -> bool
pub fn is_upload_enabled(&self) -> bool
Determines whether upload is enabled.
When upload is disabled, no data will be recorded.
sourcepub fn is_ping_enabled(&self, ping: &str) -> bool
pub fn is_ping_enabled(&self, ping: &str) -> bool
Check if a ping is enabled.
Note that some internal “ping” names are considered to be always enabled.
If a ping is not known to Glean (“unregistered”) it is always considered disabled. If a ping is known, it can be enabled/disabled at any point. Only data for enabled pings is recorded. Disabled pings are never submitted.
sourcepub fn get_application_id(&self) -> &str
pub fn get_application_id(&self) -> &str
Gets the application ID as specified on instantiation.
sourcepub fn get_data_path(&self) -> &Path
pub fn get_data_path(&self) -> &Path
Gets the data path of this instance.
sourcepub fn storage_opt(&self) -> Option<&Database>
pub fn storage_opt(&self) -> Option<&Database>
Gets an optional handle to the database.
sourcepub fn event_storage(&self) -> &EventDatabase
pub fn event_storage(&self) -> &EventDatabase
Gets a handle to the event database.
sourcepub fn get_max_events(&self) -> usize
pub fn get_max_events(&self) -> usize
Gets the maximum number of events to store before sending a ping.
sourcepub fn get_upload_task(&self) -> PingUploadTask
pub fn get_upload_task(&self) -> PingUploadTask
Gets the next task for an uploader.
This can be one of:
Wait
- which means the requester should ask again later;Upload(PingRequest)
- which means there is a ping to upload. This wraps the actual request object;Done
- which means requester should stop asking for now.
Returns
A PingUploadTask
representing the next task.
sourcepub fn process_ping_upload_response(
&self,
uuid: &str,
status: UploadResult
) -> UploadTaskAction
pub fn process_ping_upload_response( &self, uuid: &str, status: UploadResult ) -> UploadTaskAction
Processes the response from an attempt to upload a ping.
Arguments
uuid
- The UUID of the ping in question.status
- The upload result.
sourcepub fn submit_ping_by_name(&self, ping_name: &str, reason: Option<&str>) -> bool
pub fn submit_ping_by_name(&self, ping_name: &str, reason: Option<&str>) -> bool
Collects and submits a ping by name for eventual uploading.
The ping content is assembled as soon as possible, but upload is not guaranteed to happen immediately, as that depends on the upload policies.
If the ping currently contains no content, it will not be sent, unless it is configured to be sent if empty.
Arguments
ping_name
- The name of the ping to submitreason
- A reason code to include in the ping
Returns
Whether the ping was succesfully assembled and queued.
Errors
If collecting or writing the ping to disk failed.
sourcepub fn get_ping_by_name(&self, ping_name: &str) -> Option<&PingType>
pub fn get_ping_by_name(&self, ping_name: &str) -> Option<&PingType>
sourcepub fn register_ping_type(&mut self, ping: &PingType)
pub fn register_ping_type(&mut self, ping: &PingType)
Register a new PingType
.
sourcepub fn get_registered_ping_names(&self) -> Vec<&str>
pub fn get_registered_ping_names(&self) -> Vec<&str>
Gets a list of currently registered ping names.
Returns
The list of ping names that are currently registered.
sourcepub fn set_experiment_active(
&self,
experiment_id: String,
branch: String,
extra: HashMap<String, String>
)
pub fn set_experiment_active( &self, experiment_id: String, branch: String, extra: HashMap<String, String> )
Indicates that an experiment is running.
Glean will then add an experiment annotation to the environment which is sent with pings. This information is not persisted between runs.
Arguments
experiment_id
- The id of the active experiment (maximum 30 bytes).branch
- The experiment branch (maximum 30 bytes).extra
- Optional metadata to output with the ping.
sourcepub fn set_experiment_inactive(&self, experiment_id: String)
pub fn set_experiment_inactive(&self, experiment_id: String)
Indicates that an experiment is no longer running.
Arguments
experiment_id
- The id of the active experiment to deactivate (maximum 30 bytes).
sourcepub fn test_get_experiment_data(
&self,
experiment_id: String
) -> Option<RecordedExperiment>
pub fn test_get_experiment_data( &self, experiment_id: String ) -> Option<RecordedExperiment>
Test-only API (exported for FFI purposes).
Gets stored data for the requested experiment.
Arguments
experiment_id
- The id of the active experiment (maximum 30 bytes).
sourcepub fn test_get_experimentation_id(&self) -> Option<String>
pub fn test_get_experimentation_id(&self) -> Option<String>
Test-only API (exported for FFI purposes).
Gets stored experimentation id annotation.
sourcepub fn apply_server_knobs_config(&self, cfg: RemoteSettingsConfig)
pub fn apply_server_knobs_config(&self, cfg: RemoteSettingsConfig)
Set configuration to override the default state, typically initiated from a remote_settings experiment or rollout
Arguments
cfg
- The stringified JSON representation of aRemoteSettingsConfig
object
sourcepub fn persist_ping_lifetime_data(&self) -> Result<()>
pub fn persist_ping_lifetime_data(&self) -> Result<()>
Persists Lifetime::Ping
data that might be in memory in case
delay_ping_lifetime_io
is set
or was set at a previous time.
If there is no data to persist, this function does nothing.
sourcepub fn clear_application_lifetime_metrics(&self)
pub fn clear_application_lifetime_metrics(&self)
This is not meant to be used directly.
Clears all the metrics that have Lifetime::Application
.
sourcepub fn is_first_run(&self) -> bool
pub fn is_first_run(&self) -> bool
Whether or not this is the first run on this profile.
sourcepub fn set_debug_view_tag(&mut self, value: &str) -> bool
pub fn set_debug_view_tag(&mut self, value: &str) -> bool
Sets a debug view tag.
This will return false
in case value
is not a valid tag.
When the debug view tag is set, pings are sent with a X-Debug-ID
header with the value of the tag
and are sent to the “Ping Debug Viewer”.
Arguments
value
- A valid HTTP header value. Must match the regex: “[a-zA-Z0-9-]{1,20}”.
sourcepub fn debug_view_tag(&self) -> Option<&String>
pub fn debug_view_tag(&self) -> Option<&String>
Return the value for the debug view tag or None
if it hasn’t been set.
The debug_view_tag
may be set from an environment variable
(GLEAN_DEBUG_VIEW_TAG
) or through the set_debug_view_tag
function.
Sets source tags.
This will return false
in case value
contains invalid tags.
Ping tags will show in the destination datasets, after ingestion.
Note If one or more tags are invalid, all tags are ignored.
Arguments
value
- A vector of at most 5 valid HTTP header values. Individual tags must match the regex: “[a-zA-Z0-9-]{1,20}”.
sourcepub fn set_log_pings(&mut self, value: bool) -> bool
pub fn set_log_pings(&mut self, value: bool) -> bool
Sets the log pings debug option.
This will return false
in case we are unable to set the option.
When the log pings debug option is true
,
we log the payload of all succesfully assembled pings.
Arguments
value
- The value of the log pings option
sourcepub fn log_pings(&self) -> bool
pub fn log_pings(&self) -> bool
Return the value for the log pings debug option or false
if it hasn’t been set.
The log_pings
option may be set from an environment variable (GLEAN_LOG_PINGS
)
or through the set_log_pings
function.
sourcepub fn set_dirty_flag(&self, new_value: bool)
pub fn set_dirty_flag(&self, new_value: bool)
This is not meant to be used directly.
Sets the value of a “dirty flag” in the permanent storage.
The “dirty flag” is meant to have the following behaviour, implemented by the consumers of the FFI layer:
- on mobile: set to
false
when going to background or shutting down, set totrue
at startup and when going to foreground. - on non-mobile platforms: set to
true
at startup andfalse
at shutdown.
At startup, before setting its new value, if the “dirty flag” value is
true
, then Glean knows it did not exit cleanly and can implement
coping mechanisms (e.g. sending a baseline
ping).
sourcepub fn is_dirty_flag_set(&self) -> bool
pub fn is_dirty_flag_set(&self) -> bool
This is not meant to be used directly.
Checks the stored value of the “dirty flag”.
sourcepub fn handle_client_active(&mut self)
pub fn handle_client_active(&mut self)
Performs the collection/cleanup operations required by becoming active.
This functions generates a baseline ping with reason active
and then sets the dirty bit.
sourcepub fn handle_client_inactive(&mut self)
pub fn handle_client_inactive(&mut self)
Performs the collection/cleanup operations required by becoming inactive.
This functions generates a baseline and an events ping with reason
inactive
and then clears the dirty bit.
sourcepub fn test_clear_all_stores(&self)
pub fn test_clear_all_stores(&self)
Test-only API (exported for FFI purposes).
Deletes all stored metrics.
Note that this also includes the ping sequence numbers, so it has the effect of resetting those to their initial values.
sourcepub fn cancel_metrics_ping_scheduler(&self)
pub fn cancel_metrics_ping_scheduler(&self)
Instructs the Metrics Ping Scheduler’s thread to exit cleanly.
If Glean was configured with use_core_mps: false
, this has no effect.
sourcepub fn start_metrics_ping_scheduler(&self)
pub fn start_metrics_ping_scheduler(&self)
Instructs the Metrics Ping Scheduler to being scheduling metrics pings.
If Glean wsa configured with use_core_mps: false
, this has no effect.