Skip to content

Commit

Permalink
Prevent invalid modes combination in the builder in order to avoid pa…
Browse files Browse the repository at this point in the history
…nicking.
  • Loading branch information
hoolioh committed Feb 28, 2025
1 parent e40eb0d commit a5dad3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion data-pipeline/examples/send-traces-with-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
.set_language("rust")
.set_language_version(env!("CARGO_PKG_RUST_VERSION"))
.set_input_format(TraceExporterInputFormat::V04)
.set_output_format(TraceExporterOutputFormat::V07)
.set_output_format(TraceExporterOutputFormat::V04)
.enable_stats(Duration::from_secs(10))
.build()
.unwrap();
Expand Down
5 changes: 5 additions & 0 deletions data-pipeline/src/trace_exporter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum BuilderErrorKind {
InvalidUri(String),
/// Indicates that the telemetry configuration is invalid.
InvalidTelemetryConfig,
/// Indicates any incompatible configuration
InvalidConfiguration(String),
}

impl Display for BuilderErrorKind {
Expand All @@ -41,6 +43,9 @@ impl Display for BuilderErrorKind {
BuilderErrorKind::InvalidTelemetryConfig => {
write!(f, "Invalid telemetry configuration")
}
BuilderErrorKind::InvalidConfiguration(msg) => {
write!(f, "Invalid configuration: {}", msg)
}
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions data-pipeline/src/trace_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ pub enum TraceExporterInputFormat {
}

/// TraceExporterOutputFormat represents the format of the output traces.
/// The output format can be either V0.4 or v0.7, where V0.4 is the default.
/// The output format can be either V0.4 or v0.5, where V0.4 is the default.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub enum TraceExporterOutputFormat {
#[allow(missing_docs)]
#[default]
V04,
V05,
#[allow(missing_docs)]
V07,
}

impl TraceExporterOutputFormat {
Expand All @@ -76,7 +74,6 @@ impl TraceExporterOutputFormat {
match self {
TraceExporterOutputFormat::V04 => "/v0.4/traces",
TraceExporterOutputFormat::V05 => "/v0.5/traces",
TraceExporterOutputFormat::V07 => "/v0.7/traces",
},
)
}
Expand Down Expand Up @@ -932,6 +929,14 @@ impl TraceExporterBuilder {

#[allow(missing_docs)]
pub fn build(self) -> Result<TraceExporter, TraceExporterError> {
if !Self::is_mode_allowed(self.input_format, self.output_format) {
return Err(TraceExporterError::Builder(
BuilderErrorKind::InvalidConfiguration(
"Combination of input and output formats not allowed".to_string(),
),
));
}

let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
Expand Down Expand Up @@ -1022,6 +1027,14 @@ impl TraceExporterBuilder {
query_params: self.query_params,
})
}

fn is_mode_allowed(input: TraceExporterInputFormat, output: TraceExporterOutputFormat) -> bool {
match input {
TraceExporterInputFormat::Proxy => true,
TraceExporterInputFormat::V04 => matches!(output, TraceExporterOutputFormat::V04),
TraceExporterInputFormat::V05 => matches!(output, TraceExporterOutputFormat::V05),
}
}
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -1058,7 +1071,7 @@ mod tests {
.set_language_interpreter_vendor("node")
.set_git_commit_sha("797e9ea")
.set_input_format(TraceExporterInputFormat::Proxy)
.set_output_format(TraceExporterOutputFormat::V07)
.set_output_format(TraceExporterOutputFormat::V04)
.set_client_computed_stats()
.enable_telemetry(Some(TelemetryConfig {
heartbeat: 1000,
Expand All @@ -1072,7 +1085,7 @@ mod tests {
.output_format
.add_path(&exporter.endpoint.url)
.to_string(),
"http://192.168.1.1:8127/v0.7/traces"
"http://192.168.1.1:8127/v0.4/traces"
);
assert_eq!(exporter.input_format, TraceExporterInputFormat::Proxy);
assert_eq!(exporter.metadata.tracer_version, "v0.1");
Expand Down

0 comments on commit a5dad3c

Please sign in to comment.