From 7eeb72877753d7de7e04c01e08614ad35878c807 Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Tue, 26 Mar 2024 19:29:07 +0000 Subject: [PATCH 1/7] Updates to new working branch --- Cargo.toml | 5 +++++ azurekvp/Cargo.toml | 24 ++++++++++++++++++++++++ azurekvp/src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 azurekvp/Cargo.toml create mode 100644 azurekvp/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index e85ea00a..410a83dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,10 @@ tokio = { version = "1", features = ["full"] } path = "libazureinit" version = "0.1.0" +[dependencies.azurekvp] +path = "azurekvp" +version = "0.1.0" + [profile.dev] incremental = true @@ -30,4 +34,5 @@ path = "tests/functional_tests.rs" [workspace] members = [ "libazureinit", + "azurekvp", ] diff --git a/azurekvp/Cargo.toml b/azurekvp/Cargo.toml new file mode 100644 index 00000000..ba068c8d --- /dev/null +++ b/azurekvp/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "azurekvp" +version = "0.1.1" +edition = "2021" +repository = "https://github.com/Azure/azure-init/" +homepage = "https://github.com/Azure/azure-init/" +license = "MIT" +description = "A binary library for implementing OpenTelemetry KVP for Linux VMs on Azure." + + +[dependencies] +opentelemetry = "0.21" +opentelemetry_sdk = "0.21" +opentelemetry-stdout = { version = "0.2.0", features = ["trace"] } +opentelemetry-otlp = { version = "0.14.0", features = ["default"] } +tracing = "0.1.40" +tracing-opentelemetry = "0.22" +tracing-subscriber = "0.2" +tracing-appender = "0.2" + + +[[bin]] +name = "azure-kvp" +path = "src/main.rs" \ No newline at end of file diff --git a/azurekvp/src/main.rs b/azurekvp/src/main.rs new file mode 100644 index 00000000..6563a100 --- /dev/null +++ b/azurekvp/src/main.rs @@ -0,0 +1,44 @@ +use opentelemetry::trace::TracerProvider as _; +use opentelemetry_sdk::trace::TracerProvider; +use opentelemetry_stdout as stdout; +use std::fs::File; +use std::io::{self, Write}; +use tracing::{error, span}; +use tracing_subscriber::fmt::format::FmtSpan; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::Registry; +use tracing_appender::rolling::{RollingFileAppender, Rotation}; + +fn main() { + // Create a new OpenTelemetry trace pipeline that prints to stdout + let provider = TracerProvider::builder() + .with_simple_exporter(stdout::SpanExporter::default()) + .build(); + let tracer = provider.tracer("readme_example"); + + // Specify the log file path + let log_file_path = "azurekvp/src/spans.log"; + let file_name_prefix = "spans.log"; + + // Check if the log file exists, and create it if not + if !std::path::Path::new(log_file_path).exists() { + let _ = File::create(log_file_path).expect("Failed to create log file"); + } + // Create a file appender using tracing-appender + let appender = RollingFileAppender::new(Rotation:: NEVER, log_file_path, file_name_prefix); + + // Create a tracing layer with the configured tracer and file appender + let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); + let subscriber = Registry::default() + .with(telemetry) + .with(Layer::new().with_writer(appender)); + //.with(FmtSpan::default().with_writer(appender)); + + // Use the tracing subscriber `Registry` + tracing::subscriber::with_default(subscriber, || { + // Spans will be sent to the configured OpenTelemetry exporter + let root = span!(tracing::Level::TRACE, "app_start", work_units = 2); + let _enter = root.enter(); + error!("This event will be logged in the root span."); + }); +} \ No newline at end of file From ba4d5c089b808aba824c90b1df4a1a6e61bba61d Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:44:36 +0000 Subject: [PATCH 2/7] Temporary changes to program --- azurekvp/Cargo.toml | 12 +++---- azurekvp/src/main.rs | 83 +++++++++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/azurekvp/Cargo.toml b/azurekvp/Cargo.toml index ba068c8d..f5528c49 100644 --- a/azurekvp/Cargo.toml +++ b/azurekvp/Cargo.toml @@ -11,12 +11,12 @@ description = "A binary library for implementing OpenTelemetry KVP for Linux VMs [dependencies] opentelemetry = "0.21" opentelemetry_sdk = "0.21" -opentelemetry-stdout = { version = "0.2.0", features = ["trace"] } -opentelemetry-otlp = { version = "0.14.0", features = ["default"] } -tracing = "0.1.40" -tracing-opentelemetry = "0.22" -tracing-subscriber = "0.2" -tracing-appender = "0.2" +opentelemetry-stdout = "0.2.0" +tracing = "0.1" +tracing-opentelemetry = "0.11" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.68" +tracing-subscriber = "0.3" [[bin]] diff --git a/azurekvp/src/main.rs b/azurekvp/src/main.rs index 6563a100..61f1a29f 100644 --- a/azurekvp/src/main.rs +++ b/azurekvp/src/main.rs @@ -1,40 +1,67 @@ -use opentelemetry::trace::TracerProvider as _; -use opentelemetry_sdk::trace::TracerProvider; -use opentelemetry_stdout as stdout; -use std::fs::File; -use std::io::{self, Write}; +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. +use opentelemetry::trace::{TracerProvider, Tracer}; +use opentelemetry_sdk::export::trace::SpanExporter; use tracing::{error, span}; -use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::Registry; -use tracing_appender::rolling::{RollingFileAppender, Rotation}; +use std::fs::File; +use std::io::Write; +use std::pin::Pin; +use std::future::Future; +use opentelemetry::trace::TraceError; + +#[derive(Debug)] +struct FileExporter { + file: File, +} +impl FileExporter { + fn new(file: File) -> Self { + FileExporter { file } + } +} +impl SpanExporter for FileExporter { + fn export(&mut self, batch: Vec) -> Pin> + Send + 'static>> { + Box::pin(async move { + for span_data in batch { + let span_json = match serde_json::to_string(&span_data) { + Ok(json) => json, + Err(e) => { + error!("Failed to serialize span data: {:?}", e); + String::default() + } + }; + if let Err(e) = writeln!(self.file, "{}", span_json) { + error!("Failed to write span data to file: {:?}", e); + } + } + Ok(()) + }) + } + fn shutdown(&mut self) {} + } fn main() { - // Create a new OpenTelemetry trace pipeline that prints to stdout - let provider = TracerProvider::builder() - .with_simple_exporter(stdout::SpanExporter::default()) + // Open a file for writing spans + let log_file_path = "spans.log"; + let file = File::create(log_file_path).expect("Failed to create file"); + let exporter = FileExporter::new(file); + + // Create a new OpenTelemetry trace pipeline with custom file exporter + let provider = opentelemetry_sdk::trace::TracerProvider::builder() + .with_simple_exporter(exporter) .build(); + let tracer = provider.tracer("readme_example"); + // Create a tracing layer with the configured tracer + + let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); - // Specify the log file path - let log_file_path = "azurekvp/src/spans.log"; - let file_name_prefix = "spans.log"; + // Use the tracing subscriber `Registry`, or any other subscriber + // that impls `LookupSpan` + let subscriber = Registry::default().with(telemetry); - // Check if the log file exists, and create it if not - if !std::path::Path::new(log_file_path).exists() { - let _ = File::create(log_file_path).expect("Failed to create log file"); - } - // Create a file appender using tracing-appender - let appender = RollingFileAppender::new(Rotation:: NEVER, log_file_path, file_name_prefix); - - // Create a tracing layer with the configured tracer and file appender - let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); - let subscriber = Registry::default() - .with(telemetry) - .with(Layer::new().with_writer(appender)); - //.with(FmtSpan::default().with_writer(appender)); - - // Use the tracing subscriber `Registry` + // Trace executed code tracing::subscriber::with_default(subscriber, || { // Spans will be sent to the configured OpenTelemetry exporter let root = span!(tracing::Level::TRACE, "app_start", work_units = 2); From 5d8910e3c9dcd4b08e89c439050d72777b47b430 Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:22:31 +0000 Subject: [PATCH 3/7] Working redirect of spans froms stdout to a file --- azurekvp/Cargo.toml | 9 ++-- azurekvp/src/main.rs | 100 +++++++++++++++++++------------------------ 2 files changed, 49 insertions(+), 60 deletions(-) diff --git a/azurekvp/Cargo.toml b/azurekvp/Cargo.toml index f5528c49..7331dbd3 100644 --- a/azurekvp/Cargo.toml +++ b/azurekvp/Cargo.toml @@ -11,12 +11,15 @@ description = "A binary library for implementing OpenTelemetry KVP for Linux VMs [dependencies] opentelemetry = "0.21" opentelemetry_sdk = "0.21" -opentelemetry-stdout = "0.2.0" +opentelemetry-stdout = { version = "0.2.0", features = ["trace"] } tracing = "0.1" -tracing-opentelemetry = "0.11" -serde = { version = "1.0", features = ["derive"] } +tracing-opentelemetry = "0.22" +serde = { version = "1.0" } serde_json = "1.0.68" tracing-subscriber = "0.3" +tokio = { version = "1", features = ["full"] } +nix = "0.22.0" + [[bin]] diff --git a/azurekvp/src/main.rs b/azurekvp/src/main.rs index 61f1a29f..c01733ac 100644 --- a/azurekvp/src/main.rs +++ b/azurekvp/src/main.rs @@ -1,71 +1,57 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use opentelemetry::trace::{TracerProvider, Tracer}; +use opentelemetry::trace::TracerProvider as _; +use opentelemetry_sdk::trace::TracerProvider; use opentelemetry_sdk::export::trace::SpanExporter; +use opentelemetry_stdout as stdout; use tracing::{error, span}; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::Registry; use std::fs::File; -use std::io::Write; -use std::pin::Pin; -use std::future::Future; -use opentelemetry::trace::TraceError; +use std::io::{self, Write}; +use nix::unistd::dup2; +use nix::libc; +use std::os::fd::AsRawFd; -#[derive(Debug)] -struct FileExporter { - file: File, -} -impl FileExporter { - fn new(file: File) -> Self { - FileExporter { file } - } -} -impl SpanExporter for FileExporter { - fn export(&mut self, batch: Vec) -> Pin> + Send + 'static>> { - Box::pin(async move { - for span_data in batch { - let span_json = match serde_json::to_string(&span_data) { - Ok(json) => json, - Err(e) => { - error!("Failed to serialize span data: {:?}", e); - String::default() - } - }; - if let Err(e) = writeln!(self.file, "{}", span_json) { - error!("Failed to write span data to file: {:?}", e); - } - } - Ok(()) - }) - } - fn shutdown(&mut self) {} - } -fn main() { - // Open a file for writing spans - let log_file_path = "spans.log"; - let file = File::create(log_file_path).expect("Failed to create file"); - let exporter = FileExporter::new(file); +fn main() -> io::Result<()> { + // Specify the log file path + let log_file_path = "spans.log"; - // Create a new OpenTelemetry trace pipeline with custom file exporter - let provider = opentelemetry_sdk::trace::TracerProvider::builder() - .with_simple_exporter(exporter) - .build(); + // Create a new file for writing + let file = File::create(log_file_path)?; - let tracer = provider.tracer("readme_example"); - // Create a tracing layer with the configured tracer - - let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); + // Duplicate file descriptor to stdout + let stdout_fd = file.as_raw_fd(); + dup2(stdout_fd, libc::STDOUT_FILENO)?; - // Use the tracing subscriber `Registry`, or any other subscriber - // that impls `LookupSpan` - let subscriber = Registry::default().with(telemetry); + // Create a new OpenTelemetry trace pipeline that prints to stdout + let provider = TracerProvider::builder() + .with_simple_exporter(stdout::SpanExporter::default()) + .build(); + let tracer = provider.tracer("readme_example"); - // Trace executed code - tracing::subscriber::with_default(subscriber, || { - // Spans will be sent to the configured OpenTelemetry exporter - let root = span!(tracing::Level::TRACE, "app_start", work_units = 2); - let _enter = root.enter(); - error!("This event will be logged in the root span."); - }); + // Create a tracing layer with the configured tracer + let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); + + // Use the tracing subscriber `Registry`, or any other subscriber + // that impls `LookupSpan` + let subscriber = Registry::default().with(telemetry); + + // Trace executed code + tracing::subscriber::with_default(subscriber, || { + // Spans will be sent to the configured OpenTelemetry exporter + let root = span!(tracing::Level::TRACE, "app_start", work_units = 2); + let _enter = root.enter(); + + // Error events will be logged in the root span + error!("This event will be logged in the root span."); + + // Redirect stdout to the file after tracing setup + let file = File::create(log_file_path)?; + let stdout_fd = file.as_raw_fd(); + dup2(stdout_fd, libc::STDOUT_FILENO)?; + + Ok(()) + }) } \ No newline at end of file From 0a83a368441e35835b85889157ba2d7cb6dae1fa Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:35:26 +0000 Subject: [PATCH 4/7] Beginning of instrumentation process --- azurekvp/src/main.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/azurekvp/src/main.rs b/azurekvp/src/main.rs index c01733ac..923fd0ea 100644 --- a/azurekvp/src/main.rs +++ b/azurekvp/src/main.rs @@ -12,8 +12,15 @@ use std::io::{self, Write}; use nix::unistd::dup2; use nix::libc; use std::os::fd::AsRawFd; +use tracing::instrument; +#[instrument] +fn my_function() { + // Code inside my_function + error!("This is an error logged inside my_function."); +} + fn main() -> io::Result<()> { // Specify the log file path let log_file_path = "spans.log"; @@ -21,10 +28,6 @@ fn main() -> io::Result<()> { // Create a new file for writing let file = File::create(log_file_path)?; - // Duplicate file descriptor to stdout - let stdout_fd = file.as_raw_fd(); - dup2(stdout_fd, libc::STDOUT_FILENO)?; - // Create a new OpenTelemetry trace pipeline that prints to stdout let provider = TracerProvider::builder() .with_simple_exporter(stdout::SpanExporter::default()) @@ -38,17 +41,12 @@ fn main() -> io::Result<()> { // that impls `LookupSpan` let subscriber = Registry::default().with(telemetry); - // Trace executed code + // Initialize the tracing subscriber tracing::subscriber::with_default(subscriber, || { - // Spans will be sent to the configured OpenTelemetry exporter - let root = span!(tracing::Level::TRACE, "app_start", work_units = 2); - let _enter = root.enter(); - - // Error events will be logged in the root span - error!("This event will be logged in the root span."); + // Call the instrumented function + my_function(); // Redirect stdout to the file after tracing setup - let file = File::create(log_file_path)?; let stdout_fd = file.as_raw_fd(); dup2(stdout_fd, libc::STDOUT_FILENO)?; From bca37ebf8335b27a5f110f0a6e76f96756ceb3ab Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Fri, 3 May 2024 20:34:50 +0000 Subject: [PATCH 5/7] Working instrumentation and global tracer functionality --- Cargo.toml | 4 ++ azurekvp/Cargo.toml | 21 +++---- azurekvp/src/lib.rs | 4 ++ azurekvp/src/main.rs | 55 ----------------- azurekvp/src/tracing.rs | 46 ++++++++++++++ spans.log | 132 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 +++++ 7 files changed, 211 insertions(+), 66 deletions(-) create mode 100644 azurekvp/src/lib.rs delete mode 100644 azurekvp/src/main.rs create mode 100644 azurekvp/src/tracing.rs create mode 100644 spans.log diff --git a/Cargo.toml b/Cargo.toml index 410a83dc..c8a9ac20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,10 @@ description = "A reference implementation for provisioning Linux VMs on Azure." [dependencies] exitcode = "1.1.2" tokio = { version = "1", features = ["full"] } +opentelemetry_sdk = "0.21" +opentelemetry = "0.21" +tracing = "0.1" + [dependencies.libazureinit] path = "libazureinit" diff --git a/azurekvp/Cargo.toml b/azurekvp/Cargo.toml index 7331dbd3..bc20013d 100644 --- a/azurekvp/Cargo.toml +++ b/azurekvp/Cargo.toml @@ -9,19 +9,18 @@ description = "A binary library for implementing OpenTelemetry KVP for Linux VMs [dependencies] -opentelemetry = "0.21" -opentelemetry_sdk = "0.21" -opentelemetry-stdout = { version = "0.2.0", features = ["trace"] } +once_cell = "1.12.0" +opentelemetry = "0.18.0" +opentelemetry_sdk = "0.22.0" +opentelemetry-stdout = "0.2.0" tracing = "0.1" -tracing-opentelemetry = "0.22" +tracing-opentelemetry = "0.18.0" +tracing-subscriber = "0.3.11" serde = { version = "1.0" } serde_json = "1.0.68" -tracing-subscriber = "0.3" tokio = { version = "1", features = ["full"] } -nix = "0.22.0" +libc = "0.2" - - -[[bin]] -name = "azure-kvp" -path = "src/main.rs" \ No newline at end of file +[lib] +name = "azurekvp" +path = "src/tracing.rs" \ No newline at end of file diff --git a/azurekvp/src/lib.rs b/azurekvp/src/lib.rs new file mode 100644 index 00000000..a6b6f9b8 --- /dev/null +++ b/azurekvp/src/lib.rs @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +pub mod main; diff --git a/azurekvp/src/main.rs b/azurekvp/src/main.rs deleted file mode 100644 index 923fd0ea..00000000 --- a/azurekvp/src/main.rs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. -use opentelemetry::trace::TracerProvider as _; -use opentelemetry_sdk::trace::TracerProvider; -use opentelemetry_sdk::export::trace::SpanExporter; -use opentelemetry_stdout as stdout; -use tracing::{error, span}; -use tracing_subscriber::layer::SubscriberExt; -use tracing_subscriber::Registry; -use std::fs::File; -use std::io::{self, Write}; -use nix::unistd::dup2; -use nix::libc; -use std::os::fd::AsRawFd; -use tracing::instrument; - - -#[instrument] -fn my_function() { - // Code inside my_function - error!("This is an error logged inside my_function."); -} - -fn main() -> io::Result<()> { - // Specify the log file path - let log_file_path = "spans.log"; - - // Create a new file for writing - let file = File::create(log_file_path)?; - - // Create a new OpenTelemetry trace pipeline that prints to stdout - let provider = TracerProvider::builder() - .with_simple_exporter(stdout::SpanExporter::default()) - .build(); - let tracer = provider.tracer("readme_example"); - - // Create a tracing layer with the configured tracer - let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); - - // Use the tracing subscriber `Registry`, or any other subscriber - // that impls `LookupSpan` - let subscriber = Registry::default().with(telemetry); - - // Initialize the tracing subscriber - tracing::subscriber::with_default(subscriber, || { - // Call the instrumented function - my_function(); - - // Redirect stdout to the file after tracing setup - let stdout_fd = file.as_raw_fd(); - dup2(stdout_fd, libc::STDOUT_FILENO)?; - - Ok(()) - }) -} \ No newline at end of file diff --git a/azurekvp/src/tracing.rs b/azurekvp/src/tracing.rs new file mode 100644 index 00000000..017ca1de --- /dev/null +++ b/azurekvp/src/tracing.rs @@ -0,0 +1,46 @@ +use opentelemetry::{ trace::{self, TracerProvider}, trace::Tracer as _}; +use opentelemetry::sdk::trace as sdktrace; +use opentelemetry::sdk::export::trace::stdout; +use opentelemetry::global; +use tracing_opentelemetry::OpenTelemetryLayer; +use once_cell::sync::Lazy; +use std::fs::File; +use std::os::unix::io::AsRawFd; +use libc::dup2; +use tracing_subscriber::{Registry, layer::SubscriberExt}; + + +pub static TRACER: Lazy<()> = Lazy::new(|| { + // Redirect stdout to a file + let log_file_path = "spans.log"; + let file = File::create(log_file_path).expect("Failed to create log file"); + let stdout_fd = file.as_raw_fd(); + unsafe { + dup2(stdout_fd, libc::STDOUT_FILENO); + } + + // Set up the stdout exporter correctly + let exporter = stdout::Exporter::new(std::io::stdout(), true); + + // Set up the TracerProvider with the stdout exporter + let provider = sdktrace::TracerProvider::builder() + .with_simple_exporter(exporter) + .build(); + + global::set_tracer_provider(provider.clone()); + + let tracer = provider.tracer("azure-kvp"); + // Create the OpenTelemetry layer using the SDK tracer + let otel_layer = OpenTelemetryLayer::new(tracer); + + // Create a `tracing` subscriber + let subscriber = Registry::default() + .with(otel_layer); + // Set the subscriber as the global default + tracing::subscriber::set_global_default(subscriber) + .expect("Setting default subscriber failed"); + }); + +pub fn initialize_tracing() { + Lazy::force(&TRACER); +} \ No newline at end of file diff --git a/spans.log b/spans.log new file mode 100644 index 00000000..89b8062c --- /dev/null +++ b/spans.log @@ -0,0 +1,132 @@ +SpanData { + span_context: SpanContext { + trace_id: 2bc6523dc081c9c51ec42be6bdeb9ae6, + span_id: 9bb4ea2c4ca49a0e, + trace_flags: TraceFlags( + 1, + ), + is_remote: false, + trace_state: TraceState( + None, + ), + }, + parent_span_id: 0000000000000000, + span_kind: Internal, + name: "get_username", + start_time: SystemTime { + tv_sec: 1714767136, + tv_nsec: 150545680, + }, + end_time: SystemTime { + tv_sec: 1714767136, + tv_nsec: 151094189, + }, + attributes: EvictedHashMap { + map: { + Static( + "thread.name", + ): String( + Owned( + "main", + ), + ), + Static( + "code.lineno", + ): I64( + 18, + ), + Static( + "code.filepath", + ): String( + Static( + "src/main.rs", + ), + ), + Static( + "code.namespace", + ): String( + Static( + "azure_init", + ), + ), + Static( + "thread.id", + ): I64( + 1, + ), + Static( + "imds_body", + ): String( + Owned( + "\"{\\\"compute\\\":{\\\"azEnvironment\\\":\\\"AzurePublicCloud\\\",\\\"customData\\\":\\\"\\\",\\\"evictionPolicy\\\":\\\"\\\",\\\"isHostCompatibilityLayerVm\\\":\\\"false\\\",\\\"licenseType\\\":\\\"\\\",\\\"location\\\":\\\"westus2\\\",\\\"name\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"offer\\\":\\\"\\\",\\\"osProfile\\\":{\\\"adminUsername\\\":\\\"cloudenv\\\",\\\"computerName\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"disablePasswordAuthentication\\\":\\\"true\\\"},\\\"osType\\\":\\\"Linux\\\",\\\"placementGroupId\\\":\\\"\\\",\\\"plan\\\":{\\\"name\\\":\\\"\\\",\\\"product\\\":\\\"\\\",\\\"publisher\\\":\\\"\\\"},\\\"platformFaultDomain\\\":\\\"0\\\",\\\"platformUpdateDomain\\\":\\\"0\\\",\\\"priority\\\":\\\"\\\",\\\"provider\\\":\\\"Microsoft.Compute\\\",\\\"publicKeys\\\":[{\\\"keyData\\\":\\\"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPYyB2V9q43oWBJINIZMzJ4zrYoNptGCK9i28qxj9cS2Af/FkrVYTSSRMPJforJRVkyY/6M63TFzfIeMe6b93g9Q4nDoxyUDMJ5SEkLp3kw3caxrimLQF2yJz4QySoiqaFlhfot9bP3En9i8AQYjogQtxQqpw77RRzQOkinP5gyga5W2Ia/inNGBRwF2guqZccsOrTI2WF6dnHTB28LIxhHox/WH+0CmMKQrP8yX3bzoReXvsmm8RztC6PWb3G9FzEXK6fDdaLApSIvO/sc5MSEEdbwx7yo2phWsv96x7wY3QZtUGg+ZZnrtr/RE05xQPHm+ufh7qwbF87Ekt70h9R vsonline@machine\\\",\\\"path\\\":\\\"/home/cloudenv/.ssh/authorized_keys\\\"}],\\\"publisher\\\":\\\"\\\",\\\"resourceGroupName\\\":\\\"vsclk-online-prod-rel-usw2-040\\\",\\\"resourceId\\\":\\\"/subscriptions/75bc2f80-0696-46fc-9e0b-afcddbb5dc40/resourceGroups/vsclk-online-prod-rel-usw2-040/providers/Microsoft.Compute/virtualMachines/93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"securityProfile\\\":{\\\"secureBootEnabled\\\":\\\"false\\\",\\\"virtualTpmEnabled\\\":\\\"false\\\"},\\\"sku\\\":\\\"\\\",\\\"storageProfile\\\":{\\\"dataDisks\\\":[],\\\"imageReference\\\":{\\\"id\\\":\\\"/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001\\\",\\\"offer\\\":\\\"\\\",\\\"publisher\\\":\\\"\\\",\\\"sku\\\":\\\"\\\",\\\"version\\\":\\\"\\\"},\\\"osDisk\\\":{\\\"caching\\\":\\\"ReadOnly\\\",\\\"createOption\\\":\\\"FromImage\\\",\\\"diffDiskSettings\\\":{\\\"option\\\":\\\"Local\\\"},\\\"diskSizeGB\\\":\\\"30\\\",\\\"encryptionSettings\\\":{\\\"enabled\\\":\\\"false\\\"},\\\"image\\\":{\\\"uri\\\":\\\"\\\"},\\\"managedDisk\\\":{\\\"id\\\":\\\"/subscriptions/75bc2f80-0696-46fc-9e0b-afcddbb5dc40/resourceGroups/vsclk-online-prod-rel-usw2-040/providers/Microsoft.Compute/disks/93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe-disk\\\",\\\"storageAccountType\\\":\\\"Standard_LRS\\\"},\\\"name\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe-disk\\\",\\\"osType\\\":\\\"Linux\\\",\\\"vhd\\\":{\\\"uri\\\":\\\"\\\"},\\\"writeAcceleratorEnabled\\\":\\\"false\\\"},\\\"resourceDisk\\\":{\\\"size\\\":\\\"76800\\\"}},\\\"subscriptionId\\\":\\\"75bc2f80-0696-46fc-9e0b-afcddbb5dc40\\\",\\\"tags\\\":\\\"ComputeOS:Linux;OperationReason:WatchPoolSizeIncrease;PoolDefinition:c134d4e6c324e78e88996774746a60b93a0a9a43;PoolImageFamilyName:customUbuntuServer;PoolImageName:/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001;PoolLocation:westus2;PoolSkuName:Standard_D2ds_v5;PoolVersionDefinition:a88d797aeaefa8ecf4465d5e20cb0699330c60c4;ResourceComponentRecordIds:dc819a97-da14-4cb1-8ff6-51e82d20ec8f,7b8c6fa1-5b97-4f4b-bcbe-5438e48ee350,b449b564-5c26-4f1e-a872-77033e0a1370,b5c5abfa-d5a6-49cd-8fcf-9fcae36adc27,55cab94f-06b5-4b5b-bd53-c32770b2f75d;ResourceId:93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe;ResourceName:93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe;ResourceType:ComputeVM;platformsettings.host_environment.service.platform_optedin_for_rootcerts:true\\\",\\\"tagsList\\\":[{\\\"name\\\":\\\"ComputeOS\\\",\\\"value\\\":\\\"Linux\\\"},{\\\"name\\\":\\\"OperationReason\\\",\\\"value\\\":\\\"WatchPoolSizeIncrease\\\"},{\\\"name\\\":\\\"PoolDefinition\\\",\\\"value\\\":\\\"c134d4e6c324e78e88996774746a60b93a0a9a43\\\"},{\\\"name\\\":\\\"PoolImageFamilyName\\\",\\\"value\\\":\\\"customUbuntuServer\\\"},{\\\"name\\\":\\\"PoolImageName\\\",\\\"value\\\":\\\"/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001\\\"},{\\\"name\\\":\\\"PoolLocation\\\",\\\"value\\\":\\\"westus2\\\"},{\\\"name\\\":\\\"PoolSkuName\\\",\\\"value\\\":\\\"Standard_D2ds_v5\\\"},{\\\"name\\\":\\\"PoolVersionDefinition\\\",\\\"value\\\":\\\"a88d797aeaefa8ecf4465d5e20cb0699330c60c4\\\"},{\\\"name\\\":\\\"ResourceComponentRecordIds\\\",\\\"value\\\":\\\"dc819a97-da14-4cb1-8ff6-51e82d20ec8f,7b8c6fa1-5b97-4f4b-bcbe-5438e48ee350,b449b564-5c26-4f1e-a872-77033e0a1370,b5c5abfa-d5a6-49cd-8fcf-9fcae36adc27,55cab94f-06b5-4b5b-bd53-c32770b2f75d\\\"},{\\\"name\\\":\\\"ResourceId\\\",\\\"value\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\"},{\\\"name\\\":\\\"ResourceName\\\",\\\"value\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\"},{\\\"name\\\":\\\"ResourceType\\\",\\\"value\\\":\\\"ComputeVM\\\"},{\\\"name\\\":\\\"platformsettings.host_environment.service.platform_optedin_for_rootcerts\\\",\\\"value\\\":\\\"true\\\"}],\\\"userData\\\":\\\"\\\",\\\"version\\\":\\\"2024.0419.001\\\",\\\"vmId\\\":\\\"ecfeff26-c7b4-4778-96d5-2acfd6b80c2a\\\",\\\"vmScaleSetName\\\":\\\"\\\",\\\"vmSize\\\":\\\"Standard_D2ds_v5\\\",\\\"zone\\\":\\\"\\\"},\\\"network\\\":{\\\"interface\\\":[{\\\"ipv4\\\":{\\\"ipAddress\\\":[{\\\"privateIpAddress\\\":\\\"172.16.5.4\\\",\\\"publicIpAddress\\\":\\\"\\\"}],\\\"subnet\\\":[{\\\"address\\\":\\\"172.16.5.0\\\",\\\"prefix\\\":\\\"24\\\"}]},\\\"ipv6\\\":{\\\"ipAddress\\\":[]},\\\"macAddress\\\":\\\"000D3A078740\\\"}]}}\"", + ), + ), + Static( + "busy_ns", + ): I64( + 327016, + ), + Static( + "idle_ns", + ): I64( + 218334, + ), + }, + evict_list: [ + Static( + "idle_ns", + ), + Static( + "busy_ns", + ), + Static( + "imds_body", + ), + Static( + "thread.name", + ), + Static( + "thread.id", + ), + Static( + "code.lineno", + ), + Static( + "code.namespace", + ), + Static( + "code.filepath", + ), + ], + max_len: 128, + dropped_count: 0, + }, + events: EvictedQueue { + queue: None, + max_len: 128, + dropped_count: 0, + }, + links: EvictedQueue { + queue: None, + max_len: 128, + dropped_count: 0, + }, + status: Unset, + resource: Resource { + attrs: { + Static( + "service.name", + ): String( + Owned( + "unknown_service", + ), + ), + }, + schema_url: None, + }, + instrumentation_lib: InstrumentationLibrary { + name: "azure-kvp", + version: None, + schema_url: None, + }, +} diff --git a/src/main.rs b/src/main.rs index aa66c477..9ed79f29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,15 @@ use libazureinit::{ reqwest::{header, Client}, user, }; +use azurekvp::{initialize_tracing, TRACER}; +use opentelemetry::global; +use tracing::{info, instrument}; +use opentelemetry::trace::Tracer; +use opentelemetry::trace::Span; const VERSION: &str = env!("CARGO_PKG_VERSION"); +#[instrument] fn get_username( imds_body: String, ) -> Result> { @@ -43,7 +49,11 @@ fn get_username( } #[tokio::main] +#[instrument] async fn main() { + // Initialize the tracing subscriber + initialize_tracing(); + let mut default_headers = header::HeaderMap::new(); let user_agent = header::HeaderValue::from_str( format!("azure-init v{VERSION}").as_str(), @@ -61,10 +71,15 @@ async fn main() { Err(_err) => std::process::exit(exitcode::CONFIG), }; + // You can now use TRACER to create spans or perform tracing operations + let tracer = global::tracer("azure-kvp"); + let mut span = tracer.span_builder("test").start(&tracer); let username = match get_username(imds_body.clone()) { Ok(res) => res, Err(_err) => std::process::exit(exitcode::CONFIG), }; + // End the span + span.end(); let mut file_path = "/home/".to_string(); file_path.push_str(username.as_str()); From 7d05d59c080eaf11b13089228b5fc93eeac18474 Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Tue, 14 May 2024 17:51:21 +0000 Subject: [PATCH 6/7] Most Recent Changes for Draft PR --- azurekvp/src/tracing.rs | 6 +- spans.log | 280 +++++++++++++++++++++++++++++++++++++--- src/main.rs | 18 ++- 3 files changed, 279 insertions(+), 25 deletions(-) diff --git a/azurekvp/src/tracing.rs b/azurekvp/src/tracing.rs index 017ca1de..8fa105f7 100644 --- a/azurekvp/src/tracing.rs +++ b/azurekvp/src/tracing.rs @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + use opentelemetry::{ trace::{self, TracerProvider}, trace::Tracer as _}; use opentelemetry::sdk::trace as sdktrace; use opentelemetry::sdk::export::trace::stdout; @@ -36,10 +39,11 @@ pub static TRACER: Lazy<()> = Lazy::new(|| { // Create a `tracing` subscriber let subscriber = Registry::default() .with(otel_layer); + // Set the subscriber as the global default tracing::subscriber::set_global_default(subscriber) .expect("Setting default subscriber failed"); - }); +}); pub fn initialize_tracing() { Lazy::force(&TRACER); diff --git a/spans.log b/spans.log index 89b8062c..0577be90 100644 --- a/spans.log +++ b/spans.log @@ -1,7 +1,7 @@ -SpanData { +Hello, World!SpanData { span_context: SpanContext { - trace_id: 2bc6523dc081c9c51ec42be6bdeb9ae6, - span_id: 9bb4ea2c4ca49a0e, + trace_id: ecea5cd2e78812b3e9bad50d3973bde3, + span_id: ef78e8890b27fedb, trace_flags: TraceFlags( 1, ), @@ -12,17 +12,41 @@ SpanData { }, parent_span_id: 0000000000000000, span_kind: Internal, - name: "get_username", + name: "test", start_time: SystemTime { - tv_sec: 1714767136, - tv_nsec: 150545680, + tv_sec: 1714773090, + tv_nsec: 869927907, }, end_time: SystemTime { - tv_sec: 1714767136, - tv_nsec: 151094189, + tv_sec: 1714773090, + tv_nsec: 870027565, }, attributes: EvictedHashMap { map: { + Static( + "idle_ns", + ): I64( + 106123, + ), + Static( + "code.lineno", + ): I64( + 53, + ), + Static( + "code.namespace", + ): String( + Static( + "azure_init", + ), + ), + Static( + "code.filepath", + ): String( + Static( + "src/main.rs", + ), + ), Static( "thread.name", ): String( @@ -30,10 +54,125 @@ SpanData { "main", ), ), + Static( + "busy_ns", + ): I64( + 5625, + ), + Static( + "thread.id", + ): I64( + 1, + ), + }, + evict_list: [ + Static( + "idle_ns", + ), + Static( + "busy_ns", + ), + Static( + "thread.name", + ), + Static( + "thread.id", + ), Static( "code.lineno", + ), + Static( + "code.namespace", + ), + Static( + "code.filepath", + ), + ], + max_len: 128, + dropped_count: 0, + }, + events: EvictedQueue { + queue: None, + max_len: 128, + dropped_count: 0, + }, + links: EvictedQueue { + queue: None, + max_len: 128, + dropped_count: 0, + }, + status: Unset, + resource: Resource { + attrs: { + Static( + "service.name", + ): String( + Owned( + "unknown_service", + ), + ), + }, + schema_url: None, + }, + instrumentation_lib: InstrumentationLibrary { + name: "azure-kvp", + version: None, + schema_url: None, + }, +} +SpanData { + span_context: SpanContext { + trace_id: 4fac9eb4d14424ba354d2bcc375dcc34, + span_id: 5a3fcbb7b214e95b, + trace_flags: TraceFlags( + 1, + ), + is_remote: false, + trace_state: TraceState( + None, + ), + }, + parent_span_id: 0000000000000000, + span_kind: Internal, + name: "get_username", + start_time: SystemTime { + tv_sec: 1714773090, + tv_nsec: 880088576, + }, + end_time: SystemTime { + tv_sec: 1714773090, + tv_nsec: 880662450, + }, + attributes: EvictedHashMap { + map: { + Static( + "thread.id", + ): I64( + 1, + ), + Static( + "busy_ns", ): I64( - 18, + 329559, + ), + Static( + "idle_ns", + ): I64( + 234248, + ), + Static( + "thread.name", + ): String( + Owned( + "main", + ), + ), + Static( + "imds_body", + ): String( + Owned( + "\"{\\\"compute\\\":{\\\"azEnvironment\\\":\\\"AzurePublicCloud\\\",\\\"customData\\\":\\\"\\\",\\\"evictionPolicy\\\":\\\"\\\",\\\"isHostCompatibilityLayerVm\\\":\\\"false\\\",\\\"licenseType\\\":\\\"\\\",\\\"location\\\":\\\"westus2\\\",\\\"name\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"offer\\\":\\\"\\\",\\\"osProfile\\\":{\\\"adminUsername\\\":\\\"cloudenv\\\",\\\"computerName\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"disablePasswordAuthentication\\\":\\\"true\\\"},\\\"osType\\\":\\\"Linux\\\",\\\"placementGroupId\\\":\\\"\\\",\\\"plan\\\":{\\\"name\\\":\\\"\\\",\\\"product\\\":\\\"\\\",\\\"publisher\\\":\\\"\\\"},\\\"platformFaultDomain\\\":\\\"0\\\",\\\"platformUpdateDomain\\\":\\\"0\\\",\\\"priority\\\":\\\"\\\",\\\"provider\\\":\\\"Microsoft.Compute\\\",\\\"publicKeys\\\":[{\\\"keyData\\\":\\\"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPYyB2V9q43oWBJINIZMzJ4zrYoNptGCK9i28qxj9cS2Af/FkrVYTSSRMPJforJRVkyY/6M63TFzfIeMe6b93g9Q4nDoxyUDMJ5SEkLp3kw3caxrimLQF2yJz4QySoiqaFlhfot9bP3En9i8AQYjogQtxQqpw77RRzQOkinP5gyga5W2Ia/inNGBRwF2guqZccsOrTI2WF6dnHTB28LIxhHox/WH+0CmMKQrP8yX3bzoReXvsmm8RztC6PWb3G9FzEXK6fDdaLApSIvO/sc5MSEEdbwx7yo2phWsv96x7wY3QZtUGg+ZZnrtr/RE05xQPHm+ufh7qwbF87Ekt70h9R vsonline@machine\\\",\\\"path\\\":\\\"/home/cloudenv/.ssh/authorized_keys\\\"}],\\\"publisher\\\":\\\"\\\",\\\"resourceGroupName\\\":\\\"vsclk-online-prod-rel-usw2-040\\\",\\\"resourceId\\\":\\\"/subscriptions/75bc2f80-0696-46fc-9e0b-afcddbb5dc40/resourceGroups/vsclk-online-prod-rel-usw2-040/providers/Microsoft.Compute/virtualMachines/93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"securityProfile\\\":{\\\"secureBootEnabled\\\":\\\"false\\\",\\\"virtualTpmEnabled\\\":\\\"false\\\"},\\\"sku\\\":\\\"\\\",\\\"storageProfile\\\":{\\\"dataDisks\\\":[],\\\"imageReference\\\":{\\\"id\\\":\\\"/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001\\\",\\\"offer\\\":\\\"\\\",\\\"publisher\\\":\\\"\\\",\\\"sku\\\":\\\"\\\",\\\"version\\\":\\\"\\\"},\\\"osDisk\\\":{\\\"caching\\\":\\\"ReadOnly\\\",\\\"createOption\\\":\\\"FromImage\\\",\\\"diffDiskSettings\\\":{\\\"option\\\":\\\"Local\\\"},\\\"diskSizeGB\\\":\\\"30\\\",\\\"encryptionSettings\\\":{\\\"enabled\\\":\\\"false\\\"},\\\"image\\\":{\\\"uri\\\":\\\"\\\"},\\\"managedDisk\\\":{\\\"id\\\":\\\"/subscriptions/75bc2f80-0696-46fc-9e0b-afcddbb5dc40/resourceGroups/vsclk-online-prod-rel-usw2-040/providers/Microsoft.Compute/disks/93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe-disk\\\",\\\"storageAccountType\\\":\\\"Standard_LRS\\\"},\\\"name\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe-disk\\\",\\\"osType\\\":\\\"Linux\\\",\\\"vhd\\\":{\\\"uri\\\":\\\"\\\"},\\\"writeAcceleratorEnabled\\\":\\\"false\\\"},\\\"resourceDisk\\\":{\\\"size\\\":\\\"76800\\\"}},\\\"subscriptionId\\\":\\\"75bc2f80-0696-46fc-9e0b-afcddbb5dc40\\\",\\\"tags\\\":\\\"ComputeOS:Linux;OperationReason:WatchPoolSizeIncrease;PoolDefinition:c134d4e6c324e78e88996774746a60b93a0a9a43;PoolImageFamilyName:customUbuntuServer;PoolImageName:/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001;PoolLocation:westus2;PoolSkuName:Standard_D2ds_v5;PoolVersionDefinition:a88d797aeaefa8ecf4465d5e20cb0699330c60c4;ResourceComponentRecordIds:dc819a97-da14-4cb1-8ff6-51e82d20ec8f,7b8c6fa1-5b97-4f4b-bcbe-5438e48ee350,b449b564-5c26-4f1e-a872-77033e0a1370,b5c5abfa-d5a6-49cd-8fcf-9fcae36adc27,55cab94f-06b5-4b5b-bd53-c32770b2f75d;ResourceId:93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe;ResourceName:93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe;ResourceType:ComputeVM;platformsettings.host_environment.service.platform_optedin_for_rootcerts:true\\\",\\\"tagsList\\\":[{\\\"name\\\":\\\"ComputeOS\\\",\\\"value\\\":\\\"Linux\\\"},{\\\"name\\\":\\\"OperationReason\\\",\\\"value\\\":\\\"WatchPoolSizeIncrease\\\"},{\\\"name\\\":\\\"PoolDefinition\\\",\\\"value\\\":\\\"c134d4e6c324e78e88996774746a60b93a0a9a43\\\"},{\\\"name\\\":\\\"PoolImageFamilyName\\\",\\\"value\\\":\\\"customUbuntuServer\\\"},{\\\"name\\\":\\\"PoolImageName\\\",\\\"value\\\":\\\"/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001\\\"},{\\\"name\\\":\\\"PoolLocation\\\",\\\"value\\\":\\\"westus2\\\"},{\\\"name\\\":\\\"PoolSkuName\\\",\\\"value\\\":\\\"Standard_D2ds_v5\\\"},{\\\"name\\\":\\\"PoolVersionDefinition\\\",\\\"value\\\":\\\"a88d797aeaefa8ecf4465d5e20cb0699330c60c4\\\"},{\\\"name\\\":\\\"ResourceComponentRecordIds\\\",\\\"value\\\":\\\"dc819a97-da14-4cb1-8ff6-51e82d20ec8f,7b8c6fa1-5b97-4f4b-bcbe-5438e48ee350,b449b564-5c26-4f1e-a872-77033e0a1370,b5c5abfa-d5a6-49cd-8fcf-9fcae36adc27,55cab94f-06b5-4b5b-bd53-c32770b2f75d\\\"},{\\\"name\\\":\\\"ResourceId\\\",\\\"value\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\"},{\\\"name\\\":\\\"ResourceName\\\",\\\"value\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\"},{\\\"name\\\":\\\"ResourceType\\\",\\\"value\\\":\\\"ComputeVM\\\"},{\\\"name\\\":\\\"platformsettings.host_environment.service.platform_optedin_for_rootcerts\\\",\\\"value\\\":\\\"true\\\"}],\\\"userData\\\":\\\"\\\",\\\"version\\\":\\\"2024.0419.001\\\",\\\"vmId\\\":\\\"ecfeff26-c7b4-4778-96d5-2acfd6b80c2a\\\",\\\"vmScaleSetName\\\":\\\"\\\",\\\"vmSize\\\":\\\"Standard_D2ds_v5\\\",\\\"zone\\\":\\\"\\\"},\\\"network\\\":{\\\"interface\\\":[{\\\"ipv4\\\":{\\\"ipAddress\\\":[{\\\"privateIpAddress\\\":\\\"172.16.5.4\\\",\\\"publicIpAddress\\\":\\\"\\\"}],\\\"subnet\\\":[{\\\"address\\\":\\\"172.16.5.0\\\",\\\"prefix\\\":\\\"24\\\"}]},\\\"ipv6\\\":{\\\"ipAddress\\\":[]},\\\"macAddress\\\":\\\"000D3A078740\\\"}]}}\"", + ), ), Static( "code.filepath", @@ -42,6 +181,11 @@ SpanData { "src/main.rs", ), ), + Static( + "code.lineno", + ): I64( + 20, + ), Static( "code.namespace", ): String( @@ -49,27 +193,130 @@ SpanData { "azure_init", ), ), + }, + evict_list: [ + Static( + "idle_ns", + ), Static( - "thread.id", - ): I64( - 1, + "busy_ns", ), Static( "imds_body", + ), + Static( + "thread.name", + ), + Static( + "thread.id", + ), + Static( + "code.lineno", + ), + Static( + "code.namespace", + ), + Static( + "code.filepath", + ), + ], + max_len: 128, + dropped_count: 0, + }, + events: EvictedQueue { + queue: None, + max_len: 128, + dropped_count: 0, + }, + links: EvictedQueue { + queue: None, + max_len: 128, + dropped_count: 0, + }, + status: Unset, + resource: Resource { + attrs: { + Static( + "service.name", ): String( Owned( - "\"{\\\"compute\\\":{\\\"azEnvironment\\\":\\\"AzurePublicCloud\\\",\\\"customData\\\":\\\"\\\",\\\"evictionPolicy\\\":\\\"\\\",\\\"isHostCompatibilityLayerVm\\\":\\\"false\\\",\\\"licenseType\\\":\\\"\\\",\\\"location\\\":\\\"westus2\\\",\\\"name\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"offer\\\":\\\"\\\",\\\"osProfile\\\":{\\\"adminUsername\\\":\\\"cloudenv\\\",\\\"computerName\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"disablePasswordAuthentication\\\":\\\"true\\\"},\\\"osType\\\":\\\"Linux\\\",\\\"placementGroupId\\\":\\\"\\\",\\\"plan\\\":{\\\"name\\\":\\\"\\\",\\\"product\\\":\\\"\\\",\\\"publisher\\\":\\\"\\\"},\\\"platformFaultDomain\\\":\\\"0\\\",\\\"platformUpdateDomain\\\":\\\"0\\\",\\\"priority\\\":\\\"\\\",\\\"provider\\\":\\\"Microsoft.Compute\\\",\\\"publicKeys\\\":[{\\\"keyData\\\":\\\"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPYyB2V9q43oWBJINIZMzJ4zrYoNptGCK9i28qxj9cS2Af/FkrVYTSSRMPJforJRVkyY/6M63TFzfIeMe6b93g9Q4nDoxyUDMJ5SEkLp3kw3caxrimLQF2yJz4QySoiqaFlhfot9bP3En9i8AQYjogQtxQqpw77RRzQOkinP5gyga5W2Ia/inNGBRwF2guqZccsOrTI2WF6dnHTB28LIxhHox/WH+0CmMKQrP8yX3bzoReXvsmm8RztC6PWb3G9FzEXK6fDdaLApSIvO/sc5MSEEdbwx7yo2phWsv96x7wY3QZtUGg+ZZnrtr/RE05xQPHm+ufh7qwbF87Ekt70h9R vsonline@machine\\\",\\\"path\\\":\\\"/home/cloudenv/.ssh/authorized_keys\\\"}],\\\"publisher\\\":\\\"\\\",\\\"resourceGroupName\\\":\\\"vsclk-online-prod-rel-usw2-040\\\",\\\"resourceId\\\":\\\"/subscriptions/75bc2f80-0696-46fc-9e0b-afcddbb5dc40/resourceGroups/vsclk-online-prod-rel-usw2-040/providers/Microsoft.Compute/virtualMachines/93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\",\\\"securityProfile\\\":{\\\"secureBootEnabled\\\":\\\"false\\\",\\\"virtualTpmEnabled\\\":\\\"false\\\"},\\\"sku\\\":\\\"\\\",\\\"storageProfile\\\":{\\\"dataDisks\\\":[],\\\"imageReference\\\":{\\\"id\\\":\\\"/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001\\\",\\\"offer\\\":\\\"\\\",\\\"publisher\\\":\\\"\\\",\\\"sku\\\":\\\"\\\",\\\"version\\\":\\\"\\\"},\\\"osDisk\\\":{\\\"caching\\\":\\\"ReadOnly\\\",\\\"createOption\\\":\\\"FromImage\\\",\\\"diffDiskSettings\\\":{\\\"option\\\":\\\"Local\\\"},\\\"diskSizeGB\\\":\\\"30\\\",\\\"encryptionSettings\\\":{\\\"enabled\\\":\\\"false\\\"},\\\"image\\\":{\\\"uri\\\":\\\"\\\"},\\\"managedDisk\\\":{\\\"id\\\":\\\"/subscriptions/75bc2f80-0696-46fc-9e0b-afcddbb5dc40/resourceGroups/vsclk-online-prod-rel-usw2-040/providers/Microsoft.Compute/disks/93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe-disk\\\",\\\"storageAccountType\\\":\\\"Standard_LRS\\\"},\\\"name\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe-disk\\\",\\\"osType\\\":\\\"Linux\\\",\\\"vhd\\\":{\\\"uri\\\":\\\"\\\"},\\\"writeAcceleratorEnabled\\\":\\\"false\\\"},\\\"resourceDisk\\\":{\\\"size\\\":\\\"76800\\\"}},\\\"subscriptionId\\\":\\\"75bc2f80-0696-46fc-9e0b-afcddbb5dc40\\\",\\\"tags\\\":\\\"ComputeOS:Linux;OperationReason:WatchPoolSizeIncrease;PoolDefinition:c134d4e6c324e78e88996774746a60b93a0a9a43;PoolImageFamilyName:customUbuntuServer;PoolImageName:/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001;PoolLocation:westus2;PoolSkuName:Standard_D2ds_v5;PoolVersionDefinition:a88d797aeaefa8ecf4465d5e20cb0699330c60c4;ResourceComponentRecordIds:dc819a97-da14-4cb1-8ff6-51e82d20ec8f,7b8c6fa1-5b97-4f4b-bcbe-5438e48ee350,b449b564-5c26-4f1e-a872-77033e0a1370,b5c5abfa-d5a6-49cd-8fcf-9fcae36adc27,55cab94f-06b5-4b5b-bd53-c32770b2f75d;ResourceId:93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe;ResourceName:93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe;ResourceType:ComputeVM;platformsettings.host_environment.service.platform_optedin_for_rootcerts:true\\\",\\\"tagsList\\\":[{\\\"name\\\":\\\"ComputeOS\\\",\\\"value\\\":\\\"Linux\\\"},{\\\"name\\\":\\\"OperationReason\\\",\\\"value\\\":\\\"WatchPoolSizeIncrease\\\"},{\\\"name\\\":\\\"PoolDefinition\\\",\\\"value\\\":\\\"c134d4e6c324e78e88996774746a60b93a0a9a43\\\"},{\\\"name\\\":\\\"PoolImageFamilyName\\\",\\\"value\\\":\\\"customUbuntuServer\\\"},{\\\"name\\\":\\\"PoolImageName\\\",\\\"value\\\":\\\"/subscriptions/979523fb-a19c-4bb0-a8ee-cef29597b0a4/resourceGroups/vsclk-online-prod-images-usw2/providers/Microsoft.Compute/galleries/gallery_usw2/images/ubuntu/versions/2024.0419.001\\\"},{\\\"name\\\":\\\"PoolLocation\\\",\\\"value\\\":\\\"westus2\\\"},{\\\"name\\\":\\\"PoolSkuName\\\",\\\"value\\\":\\\"Standard_D2ds_v5\\\"},{\\\"name\\\":\\\"PoolVersionDefinition\\\",\\\"value\\\":\\\"a88d797aeaefa8ecf4465d5e20cb0699330c60c4\\\"},{\\\"name\\\":\\\"ResourceComponentRecordIds\\\",\\\"value\\\":\\\"dc819a97-da14-4cb1-8ff6-51e82d20ec8f,7b8c6fa1-5b97-4f4b-bcbe-5438e48ee350,b449b564-5c26-4f1e-a872-77033e0a1370,b5c5abfa-d5a6-49cd-8fcf-9fcae36adc27,55cab94f-06b5-4b5b-bd53-c32770b2f75d\\\"},{\\\"name\\\":\\\"ResourceId\\\",\\\"value\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\"},{\\\"name\\\":\\\"ResourceName\\\",\\\"value\\\":\\\"93ebfb57-6bdd-4f3e-8ac0-d60156e71fbe\\\"},{\\\"name\\\":\\\"ResourceType\\\",\\\"value\\\":\\\"ComputeVM\\\"},{\\\"name\\\":\\\"platformsettings.host_environment.service.platform_optedin_for_rootcerts\\\",\\\"value\\\":\\\"true\\\"}],\\\"userData\\\":\\\"\\\",\\\"version\\\":\\\"2024.0419.001\\\",\\\"vmId\\\":\\\"ecfeff26-c7b4-4778-96d5-2acfd6b80c2a\\\",\\\"vmScaleSetName\\\":\\\"\\\",\\\"vmSize\\\":\\\"Standard_D2ds_v5\\\",\\\"zone\\\":\\\"\\\"},\\\"network\\\":{\\\"interface\\\":[{\\\"ipv4\\\":{\\\"ipAddress\\\":[{\\\"privateIpAddress\\\":\\\"172.16.5.4\\\",\\\"publicIpAddress\\\":\\\"\\\"}],\\\"subnet\\\":[{\\\"address\\\":\\\"172.16.5.0\\\",\\\"prefix\\\":\\\"24\\\"}]},\\\"ipv6\\\":{\\\"ipAddress\\\":[]},\\\"macAddress\\\":\\\"000D3A078740\\\"}]}}\"", + "unknown_service", ), ), + }, + schema_url: None, + }, + instrumentation_lib: InstrumentationLibrary { + name: "azure-kvp", + version: None, + schema_url: None, + }, +} +SpanData { + span_context: SpanContext { + trace_id: e5f08ba136344eb0c6e387a5a21ed849, + span_id: a22ea2674c9dd6dd, + trace_flags: TraceFlags( + 1, + ), + is_remote: false, + trace_state: TraceState( + None, + ), + }, + parent_span_id: 0000000000000000, + span_kind: Internal, + name: "query-imds", + start_time: SystemTime { + tv_sec: 1714773090, + tv_nsec: 880038063, + }, + end_time: SystemTime { + tv_sec: 1714773090, + tv_nsec: 977431564, + }, + attributes: EvictedHashMap { + map: { Static( "busy_ns", ): I64( - 327016, + 4152, + ), + Static( + "thread.id", + ): I64( + 1, ), Static( "idle_ns", ): I64( - 218334, + 37600, + ), + Static( + "code.lineno", + ): I64( + 82, + ), + Static( + "code.filepath", + ): String( + Static( + "src/main.rs", + ), + ), + Static( + "code.namespace", + ): String( + Static( + "azure_init", + ), + ), + Static( + "thread.name", + ): String( + Owned( + "main", + ), ), }, evict_list: [ @@ -79,9 +326,6 @@ SpanData { Static( "busy_ns", ), - Static( - "imds_body", - ), Static( "thread.name", ), diff --git a/src/main.rs b/src/main.rs index 9ed79f29..442f6fcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ use opentelemetry::global; use tracing::{info, instrument}; use opentelemetry::trace::Tracer; use opentelemetry::trace::Span; +use tracing::span; +use tracing::Level; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -48,11 +50,16 @@ fn get_username( } } -#[tokio::main] #[instrument] +fn test () { + print!("Hello, World!"); +} + +#[tokio::main] async fn main() { // Initialize the tracing subscriber initialize_tracing(); + test(); let mut default_headers = header::HeaderMap::new(); let user_agent = header::HeaderValue::from_str( @@ -66,20 +73,19 @@ async fn main() { .build() .unwrap(); let query_result = imds::query_imds(&client).await; + + let mut query_span = span!(Level::TRACE, "query-imds").entered(); + let imds_body = match query_result { Ok(imds_body) => imds_body, Err(_err) => std::process::exit(exitcode::CONFIG), }; + let mut query_span = query_span.exit(); - // You can now use TRACER to create spans or perform tracing operations - let tracer = global::tracer("azure-kvp"); - let mut span = tracer.span_builder("test").start(&tracer); let username = match get_username(imds_body.clone()) { Ok(res) => res, Err(_err) => std::process::exit(exitcode::CONFIG), }; - // End the span - span.end(); let mut file_path = "/home/".to_string(); file_path.push_str(username.as_str()); From 052fa5bf4fe3e68385003a3ae7faad2130937f4b Mon Sep 17 00:00:00 2001 From: Peyton Robertson <93797227+peytonr18@users.noreply.github.com> Date: Tue, 14 May 2024 18:02:46 +0000 Subject: [PATCH 7/7] Final merged changes from main --- spans.log | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spans.log b/spans.log index 0577be90..b4dc6e25 100644 --- a/spans.log +++ b/spans.log @@ -1,4 +1,4 @@ -Hello, World!SpanData { +SpanData { span_context: SpanContext { trace_id: ecea5cd2e78812b3e9bad50d3973bde3, span_id: ef78e8890b27fedb,