Bug 1632131 - Implement a string metric for the in-tree Glean API. r=chutten

Currently there's no user, so we do some trickery to actually make sure
it gets compiled.

It also lacks a test, that will follow in the next commit,
as it's a bit more complex to set up a Glean instance for a test run.

Differential Revision: https://phabricator.services.mozilla.com/D72128
This commit is contained in:
Jan-Erik Rediger 2020-04-24 19:08:25 +00:00
Родитель 2291f694fb
Коммит 0542e950e5
8 изменённых файлов: 139 добавлений и 3 удалений

9
Cargo.lock сгенерированный
Просмотреть файл

@ -1413,6 +1413,7 @@ checksum = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
name = "fog"
version = "0.1.0"
dependencies = [
"glean",
"glean-core",
"log",
"nserror",
@ -1810,6 +1811,14 @@ dependencies = [
"gl_generator",
]
[[package]]
name = "glean"
version = "0.1.0"
dependencies = [
"glean-core",
"log",
]
[[package]]
name = "glean-core"
version = "25.1.0"

Просмотреть файл

@ -1,7 +1,7 @@
[package]
name = "fog"
version = "0.1.0"
authors = ["The Mozilla Project Developers"]
authors = ["Glean SDK team <glean-team@mozilla.com>"]
edition = "2018"
license = "MPL-2.0"
@ -13,3 +13,4 @@ nsstring = { path = "../../../xpcom/rust/nsstring" }
static_prefs = { path = "../../../modules/libpref/init/static_prefs" }
xpcom = { path = "../../../xpcom/rust/xpcom" }
once_cell = "1.2.0"
glean = { path = "./api" }

Просмотреть файл

@ -0,0 +1,10 @@
[package]
name = "glean"
version = "0.1.0"
authors = ["Glean SDK team <glean-team@mozilla.com>"]
edition = "2018"
publish = false
[dependencies]
glean-core = "25.1.0"
log = "0.4"

Просмотреть файл

@ -0,0 +1,29 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! The public Glean SDK API, for Rust consumers.
//!
//! ## Example:
//!
//! ```rust,ignore
//! assert!(glean::is_upload_enabled())
//! ```
pub mod metrics;
/// Run a closure with a mutable reference to the locked global Glean object.
fn with_glean<F, R>(f: F) -> R
where
F: FnOnce(&glean_core::Glean) -> R,
{
let lock = glean_core::global_glean().lock().unwrap();
f(&lock)
}
/// Determine whether upload is enabled.
///
/// See `glean_core::Glean.is_upload_enabled`.
pub fn is_upload_enabled() -> bool {
with_glean(|glean| glean.is_upload_enabled())
}

Просмотреть файл

@ -0,0 +1,14 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! The different metric types supported by the Glean SDK to handle data.
// Re-export of `glean_core` types we can re-use.
// That way a user only needs to depend on this crate, not on glean_core (and there can't be a
// version mismatch).
pub use glean_core::{CommonMetricData, Lifetime};
mod string;
pub use string::StringMetric;

Просмотреть файл

@ -0,0 +1,67 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use glean_core::CommonMetricData;
/// A string metric.
///
/// Record an Unicode string value with arbitrary content.
/// Strings are length-limited to `MAX_LENGTH_VALUE` bytes.
///
/// # Example
///
/// The following piece of code will be generated by `glean_parser`:
///
/// ```rust,ignore
/// use glean::metrics::{StringMetric, CommonMetricData, Lifetime};
/// use once_cell::sync::Lazy;
///
/// mod browser {
/// pub static search_engine: Lazy<StringMetric> = Lazy::new(|| StringMetric::new(CommonMetricData {
/// name: "search_engine".into(),
/// category: "browser".into(),
/// lifetime: Lifetime::Ping,
/// disabled: false,
/// dynamic_label: None
/// }));
/// }
/// ```
///
/// It can then be used with:
///
/// ```rust,ignore
/// browser::search_engine.set("websearch");
/// ```
#[derive(Clone, Debug)]
pub struct StringMetric(glean_core::metrics::StringMetric);
impl StringMetric {
/// Create a new string metric.
pub fn new(meta: CommonMetricData) -> Self {
Self(glean_core::metrics::StringMetric::new(meta))
}
/// Set to the specified value.
///
/// ## Arguments
///
/// * `value` - The string to set the metric to.
///
/// ## Notes
///
/// Truncates the value if it is longer than `MAX_STRING_LENGTH` bytes and logs an error.
/// See [String metric limits](https://mozilla.github.io/glean/book/user/metrics/string.html#limits).
pub fn set<S: Into<String>>(&self, value: S) {
crate::with_glean(move |glean| self.0.set(glean, value))
}
/// **Test-only API.**
///
/// Get the currently stored value as a string.
///
/// This doesn't clear the stored value.
pub fn test_get_value(&self, storage_name: &str) -> Option<String> {
crate::with_glean(move |glean| self.0.test_get_value(glean, storage_name))
}
}

Просмотреть файл

@ -98,7 +98,9 @@ fn initialize_core_metrics(glean: &Glean, client_info: &ClientInfo) {
core_metrics
.os_version
.set(glean, &client_info.os_version[..]);
core_metrics.architecture.set(glean, &client_info.architecture);
core_metrics
.architecture
.set(glean, &client_info.architecture);
// FIXME(bug 1625207): Device manufacturer should be made optional.
core_metrics
.device_manufacturer

Просмотреть файл

@ -17,6 +17,10 @@
//! [privacy-policy]: https://www.mozilla.org/privacy/
//! [docs]: https://firefox-source-docs.mozilla.org/toolkit/components/glean/
// No one is currently using the Glean SDK, so let's export it, so we know it gets
// compiled.
pub extern crate glean;
use std::ffi::CStr;
use std::os::raw::c_char;
@ -41,7 +45,7 @@ pub unsafe extern "C" fn fog_init(
app_display_version: &nsACString,
channel: *const c_char,
os_version: &nsACString,
architecture: &nsACString
architecture: &nsACString,
) -> nsresult {
log::debug!("Initializing FOG.");