Skip to content

Commit 4c693ce

Browse files
committed
Add otlp_tracing test to ensure the traces are accessible for collectors
Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 21450b1 commit 4c693ce

File tree

1 file changed

+67
-8
lines changed
  • src/hyperlight_host/examples/otlp_tracing

1 file changed

+67
-8
lines changed

src/hyperlight_host/examples/otlp_tracing/main.rs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use opentelemetry_semantic_conventions::SCHEMA_URL;
4242
use tracing_subscriber::EnvFilter;
4343
use uuid::Uuid;
4444

45-
const ADDR: &str = "http://localhost:4317";
45+
const ENDPOINT_ADDR: &str = "http://localhost:4317";
4646

4747
fn fn_writer(_msg: String) -> HyperlightResult<i32> {
4848
Ok(0)
@@ -52,9 +52,9 @@ fn fn_writer(_msg: String) -> HyperlightResult<i32> {
5252

5353
#[tokio::main]
5454
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
55-
init_tracing_subscriber(ADDR)?;
55+
init_tracing_subscriber(ENDPOINT_ADDR)?;
5656

57-
Ok(run_example()?)
57+
Ok(run_example(true)?)
5858
}
5959

6060
fn init_tracing_subscriber(addr: &str) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
@@ -81,15 +81,22 @@ fn init_tracing_subscriber(addr: &str) -> Result<(), Box<dyn Error + Send + Sync
8181

8282
let otel_layer = OpenTelemetryLayer::new(tracer);
8383

84+
// Try using the environment otherwise set default filters
85+
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
86+
EnvFilter::from_default_env()
87+
.add_directive("hyperlight_host=info".parse().unwrap())
88+
.add_directive("tracing=info".parse().unwrap())
89+
});
90+
8491
tracing_subscriber::registry()
85-
.with(EnvFilter::from_default_env())
92+
.with(filter)
8693
.with(otel_layer)
8794
.try_init()?;
8895

8996
Ok(())
9097
}
9198

92-
fn run_example() -> HyperlightResult<()> {
99+
fn run_example(wait_input: bool) -> HyperlightResult<()> {
93100
// Get the path to a simple guest binary.
94101
let hyperlight_guest_path =
95102
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
@@ -187,9 +194,12 @@ fn run_example() -> HyperlightResult<()> {
187194
join_handles.push(handle);
188195
}
189196

190-
println!("Press enter to exit...");
191-
let mut input = String::new();
192-
stdin().read_line(&mut input)?;
197+
if wait_input {
198+
println!("Press enter to exit...");
199+
let mut input = String::new();
200+
stdin().read_line(&mut input)?;
201+
}
202+
193203
*should_exit.try_lock().unwrap() = true;
194204
for join_handle in join_handles {
195205
let result = join_handle.join();
@@ -199,3 +209,52 @@ fn run_example() -> HyperlightResult<()> {
199209

200210
Ok(())
201211
}
212+
213+
#[cfg(test)]
214+
mod test {
215+
use hyperlight_host::{HyperlightError, Result};
216+
use tokio::io::AsyncReadExt;
217+
use tokio::net::{TcpListener, TcpStream};
218+
219+
use super::*;
220+
221+
const TESTER_ADDR: &str = "127.0.0.1:4317";
222+
223+
async fn handle(mut stream: TcpStream) -> Result<()> {
224+
let mut buf = Vec::with_capacity(128);
225+
let size = stream.read_buf(&mut buf).await?;
226+
227+
if size > 0 {
228+
Ok(())
229+
} else {
230+
Err(HyperlightError::Error("Cannot read req body".to_string()))
231+
}
232+
}
233+
234+
async fn check_otl_connection(addr: &str) -> Result<()> {
235+
let listener = TcpListener::bind(addr).await?;
236+
237+
let (stream, _) = listener.accept().await?;
238+
239+
handle(stream).await
240+
}
241+
242+
#[tokio::test]
243+
async fn test_subscriber() {
244+
// Create task that generates spans
245+
let task = tokio::spawn(async move {
246+
let _ = init_tracing_subscriber(ENDPOINT_ADDR);
247+
248+
// No need to wait for input, just generate some spans and exit
249+
let _ = run_example(false);
250+
});
251+
252+
// Create server that listens and checks to see if traces are received
253+
let result = check_otl_connection(TESTER_ADDR).await;
254+
255+
// Abort task in case it doesn't finish
256+
task.abort();
257+
258+
assert!(result.is_ok());
259+
}
260+
}

0 commit comments

Comments
 (0)