Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telemetry): add opentelemetry support #346

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
331 changes: 319 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion attest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
use std::env;
use tracing::info_span;

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
orb_telemetry::TelemetryConfig::new()

let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
orb_attest::SYSLOG_IDENTIFIER,
"1.0.0",
env::var("ORB_BACKEND")
.expect("ORB_BACKEND environment variable must be set")
.to_lowercase(),
);

let _telemetry_guard = orb_telemetry::TelemetryConfig::new()
.with_journald(orb_attest::SYSLOG_IDENTIFIER)
.with_opentelemetry(otel_config)
.init();

let main_span =
info_span!("orb_attestation", version = "1.0.0", component = "main");
let _main_guard = main_span.enter();

let app_span = info_span!("application_execution");
let _app_guard = app_span.enter();

orb_attest::main().await
}
16 changes: 10 additions & 6 deletions attest/src/remote_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,16 @@ printf dmFsaWRzaWduYXR1cmU=
// A happy path
#[tokio::test]
async fn get_token() {
orb_telemetry::TelemetryConfig::new().init();
let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
"test-orb-auth",
"test",
"test",
);

let _telemetry_guard = orb_telemetry::TelemetryConfig::new()
.with_opentelemetry(otel_config)
.init();

let mock_server = MockServer::start().await;

Expand Down Expand Up @@ -559,15 +568,12 @@ printf dmFsaWRzaWduYXR1cmU=
.unwrap();
let token_challenge = base_url.join("tokenchallenge").unwrap();

// 1. get challenge
let challenge = crate::remote_api::Challenge::request(orb_id, &token_challenge)
.await
.unwrap();

let clone_of_challenge = challenge.clone();

// Create a mock signing script orb-sign-attestation that returns pre-defined challenge and
// add it to PATH
let mut path = std::env::var("PATH").unwrap();
let temp_dir = tempfile::tempdir().unwrap();
let script = temp_dir.path().join("orb-sign-attestation");
Expand All @@ -578,7 +584,6 @@ printf dmFsaWRzaWduYXR1cmU=
path.push_str(temp_dir.path().to_str().unwrap());
std::env::set_var("PATH", path);

// 2. sign challenge
let signature = tokio::task::spawn_blocking(move || clone_of_challenge.sign())
.await
.unwrap();
Expand All @@ -589,7 +594,6 @@ printf dmFsaWRzaWduYXR1cmU=
signature.unwrap_err()
);

// 3. get token
let token = crate::remote_api::Token::request(
&base_url.join("token").unwrap(),
orb_id,
Expand Down
19 changes: 14 additions & 5 deletions backend-state/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod context;
mod dbus_interface;
mod state;

use std::env;
use std::time::{Duration, Instant};

use clap::Parser;
Expand Down Expand Up @@ -32,12 +33,22 @@ const SYSLOG_IDENTIFIER: &str = "worldcoin-backend-state";
#[command(about, author, version=BUILD_INFO.version, styles=make_clap_v3_styles())]
struct Cli {}

// No need to waste RAM with a threadpool.
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
color_eyre::install()?;
orb_telemetry::TelemetryConfig::new()

let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
SYSLOG_IDENTIFIER,
BUILD_INFO.version,
env::var("ORB_BACKEND")
.expect("ORB_BACKEND environment variable must be set")
.to_lowercase(),
);

let _telemetry_guard = orb_telemetry::TelemetryConfig::new()
.with_journald(SYSLOG_IDENTIFIER)
.with_opentelemetry(otel_config)
.init();

