Skip to content

Commit 43763df

Browse files
author
Ganesh Jangir
authored
feat; add client_computed_stats to TraceExporterConfig and expose API to set it (#1052)
1 parent 6c7ce49 commit 43763df

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

data-pipeline-ffi/src/trace_exporter.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub struct TraceExporterConfig {
6565
input_format: TraceExporterInputFormat,
6666
output_format: TraceExporterOutputFormat,
6767
compute_stats: bool,
68+
client_computed_stats: bool,
6869
telemetry_cfg: Option<TelemetryConfig>,
6970
test_session_token: Option<String>,
7071
}
@@ -275,6 +276,31 @@ pub unsafe extern "C" fn ddog_trace_exporter_config_set_compute_stats(
275276
}
276277
}
277278

279+
/// Sets `Datadog-Client-Computed-Stats` header to `true`.
280+
/// This indicates that the upstream system has already computed the stats,
281+
/// and no further stats computation should be performed.
282+
///
283+
/// <div class="warning">
284+
/// This method must not be used when `compute_stats` is enabled, as it could
285+
/// result in duplicate stats computation.
286+
/// </div>
287+
///
288+
/// A common use case is in Application Security Monitoring (ASM) scenarios:
289+
/// when APM is disabled but ASM is enabled, setting this header to `true`
290+
/// ensures that no stats are computed at any level (exporter or agent).
291+
#[no_mangle]
292+
pub unsafe extern "C" fn ddog_trace_exporter_config_set_client_computed_stats(
293+
config: Option<&mut TraceExporterConfig>,
294+
client_computed_stats: bool,
295+
) -> Option<Box<ExporterError>> {
296+
if let Option::Some(config) = config {
297+
config.client_computed_stats = client_computed_stats;
298+
None
299+
} else {
300+
gen_error!(ErrorCode::InvalidArgument)
301+
}
302+
}
303+
278304
/// Sets the `X-Datadog-Test-Session-Token` header. Only used for testing with the test agent.
279305
#[no_mangle]
280306
pub unsafe extern "C" fn ddog_trace_exporter_config_set_test_session_token(
@@ -325,6 +351,8 @@ pub unsafe extern "C" fn ddog_trace_exporter_new(
325351
.set_output_format(config.output_format);
326352
if config.compute_stats {
327353
builder.enable_stats(Duration::from_secs(10));
354+
} else if config.client_computed_stats {
355+
builder.set_client_computed_stats();
328356
}
329357

330358
if let Some(cfg) = &config.telemetry_cfg {
@@ -610,6 +638,24 @@ mod tests {
610638
}
611639
}
612640

641+
#[test]
642+
fn config_client_computed_stats_test() {
643+
unsafe {
644+
let error = ddog_trace_exporter_config_set_client_computed_stats(None, true);
645+
assert_eq!(error.as_ref().unwrap().code, ErrorCode::InvalidArgument);
646+
647+
ddog_trace_exporter_error_free(error);
648+
649+
let mut config = Some(TraceExporterConfig::default());
650+
let error = ddog_trace_exporter_config_set_client_computed_stats(config.as_mut(), true);
651+
652+
assert_eq!(error, None);
653+
654+
let cfg = config.unwrap();
655+
assert!(cfg.client_computed_stats);
656+
}
657+
}
658+
613659
#[test]
614660
fn config_telemetry_test() {
615661
unsafe {
@@ -781,6 +827,7 @@ mod tests {
781827
compute_stats: false,
782828
telemetry_cfg: None,
783829
test_session_token: None,
830+
client_computed_stats: false,
784831
};
785832

786833
let mut ptr: MaybeUninit<Box<TraceExporter>> = MaybeUninit::uninit();
@@ -851,6 +898,7 @@ mod tests {
851898
compute_stats: false,
852899
telemetry_cfg: None,
853900
test_session_token: None,
901+
client_computed_stats: false,
854902
};
855903

856904
let mut ptr: MaybeUninit<Box<TraceExporter>> = MaybeUninit::uninit();
@@ -927,6 +975,7 @@ mod tests {
927975
debug_enabled: true,
928976
}),
929977
test_session_token: None,
978+
client_computed_stats: false,
930979
};
931980

932981
let mut ptr: MaybeUninit<Box<TraceExporter>> = MaybeUninit::uninit();

0 commit comments

Comments
 (0)