From c363242902e73b3de3decf0ef40f651fe21d1933 Mon Sep 17 00:00:00 2001
From: Dom Dwyer <dom@itsallbroken.com>
Date: Wed, 20 Apr 2022 12:28:18 +0100
Subject: [PATCH] refactor: emit panic metrics for server types

Configures the long-running / server modes to emit panic metrics.
---
 influxdb_iox/src/commands/run/all_in_one.rs |  4 ++--
 influxdb_iox/src/commands/run/compactor.rs  |  4 ++--
 influxdb_iox/src/commands/run/database.rs   |  7 ++++++-
 influxdb_iox/src/commands/run/ingester.rs   |  4 ++--
 influxdb_iox/src/commands/run/main.rs       | 10 ++++++++--
 influxdb_iox/src/commands/run/querier.rs    |  4 ++--
 influxdb_iox/src/commands/run/router.rs     |  7 ++++++-
 influxdb_iox/src/commands/run/router2.rs    |  2 +-
 influxdb_iox/src/commands/run/test.rs       |  7 ++++++-
 9 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/influxdb_iox/src/commands/run/all_in_one.rs b/influxdb_iox/src/commands/run/all_in_one.rs
index 29e24247e2..787880286b 100644
--- a/influxdb_iox/src/commands/run/all_in_one.rs
+++ b/influxdb_iox/src/commands/run/all_in_one.rs
@@ -370,7 +370,7 @@ pub async fn command(config: Config) -> Result<()> {
     info!(?ingester_addresses, "starting querier");
     let querier = create_querier_server_type(
         &common_state,
-        metrics,
+        Arc::clone(&metrics),
         catalog,
         object_store,
         time_provider,
@@ -388,5 +388,5 @@ pub async fn command(config: Config) -> Result<()> {
         Service::create_grpc_only(querier, &querier_run_config),
     ];
 
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(common_state, services, metrics).await?)
 }
diff --git a/influxdb_iox/src/commands/run/compactor.rs b/influxdb_iox/src/commands/run/compactor.rs
index 1fc8657561..04a6479f64 100644
--- a/influxdb_iox/src/commands/run/compactor.rs
+++ b/influxdb_iox/src/commands/run/compactor.rs
@@ -91,7 +91,7 @@ pub async fn command(config: Config) -> Result<(), Error> {
 
     let server_type = create_compactor_server_type(
         &common_state,
-        metric_registry,
+        Arc::clone(&metric_registry),
         catalog,
         object_store,
         exec,
@@ -103,5 +103,5 @@ pub async fn command(config: Config) -> Result<(), Error> {
     info!("starting compactor");
 
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(common_state, services, metric_registry).await?)
 }
diff --git a/influxdb_iox/src/commands/run/database.rs b/influxdb_iox/src/commands/run/database.rs
index f57cb852ff..48db2dca59 100644
--- a/influxdb_iox/src/commands/run/database.rs
+++ b/influxdb_iox/src/commands/run/database.rs
@@ -130,5 +130,10 @@ pub async fn command(config: Config) -> Result<()> {
     ));
 
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(
+        common_state,
+        services,
+        Arc::new(metric::Registry::default()),
+    )
+    .await?)
 }
