From b94a4ee6413ea4efe0ab78862ccdc435ab750a79 Mon Sep 17 00:00:00 2001 From: MaeIsBad <26093674+MaeIsBad@users.noreply.github.com> Date: Thu, 29 Feb 2024 17:10:26 +0100 Subject: [PATCH] [PLATFORM-1471]: Add e2e tests for tracing (#90) --- .github/workflows/ci.yml | 13 ++++++++- Cargo.toml | 1 + Makefile.toml | 2 +- docker-compose.yml | 37 ++++++++++++++++++++++++ src/config/mod.rs | 2 +- src/lib.rs | 4 ++- tests/e2e/mod.rs | 61 ++++++++++++++++++++++++++++++++++++++++ tests/main.rs | 1 + 8 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 docker-compose.yml create mode 100644 tests/e2e/mod.rs create mode 100644 tests/main.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a511a3f..edba378 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,18 @@ jobs: # Avoid duplicate jobs on PR from a branch on the same repo if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name runs-on: ubuntu-latest + services: + jaeger: + image: jaegertracing/all-in-one:1.35 + env: + COLLECTOR_OTLP_ENABLED: "true" + COLLECTOR_OTLP_HTTP_HOST_PORT: 55681 + ports: + - 16685:16685 + - 55681:55681 + steps: + - run: sudo echo "127.0.0.1 jaeger" | sudo tee -a /etc/hosts - uses: actions/checkout@v4 - uses: Swatinem/rust-cache@v2 - uses: taiki-e/install-action@cargo-make @@ -34,4 +45,4 @@ jobs: - lint - test steps: - - run: ${{ !contains(needs.*.result, 'failure') }} \ No newline at end of file + - run: ${{ !contains(needs.*.result, 'failure') }} diff --git a/Cargo.toml b/Cargo.toml index 8b582ec..91d44cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ chrono = {version = "^0.4", default-features = false, features = ["serde", "cloc [dev-dependencies] actix-web = "4.0.1" +opentelemetry-jaeger = {version = "0.20", features = ["integration_test"]} prima_bridge = "0.15" tokio = {version = "1.17", features = ["rt", "macros", "rt-multi-thread"]} tracing-actix-web = {version = "0.7.0", features = ["opentelemetry_0_21"]} diff --git a/Makefile.toml b/Makefile.toml index 67f7507..e372d6f 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -17,7 +17,7 @@ args = ["fmt", "--", "--check"] [tasks.test] description = "Run tests." command = "cargo" -args = ["test", "${@}"] +args = ["test", "--all-features", "${@}"] [tasks.clippy] command = "cargo" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e1365cb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: "3" + +services: + app: + user: root + build: . + volumes: + - .:/code + - "app:/home/app/" + - "~/.ssh:/home/app/.ssh" + - "~/.aws:/home/app/.aws" + - "~/.gitconfig:/home/app/.gitconfig" + - "~/.gitignore:/home/app/.gitignore" + working_dir: /code + ports: + - "3000:3000" + depends_on: + - jaeger + environment: + BUILD_ENV: dev + CARGO_HOME: /home/app/.cargo + CARGO_TARGET_DIR: /home/app/target + CARGO_MAKE_DISABLE_UPDATE_CHECK: 1 + networks: + - default + + jaeger: + image: jaegertracing/all-in-one:1.35 + ports: + - "16685:16685" + - "55681:55681" + environment: + COLLECTOR_OTLP_ENABLED: "true" + COLLECTOR_OTLP_HTTP_HOST_PORT: 55681 + +volumes: + app: diff --git a/src/config/mod.rs b/src/config/mod.rs index b15d829..7044222 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,5 +1,5 @@ pub use self::{ - country::{Country, CountryParseError}, + country::Country, environment::{Environment, EnvironmentParseError}, }; #[cfg(feature = "json-logger")] diff --git a/src/lib.rs b/src/lib.rs index 2faa6cf..6ae3ac8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,8 @@ //! ```rust //! //! use prima_tracing::{builder, configure_subscriber, Country, Environment, init_subscriber}; -//! +//! # #[cfg(not(feature = "traces"))] +//! # { //! let subscriber = configure_subscriber( //! builder("ping") //! .with_country(Country::Common) @@ -14,6 +15,7 @@ //! ); //! //! let _guard = init_subscriber(subscriber); +//! # } //! ``` mod config; diff --git a/tests/e2e/mod.rs b/tests/e2e/mod.rs new file mode 100644 index 0000000..8959095 --- /dev/null +++ b/tests/e2e/mod.rs @@ -0,0 +1,61 @@ +use opentelemetry_jaeger::testing::jaeger_api_v2::Span; +use opentelemetry_jaeger::testing::jaeger_client::JaegerTestClient; +use prima_tracing::{builder, configure_subscriber, init_subscriber, Country, Environment}; +use std::time::SystemTime; + +async fn get_spans(f: impl FnOnce()) -> Option> { + std::env::set_var("RUST_LOG", "info"); + + // Unique id for this test run + let seed = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis(); + let service_name = format!("e2e-test-{seed}"); + + let collector_url = "http://jaeger:55681"; + let query_api_url = "http://jaeger:16685"; + + let subscriber = configure_subscriber( + builder(&service_name) + .with_country(Country::Common) + .with_env(Environment::Dev) + .with_telemetry(collector_url.to_string(), service_name.clone()) + .build(), + ); + + { + let _guard = init_subscriber(subscriber); + f() + } + + let mut client = JaegerTestClient::new(query_api_url); + + if !client.contain_service(&service_name).await { + None + } else { + let spans = client.find_traces_from_services(&service_name).await; + Some(spans) + } +} + +#[cfg(feature = "traces")] +#[tokio::test(flavor = "multi_thread")] +async fn it_sends_traces_to_jaeger() { + let log_message = "hello it_sends_traces_to_jaeger"; + + let spans = get_spans(|| { + let span = tracing::info_span!("my span"); + span.in_scope(|| { + tracing::info!("{log_message}"); + }); + }) + .await + .expect("Failed to fetch traces from jaeger"); + + assert_eq!(spans.len(), 1); + assert_eq!(spans[0].logs.len(), 1); + + let msg = spans[0].logs[0].fields[0].v_str.as_str(); + assert_eq!(log_message, msg); +} diff --git a/tests/main.rs b/tests/main.rs new file mode 100644 index 0000000..b6ae44f --- /dev/null +++ b/tests/main.rs @@ -0,0 +1 @@ +mod e2e;