Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 135 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ bip39 = "2.1.0"
octocrab = "0.44"
serde_with = "3.14.0"

# OpenTelemetry for metrics (OTLP over HTTP)
opentelemetry = { version = "0.30.0", features = ["metrics"] }
opentelemetry-otlp = { version = "0.30.0", features = ["metrics"] }
opentelemetry_sdk = { version = "0.30.0", features = ["metrics"] }

[features]
unstable = []

Expand Down
7 changes: 7 additions & 0 deletions src/commands/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ fn print_status(config: &crate::global::Config) {
if config.telemetry.enabled {
print_telemetry_info();
println!("Telemetry: ON");

// Shows user fingerprint if available
if let Some(ref user_fingerprint) = config.telemetry.user_fingerprint {
println!("User Fingerprint: {}", user_fingerprint);
} else if let Ok(user_fingerprint) = crate::telemetry::get_user_fingerprint() {
println!("User Fingerprint: {}", user_fingerprint);
}
return;
}

Expand Down
27 changes: 23 additions & 4 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,43 @@ pub struct Config {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TelemetryConfig {
pub enabled: bool,
pub user_fingerprint: Option<String>,
pub otlp_endpoint: Option<String>,
}

impl Default for TelemetryConfig {
fn default() -> Self {
Self { enabled: true }
Self {
enabled: true,
user_fingerprint: None,
otlp_endpoint: None,
}
}
}

pub fn ensure_global_config() -> miette::Result<()> {
pub fn ensure_global_config() -> miette::Result<Config> {
let mut trix_path = crate::home::tx3_dir()?;
trix_path.push("trix/config.toml");

let mut config = Config::default();

if !trix_path.exists() {
std::fs::create_dir_all(trix_path.parent().unwrap()).into_diagnostic()?;
save_config(&Config::default())?;
// Generate user fingerprint when creating config for the first time
config.telemetry.user_fingerprint = Some(crate::telemetry::generate_user_fingerprint()?);
config.telemetry.otlp_endpoint = Some(crate::telemetry::DEFAULT_TELEMETRY_ENDPOINT.to_string());
save_config(&config)?;
print_telemetry_info();
} else {
// Ensure existing config has a user fingerprint and otlp_endpoint
config = read_config()?;
if config.telemetry.user_fingerprint.is_none() {
config.telemetry.user_fingerprint = Some(crate::telemetry::generate_user_fingerprint()?);
save_config(&config)?;
}
}

Ok(())
Ok(config)
}

pub fn print_telemetry_info() {
Expand Down
47 changes: 45 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod dirs;
mod global;
mod home;
mod spawn;
mod telemetry;
mod updates;

use commands as cmds;
Expand Down Expand Up @@ -62,6 +63,25 @@ enum Commands {
Telemetry(cmds::telemetry::Args),
}

impl Commands {
fn name(&self) -> &'static str {
match self {
Commands::Init(_) => "init",
Commands::Invoke(_) => "invoke",
Commands::Devnet(_) => "devnet",
Commands::Explore(_) => "explore",
Commands::Bindgen(_) => "bindgen",
Commands::Check(_) => "check",
Commands::Inspect(_) => "inspect",
Commands::Test(_) => "test",
Commands::Build(_) => "build",
Commands::Wallet(_) => "wallet",
Commands::Publish(_) => "publish",
Commands::Telemetry(_) => "telemetry",
}
}
}

pub fn load_config() -> Result<Option<Config>> {
let current_dir = std::env::current_dir().into_diagnostic()?;

Expand All @@ -85,9 +105,22 @@ async fn main() -> Result<()> {

let config = load_config()?;

global::ensure_global_config()?;
let global_config = global::ensure_global_config()?;

// init
let mut meter_provider: Option<opentelemetry_sdk::metrics::SdkMeterProvider> = None;

match config {
if global_config.telemetry.enabled {
let result = telemetry::init_telemetry(global_config.telemetry.otlp_endpoint);
meter_provider = match result {
Ok(provider) => Some(provider),
Err(_) => None
};
}

let command_name: &'static str = cli.command.name();

let result = match config {
Some(config) => match cli.command {
Commands::Init(args) => cmds::init::run(args, Some(&config)),
Commands::Invoke(args) => cmds::devnet::invoke::run(args, &config),
Expand All @@ -107,5 +140,15 @@ async fn main() -> Result<()> {
Commands::Telemetry(args) => cmds::telemetry::run(args),
_ => Err(miette::miette!("No trix.toml found in current directory")),
},
};

// If it's none is because failure or telemetry is off
if let Some(meter_provider) = meter_provider {
// Report command result (success or error)
telemetry::report_command_result(command_name, &result.is_ok());

let _ = meter_provider.shutdown();
}

result
}
Loading
Loading