diff --git a/influxdb_iox/src/commands/run/ingester.rs b/influxdb_iox/src/commands/run/ingester.rs
index 4390a947f0..435f48d87d 100644
--- a/influxdb_iox/src/commands/run/ingester.rs
+++ b/influxdb_iox/src/commands/run/ingester.rs
@@ -89,7 +89,7 @@ pub async fn command(config: Config) -> Result<()> {
     let exec = Arc::new(Executor::new(config.query_exec_thread_count));
     let server_type = create_ingester_server_type(
         &common_state,
-        metric_registry,
+        Arc::clone(&metric_registry),
         catalog,
         object_store,
         exec,
@@ -101,5 +101,5 @@ pub async fn command(config: Config) -> Result<()> {
     info!("starting ingester");
 
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(common_state, services, metric_registry).await?)
 }
diff --git a/influxdb_iox/src/commands/run/main.rs b/influxdb_iox/src/commands/run/main.rs
index 88302f009b..79b4c60b09 100644
--- a/influxdb_iox/src/commands/run/main.rs
+++ b/influxdb_iox/src/commands/run/main.rs
@@ -1,3 +1,5 @@
+use std::sync::Arc;
+
 use ioxd_common::Service;
 use ioxd_common::{grpc_listener, http_listener, serve, server_type::CommonServerState};
 use observability_deps::tracing::{error, info};
@@ -66,7 +68,11 @@ fn build_malloc_conf() -> String {
 ///
 /// Due to its invasive nature (install global panic handling,
 /// logging, etc) this function should not be used during unit tests.
-pub async fn main(common_state: CommonServerState, services: Vec<Service>) -> Result<()> {
+pub async fn main(
+    common_state: CommonServerState,
+    services: Vec<Service>,
+    metrics: Arc<metric::Registry>,
+) -> Result<()> {
     let git_hash = env!("GIT_HASH", "starting influxdb_iox server");
     let num_cpus = num_cpus::get();
     let build_malloc_conf = build_malloc_conf();
@@ -98,7 +104,7 @@ pub async fn main(common_state: CommonServerState, services: Vec<Service>) -> Re
     // lifetime of the program - this is actually a good thing, as it prevents
     // the panic handler from being removed while unwinding a panic (which in
     // turn, causes a panic - see #548)
-    let f = SendPanicsToTracing::new();
+    let f = SendPanicsToTracing::new().with_metrics(&*metrics);
     std::mem::forget(f);
 
     // Register jemalloc metrics
diff --git a/influxdb_iox/src/commands/run/querier.rs b/influxdb_iox/src/commands/run/querier.rs
index 09758c18a1..0f1495f415 100644
--- a/influxdb_iox/src/commands/run/querier.rs
+++ b/influxdb_iox/src/commands/run/querier.rs
@@ -89,7 +89,7 @@ pub async fn command(config: Config) -> Result<(), Error> {
     let exec = Arc::new(Executor::new(num_threads));
     let server_type = create_querier_server_type(
         &common_state,
-        metric_registry,
+        Arc::clone(&metric_registry),
         catalog,
         object_store,
         time_provider,
@@ -101,5 +101,5 @@ pub async fn command(config: Config) -> Result<(), Error> {
     info!("starting querier");
 
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(common_state, services, metric_registry).await?)
 }
diff --git a/influxdb_iox/src/commands/run/router.rs b/influxdb_iox/src/commands/run/router.rs
index 25dbb17b60..16bcd22543 100644
--- a/influxdb_iox/src/commands/run/router.rs
+++ b/influxdb_iox/src/commands/run/router.rs
@@ -159,5 +159,10 @@ pub async fn command(config: Config) -> Result<()> {
     ));
 
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(
+        common_state,
+        services,
+        Arc::new(metric::Registry::default()),
+    )
+    .await?)
 }
diff --git a/influxdb_iox/src/commands/run/router2.rs b/influxdb_iox/src/commands/run/router2.rs
index 5970d27c69..9f198e8add 100644
--- a/influxdb_iox/src/commands/run/router2.rs
+++ b/influxdb_iox/src/commands/run/router2.rs
@@ -96,5 +96,5 @@ pub async fn command(config: Config) -> Result<()> {
 
     info!("starting router2");
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(common_state, services, metrics).await?)
 }
diff --git a/influxdb_iox/src/commands/run/test.rs b/influxdb_iox/src/commands/run/test.rs
index b2ac0a0924..4c12f6e628 100644
--- a/influxdb_iox/src/commands/run/test.rs
+++ b/influxdb_iox/src/commands/run/test.rs
@@ -61,5 +61,10 @@ pub async fn command(config: Config) -> Result<()> {
     ));
 
     let services = vec![Service::create(server_type, common_state.run_config())];
-    Ok(main::main(common_state, services).await?)
+    Ok(main::main(
+        common_state,
+        services,
+        Arc::new(metric::Registry::default()),
+    )
+    .await?)
 }