Skip to content

Commit df30c5f

Browse files
committed
Add more metric integration tests
1 parent 9dfcff1 commit df30c5f

File tree

2 files changed

+125
-25
lines changed

2 files changed

+125
-25
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"resourceMetrics": [
3+
{
4+
"resource": {
5+
"attributes": [
6+
{
7+
"key": "service.name",
8+
"value": {
9+
"stringValue": "metrics-integration-test"
10+
}
11+
}
12+
]
13+
},
14+
"scopeMetrics": [
15+
{
16+
"scope": {
17+
"name": "test_meter"
18+
},
19+
"metrics": [
20+
{
21+
"name": "test_counter",
22+
"sum": {
23+
"dataPoints": [
24+
{
25+
"attributes": [
26+
{
27+
"key": "mykey1",
28+
"value": {
29+
"stringValue": "myvalue1"
30+
}
31+
},
32+
{
33+
"key": "mykey2",
34+
"value": {
35+
"stringValue": "myvalue2"
36+
}
37+
}
38+
],
39+
"startTimeUnixNano": "1734255506254812000",
40+
"timeUnixNano": "1734255533415552000",
41+
"asInt": "10"
42+
}
43+
],
44+
"aggregationTemporality": 2,
45+
"isMonotonic": true
46+
}
47+
}
48+
]
49+
}
50+
]
51+
}
52+
]
53+
}

opentelemetry-otlp/tests/integration_test/tests/metrics.rs

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@ use opentelemetry_proto::tonic::metrics::v1::MetricsData;
1616
use opentelemetry_sdk::metrics::{MeterProviderBuilder, PeriodicReader, SdkMeterProvider};
1717
use opentelemetry_sdk::Resource;
1818
use serde_json::Value;
19+
use std::error::Error;
1920
use std::fs;
2021
use std::fs::File;
21-
use std::sync::Mutex;
2222
use std::time::Duration;
2323

24-
static SETUP_DONE: Mutex<bool> = Mutex::new(false);
24+
const SLEEP_DURATION: Duration = Duration::from_secs(2);
2525

2626
static RESULT_PATH: &str = "actual/metrics.json";
2727

