@@ -10,14 +10,11 @@ use std::io;
1010use std:: net:: SocketAddr ;
1111use std:: sync:: Arc ;
1212use std:: time:: Instant ;
13- use tokio:: sync:: {
14- Mutex ,
15- mpsc:: { self , Receiver , Sender } ,
16- } ;
13+ use tokio:: sync:: mpsc:: { self , Receiver , Sender } ;
1714use tracing:: { debug, error} ;
1815
1916use crate :: http_utils:: { self , log_and_create_http_response} ;
20- use crate :: { config, env_verifier, stats_flusher, stats_processor, trace_flusher, trace_processor, proxy_aggregator:: { self , ProxyRequest } } ;
17+ use crate :: { config, env_verifier, stats_flusher, stats_processor, trace_flusher, trace_processor, proxy_aggregator:: ProxyRequest , proxy_flusher } ;
2118use datadog_trace_protobuf:: pb;
2219use datadog_trace_utils:: trace_utils;
2320use datadog_trace_utils:: trace_utils:: SendData ;
@@ -29,6 +26,7 @@ const INFO_ENDPOINT_PATH: &str = "/info";
2926const PROFILING_ENDPOINT_PATH : & str = "/profiling/v1/input" ;
3027const TRACER_PAYLOAD_CHANNEL_BUFFER_SIZE : usize = 10 ;
3128const STATS_PAYLOAD_CHANNEL_BUFFER_SIZE : usize = 10 ;
29+ const PROXY_PAYLOAD_CHANNEL_BUFFER_SIZE : usize = 10 ;
3230
3331pub struct MiniAgent {
3432 pub config : Arc < config:: Config > ,
@@ -37,7 +35,7 @@ pub struct MiniAgent {
3735 pub stats_processor : Arc < dyn stats_processor:: StatsProcessor + Send + Sync > ,
3836 pub stats_flusher : Arc < dyn stats_flusher:: StatsFlusher + Send + Sync > ,
3937 pub env_verifier : Arc < dyn env_verifier:: EnvVerifier + Send + Sync > ,
40- pub proxy_aggregator : Arc < Mutex < proxy_aggregator :: Aggregator > > ,
38+ pub proxy_flusher : Arc < proxy_flusher :: ProxyFlusher > ,
4139}
4240
4341impl MiniAgent {
@@ -89,18 +87,23 @@ impl MiniAgent {
8987 . start_stats_flusher ( stats_config, stats_rx)
9088 . await ;
9189 } ) ;
90+ // channels to send processed profiling requests to our proxy flusher.
91+ let ( proxy_tx, proxy_rx) : (
92+ Sender < ProxyRequest > ,
93+ Receiver < ProxyRequest > ,
94+ ) = mpsc:: channel ( PROXY_PAYLOAD_CHANNEL_BUFFER_SIZE ) ;
9295
9396 // start our proxy flusher for profiling requests
94- let proxy_aggregator_for_flusher = self . proxy_aggregator . clone ( ) ;
97+ let proxy_flusher = self . proxy_flusher . clone ( ) ;
9598 tokio:: spawn ( async move {
96- let mut interval = tokio:: time:: interval ( tokio:: time:: Duration :: from_secs ( 10 ) ) ;
99+ let proxy_flusher = proxy_flusher. clone ( ) ;
100+ proxy_flusher. start_proxy_flusher ( proxy_rx) . await ;
97101 } ) ;
98102
99103 // setup our hyper http server, where the endpoint_handler handles incoming requests
100104 let trace_processor = self . trace_processor . clone ( ) ;
101105 let stats_processor = self . stats_processor . clone ( ) ;
102106 let endpoint_config = self . config . clone ( ) ;
103- let proxy_aggregator = self . proxy_aggregator . clone ( ) ;
104107
105108 let service = service_fn ( move |req| {
106109 let trace_processor = trace_processor. clone ( ) ;
@@ -111,7 +114,8 @@ impl MiniAgent {
111114
112115 let endpoint_config = endpoint_config. clone ( ) ;
113116 let mini_agent_metadata = Arc :: clone ( & mini_agent_metadata) ;
114- let proxy_aggregator = proxy_aggregator. clone ( ) ;
117+
118+ let proxy_tx = proxy_tx. clone ( ) ;
115119
116120 MiniAgent :: trace_endpoint_handler (
117121 endpoint_config. clone ( ) ,
@@ -121,7 +125,7 @@ impl MiniAgent {
121125 stats_processor. clone ( ) ,
122126 stats_tx. clone ( ) ,
123127 Arc :: clone ( & mini_agent_metadata) ,
124- proxy_aggregator . clone ( ) ,
128+ proxy_tx . clone ( ) ,
125129 )
126130 } ) ;
127131
@@ -185,7 +189,7 @@ impl MiniAgent {
185189 stats_processor : Arc < dyn stats_processor:: StatsProcessor + Send + Sync > ,
186190 stats_tx : Sender < pb:: ClientStatsPayload > ,
187191 mini_agent_metadata : Arc < trace_utils:: MiniAgentMetadata > ,
188- proxy_aggregator : Arc < Mutex < proxy_aggregator :: Aggregator > > ,
192+ proxy_tx : Sender < ProxyRequest > ,
189193 ) -> http:: Result < hyper_migration:: HttpResponse > {
190194 match ( req. method ( ) , req. uri ( ) . path ( ) ) {
191195 ( & Method :: PUT | & Method :: POST , TRACE_ENDPOINT_PATH ) => {
@@ -210,7 +214,7 @@ impl MiniAgent {
210214 }
211215 }
212216 ( & Method :: POST , PROFILING_ENDPOINT_PATH ) => {
213- match Self :: profiling_proxy_handler ( config, req, proxy_aggregator ) . await {
217+ match Self :: profiling_proxy_handler ( config, req, proxy_tx ) . await {
214218 Ok ( res) => Ok ( res) ,
215219 Err ( err) => log_and_create_http_response (
216220 & format ! ( "Error processing profiling request: {err}" ) ,
@@ -236,8 +240,8 @@ impl MiniAgent {
236240 async fn profiling_proxy_handler (
237241 config : Arc < config:: Config > ,
238242 request : hyper_migration:: HttpRequest ,
239- proxy_aggregator : Arc < Mutex < proxy_aggregator :: Aggregator > > ,
240- ) -> Result < hyper_migration:: HttpResponse , Box < dyn std :: error :: Error + Send + Sync > > {
243+ proxy_tx : Sender < ProxyRequest > ,
244+ ) -> http :: Result < hyper_migration:: HttpResponse > {
241245 debug ! ( "Trace Agent | Proxied request for profiling" ) ;
242246
243247 // Extract headers and body
@@ -247,25 +251,37 @@ impl MiniAgent {
247251 config. max_request_content_length ,
248252 "Error processing profiling request" ,
249253 ) {
250- return response. map_err ( |e| Box :: new ( e ) as Box < dyn std :: error :: Error + Send + Sync > ) ;
254+ return response;
251255 }
252256
253- let body_bytes = body. collect ( ) . await ?. to_bytes ( ) ;
257+ let body_bytes = match body. collect ( ) . await {
258+ Ok ( collected) => collected. to_bytes ( ) ,
259+ Err ( e) => {
260+ return log_and_create_http_response (
261+ & format ! ( "Error reading profiling request body: {e}" ) ,
262+ StatusCode :: BAD_REQUEST ,
263+ ) ;
264+ }
265+ } ;
254266
255267 // Create proxy request
256268 let proxy_request = ProxyRequest {
257269 headers : parts. headers ,
258270 body : body_bytes,
259- target_url : format ! ( "https://intake.profile.{}/api/v2/profile" , config . dd_site ) ,
271+ target_url : config . proxy_intake . url . to_string ( ) ,
260272 } ;
261273
262- let mut proxy_aggregator = proxy_aggregator. lock ( ) . await ;
263- proxy_aggregator. add ( proxy_request) ;
264-
265- Response :: builder ( )
266- . status ( 200 )
267- . body ( hyper_migration:: Body :: from ( "Acknowledged profiling request" ) )
268- . map_err ( |e| Box :: new ( e) as Box < dyn std:: error:: Error + Send + Sync > )
274+ // Send to channel - flusher will aggregate and send
275+ match proxy_tx. send ( proxy_request) . await {
276+ Ok ( _) => log_and_create_http_response (
277+ "Successfully buffered profiling request to be flushed" ,
278+ StatusCode :: OK ,
279+ ) ,
280+ Err ( err) => log_and_create_http_response (
281+ & format ! ( "Error sending profiling request to the proxy flusher: {err}" ) ,
282+ StatusCode :: INTERNAL_SERVER_ERROR ,
283+ ) ,
284+ }
269285 }
270286
271287 fn info_handler ( dd_dogstatsd_port : u16 ) -> http:: Result < hyper_migration:: HttpResponse > {
0 commit comments