1- //! This module handles trace summary generation
2- //! It reads trace completion messages from RabbitMQ and generates summaries via internal API
3-
41use std:: env;
52use std:: sync:: Arc ;
63
74use backoff:: ExponentialBackoffBuilder ;
85use serde:: { Deserialize , Serialize } ;
96use uuid:: Uuid ;
107
11- use super :: { TRACE_SUMMARY_EXCHANGE , TRACE_SUMMARY_QUEUE , TRACE_SUMMARY_ROUTING_KEY } ;
12- use crate :: mq:: {
13- MessageQueue , MessageQueueAcker , MessageQueueDeliveryTrait , MessageQueueReceiverTrait ,
14- MessageQueueTrait ,
8+ use super :: {
9+ TRACE_SUMMARY_EXCHANGE , TRACE_SUMMARY_QUEUE , TRACE_SUMMARY_ROUTING_KEY ,
10+ eligibility:: check_trace_eligibility,
11+ } ;
12+ use crate :: {
13+ cache:: Cache ,
14+ db:: DB ,
15+ mq:: {
16+ MessageQueue , MessageQueueAcker , MessageQueueDeliveryTrait , MessageQueueReceiverTrait ,
17+ MessageQueueTrait ,
18+ } ,
1519} ;
1620
1721#[ derive( Serialize , Deserialize , Debug , Clone ) ]
@@ -51,14 +55,14 @@ pub async fn push_to_trace_summary_queue(
5155}
5256
5357/// Main worker function to process trace summary messages
54- pub async fn process_trace_summaries ( queue : Arc < MessageQueue > ) {
58+ pub async fn process_trace_summaries ( db : Arc < DB > , cache : Arc < Cache > , queue : Arc < MessageQueue > ) {
5559 loop {
56- inner_process_trace_summaries ( queue. clone ( ) ) . await ;
60+ inner_process_trace_summaries ( db . clone ( ) , cache . clone ( ) , queue. clone ( ) ) . await ;
5761 log:: warn!( "Trace summary listener exited. Rebinding queue connection..." ) ;
5862 }
5963}
6064
61- async fn inner_process_trace_summaries ( queue : Arc < MessageQueue > ) {
65+ async fn inner_process_trace_summaries ( db : Arc < DB > , cache : Arc < Cache > , queue : Arc < MessageQueue > ) {
6266 // Add retry logic with exponential backoff for connection failures
6367 let get_receiver = || async {
6468 queue
@@ -119,7 +123,15 @@ async fn inner_process_trace_summaries(queue: Arc<MessageQueue>) {
119123 } ;
120124
121125 // Process the trace summary generation
122- if let Err ( e) = process_single_trace_summary ( & client, trace_summary_message, acker) . await {
126+ if let Err ( e) = process_single_trace_summary (
127+ & client,
128+ db. clone ( ) ,
129+ cache. clone ( ) ,
130+ trace_summary_message,
131+ acker,
132+ )
133+ . await
134+ {
123135 log:: error!( "Failed to process trace summary: {:?}" , e) ;
124136 }
125137 }
@@ -129,43 +141,70 @@ async fn inner_process_trace_summaries(queue: Arc<MessageQueue>) {
129141
130142async fn process_single_trace_summary (
131143 client : & reqwest:: Client ,
144+ db : Arc < DB > ,
145+ cache : Arc < Cache > ,
132146 message : TraceSummaryMessage ,
133147 acker : MessageQueueAcker ,
134148) -> anyhow:: Result < ( ) > {
135- // Get the internal API base URL - this should be the internal service URL
136- let internal_api_base_url =
137- env:: var ( "NEXT_BACKEND_URL" ) . unwrap_or_else ( |_| "http://localhost:3000" . to_string ( ) ) ;
149+ let eligibility_result = check_trace_eligibility ( db, cache, message. project_id ) . await ?;
150+
151+ if !eligibility_result. is_eligible {
152+ log:: info!(
153+ "Skipping trace summary generation: trace_id={}, project_id={}, reason={}" ,
154+ message. trace_id,
155+ message. project_id,
156+ eligibility_result. reason. unwrap_or_default( )
157+ ) ;
158+ if let Err ( e) = acker. ack ( ) . await {
159+ log:: error!( "Failed to ack trace summary message: {:?}" , e) ;
160+ }
161+ return Ok ( ( ) ) ;
162+ }
163+
164+ let summarizer_service_url = env:: var ( "TRACE_SUMMARIZER_URL" )
165+ . map_err ( |_| anyhow:: anyhow!( "TRACE_SUMMARIZER_URL environment variable not set" ) ) ?;
138166
139- let url = format ! ( "{}/api/traces/summary" , internal_api_base_url) ;
167+ let auth_token = env:: var ( "TRACE_SUMMARIZER_SECRET_KEY" )
168+ . map_err ( |_| anyhow:: anyhow!( "TRACE_SUMMARIZER_SECRET_KEY environment variable not set" ) ) ?;
140169
141170 let request_body = serde_json:: json!( {
142171 "projectId" : message. project_id. to_string( ) ,
143172 "traceId" : message. trace_id. to_string( ) ,
173+ "maxRetries" : 5
144174 } ) ;
145175
146- let call_internal_api = || async {
176+ let call_summarizer_service = || async {
147177 let response = client
148- . post ( & url)
178+ . post ( & summarizer_service_url)
179+ . header ( "Authorization" , format ! ( "Bearer {}" , auth_token) )
180+ . header ( "Content-Type" , "application/json" )
149181 . json ( & request_body)
150182 . send ( )
151183 . await
152184 . map_err ( |e| {
153- log:: warn!( "Failed to call internal API for trace summary: {:?}" , e) ;
185+ log:: warn!( "Failed to call summarizer service for trace summary: {:?}" , e) ;
154186 backoff:: Error :: transient ( anyhow:: Error :: from ( e) )
155187 } ) ?;
156188
157189 if response. status ( ) . is_success ( ) {
158- Ok ( response)
190+ let response_text = response. text ( ) . await . unwrap_or_default ( ) ;
191+ log:: debug!(
192+ "Summarizer service response for trace_id={}, project_id={}: {}" ,
193+ message. trace_id,
194+ message. project_id,
195+ response_text
196+ ) ;
197+ Ok ( ( ) )
159198 } else {
160199 let status = response. status ( ) ;
161200 let response_text = response. text ( ) . await . unwrap_or_default ( ) ;
162201 log:: warn!(
163- "Internal API returned error status for trace summary: {}, Response: {}" ,
202+ "Summarizer service returned error status for trace summary: {}, Response: {}" ,
164203 status,
165204 response_text
166205 ) ;
167206 Err ( backoff:: Error :: transient ( anyhow:: anyhow!(
168- "Internal API error: {}, Response: {}" ,
207+ "Summarizer service error: {}, Response: {}" ,
169208 status,
170209 response_text
171210 ) ) )
@@ -178,8 +217,13 @@ async fn process_single_trace_summary(
178217 . with_max_elapsed_time ( Some ( std:: time:: Duration :: from_secs ( 60 * 5 ) ) ) // 5 minutes max
179218 . build ( ) ;
180219
181- match backoff:: future:: retry ( backoff, call_internal_api) . await {
182- Ok ( _response) => {
220+ match backoff:: future:: retry ( backoff, call_summarizer_service) . await {
221+ Ok ( _) => {
222+ log:: info!(
223+ "Successfully generated trace summary: trace_id={}, project_id={}" ,
224+ message. trace_id,
225+ message. project_id
226+ ) ;
183227 if let Err ( e) = acker. ack ( ) . await {
184228 log:: error!( "Failed to ack trace summary message: {:?}" , e) ;
185229 }
0 commit comments