Skip to content

Commit 4d9e124

Browse files
committed
Leverage Telemetry in client
1 parent f966901 commit 4d9e124

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

components/remote_settings/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ pub use client::{Attachment, RemoteSettingsRecord, RemoteSettingsResponse, RsJso
2525
pub use config::{BaseUrl, RemoteSettingsConfig, RemoteSettingsConfig2, RemoteSettingsServer};
2626
pub use context::RemoteSettingsContext;
2727
pub use error::{trace, ApiResult, RemoteSettingsError, Result};
28+
pub use telemetry::{RemoteSettingsSyncStatus, RemoteSettingsTelemetry, SyncStatusExtras};
2829

2930
use client::Client;
3031
use error::Error;
3132
use storage::Storage;
33+
use telemetry::RemoteSettingsTelemetryWrapper;
3234

3335
uniffi::setup_scaffolding!("remote_settings");
3436

@@ -61,6 +63,14 @@ impl RemoteSettingsService {
6163
}
6264
}
6365

66+
/// Set the telemetry implementation used to record Glean metrics.
67+
/// This should be set to a real implementation (eg. Kotlin, Swift).
68+
/// If not set, all metric recording is a no-op.
69+
pub fn set_telemetry(&self, telemetry: Arc<dyn RemoteSettingsTelemetry>) {
70+
self.internal
71+
.set_telemetry(RemoteSettingsTelemetryWrapper::new(telemetry));
72+
}
73+
6474
/// Create a new Remote Settings client
6575
///
6676
/// This method performs no IO or network requests and is safe to run in a main thread that can't be blocked.

components/remote_settings/src/service.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use url::Url;
1515
use viaduct::Request;
1616

1717
use crate::{
18-
client::RemoteState, config::BaseUrl, error::Error, storage::Storage, RemoteSettingsClient,
19-
RemoteSettingsConfig2, RemoteSettingsContext, RemoteSettingsServer, Result,
18+
client::RemoteState, config::BaseUrl, error::Error, storage::Storage,
19+
telemetry::RemoteSettingsTelemetryWrapper, RemoteSettingsClient, RemoteSettingsConfig2,
20+
RemoteSettingsContext, RemoteSettingsServer, Result,
2021
};
2122

2223
/// Internal Remote settings service API
@@ -30,6 +31,7 @@ struct RemoteSettingsServiceInner {
3031
bucket_name: String,
3132
app_context: Option<RemoteSettingsContext>,
3233
remote_state: RemoteState,
34+
telemetry: RemoteSettingsTelemetryWrapper,
3335
/// Weakrefs for all clients that we've created. Note: this stores the
3436
/// top-level/public `RemoteSettingsClient` structs rather than `client::RemoteSettingsClient`.
3537
/// The reason for this is that we return Arcs to the public struct to the foreign code, so we
@@ -57,11 +59,16 @@ impl RemoteSettingsService {
5759
bucket_name,
5860
app_context: config.app_context,
5961
remote_state: RemoteState::default(),
62+
telemetry: RemoteSettingsTelemetryWrapper::noop(),
6063
clients: vec![],
6164
}),
6265
}
6366
}
6467

68+
pub fn set_telemetry(&self, telemetry: RemoteSettingsTelemetryWrapper) {
69+
self.inner.lock().telemetry = telemetry;
70+
}
71+
6572
pub fn make_client(&self, collection_name: String) -> Arc<RemoteSettingsClient> {
6673
let mut inner = self.inner.lock();
6774
// Allow using in-memory databases for testing of external crates.
@@ -84,11 +91,16 @@ impl RemoteSettingsService {
8491

8592
/// Sync collections for all active clients
8693
pub fn sync(&self) -> Result<Vec<String>> {
94+
const TELEMETRY_SOURCE_POLL: &str = "settings-changes-monitoring";
8795
// Make sure we only sync each collection once, even if there are multiple clients
8896
let mut synced_collections = HashSet::new();
8997

9098
let mut inner = self.inner.lock();
91-
let changes = inner.fetch_changes()?;
99+
let changes_result = inner.fetch_changes();
100+
if let Err(ref e) = changes_result {
101+
inner.telemetry.report_sync_error(e, TELEMETRY_SOURCE_POLL);
102+
}
103+
let changes = changes_result?;
92104
let change_map: HashMap<_, _> = changes
93105
.changes
94106
.iter()
@@ -99,18 +111,27 @@ impl RemoteSettingsService {
99111
for client in inner.active_clients() {
100112
let client = &client.internal;
101113
let collection_name = client.collection_name();
114+
let cid = format!("{bucket_name}/{collection_name}");
102115
if let Some(client_last_modified) = client.get_last_modified_timestamp()? {
103116
if let Some(server_last_modified) = change_map.get(&(collection_name, &bucket_name))
104117
{
105118
if client_last_modified == *server_last_modified {
106119
trace!("skipping up-to-date collection: {collection_name}");
120+
inner.telemetry.report_up_to_date(&cid, None);
107121
continue;
108122
}
109123
}
110124
}
111125
if synced_collections.insert(collection_name.to_string()) {
112126
trace!("syncing collection: {collection_name}");
113-
client.sync()?;
127+
let start_time = std::time::Instant::now();
128+
let sync_result = client.sync();
129+
let duration: u64 = start_time.elapsed().as_millis().try_into().unwrap_or(0);
130+
match &sync_result {
131+
Ok(()) => inner.telemetry.report_success(&cid, Some(duration)),
132+
Err(e) => inner.telemetry.report_sync_error(e, &cid),
133+
}
134+
sync_result?;
114135
}
115136
}
116137
Ok(synced_collections.into_iter().collect())

0 commit comments

Comments
 (0)