let _args = Cli::parse();
Expand All @@ -47,13 +58,11 @@ async fn main() -> Result<()> {
.wrap_err("failed to connect to zbus session")?;
let msg_stream = zbus::MessageStream::from(conn.clone());
let dbus_disconnect_task_handle = tokio::spawn(async move {
// Until the stream terminates, this will never complete.
let _ = msg_stream.count().await;
bail!("zbus connection terminated!");
});

let (watch_token_task_handle, ctx) = {
// Use env var if present - useful for testing
const VAR: &str = "ORB_AUTH_TOKEN";
if let Ok(token) = std::env::var(VAR) {
std::env::remove_var(VAR);
Expand Down Expand Up @@ -186,7 +195,7 @@ async fn poll_backend(mut ctx: Context) -> ! {

/// Listens for changes to state, and signals that change to the dbus interface.
fn spawn_notify_state_task(
iface: zbus::InterfaceRef<crate::dbus_interface::Interface>,
iface: zbus::InterfaceRef<dbus_interface::Interface>,
mut ctx: Context,
) -> tokio::task::JoinHandle<Result<()>> {
tokio::task::spawn(async move {
Expand Down
27 changes: 22 additions & 5 deletions experiments/zenoh/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{pin::pin, time::Duration};
use std::{env, pin::pin, time::Duration};

use clap::Parser as _;
use color_eyre::Result;
Expand All @@ -16,12 +16,29 @@ enum Args {
}

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
orb_telemetry::TelemetryConfig::new().init();
tracing::debug!("debug logging is enabled");

async fn main() -> Result<()> {
let args = Args::parse();

let service_name = match args {
Args::Alice { .. } => "zenoh-bench-sender",
Args::Bob { .. } => "zenoh-bench-receiver",
};

let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
service_name,
env!("CARGO_PKG_VERSION"),
env::var("ORB_BACKEND")
.expect("ORB_BACKEND environment variable must be set")
.to_lowercase(),
);

let _telemetry_guard = orb_telemetry::TelemetryConfig::new()
.with_opentelemetry(otel_config)
.init();

tracing::debug!("debug logging is enabled");

match args {
Args::Alice { .. } => alice(args).await,
Args::Bob { .. } => bob(args).await,
Expand Down
15 changes: 14 additions & 1 deletion mcu-util/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![forbid(unsafe_code)]

use std::env;
use std::path::PathBuf;
use std::time::Duration;

Expand Down Expand Up @@ -267,7 +268,19 @@ fn clap_v3_styles() -> Styles {
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
orb_telemetry::TelemetryConfig::new().init();

let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
"orb-mcu-util",
BUILD_INFO.version,
env::var("ORB_BACKEND")
.expect("ORB_BACKEND environment variable must be set")
.to_lowercase(),
);

let _telemetry_guard = orb_telemetry::TelemetryConfig::new()
.with_opentelemetry(otel_config)
.init();

let args = Args::parse();

Expand Down
15 changes: 14 additions & 1 deletion supervisor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use clap::{
};
use color_eyre::eyre::WrapErr as _;
use orb_supervisor::startup::{Application, Settings};
use std::env;
use tracing::debug;

use orb_supervisor::BUILD_INFO;
Expand All @@ -30,9 +31,21 @@ fn clap_v3_styles() -> Styles {
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
orb_telemetry::TelemetryConfig::new()

let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
SYSLOG_IDENTIFIER,
BUILD_INFO.version,
env::var("ORB_BACKEND")
.expect("ORB_BACKEND environment variable must be set")
.to_lowercase(),
);

let _telemetry_guard = orb_telemetry::TelemetryConfig::new()
.with_journald(SYSLOG_IDENTIFIER)
.with_opentelemetry(otel_config)
.init();

debug!("initialized telemetry");

let _args = Cli::parse();
Expand Down
19 changes: 16 additions & 3 deletions supervisor/tests/it/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
use std::io;
use std::{env, io};

use dbus_launch::{BusType, Daemon};
use once_cell::sync::Lazy;
use orb_supervisor::startup::{Application, Settings};
use orb_supervisor::BUILD_INFO;
use tokio::task::JoinHandle;
use zbus::{
fdo, interface, proxy, zvariant::OwnedObjectPath, ProxyDefault, SignalContext,
};

pub const WORLDCOIN_CORE_SERVICE_OBJECT_PATH: &str =
"/org/freedesktop/systemd1/unit/worldcoin_2dcore_2eservice";
static TRACING: Lazy<()> = Lazy::new(|| {
orb_telemetry::TelemetryConfig::new().init();

static TRACING: Lazy<orb_telemetry::TelemetryShutdownHandler> = Lazy::new(|| {
let otel_config = orb_telemetry::OpenTelemetryConfig::new(
"http://localhost:4317",
"orb-dbus-manager",
BUILD_INFO.version,
env::var("ORB_BACKEND")
.expect("ORB_BACKEND environment variable must be set")
.to_lowercase(),
);

orb_telemetry::TelemetryConfig::new()
.with_opentelemetry(otel_config)
.init()
});

#[derive(Debug)]
Expand Down
11 changes: 9 additions & 2 deletions telemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ rust-version.workspace = true

[dependencies]
tracing-journald.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
opentelemetry = { version = "0.21", features = ["trace"] }
opentelemetry-otlp = { version = "0.14", features = ["trace", "tonic"] }
opentelemetry_sdk = { version = "0.21", features = ["trace", "rt-tokio"] }
opentelemetry-datadog = "0.10"
tracing-opentelemetry = "0.22"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json", "time"] }
tracing = "0.1.40"
thiserror = "1.0.65"


[target.'cfg(tokio_unstable)'.dependencies]
console-subscriber.workspace = true
Expand Down
Loading
Loading