Skip to content

Commit 6e0cc6b

Browse files
committed
Add more unit tests and improve documentation
1 parent 15dff50 commit 6e0cc6b

File tree

14 files changed

+216
-14
lines changed

14 files changed

+216
-14
lines changed

opentelemetry-declarative-config/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ opentelemetry-stdout = { version = "0.31.0" }
2525
opentelemetry-otlp = { version = "0.31.0" }
2626
serde = { workspace = true, features = ["derive"] }
2727
serde_yaml = "0.9.34"
28-
tokio = { workspace = true, features = ["sync", "rt"], optional = true }
2928

3029
[dev-dependencies]
3130
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

opentelemetry-declarative-config/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ of telemetry is intentionally left to other tools.
2323

2424
[`Prometheus`]: https://prometheus.io
2525
[`OpenTelemetry`]: https://crates.io/crates/opentelemetry
26+
[`Jaeger`]: https://www.jaegertracing.io
2627

2728
## Release Notes
2829

opentelemetry-declarative-config/examples/declarative_basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
2929
"#;
3030
let result = configurator.configure_telemetry_from_yaml(config_yaml.into());
3131
if let Err(ref e) = result {
32-
panic!("Failed to configure telemetry from YAML file: {}", e);
32+
panic!("Failed to configure telemetry from YAML string: {}", e);
3333
}
3434
assert!(result.is_ok());
3535
let telemetry_providers = result.unwrap();

opentelemetry-declarative-config/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Configurator {
6262
.with_logs_provider(sdk_logger_provider_option);
6363
}
6464

65-
//TODO: Add similar configuration for traces and logs when implemented
65+
//TODO: Add similar configuration for traces when implemented
6666
Ok(configured_telemetry_providers)
6767
}
6868

opentelemetry-declarative-config/src/logs.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,25 @@ impl BatchExporterFactory {
4444
// Get factory by name (useful for configuration-driven creation)
4545
pub fn from_name(name: &str) -> Result<Self, Box<dyn std::error::Error>> {
4646
match name {
47-
"stdout" => Ok(BatchExporterFactory::Stdout(StdoutLogsBatchExporterFactory::new())),
47+
"stdout" => Ok(Self::stdout()),
4848
"otlp" => Ok(Self::otlp()),
4949
_ => Err(format!("Unknown logs exporter factory: {}", name).into()),
5050
}
5151
}
5252
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
#[test]
59+
fn test_logs_batch_exporter_factory_from_name() {
60+
let stdout_factory = BatchExporterFactory::from_name("stdout").unwrap();
61+
let otlp_factory = BatchExporterFactory::from_name("otlp").unwrap();
62+
let unknown_factory = BatchExporterFactory::from_name("unknown");
63+
64+
assert!(matches!(stdout_factory, BatchExporterFactory::Stdout(_)));
65+
assert!(matches!(otlp_factory, BatchExporterFactory::Otlp(_)));
66+
assert!(unknown_factory.is_err());
67+
}
68+
}

opentelemetry-declarative-config/src/logs/exporters/otlp_batch_exporter.rs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//! # Stdout log exporter module.
1+
//! # OTLP log exporter module.
22
//!
3-
//! This module contains the definitions and common implementations for the stdout logs exporter
3+
//! This module contains the definitions and common implementations for the OTLP logs exporter
44
//! that can be used with OpenTelemetry SDKs. Exporters are responsible for sending
5-
//! collected logs data to different backends or systems.
5+
//! collected logs data to OTLP-compatible backends or systems.
66
77
use opentelemetry_sdk::logs::LogExporter;
88
use serde::{Deserialize, Deserializer};
@@ -30,7 +30,7 @@ impl Default for OtlpLogsBatchExporterFactory {
3030
impl LogsBatchExporterFactory for OtlpLogsBatchExporterFactory {
3131

3232
/// Creates a LogExporter based on the provided configuration
33-
fn create_logs_batch_exporter(&self, config: &Value) -> Result<impl LogExporter + 'static, Box<dyn std::error::Error>> {
33+
fn create_logs_batch_exporter(&self, _config: &Value) -> Result<impl LogExporter + 'static, Box<dyn std::error::Error>> {
3434
let exporter_builder = opentelemetry_otlp::LogExporter::builder();
3535

3636
#[cfg(feature = "tonic-client")]
@@ -41,11 +41,13 @@ impl LogsBatchExporterFactory for OtlpLogsBatchExporterFactory {
4141
feature = "reqwest-client",
4242
feature = "reqwest-blocking-client"
4343
))]
44-
let mut exporter_builder = exporter_builder.with_http();
44+
let exporter_builder = exporter_builder.with_http();
4545

