Skip to content

Commit f6a61a1

Browse files
authored
Merge pull request #49 from birkmose/main
Stress test to test effects of gzip compression + measure upload times
2 parents 020965d + 6dc5efd commit f6a61a1

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ required-features = ["metrics"]
8686
[[example]]
8787
name = "opentelemetry"
8888
required-features = ["reqwest-client", "opentelemetry/rt-tokio"]
89+
90+
[[example]]
91+
name = "stress_test"
92+
required-features = ["reqwest-client", "opentelemetry/rt-tokio"]

examples/stress_test.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use opentelemetry::{
2+
global,
3+
trace::{FutureExt, SpanKind, TraceContextExt, Tracer},
4+
Context, KeyValue,
5+
};
6+
use std::env;
7+
use std::error::Error;
8+
use std::ops::Add;
9+
use std::time::{Duration, Instant, SystemTime};
10+
11+
async fn mock_sql_call(n: u64, duration: u64) {
12+
let tracer = global::tracer("run_in_child_process_new");
13+
let now = SystemTime::now();
14+
let end_time = now.add(Duration::from_millis(duration));
15+
tracer
16+
.span_builder("test_db")
17+
.with_kind(SpanKind::Client)
18+
.with_attributes(vec![
19+
KeyValue::new("service.name", "test-database"),
20+
KeyValue::new("db.system", "SQL"),
21+
KeyValue::new(
22+
"db.statement",
23+
format!("SELECT * FROM test WHERE test_id = {}", n),
24+
),
25+
])
26+
.with_start_time(now)
27+
.with_end_time(end_time)
28+
.start(&tracer);
29+
}
30+
31+
async fn mock_serve_http_request(n: u64) {
32+
let tracer = global::tracer("named tracer");
33+
let now = SystemTime::now();
34+
let duration = 10 + (n % 50);
35+
let end_time = now.add(Duration::from_millis(duration));
36+
let span = tracer
37+
.span_builder("localhost")
38+
.with_attributes(vec![
39+
KeyValue::new("http.status_code", 200),
40+
KeyValue::new("http.client_id", "127.0.0.1"),
41+
KeyValue::new("http.server_name", "localhost:80"),
42+
KeyValue::new("http.http_method", "GET"),
43+
KeyValue::new("http.target", format!("/test/{}", n)),
44+
KeyValue::new("http.flavor", "1.1"),
45+
KeyValue::new("net.peer.id", "127.0.0.1:42424"),
46+
KeyValue::new("http.route", "/test/:test_id"),
47+
KeyValue::new("http.host", "localhost:80"),
48+
KeyValue::new("service.name", "test-http-server"),
49+
])
50+
.with_start_time(now)
51+
.with_end_time(end_time)
52+
.with_kind(SpanKind::Server)
53+
.start(&tracer);
54+
55+
let cx = Context::new().with_span(span);
56+
mock_sql_call(n, duration - 5).with_context(cx).await;
57+
}
58+
59+
// This example emulates the traces that a typical HTTP server with a SQL server dependency would generate.
60+
// The amount of traces generated is controlled by the NUM_ROOT_SPANS environment variable.
61+
// WARNING: Please notice at large NUM_ROOT_SPANS settings, this can incur real costs at your application insights resource - so be cautious!
62+
#[tokio::main]
63+
async fn main() -> Result<(), Box<dyn Error>> {
64+
env_logger::init();
65+
66+
let instrumentation_key =
67+
env::var("INSTRUMENTATION_KEY").expect("env var INSTRUMENTATION_KEY should exist");
68+
69+
// Please note with large NUM_ROOT_SPANS settings the batch span processor might start falling behind
70+
// You can mitigate this by configuring the batch span processor using the standard SDK environment variables
71+
// for instance:
72+
//
73+
// export OTEL_BSP_MAX_QUEUE_SIZE=200000
74+
//
75+
// For further details please refer to: https://opentelemetry.io/docs/reference/specification/sdk-environment-variables/#batch-span-processor
76+
let num_root_spans = env::var("NUM_ROOT_SPANS")
77+
.expect("env var NUM_ROOT_SPANS should exist")
78+
.parse::<u64>()
79+
.expect("NUM_ROOT_SPANS could not be parsed");
80+
81+
let timer = Instant::now();
82+
83+
opentelemetry_application_insights::new_pipeline(instrumentation_key)
84+
.with_service_name("stress-test")
85+
.with_client(reqwest::Client::new())
86+
.install_batch(opentelemetry::runtime::Tokio);
87+
88+
for i in 1..num_root_spans + 1 {
89+
mock_serve_http_request(i).await;
90+
if i % 1000 == 0 {
91+
println!("Mocked {} root spans", i);
92+
}
93+
}
94+
95+
opentelemetry::global::shutdown_tracer_provider();
96+
97+
let duration = timer.elapsed();
98+
99+
println!(
100+
"Finished uploading {} root spans in: {:?}",
101+
num_root_spans, duration
102+
);
103+
104+
Ok(())
105+
}

0 commit comments

Comments
 (0)