Skip to content

Commit dfe419b

Browse files
refactor(stats): take stats_url as a StatsFlusher::new parameter (#1210)
## Summary of changes Refactor `StatsFlusher::new` to accept a `stats_url: String` parameter instead of deriving the URL internally from `config.site`. ## Reason for change 1. **Removes internal duplication** — `trace_stats_url(&self.config.site)` was being called in two separate places inside `StatsFlusher` (in `get_or_init` and again in the retry loop). It's now computed once at the call site. 2. **Enables future flexibility** — callers can supply any URL (e.g. a custom endpoint or test harness) without `StatsFlusher` needing to know about it. 3. **Better separation of concerns** — `StatsFlusher` no longer owns URL resolution; it simply uses the URL it's given. This mirrors how `TraceFlusher` already works, where the URL comes from outside via `SendData`'s embedded `Endpoint`. ## Implementation details - Added a `stats_url: String` field to `StatsFlusher` and a corresponding parameter to `StatsFlusher::new`. - Removed the `trace_stats_url` import from `stats_flusher.rs`; the call now lives in `main.rs::start_trace_agent`. - Replaced the two internal `trace_stats_url(&self.config.site)` calls with `self.stats_url`. - Retry-loop debug logs now report `endpoint.url` so they reflect the URL actually contacted instead of a re-derived site URL. ## Test coverage No new tests — this is a pure refactor of an internal constructor signature with no behavior change. Verified existing checks pass: - [x] `cargo build` on the bottlecap crate - [x] `cargo check --workspace` — clean - [x] `RUSTFLAGS="-D warnings" cargo clippy --workspace --all-targets --features default` — clean - [x] `cargo fmt --all -- --check` — clean - [x] `cargo nextest run --workspace` — 526/526 passed Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5586013 commit dfe419b

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

bottlecap/src/bin/bottlecap/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ fn start_trace_agent(
11311131
stats_aggregator.clone(),
11321132
Arc::clone(config),
11331133
trace_http_client.clone(),
1134+
libdd_trace_utils::config_utils::trace_stats_url(&config.site),
11341135
));
11351136

11361137
let stats_processor = Arc::new(stats_processor::ServerlessStatsProcessor {});

bottlecap/src/traces/stats_flusher.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ use crate::traces::stats_aggregator::StatsAggregator;
1414
use dogstatsd::api_key::ApiKeyFactory;
1515
use libdd_common::Endpoint;
1616
use libdd_trace_protobuf::pb;
17-
use libdd_trace_utils::{config_utils::trace_stats_url, stats_utils};
17+
use libdd_trace_utils::stats_utils;
1818
use tracing::{debug, error};
1919

2020
pub struct StatsFlusher {
2121
aggregator: Arc<Mutex<StatsAggregator>>,
2222
config: Arc<config::Config>,
2323
api_key_factory: Arc<ApiKeyFactory>,
24+
stats_url: String,
2425
endpoint: OnceCell<Endpoint>,
2526
http_client: HttpClient,
2627
}
@@ -32,11 +33,13 @@ impl StatsFlusher {
3233
aggregator: Arc<Mutex<StatsAggregator>>,
3334
config: Arc<config::Config>,
3435
http_client: HttpClient,
36+
stats_url: String,
3537
) -> Self {
3638
StatsFlusher {
3739
aggregator,
3840
config,
3941
api_key_factory,
42+
stats_url,
4043
endpoint: OnceCell::new(),
4144
http_client,
4245
}
@@ -64,9 +67,8 @@ impl StatsFlusher {
6467
.endpoint
6568
.get_or_init({
6669
move || async move {
67-
let stats_url = trace_stats_url(&self.config.site);
6870
Endpoint {
69-
url: hyper::Uri::from_str(&stats_url)
71+
url: hyper::Uri::from_str(&self.stats_url)
7072
.expect("can't make URI from stats url, exiting"),
7173
api_key: Some(api_key_clone.into()),
7274
timeout_ms: self.config.flush_timeout * S_TO_MS,
@@ -92,8 +94,6 @@ impl StatsFlusher {
9294
}
9395
};
9496

95-
let stats_url = trace_stats_url(&self.config.site);
96-
9797
for attempt in 1..=FLUSH_RETRY_COUNT {
9898
let start = std::time::Instant::now();
9999
let resp = stats_utils::send_stats_payload_with_client(
@@ -108,14 +108,16 @@ impl StatsFlusher {
108108
match resp {
109109
Ok(()) => {
110110
debug!(
111-
"STATS | Successfully flushed stats to {stats_url} in {} ms (attempt {attempt}/{FLUSH_RETRY_COUNT})",
111+
"STATS | Successfully flushed stats to {} in {} ms (attempt {attempt}/{FLUSH_RETRY_COUNT})",
112+
endpoint.url,
112113
elapsed.as_millis()
113114
);
114115
return None;
115116
}
116117
Err(e) => {
117118
debug!(
118-
"STATS | Failed to send stats to {stats_url} in {} ms (attempt {attempt}/{FLUSH_RETRY_COUNT}): {e:?}",
119+
"STATS | Failed to send stats to {} in {} ms (attempt {attempt}/{FLUSH_RETRY_COUNT}): {e:?}",
120+
endpoint.url,
119121
elapsed.as_millis()
120122
);
121123
}

0 commit comments

Comments
 (0)