46-
let _config_parsed = serde_yaml::from_value::<OtlpConfig>(config.clone())?;
46+
/*
47+
let config_parsed = serde_yaml::from_value::<OtlpConfig>(config.clone())?;
48+
*/
4749

48-
// TODO: Configure the exporter based on config_parsed fields. There are no methods in trhe builder to pass its parameters yet..
50+
// TODO: Configure the exporter based on config_parsed fields. There are no methods in the builder to pass its parameters yet..
4951

5052
let exporter = exporter_builder
5153
.build()?;
@@ -77,3 +79,55 @@ where
7779
_ => Err(serde::de::Error::custom(format!("Invalid protocol: {}", s))),
7880
}
7981
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
use serde_yaml::from_str;
87+
88+
#[tokio::test]
89+
async fn test_default_otlp_logs_batch_exporter_factory() {
90+
let factory = OtlpLogsBatchExporterFactory::default();
91+
assert!(factory.create_logs_batch_exporter(&Value::Null).is_ok());
92+
}
93+
94+
#[test]
95+
fn test_deserialize_otlp_config() {
96+
let yaml_data = r#"
97+
protocol: "grpc"
98+
endpoint: "http://localhost:4317"
99+
"#;
100+
let config: OtlpConfig = from_str(yaml_data).unwrap();
101+
assert_eq!(config.protocol, Some(opentelemetry_otlp::Protocol::Grpc));
102+
assert_eq!(config.endpoint, Some("http://localhost:4317".into()));
103+
}
104+
105+
#[test]
106+
fn test_deserialize_protocol() {
107+
let yaml_grpc = r#"
108+
protocol: "grpc"
109+
"#;
110+
let config: OtlpConfig = from_str(yaml_grpc).unwrap();
111+
assert_eq!(config.protocol, Some(opentelemetry_otlp::Protocol::Grpc));
112+
113+
let yaml_http_binary = r#"
114+
protocol: "http/protobuf"
115+
"#;
116+
let config: OtlpConfig = from_str(yaml_http_binary).unwrap();
117+
assert_eq!(config.protocol, Some(opentelemetry_otlp::Protocol::HttpBinary));
118+
119+
let yaml_http_json = r#"
120+
protocol: "http/json"
121+
"#;
122+
let config: OtlpConfig = from_str(yaml_http_json).unwrap();
123+
assert_eq!(config.protocol, Some(opentelemetry_otlp::Protocol::HttpJson));
124+
125+
let yaml_unknown = r#"
126+
protocol: "http/unknown"
127+
"#;
128+
let config: Result<OtlpConfig, _> = from_str(yaml_unknown);
129+
assert!(config.is_err());
130+
131+
}
132+
133+
}

opentelemetry-declarative-config/src/logs/exporters/stdout_batch_exporter.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ impl LogsBatchExporterFactory for StdoutLogsBatchExporterFactory {
3333
Ok(exporter)
3434
}
3535
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn test_default_stdout_logs_batch_exporter_factory() {
43+
let factory = StdoutLogsBatchExporterFactory::default();
44+
assert!(factory.create_logs_batch_exporter(&Value::Null).is_ok());
45+
}
46+
}

