From 35034f47591df46be9f8ddfb67f71d820705bb7d Mon Sep 17 00:00:00 2001 From: Scott Gerring Date: Sun, 15 Dec 2024 17:40:55 +0100 Subject: [PATCH] chore: formatting --- .../tests/integration_test/src/images.rs | 21 +++++----- .../tests/integration_test/src/lib.rs | 3 +- .../integration_test/src/metrics_asserter.rs | 9 ++-- .../tests/integration_test/src/test_utils.rs | 10 ++--- .../integration_test/src/trace_asserter.rs | 2 +- .../tests/integration_test/tests/logs.rs | 7 ++-- .../tests/integration_test/tests/metrics.rs | 42 ++++++++++--------- .../tests/integration_test/tests/traces.rs | 7 ++-- 8 files changed, 49 insertions(+), 52 deletions(-) diff --git a/opentelemetry-otlp/tests/integration_test/src/images.rs b/opentelemetry-otlp/tests/integration_test/src/images.rs index 4bfb901f7e..7c09d697c8 100644 --- a/opentelemetry-otlp/tests/integration_test/src/images.rs +++ b/opentelemetry-otlp/tests/integration_test/src/images.rs @@ -36,22 +36,21 @@ impl Image for Collector { impl Default for Collector { fn default() -> Self { Collector { - volumes: HashMap::from([( - "./otel-collector-config-2.yaml".into(), - "/etc/otelcol/config.yaml".into(), - ), + volumes: HashMap::from([ + ( + "./otel-collector-config-2.yaml".into(), + "/etc/otelcol/config.yaml".into(), + ), ( "./actual/traces.json".into(), - "/testresults/traces.json".into() + "/testresults/traces.json".into(), ), - ( - "./actual/logs.json".into(), - "/testresults/logs.json".into() - ), + ("./actual/logs.json".into(), "/testresults/logs.json".into()), ( "./actual/metrics.json".into(), - "/testresults/metrics.json".into() - )]), + "/testresults/metrics.json".into(), + ), + ]), } } } diff --git a/opentelemetry-otlp/tests/integration_test/src/lib.rs b/opentelemetry-otlp/tests/integration_test/src/lib.rs index e30dabad56..3b6903633e 100644 --- a/opentelemetry-otlp/tests/integration_test/src/lib.rs +++ b/opentelemetry-otlp/tests/integration_test/src/lib.rs @@ -3,6 +3,5 @@ use ctor::dtor; pub mod images; pub mod logs_asserter; pub mod metrics_asserter; -pub mod trace_asserter; pub mod test_utils; - +pub mod trace_asserter; diff --git a/opentelemetry-otlp/tests/integration_test/src/metrics_asserter.rs b/opentelemetry-otlp/tests/integration_test/src/metrics_asserter.rs index 37221bcd3b..9fa9b2a6cf 100644 --- a/opentelemetry-otlp/tests/integration_test/src/metrics_asserter.rs +++ b/opentelemetry-otlp/tests/integration_test/src/metrics_asserter.rs @@ -1,9 +1,9 @@ -use std::fs::File; -use std::io::{BufReader, Read}; -use serde_json::Value; +use crate::test_utils; use anyhow::Result; use ctor::dtor; -use crate::test_utils; +use serde_json::Value; +use std::fs::File; +use std::io::{BufReader, Read}; pub fn read_metrics_from_json(file: File) -> Result { // Create a buffered reader for the file @@ -18,7 +18,6 @@ pub fn read_metrics_from_json(file: File) -> Result { // Parse the contents into a JSON Value let metrics_data: Value = serde_json::from_str(&contents)?; Ok(metrics_data) - } pub struct MetricsAsserter { diff --git a/opentelemetry-otlp/tests/integration_test/src/test_utils.rs b/opentelemetry-otlp/tests/integration_test/src/test_utils.rs index 018a08121d..ead933a71f 100644 --- a/opentelemetry-otlp/tests/integration_test/src/test_utils.rs +++ b/opentelemetry-otlp/tests/integration_test/src/test_utils.rs @@ -1,13 +1,12 @@ - +use crate::images::Collector; +use ctor::dtor; use std::fs; use std::fs::File; use std::os::unix::fs::PermissionsExt; -use std::sync::{Arc, OnceLock, Mutex}; -use ctor::dtor; +use std::sync::{Arc, Mutex, OnceLock}; use testcontainers::clients::Cli; use testcontainers::core::Port; use testcontainers::{Container, RunnableImage}; -use crate::images::Collector; // Static references for container management static COLLECTOR_ARC: OnceLock>>>> = OnceLock::new(); @@ -32,7 +31,6 @@ pub async fn start_collector_container() -> Arc> { if let Some(container) = &*arc_guard { Arc::clone(container) } else { - // Make sure all our test data is mounted upsert_empty_file(METRICS_FILE); upsert_empty_file(TRACES_FILE); @@ -75,4 +73,4 @@ pub fn stop_collector_container() { container.stop(); println!("Collector container stopped."); } -} \ No newline at end of file +} diff --git a/opentelemetry-otlp/tests/integration_test/src/trace_asserter.rs b/opentelemetry-otlp/tests/integration_test/src/trace_asserter.rs index 763a727fa3..ce7eec928a 100644 --- a/opentelemetry-otlp/tests/integration_test/src/trace_asserter.rs +++ b/opentelemetry-otlp/tests/integration_test/src/trace_asserter.rs @@ -1,8 +1,8 @@ +use anyhow::Result; use opentelemetry_proto::tonic::trace::v1::{ResourceSpans, Span, TracesData}; use std::collections::{HashMap, HashSet}; use std::fmt::{Debug, Formatter}; use std::fs::File; -use anyhow::Result; // Given two ResourceSpans, assert that they are equal except for the timestamps pub struct TraceAsserter { diff --git a/opentelemetry-otlp/tests/integration_test/tests/logs.rs b/opentelemetry-otlp/tests/integration_test/tests/logs.rs index f378370e3e..54e5bb9c41 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/logs.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/logs.rs @@ -1,6 +1,9 @@ #![cfg(unix)] +use anyhow::Result; +use ctor::dtor; use integration_test_runner::logs_asserter::{read_logs_from_json, LogsAsserter}; +use integration_test_runner::test_utils; use log::{info, Level}; use opentelemetry::KeyValue; use opentelemetry_appender_log::OpenTelemetryLogBridge; @@ -11,9 +14,6 @@ use std::error::Error; use std::fs::File; use std::os::unix::fs::MetadataExt; use std::time::Duration; -use integration_test_runner::test_utils; -use anyhow::Result; -use ctor::dtor; fn init_logs() -> Result { let exporter_builder = LogExporter::builder(); @@ -40,7 +40,6 @@ fn init_logs() -> Result { #[tokio::test(flavor = "multi_thread", worker_threads = 4)] pub async fn test_logs() -> Result<()> { - // Make sure the container is running test_utils::start_collector_container().await; diff --git a/opentelemetry-otlp/tests/integration_test/tests/metrics.rs b/opentelemetry-otlp/tests/integration_test/tests/metrics.rs index e1065d1216..c929ea5736 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/metrics.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/metrics.rs @@ -1,20 +1,20 @@ #![cfg(unix)] -use std::fs; +use anyhow::{Context, Result}; +use ctor::dtor; use integration_test_runner::metrics_asserter::{read_metrics_from_json, MetricsAsserter}; +use integration_test_runner::test_utils; +use integration_test_runner::test_utils::start_collector_container; use opentelemetry::KeyValue; use opentelemetry_otlp::MetricExporter; +use opentelemetry_proto::tonic::metrics::v1::MetricsData; use opentelemetry_sdk::metrics::{MeterProviderBuilder, PeriodicReader, SdkMeterProvider}; use opentelemetry_sdk::Resource; +use serde_json::Value; +use std::fs; use std::fs::File; use std::sync::Mutex; use std::time::Duration; -use serde_json::Value; -use anyhow::{Result, Context}; -use ctor::dtor; -use integration_test_runner::test_utils; -use integration_test_runner::test_utils::start_collector_container; -use opentelemetry_proto::tonic::metrics::v1::MetricsData; static SETUP_DONE: Mutex = Mutex::new(false); @@ -22,7 +22,6 @@ static RESULT_PATH: &str = "actual/metrics.json"; /// Initializes the OpenTelemetry metrics pipeline async fn init_metrics() -> SdkMeterProvider { - let exporter_builder = MetricExporter::builder(); #[cfg(feature = "tonic-client")] @@ -72,9 +71,15 @@ pub fn fetch_latest_metrics_for_scope(scope_name: &str) -> Result { // Parse the JSON and filter metrics strictly by the scope name let mut filtered_json = json_line; - if let Some(resource_metrics) = filtered_json.get_mut("resourceMetrics").and_then(|v| v.as_array_mut()) { + if let Some(resource_metrics) = filtered_json + .get_mut("resourceMetrics") + .and_then(|v| v.as_array_mut()) + { resource_metrics.retain_mut(|resource| { - if let Some(scope_metrics) = resource.get_mut("scopeMetrics").and_then(|v| v.as_array_mut()) { + if let Some(scope_metrics) = resource + .get_mut("scopeMetrics") + .and_then(|v| v.as_array_mut()) + { // Retain only `ScopeMetrics` that match the specified scope_name scope_metrics.retain(|scope| { scope @@ -98,11 +103,10 @@ pub fn fetch_latest_metrics_for_scope(scope_name: &str) -> Result { /// Performs setup for metrics tests, including environment setup and data seeding. /// This only needs to be done once for the whole test suite async fn setup_metrics_test() { - let mut done = SETUP_DONE.lock().unwrap(); if !*done { println!("Running setup before any tests..."); - *done = true; // Mark setup as done + *done = true; // Mark setup as done // Make sure the collector container is running start_collector_container().await; @@ -129,8 +133,10 @@ pub fn validate_metrics_against_results(scope_name: &str) -> Result<()> { // Read the expected metrics from the results file let expected_metrics = { - let file = File::open(&results_file_path) - .context(format!("Failed to open results file: {}", results_file_path))?; + let file = File::open(&results_file_path).context(format!( + "Failed to open results file: {}", + results_file_path + ))?; read_metrics_from_json(file) }?; @@ -171,8 +177,7 @@ mod tests { const METER_NAME: &str = "test_u64_counter_meter"; // Add data to u64_counter - let meter = opentelemetry::global::meter_provider() - .meter(METER_NAME); + let meter = opentelemetry::global::meter_provider().meter(METER_NAME); let counter = meter.u64_counter("counter_u64").build(); counter.add( @@ -191,7 +196,6 @@ mod tests { Ok(()) } - #[tokio::test(flavor = "multi_thread", worker_threads = 4)] // #[ignore] // skip when running unit test async fn test_histogram() -> Result<()> { @@ -199,7 +203,7 @@ mod tests { const METER_NAME: &str = "test_histogram_meter"; // Add data to histogram - let meter= opentelemetry::global::meter_provider().meter(METER_NAME); + let meter = opentelemetry::global::meter_provider().meter(METER_NAME); let histogram = meter.u64_histogram("example_histogram").build(); histogram.record(42, &[KeyValue::new("mykey3", "myvalue4")]); tokio::time::sleep(Duration::from_secs(5)).await; @@ -216,7 +220,7 @@ mod tests { const METER_NAME: &str = "test_up_down_meter"; // Add data to up_down_counter - let meter= opentelemetry::global::meter_provider().meter(METER_NAME); + let meter = opentelemetry::global::meter_provider().meter(METER_NAME); let up_down_counter = meter.i64_up_down_counter("example_up_down_counter").build(); up_down_counter.add(-1, &[KeyValue::new("mykey5", "myvalue5")]); tokio::time::sleep(Duration::from_secs(5)).await; diff --git a/opentelemetry-otlp/tests/integration_test/tests/traces.rs b/opentelemetry-otlp/tests/integration_test/tests/traces.rs index 24dd9602bc..98aebab7eb 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/traces.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/traces.rs @@ -9,6 +9,9 @@ use opentelemetry::{ }; use opentelemetry_otlp::SpanExporter; +use anyhow::Result; +use ctor::dtor; +use integration_test_runner::test_utils; use opentelemetry_proto::tonic::trace::v1::TracesData; use opentelemetry_sdk::{runtime, trace as sdktrace, Resource}; use std::error::Error; @@ -16,10 +19,7 @@ use std::fs::File; use std::io::Write; use std::os::unix::fs::MetadataExt; use std::time::Duration; -use anyhow::Result; -use ctor::dtor; use tokio::time::sleep; -use integration_test_runner::test_utils; fn init_tracer_provider() -> Result { let exporter_builder = SpanExporter::builder(); @@ -49,7 +49,6 @@ const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another"); #[tokio::test(flavor = "multi_thread", worker_threads = 4)] pub async fn traces() -> Result<()> { - test_utils::start_collector_container().await; let tracer_provider = init_tracer_provider().expect("Failed to initialize tracer provider.");