Skip to content

Commit

Permalink
Diagnostics: support user-defined attribution.
Browse files Browse the repository at this point in the history
Allows a user to specify an additional value to associate their diagnostics with that value. nix-installer doesn't generate or store these values, and most users have no need for it.
  • Loading branch information
grahamc committed Sep 19, 2023
1 parent 3961e46 commit 90b5543
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ Here is a table of the [diagnostic data we collect][diagnosticdata]:
| `is_ci` | Whether the installer is being used in CI (e.g. GitHub Actions). |
| `action` | Either `Install` or `Uninstall`. |
| `status` | One of `Success`, `Failure`, `Pending`, or `Cancelled`. |
| `attribution` | Optionally defined by the user, associate the diagnostics of this run to the provided value. |
| `failure_chain` | A high level description of what the failure was, if any. For example: `Command("diskutil")` if the command `diskutil list` failed. |
To disable diagnostic reporting, set the diagnostics URL to an empty string by passing `--diagnostic-endpoint=""` or setting `NIX_INSTALLER_DIAGNOSTIC_ENDPOINT=""`.
Expand Down
1 change: 1 addition & 0 deletions src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl Planner for MyPlanner {
#[cfg(feature = "diagnostics")]
async fn diagnostic_data(&self) -> Result<nix_installer::diagnostics::DiagnosticData, PlannerError> {
Ok(nix_installer::diagnostics::DiagnosticData::new(
self.common.attribution.clone(),
self.common.diagnostic_endpoint.clone(),
self.typetag_name().into(),
self.configured_settings()
Expand Down
6 changes: 6 additions & 0 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum DiagnosticAction {
/// A report sent to an endpoint
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct DiagnosticReport {
pub attribution: Option<String>,
pub version: String,
pub planner: String,
pub configured_settings: Vec<String>,
Expand All @@ -50,6 +51,7 @@ pub struct DiagnosticReport {
/// A preparation of data to be sent to the `endpoint`.
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, Default)]
pub struct DiagnosticData {
attribution: Option<String>,
version: String,
planner: String,
configured_settings: Vec<String>,
Expand All @@ -65,6 +67,7 @@ pub struct DiagnosticData {

impl DiagnosticData {
pub fn new(
attribution: Option<String>,
endpoint: Option<String>,
planner: String,
configured_settings: Vec<String>,
Expand All @@ -81,6 +84,7 @@ impl DiagnosticData {
let is_ci = is_ci::cached()
|| std::env::var("NIX_INSTALLER_CI").unwrap_or_else(|_| "0".into()) == "1";
Ok(Self {
attribution,
endpoint,
version: env!("CARGO_PKG_VERSION").into(),
planner,
Expand Down Expand Up @@ -131,6 +135,7 @@ impl DiagnosticData {

pub fn report(&self, action: DiagnosticAction, status: DiagnosticStatus) -> DiagnosticReport {
let Self {
attribution,
version,
planner,
configured_settings,
Expand All @@ -143,6 +148,7 @@ impl DiagnosticData {
failure_chain,
} = self;
DiagnosticReport {
attribution: attribution.clone(),
version: version.clone(),
planner: planner.clone(),
configured_settings: configured_settings.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/planner/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl Planner for Linux {
#[cfg(feature = "diagnostics")]
async fn diagnostic_data(&self) -> Result<crate::diagnostics::DiagnosticData, PlannerError> {
Ok(crate::diagnostics::DiagnosticData::new(
self.settings.attribution.clone(),
self.settings.diagnostic_endpoint.clone(),
self.typetag_name().into(),
self.configured_settings()
Expand Down
1 change: 1 addition & 0 deletions src/planner/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Planner for Macos {
#[cfg(feature = "diagnostics")]
async fn diagnostic_data(&self) -> Result<crate::diagnostics::DiagnosticData, PlannerError> {
Ok(crate::diagnostics::DiagnosticData::new(
self.settings.attribution.clone(),
self.settings.diagnostic_endpoint.clone(),
self.typetag_name().into(),
self.configured_settings()
Expand Down
1 change: 1 addition & 0 deletions src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Planner for MyPlanner {
#[cfg(feature = "diagnostics")]
async fn diagnostic_data(&self) -> Result<nix_installer::diagnostics::DiagnosticData, PlannerError> {
Ok(nix_installer::diagnostics::DiagnosticData::new(
self.common.attribution.clone(),
self.common.diagnostic_endpoint.clone(),
self.typetag_name().into(),
self.configured_settings()
Expand Down
1 change: 1 addition & 0 deletions src/planner/ostree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ impl Planner for Ostree {
#[cfg(feature = "diagnostics")]
async fn diagnostic_data(&self) -> Result<crate::diagnostics::DiagnosticData, PlannerError> {
Ok(crate::diagnostics::DiagnosticData::new(
self.settings.attribution.clone(),
self.settings.diagnostic_endpoint.clone(),
self.typetag_name().into(),
self.configured_settings()
Expand Down
1 change: 1 addition & 0 deletions src/planner/steam_deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ impl Planner for SteamDeck {
#[cfg(feature = "diagnostics")]
async fn diagnostic_data(&self) -> Result<crate::diagnostics::DiagnosticData, PlannerError> {
Ok(crate::diagnostics::DiagnosticData::new(
self.settings.attribution.clone(),
self.settings.diagnostic_endpoint.clone(),
self.typetag_name().into(),
self.configured_settings()
Expand Down
14 changes: 14 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ Settings which only apply to certain [`Planner`](crate::planner::Planner)s shoul
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct CommonSettings {
/// Attribute the diagnostics of this run to a specific usage category
#[cfg_attr(
feature = "cli",
clap(
long,
default_value = None,
env = "NIX_INSTALLER_ATTRIBUTION",
global = true
)
)]
pub attribution: Option<String>,

/// Modify the user profile to automatically load nix
#[cfg_attr(
feature = "cli",
Expand Down Expand Up @@ -289,6 +301,7 @@ impl CommonSettings {
};

Ok(Self {
attribution: None,
modify_profile: true,
nix_build_group_name: String::from("nixbld"),
nix_build_group_id: 30_000,
Expand All @@ -308,6 +321,7 @@ impl CommonSettings {
/// A listing of the settings, suitable for [`Planner::settings`](crate::planner::Planner::settings)
pub fn settings(&self) -> Result<HashMap<String, serde_json::Value>, InstallSettingsError> {
let Self {
attribution: _,
modify_profile,
nix_build_group_name,
nix_build_group_id,
Expand Down

0 comments on commit 90b5543

Please sign in to comment.