Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgerring committed Dec 15, 2024
1 parent 02e7a85 commit 35034f4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 52 deletions.
21 changes: 10 additions & 11 deletions opentelemetry-otlp/tests/integration_test/src/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
]),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-otlp/tests/integration_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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<Value> {
// Create a buffered reader for the file
Expand All @@ -18,7 +18,6 @@ pub fn read_metrics_from_json(file: File) -> Result<Value> {
// Parse the contents into a JSON Value
let metrics_data: Value = serde_json::from_str(&contents)?;
Ok(metrics_data)

}

pub struct MetricsAsserter {
Expand Down
10 changes: 4 additions & 6 deletions opentelemetry-otlp/tests/integration_test/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<Option<Arc<Container<Collector>>>>> = OnceLock::new();
Expand All @@ -32,7 +31,6 @@ pub async fn start_collector_container() -> Arc<Container<'static, Collector>> {
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);
Expand Down Expand Up @@ -75,4 +73,4 @@ pub fn stop_collector_container() {
container.stop();
println!("Collector container stopped.");
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions opentelemetry-otlp/tests/integration_test/tests/logs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<sdklogs::LoggerProvider> {
let exporter_builder = LogExporter::builder();
Expand All @@ -40,7 +40,6 @@ fn init_logs() -> Result<sdklogs::LoggerProvider> {

#[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;

Expand Down
42 changes: 23 additions & 19 deletions opentelemetry-otlp/tests/integration_test/tests/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
#![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<bool> = Mutex::new(false);

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")]
Expand Down Expand Up @@ -72,9 +71,15 @@ pub fn fetch_latest_metrics_for_scope(scope_name: &str) -> Result<Value> {

// 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
Expand All @@ -98,11 +103,10 @@ pub fn fetch_latest_metrics_for_scope(scope_name: &str) -> Result<Value> {
/// 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;
Expand All @@ -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)
}?;

Expand Down Expand Up @@ -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(
Expand All @@ -191,15 +196,14 @@ mod tests {
Ok(())
}


#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
// #[ignore] // skip when running unit test
async fn test_histogram() -> Result<()> {
_ = setup_metrics_test().await;
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;
Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions opentelemetry-otlp/tests/integration_test/tests/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ 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;
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<sdktrace::TracerProvider, TraceError> {
let exporter_builder = SpanExporter::builder();
Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit 35034f4

Please sign in to comment.