Skip to content

Commit

Permalink
feat(analytics): added country information in events (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmorarian authored Mar 7, 2024
1 parent ca3da14 commit 41899df
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
9 changes: 9 additions & 0 deletions modules/meteroid/src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ pub async fn start_api_server(
.await;

if config.analytics.enabled {
let country = match crate::eventbus::analytics_handler::get_geoip().await {
Ok(geoip) => Some(geoip.country),
Err(err) => {
log::warn!("Failed to obtain data for analytics: {}", err);
None
}
};

eventbus
.subscribe(Arc::new(AnalyticsHandler::new(
config.analytics.clone(),
pool.clone(),
country,
)))
.await;
} else {
Expand Down
6 changes: 4 additions & 2 deletions modules/meteroid/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::sync::Arc;

use tokio::signal;

use common_build_info::BuildInfo;
use common_logging::init::init_telemetry;
use meteroid::adapters::stripe::Stripe;
Expand All @@ -6,8 +10,6 @@ use meteroid::repo::get_pool;
use meteroid::repo::provider_config::{ProviderConfigRepo, ProviderConfigRepoCornucopia};
use meteroid::webhook_in_api;
use meteroid_repository::migrations;
use std::sync::Arc;
use tokio::signal;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
21 changes: 20 additions & 1 deletion modules/meteroid/src/eventbus/analytics_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct AnalyticsHandler {
}

impl AnalyticsHandler {
pub fn new(config: AnalyticsConfig, pool: Pool) -> Self {
pub fn new(config: AnalyticsConfig, pool: Pool, country: Option<String>) -> Self {
let build_info = BuildInfo::get();

// https://segment.com/docs/connections/spec/common/#context
Expand All @@ -34,6 +34,7 @@ impl AnalyticsHandler {
"arch": build_info.target_arch,
},
"git_info": build_info.git_info,
"country": country.unwrap_or_else(|| "unknown".to_string()),
});

AnalyticsHandler {
Expand Down Expand Up @@ -572,3 +573,21 @@ impl EventHandler<Event> for AnalyticsHandler {
Ok(())
}
}

#[derive(Debug, serde::Deserialize)]
pub struct GeoIp {
pub country: String,
}

pub async fn get_geoip() -> Result<GeoIp, String> {
let response = reqwest::Client::new()
.get("https://metero.id/ossapi/geoip")
.send()
.await
.map_err(|e| e.to_string());

match response {
Ok(response) => response.json::<GeoIp>().await.map_err(|e| e.to_string()),
Err(e) => Err(e),
}
}
9 changes: 9 additions & 0 deletions modules/meteroid/src/eventbus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,18 @@ impl EventBusStatic {
.await;

if config.analytics.enabled {
let country = match crate::eventbus::analytics_handler::get_geoip().await {
Ok(geoip) => Some(geoip.country),
Err(err) => {
log::warn!("Failed to obtain data for analytics: {}", err);
None
}
};

bus.subscribe(Arc::new(analytics_handler::AnalyticsHandler::new(
config.analytics.clone(),
pool.clone(),
country,
)))
.await;
} else {
Expand Down

0 comments on commit 41899df

Please sign in to comment.