From ffcea325f6a82475881b3086985c2f837ac6d9cc Mon Sep 17 00:00:00 2001
From: Daniel Lundin <dln@arity.se>
Date: Mon, 16 Dec 2024 18:46:14 +0100
Subject: [PATCH] Move instrumentation to common crate

---
 Cargo.lock                                    | 31 ++++++++++++++++++-
 Cargo.toml                                    |  3 +-
 agent/Cargo.toml                              |  1 +
 agent/src/main.rs                             |  9 ++----
 common/Cargo.toml                             | 29 +++++++++++++++++
 {controller => common}/src/instrumentation.rs |  0
 common/src/lib.rs                             |  1 +
 controller/Cargo.toml                         |  5 +--
 controller/src/bin/patagia-controller.rs      |  2 +-
 controller/src/lib.rs                         |  1 -
 10 files changed, 67 insertions(+), 15 deletions(-)
 create mode 100644 common/Cargo.toml
 rename {controller => common}/src/instrumentation.rs (100%)
 create mode 100644 common/src/lib.rs

diff --git a/Cargo.lock b/Cargo.lock
index 0f3a1f8..f18a400 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,6 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
-version = 3
+version = 4
 
 [[package]]
 name = "addr2line"
@@ -1558,6 +1558,7 @@ version = "0.1.0"
 dependencies = [
  "anyhow",
  "clap",
+ "patagia-common",
  "progenitor",
  "reqwest",
  "schemars",
@@ -1568,6 +1569,33 @@ dependencies = [
  "tracing-subscriber",
 ]
 
+[[package]]
+name = "patagia-common"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "dropshot",
+ "http",
+ "once_cell",
+ "opentelemetry",
+ "opentelemetry-appender-tracing",
+ "opentelemetry-otlp",
+ "opentelemetry-semantic-conventions",
+ "opentelemetry-stdout",
+ "opentelemetry_sdk",
+ "schemars",
+ "serde",
+ "slog",
+ "slog-async",
+ "tokio",
+ "tracing",
+ "tracing-chrome",
+ "tracing-core",
+ "tracing-opentelemetry",
+ "tracing-slog",
+ "tracing-subscriber",
+]
+
 [[package]]
 name = "patagia-controller"
 version = "0.1.0"
