Skip to content

Commit

Permalink
[rust] Tracking Selenium Manager usage through Plausible (#11211) (#1…
Browse files Browse the repository at this point in the history
…3173)

* [rust] Send stats to Plausible (#11211)

* [rust] Read selenium version from clap cargo feature

* [rust] Set language binding in test

* [rust] Include assertion about stats error message

* [rust] Use selenium domain (selenium.dev) as plausible stats target

* [rust] Make asynchronous request to plausible

* [rust] Include argument --avoid-stats to not send statistics to plausible.io

* [rust] Include request timeout and improve error handling

* [rust] Spawn new thread for calling to stats function

* [rust] Use message channel for passing errors when sending stats

* [rust] Update checksum un lock file

* Update rust/src/stats.rs

* [rust] Check if SM is offline before sending the stats

* [rust] Send custom properties to plausible in lowercase

---------

Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
bonigarcia and diemol authored Dec 13, 2023
1 parent 5b1b449 commit e2b6bd7
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 5 deletions.
4 changes: 3 additions & 1 deletion rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "0b0c238a314773ea2af41d5e13962f5a27ffdf2974cab7953267ab4b6b99c34f",
"checksum": "3468f2b2abc234888e072c953d9cafe64affe1c53711fb6bf961f97743525229",
"crates": {
"addr2line 0.19.0": {
"name": "addr2line",
Expand Down Expand Up @@ -1610,6 +1610,7 @@
],
"crate_features": {
"common": [
"cargo",
"color",
"default",
"derive",
Expand Down Expand Up @@ -1671,6 +1672,7 @@
],
"crate_features": {
"common": [
"cargo",
"color",
"error-context",
"help",
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Selenium Manager is a CLI tool that automatically manages the browser/driver inf
"""

[dependencies]
clap = { version = "4.4.8", features = ["derive"] }
clap = { version = "4.4.8", features = ["derive", "cargo"] }
log = "0.4.20"
env_logger = "0.10.1"
regex = "1.10.2"
Expand Down
15 changes: 15 additions & 0 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::option::Option;
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

pub const CHROME_NAME: &str = "chrome";
pub const CHROMEDRIVER_NAME: &str = "chromedriver";
Expand All @@ -55,6 +57,8 @@ pub struct ChromeManager {
pub config: ManagerConfig,
pub http_client: Client,
pub log: Logger,
pub tx: Sender<String>,
pub rx: Receiver<String>,
pub download_browser: bool,
pub driver_url: Option<String>,
pub browser_url: Option<String>,
Expand All @@ -67,12 +71,15 @@ impl ChromeManager {
let config = ManagerConfig::default(browser_name, driver_name);
let default_timeout = config.timeout.to_owned();
let default_proxy = &config.proxy;
let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
Ok(Box::new(ChromeManager {
browser_name,
driver_name,
http_client: create_http_client(default_timeout, default_proxy)?,
config,
log: Logger::new(),
tx,
rx,
download_browser: false,
driver_url: None,
browser_url: None,
Expand Down Expand Up @@ -416,6 +423,14 @@ impl SeleniumManager for ChromeManager {
self.log = log;
}

fn get_sender(&self) -> &Sender<String> {
&self.tx
}

fn get_receiver(&self) -> &Receiver<String> {
&self.rx
}

fn get_platform_label(&self) -> &str {
let os = self.get_os();
let arch = self.get_arch();
Expand Down
6 changes: 6 additions & 0 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub struct ManagerConfig {
pub offline: bool,
pub force_browser_download: bool,
pub avoid_browser_download: bool,
pub language_binding: String,
pub selenium_version: String,
pub avoid_stats: bool,
}

impl ManagerConfig {
Expand Down Expand Up @@ -111,6 +114,9 @@ impl ManagerConfig {
offline: BooleanKey("offline", false).get_value(),
force_browser_download: BooleanKey("force-browser-download", false).get_value(),
avoid_browser_download: BooleanKey("avoid-browser-download", false).get_value(),
language_binding: StringKey(vec!["language-binding"], "").get_value(),
selenium_version: StringKey(vec!["selenium-version"], "").get_value(),
avoid_stats: BooleanKey("avoid-stats", false).get_value(),
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

pub const EDGE_NAMES: &[&str] = &[
"edge",
Expand Down Expand Up @@ -61,6 +63,8 @@ pub struct EdgeManager {
pub config: ManagerConfig,
pub http_client: Client,
pub log: Logger,
pub tx: Sender<String>,
pub rx: Receiver<String>,
pub download_browser: bool,
pub browser_url: Option<String>,
}
Expand All @@ -76,12 +80,15 @@ impl EdgeManager {
let config = ManagerConfig::default(static_browser_name, driver_name);
let default_timeout = config.timeout.to_owned();
let default_proxy = &config.proxy;
let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
Ok(Box::new(EdgeManager {
browser_name: static_browser_name,
driver_name,
http_client: create_http_client(default_timeout, default_proxy)?,
config,
log: Logger::new(),
tx,
rx,
download_browser: false,
browser_url: None,
}))
Expand Down Expand Up @@ -322,6 +329,14 @@ impl SeleniumManager for EdgeManager {
self.log = log;
}

fn get_sender(&self) -> &Sender<String> {
&self.tx
}

fn get_receiver(&self) -> &Receiver<String> {
&self.rx
}

fn get_platform_label(&self) -> &str {
let os = self.get_os();
let arch = self.get_arch();
Expand Down
15 changes: 15 additions & 0 deletions rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

pub const FIREFOX_NAME: &str = "firefox";
pub const GECKODRIVER_NAME: &str = "geckodriver";
Expand Down Expand Up @@ -71,6 +73,8 @@ pub struct FirefoxManager {
pub config: ManagerConfig,
pub http_client: Client,
pub log: Logger,
pub tx: Sender<String>,
pub rx: Receiver<String>,
pub download_browser: bool,
}

Expand All @@ -81,12 +85,15 @@ impl FirefoxManager {
let config = ManagerConfig::default(browser_name, driver_name);
let default_timeout = config.timeout.to_owned();
let default_proxy = &config.proxy;
let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
Ok(Box::new(FirefoxManager {
browser_name,
driver_name,
http_client: create_http_client(default_timeout, default_proxy)?,
config,
log: Logger::new(),
tx,
rx,
download_browser: false,
}))
}
Expand Down Expand Up @@ -361,6 +368,14 @@ impl SeleniumManager for FirefoxManager {
self.log = log;
}

fn get_sender(&self) -> &Sender<String> {
&self.tx
}

fn get_receiver(&self) -> &Receiver<String> {
&self.rx
}

fn get_platform_label(&self) -> &str {
let driver_version = self.get_driver_version();
let os = self.get_os();
Expand Down
15 changes: 15 additions & 0 deletions rust/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use anyhow::Error;
use reqwest::Client;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

pub const GRID_NAME: &str = "grid";
const GRID_RELEASE: &str = "selenium-server";
Expand All @@ -45,6 +47,8 @@ pub struct GridManager {
pub config: ManagerConfig,
pub http_client: Client,
pub log: Logger,
pub tx: Sender<String>,
pub rx: Receiver<String>,
pub download_browser: bool,
pub driver_url: Option<String>,
}
Expand All @@ -57,12 +61,15 @@ impl GridManager {
config.driver_version = driver_version;
let default_timeout = config.timeout.to_owned();
let default_proxy = &config.proxy;
let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
Ok(Box::new(GridManager {
browser_name,
driver_name,
http_client: create_http_client(default_timeout, default_proxy)?,
config,
log: Logger::new(),
tx,
rx,
download_browser: false,
driver_url: None,
}))
Expand Down Expand Up @@ -227,6 +234,14 @@ impl SeleniumManager for GridManager {
self.log = log;
}

fn get_sender(&self) -> &Sender<String> {
&self.tx
}

fn get_receiver(&self) -> &Receiver<String> {
&self.rx
}

fn get_platform_label(&self) -> &str {
""
}
Expand Down
15 changes: 15 additions & 0 deletions rust/src/iexplorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use anyhow::Error;
use reqwest::Client;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

use crate::metadata::{
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
Expand All @@ -51,6 +53,8 @@ pub struct IExplorerManager {
pub config: ManagerConfig,
pub http_client: Client,
pub log: Logger,
pub tx: Sender<String>,
pub rx: Receiver<String>,
pub download_browser: bool,
pub driver_url: Option<String>,
}
Expand All @@ -63,12 +67,15 @@ impl IExplorerManager {
let default_timeout = config.timeout.to_owned();
let default_proxy = &config.proxy;
config.os = WINDOWS.to_str_vector().first().unwrap().to_string();
let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
Ok(Box::new(IExplorerManager {
browser_name,
driver_name,
http_client: create_http_client(default_timeout, default_proxy)?,
config,
log: Logger::new(),
tx,
rx,
download_browser: false,
driver_url: None,
}))
Expand Down Expand Up @@ -233,6 +240,14 @@ impl SeleniumManager for IExplorerManager {
self.log = log;
}

fn get_sender(&self) -> &Sender<String> {
&self.tx
}

fn get_receiver(&self) -> &Receiver<String> {
&self.rx
}

fn get_platform_label(&self) -> &str {
"win32"
}
Expand Down
Loading

0 comments on commit e2b6bd7

Please sign in to comment.