Skip to content

Commit

Permalink
Enable METRICS_PORT env var for configuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Sep 8, 2021
1 parent cf7895b commit 459a9ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
10 changes: 9 additions & 1 deletion agent/src/main.rs
Expand Up @@ -34,6 +34,9 @@ lazy_static! {
pub static ref DISCOVERY_RESPONSE_TIME_METRIC: HistogramVec = prometheus::register_histogram_vec!("akri_discovery_response_time", "Akri Discovery Response Time", &["configuration"]).unwrap();
}

/// Environment variable name for setting metrics port
const METRICS_PORT_LABEL: &str = "METRICS_PORT";

/// This is the entry point for the Akri Agent.
/// It must be built on unix systems, since the underlying libraries for the `DevicePluginService` unix socket connection are unix only.
#[cfg(unix)]
Expand All @@ -53,9 +56,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>

let mut tasks = Vec::new();

let port = match std::env::var(METRICS_PORT_LABEL) {
Ok(p) => p.parse::<u16>()?,
Err(_) => 8080,
};

// Start server for Prometheus metrics
tasks.push(tokio::spawn(async move {
run_metrics_server().await.unwrap();
run_metrics_server(port).await.unwrap();
}));

let discovery_handler_map = Arc::new(Mutex::new(HashMap::new()));
Expand Down
10 changes: 9 additions & 1 deletion controller/src/main.rs
Expand Up @@ -16,6 +16,9 @@ lazy_static! {
pub static ref BROKER_POD_COUNT_METRIC: IntGaugeVec = prometheus::register_int_gauge_vec!("akri_broker_pod_count", "Akri Broker Pod Count", &["configuration", "node"]).unwrap();
}

/// Environment variable name for setting metrics port
const METRICS_PORT_LABEL: &str = "METRICS_PORT";

/// This is the entry point for the controller.
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand All @@ -37,9 +40,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
let instance_watch_synchronization = synchronization.clone();
let mut tasks = Vec::new();

let port = match std::env::var(METRICS_PORT_LABEL) {
Ok(p) => p.parse::<u16>()?,
Err(_) => 8080,
};

// Start server for prometheus metrics
tasks.push(tokio::spawn(async move {
run_metrics_server().await.unwrap();
run_metrics_server(port).await.unwrap();
}));

// Handle existing instances
Expand Down
2 changes: 1 addition & 1 deletion samples/brokers/udev-video-broker/src/main.rs
Expand Up @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
info!("{} Udev Broker logging started", API_NAMESPACE);

tokio::spawn(async move {
run_metrics_server().await.unwrap();
run_metrics_server(8080).await.unwrap();
});

let env_var_query = ActualEnvVarQuery {};
Expand Down
9 changes: 5 additions & 4 deletions shared/src/akri/metrics.rs
Expand Up @@ -19,10 +19,11 @@ async fn metrics_handler() -> Result<impl Reply, Rejection> {
}

/// Serves prometheus metrics over a web service at /metrics
pub async fn run_metrics_server() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
{
info!("starting metrics server on port 8080 at /metrics");
pub async fn run_metrics_server(
port: u16,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
info!("starting metrics server on port {} at /metrics", port);
let metrics_route = warp::path!("metrics").and_then(metrics_handler);
warp::serve(metrics_route).run(([0, 0, 0, 0], 8080)).await;
warp::serve(metrics_route).run(([0, 0, 0, 0], port)).await;
Ok(())
}

0 comments on commit 459a9ba

Please sign in to comment.