@@ -1583,6 +1611,7 @@ dependencies = [
  "opentelemetry-semantic-conventions",
  "opentelemetry-stdout",
  "opentelemetry_sdk",
+ "patagia-common",
  "schemars",
  "serde",
  "slog",
diff --git a/Cargo.toml b/Cargo.toml
index bd87172..4cd3a57 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,16 +2,17 @@
 resolver = "2"
 members = [
   "agent",
+  "common",
   "controller",
   "xtask",
 ]
 default-members = [
   "agent",
+  "common",
   "controller",
   "xtask",
 ]
 
-
 [workspace.package]
 version = "0.2.0"
 edition = "2021"
diff --git a/agent/Cargo.toml b/agent/Cargo.toml
index 9a87168..6f7adb9 100644
--- a/agent/Cargo.toml
+++ b/agent/Cargo.toml
@@ -7,6 +7,7 @@ license = "MPL-2.0"
 [dependencies]
 anyhow.workspace = true
 clap.workspace = true
+patagia-common = { path = "../common" }
 progenitor.workspace = true
 reqwest.workspace = true
 schemars.workspace = true
diff --git a/agent/src/main.rs b/agent/src/main.rs
index c3e72ed..4395b73 100644
--- a/agent/src/main.rs
+++ b/agent/src/main.rs
@@ -1,9 +1,9 @@
 use anyhow::Result;
 use clap::Parser;
 use tokio::time::{sleep, Duration};
-use tracing_subscriber::prelude::*;
 
 mod patagia_api;
+use patagia_common::instrumentation;
 
 #[derive(Parser, Debug)]
 #[command(version, about, long_about = None)]
@@ -12,12 +12,7 @@ struct Cli {}
 #[tokio::main]
 async fn main() -> Result<()> {
     let _args = Cli::parse();
-    let fmt_layer = tracing_subscriber::fmt::layer();
-
-    tracing_subscriber::registry()
-        .with(tracing_subscriber::EnvFilter::from_default_env())
-        .with(fmt_layer)
-        .init();
+    let _tracing = instrumentation::init_tracing_subscriber()?;
 
     tracing::info!("Patagia Agent");
 
diff --git a/common/Cargo.toml b/common/Cargo.toml
new file mode 100644
index 0000000..596a442
--- /dev/null
+++ b/common/Cargo.toml
@@ -0,0 +1,29 @@
+[package]
+name = "patagia-common"
+description = "Common control plane modules"
+version = "0.1.0"
+edition = "2021"
+license = "MPL-2.0"
+
+[dependencies]
+anyhow.workspace = true
+dropshot.workspace = true
+http.workspace = true
+once_cell.workspace = true
+opentelemetry-appender-tracing.workspace = true
+opentelemetry-otlp.workspace = true
+opentelemetry_sdk.workspace = true
+opentelemetry-semantic-conventions.workspace = true
+opentelemetry-stdout.workspace = true
+opentelemetry.workspace = true
+schemars.workspace = true
+serde.workspace = true
+slog-async.workspace = true
+slog.workspace = true
+tokio.workspace = true
+tracing-chrome.workspace = true
+tracing-core.workspace = true
+tracing-opentelemetry.workspace = true
+tracing-slog.workspace = true
+tracing-subscriber.workspace = true
+tracing.workspace = true
diff --git a/controller/src/instrumentation.rs b/common/src/instrumentation.rs
similarity index 100%
rename from controller/src/instrumentation.rs
rename to common/src/instrumentation.rs
diff --git a/common/src/lib.rs b/common/src/lib.rs
new file mode 100644
index 0000000..9b48f64
--- /dev/null
+++ b/common/src/lib.rs
@@ -0,0 +1 @@
+pub mod instrumentation;
diff --git a/controller/Cargo.toml b/controller/Cargo.toml
index 4bab331..4acc0d1 100644
--- a/controller/Cargo.toml
+++ b/controller/Cargo.toml
@@ -17,6 +17,7 @@ opentelemetry_sdk.workspace = true
 opentelemetry-semantic-conventions.workspace = true
 opentelemetry-stdout.workspace = true
 opentelemetry.workspace = true
+patagia-common = { path = "../common" }
 schemars.workspace = true
 serde.workspace = true
 slog-async.workspace = true
@@ -28,7 +29,3 @@ tracing-opentelemetry.workspace = true
 tracing-slog.workspace = true
 tracing-subscriber.workspace = true
 tracing.workspace = true
-#
-# [[bin]]
-# name = "patagia-controller"
-# doc = false
diff --git a/controller/src/bin/patagia-controller.rs b/controller/src/bin/patagia-controller.rs
index 0a132f4..3c5e88e 100644
--- a/controller/src/bin/patagia-controller.rs
+++ b/controller/src/bin/patagia-controller.rs
@@ -9,9 +9,9 @@ use std::net::SocketAddr;
 use std::str::FromStr;
 use std::sync::Arc;
 
+use patagia_common::instrumentation;
 use patagia_controller::api;
 use patagia_controller::context::ControllerContext;
-use patagia_controller::instrumentation;
 
 #[derive(Parser, Debug)]
 #[command(version, about, long_about = None)]
diff --git a/controller/src/lib.rs b/controller/src/lib.rs
index 9031343..0caaf72 100644
--- a/controller/src/lib.rs
+++ b/controller/src/lib.rs
@@ -1,5 +1,4 @@
 pub mod api;
 pub mod context;
-pub mod instrumentation;
 
 mod version;