2828
/// Initializes the OpenTelemetry metrics pipeline
29-
async fn init_metrics() -> SdkMeterProvider {
29+
fn init_metrics() -> SdkMeterProvider {
3030
let exporter = create_exporter();
3131

32-
let reader = PeriodicReader::builder(exporter)
33-
.with_interval(Duration::from_millis(500))
34-
.with_timeout(Duration::from_secs(1))
35-
.build();
32+
let reader = PeriodicReader::builder(exporter).build();
3633

3734
let resource = Resource::builder_empty()
3835
.with_service_name("metrics-integration-test")
@@ -137,23 +134,15 @@ pub fn fetch_latest_metrics_for_scope(scope_name: &str) -> Result<Value> {
137134
///
138135
/// Performs setup for metrics tests
139136
///
140-
async fn setup_metrics_test() -> Result<()> {
137+
async fn setup_metrics_test() -> Result<SdkMeterProvider, Box<dyn Error>> {
141138
// Make sure the collector container is running
142139
start_collector_container().await?;
143140

144-
let mut done = SETUP_DONE.lock().unwrap();
145-
if !*done {
146-
println!("Running setup before any tests...");
147-
*done = true; // Mark setup as done
148-
149-
// Initialize the metrics subsystem
150-
_ = init_metrics().await;
151-
}
152-
153141
// Truncate results
154142
_ = File::create(RESULT_PATH).expect("it's good");
155143

156-
Ok(())
144+
let meter_provider = init_metrics();
145+
Ok(meter_provider)
157146
}
158147

159148
///
@@ -219,9 +208,55 @@ mod metrictests {
219208
Ok(())
220209
}
221210

211+
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
212+
async fn counter_tokio_multi_thread() -> Result<()> {
213+
metric_helper().await
214+
}
215+
216+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
217+
async fn counter_tokio_multi_thread_one_worker() -> Result<()> {
218+
metric_helper().await
219+
}
220+
221+
#[tokio::test(flavor = "current_thread")]
222+
#[ignore] // TODO: Investigate why this test is failing
223+
async fn counter_tokio_current() -> Result<()> {
224+
metric_helper().await
225+
}
226+
227+
async fn metric_helper() -> Result<()> {
228+
let meter_provider = setup_metrics_test()
229+
.await
230+
.expect("MeterProvider must be available for test to run");
231+
const METER_NAME: &str = "test_meter";
232+
233+
// Add data to u64_counter
234+
let meter = opentelemetry::global::meter_provider().meter(METER_NAME);
235+
236+
let counter = meter.u64_counter("test_counter").build();
237+
counter.add(
238+
10,
239+
&[
240+
KeyValue::new("mykey1", "myvalue1"),
241+
KeyValue::new("mykey2", "myvalue2"),
242+
],
243+
);
244+
245+
meter_provider.shutdown()?;
246+
// We still need to sleep, to give otel-collector a chance to flush to disk
247+
tokio::time::sleep(SLEEP_DURATION).await;
248+
249+
// Validate metrics against results file
250+
validate_metrics_against_results(METER_NAME)?;
251+
252+
Ok(())
253+
}
254+
222255
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
223256
async fn test_u64_counter() -> Result<()> {
224-
let _result_path = setup_metrics_test().await;
257+
let meter_provider = setup_metrics_test()
258+
.await
259+
.expect("MeterProvider must be available for test to run");
225260
const METER_NAME: &str = "test_u64_counter_meter";
226261

227262
// Add data to u64_counter
@@ -236,7 +271,9 @@ mod metrictests {
236271
],
237272
);
238273

239-
tokio::time::sleep(Duration::from_secs(2)).await;
274+
meter_provider.shutdown()?;
275+
// We still need to sleep, to give otel-collector a chance to flush to disk
276+
tokio::time::sleep(SLEEP_DURATION).await;
240277

241278
// Validate metrics against results file
242279
validate_metrics_against_results(METER_NAME)?;
@@ -246,14 +283,19 @@ mod metrictests {
246283

247284
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
248285
async fn test_histogram() -> Result<()> {
249-
_ = setup_metrics_test().await;
286+
let meter_provider = setup_metrics_test()
287+
.await
288+
.expect("MeterProvider must be available for test to run");
250289
const METER_NAME: &str = "test_histogram_meter";
251290

252291
// Add data to histogram
253292
let meter = opentelemetry::global::meter_provider().meter(METER_NAME);
254293
let histogram = meter.u64_histogram("example_histogram").build();
255294
histogram.record(42, &[KeyValue::new("mykey3", "myvalue4")]);
256-
tokio::time::sleep(Duration::from_secs(5)).await;
295+
296+
meter_provider.shutdown()?;
297+
// We still need to sleep, to give otel-collector a chance to flush to disk
298+
tokio::time::sleep(SLEEP_DURATION).await;
257299

258300
validate_metrics_against_results(METER_NAME)?;
259301

@@ -262,14 +304,19 @@ mod metrictests {
262304

263305
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
264306
async fn test_up_down_counter() -> Result<()> {
265-
_ = setup_metrics_test().await;
307+
let meter_provider = setup_metrics_test()
308+
.await
309+
.expect("MeterProvider must be available for test to run");
266310
const METER_NAME: &str = "test_up_down_meter";
267311

268312
// Add data to up_down_counter
269313
let meter = opentelemetry::global::meter_provider().meter(METER_NAME);
270314
let up_down_counter = meter.i64_up_down_counter("example_up_down_counter").build();
271315
up_down_counter.add(-1, &[KeyValue::new("mykey5", "myvalue5")]);
272-
tokio::time::sleep(Duration::from_secs(5)).await;
316+
317+
meter_provider.shutdown()?;
318+
// We still need to sleep, to give otel-collector a chance to flush to disk
319+
tokio::time::sleep(SLEEP_DURATION).await;
273320

274321
validate_metrics_against_results(METER_NAME)?;
275322

@@ -310,7 +357,7 @@ mod metrictests {
310357
meter_provider.shutdown()?;
311358

312359
// We still need to sleep, to give otel-collector a chance to flush to disk
313-
tokio::time::sleep(Duration::from_secs(2)).await;
360+
tokio::time::sleep(SLEEP_DURATION).await;
314361

315362
validate_metrics_against_results(METER_NAME)?;
316363

0 commit comments

Comments
 (0)