opentelemetry-declarative-config/src/metrics.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,25 @@ impl PullExporterFactory {
7979
}
8080
}
8181
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
#[test]
87+
fn test_periodic_exporter_factory_from_name() {
88+
let stdout_factory = PeriodicExporterFactory::from_name("stdout").unwrap();
89+
let otlp_factory = PeriodicExporterFactory::from_name("otlp").unwrap();
90+
let unknown_factory = PeriodicExporterFactory::from_name("unknown");
91+
assert!(matches!(stdout_factory, PeriodicExporterFactory::Stdout(_)));
92+
assert!(matches!(otlp_factory, PeriodicExporterFactory::Otlp(_)));
93+
assert!(unknown_factory.is_err());
94+
}
95+
96+
#[test]
97+
fn test_pull_exporter_factory_from_name() {
98+
let prometheus_factory = PullExporterFactory::from_name("prometheus").unwrap();
99+
let unknown_factory = PullExporterFactory::from_name("unknown");
100+
assert!(matches!(prometheus_factory, PullExporterFactory::Prometheus(_)));
101+
assert!(unknown_factory.is_err());
102+
}
103+
}

opentelemetry-declarative-config/src/metrics/exporters.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,46 @@ where
2525
None => Ok(None),
2626
}
2727
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[derive(Debug, Deserialize)]
34+
struct MockTemporalityHolder {
35+
#[serde(default, deserialize_with = "deserialize_temporality")]
36+
pub temporality: Option<opentelemetry_sdk::metrics::Temporality>,
37+
}
38+
39+
#[test]
40+
fn test_deserialize_cumulative_temporality() {
41+
let yaml_cumulative = r#"
42+
temporality: cumulative
43+
"#;
44+
let temporality_holder: MockTemporalityHolder = serde_yaml::from_str(yaml_cumulative).unwrap();
45+
assert_eq!(temporality_holder.temporality, Some(opentelemetry_sdk::metrics::Temporality::Cumulative));
46+
}
47+
48+
#[test]
49+
fn test_deserialize_delta_temporality() {
50+
let yaml_cumulative = r#"
51+
temporality: delta
52+
"#;
53+
let temporality_holder: MockTemporalityHolder = serde_yaml::from_str(yaml_cumulative).unwrap();
54+
assert_eq!(temporality_holder.temporality, Some(opentelemetry_sdk::metrics::Temporality::Delta));
55+
}
56+
57+
#[test]
58+
fn test_deserialize_unknown_temporality() {
59+
let yaml_cumulative = r#"
60+
temporality: unknown
61+
"#;
62+
let temporality_holder_result = serde_yaml::from_str::<MockTemporalityHolder>(yaml_cumulative);
63+
if let Err(e) = temporality_holder_result {
64+
let err_msg = e.to_string();
65+
assert!(err_msg.contains("Invalid temporality"));
66+
} else {
67+
panic!("Expected error for unknown temporality");
68+
}
69+
}
70+
}

opentelemetry-declarative-config/src/metrics/exporters/otlp_periodic_exporter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ where
9797
mod tests {
9898
use super::*;
9999

100+
#[tokio::test]
101+
async fn test_default_otlp_metrics_periodic_exporter_factory() {
102+
let factory = OtlpMetricsPeriodicExporterFactory::default();
103+
assert!(factory.create_metrics_periodic_exporter(&Value::Null).is_ok());
104+
}
105+
100106
#[test]
101107
fn test_deserialize_config_all_fields_in() {
102108
let yaml_grpc = r#"
@@ -130,7 +136,7 @@ mod tests {
130136
unknown: field
131137
"#;
132138

133-
let config_result: Result<OtlpConfig, _> = serde_yaml::from_str(yaml_grpc);
139+
let config_result = serde_yaml::from_str::<OtlpConfig>(yaml_grpc);
134140
assert!(config_result.is_err());
135141
if let Err(e) = config_result {
136142
let err_msg = e.to_string();

0 commit comments

Comments
 (0)