Skip to content

Commit 93d815d

Browse files
duncanistaCopilot
andauthored
chore(otlp): move agent to axum (#711)
# What? Moves OTLP Agent to Axum – also adds a cancellation token to be used on shutdown # Motivation Leverage Axum handle connection errors --------- Co-authored-by: Copilot <[email protected]>
1 parent 440fb3e commit 93d815d

File tree

7 files changed

+165
-162
lines changed

7 files changed

+165
-162
lines changed

bottlecap/src/bin/bottlecap/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ async fn extension_loop_active(
484484
let telemetry_listener_cancel_token =
485485
setup_telemetry_client(&r.extension_id, logs_agent_channel).await?;
486486

487-
start_otlp_agent(
487+
let otlp_shutdown_token = start_otlp_agent(
488488
config,
489489
tags_provider.clone(),
490490
trace_processor.clone(),
@@ -682,6 +682,9 @@ async fn extension_loop_active(
682682
if let Some(api_runtime_proxy_cancel_token) = api_runtime_proxy_shutdown_signal {
683683
api_runtime_proxy_cancel_token.cancel();
684684
}
685+
if let Some(otlp_shutdown_token) = otlp_shutdown_token {
686+
otlp_shutdown_token.cancel();
687+
}
685688
dogstatsd_cancel_token.cancel();
686689
telemetry_listener_cancel_token.cancel();
687690

@@ -1066,18 +1069,20 @@ fn start_otlp_agent(
10661069
tags_provider: Arc<TagProvider>,
10671070
trace_processor: Arc<dyn trace_processor::TraceProcessor + Send + Sync>,
10681071
trace_tx: Sender<SendData>,
1069-
) {
1072+
) -> Option<CancellationToken> {
10701073
if !should_enable_otlp_agent(config) {
1071-
return;
1074+
return None;
10721075
}
10731076

10741077
let agent = OtlpAgent::new(config.clone(), tags_provider, trace_processor, trace_tx);
1075-
1078+
let shutdown_token = agent.shutdown_token();
10761079
tokio::spawn(async move {
1077-
if let Err(e) = agent.start().await {
1080+
if let Err(e) = agent.start() {
10781081
error!("Error starting OTLP agent: {e:?}");
10791082
}
10801083
});
1084+
1085+
Some(shutdown_token)
10811086
}
10821087

10831088
fn start_api_runtime_proxy(

bottlecap/src/http_client.rs renamed to bottlecap/src/http.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
use crate::config;
2+
use axum::{
3+
extract::{FromRequest, Request},
4+
http::{self, StatusCode},
5+
response::{IntoResponse, Response},
6+
};
7+
use bytes::Bytes;
28
use core::time::Duration;
39
use datadog_fips::reqwest_adapter::create_reqwest_client_builder;
410
use std::error::Error;
@@ -49,3 +55,16 @@ fn build_client(config: Arc<config::Config>) -> Result<reqwest::Client, Box<dyn
4955
Ok(client.build()?)
5056
}
5157
}
58+
59+
pub async fn handler_not_found() -> Response {
60+
(StatusCode::NOT_FOUND, "Not Found").into_response()
61+
}
62+
63+
pub async fn extract_request_body(
64+
request: Request,
65+
) -> Result<(http::request::Parts, Bytes), Box<dyn std::error::Error>> {
66+
let (parts, body) = request.into_parts();
67+
let bytes = Bytes::from_request(Request::from_parts(parts.clone(), body), &()).await?;
68+
69+
Ok((parts, bytes))
70+
}

bottlecap/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod config;
2121
pub mod event_bus;
2222
pub mod events;
2323
pub mod fips;
24-
pub mod http_client;
24+
pub mod http;
2525
pub mod lifecycle;
2626
pub mod logger;
2727
pub mod logs;

bottlecap/src/logs/flusher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::config;
2-
use crate::http_client;
2+
use crate::http::get_client;
33
use crate::logs::aggregator::Aggregator;
44
use crate::FLUSH_RETRY_COUNT;
55
use reqwest::header::HeaderMap;
@@ -35,7 +35,7 @@ impl Flusher {
3535
aggregator: Arc<Mutex<Aggregator>>,
3636
config: Arc<config::Config>,
3737
) -> Self {
38-
let client = http_client::get_client(config.clone());
38+
let client = get_client(config.clone());
3939
let mut headers = HeaderMap::new();
4040
headers.insert(
4141
"DD-API-KEY",

0 commit comments

Comments
 (0)