Skip to content

Commit

Permalink
[PLATFORM-1471]: Add e2e tests for tracing (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad authored Feb 29, 2024
1 parent 5990f83 commit b94a4ee
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 4 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,4 +45,4 @@ jobs:
- lint
- test
steps:
- run: ${{ !contains(needs.*.result, 'failure') }}
- run: ${{ !contains(needs.*.result, 'failure') }}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ args = ["fmt", "--", "--check"]
[tasks.test]
description = "Run tests."
command = "cargo"
args = ["test", "${@}"]
args = ["test", "--all-features", "${@}"]

[tasks.clippy]
command = "cargo"
Expand Down
37 changes: 37 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use self::{
country::{Country, CountryParseError},
country::Country,
environment::{Environment, EnvironmentParseError},
};
#[cfg(feature = "json-logger")]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,6 +15,7 @@
//! );
//!
//! let _guard = init_subscriber(subscriber);
//! # }
//! ```
mod config;
Expand Down
61 changes: 61 additions & 0 deletions tests/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<Span>> {
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);
}
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod e2e;

0 comments on commit b94a4ee

Please sign in to comment.