@@ -124,16 +124,15 @@ func TailLogGroup(ctx context.Context, input LogGroupInput) (LiveTailStream, err
124124 return slto .GetStream (), nil
125125}
126126
127- func QueryLogGroups (ctx context.Context , start , end time.Time , limit int , logGroups ... LogGroupInput ) (<- chan LogEvent , <- chan error ) {
128- // Gather logs from the CD task, kaniko, ECS events, and all services
127+ func QueryLogGroups (ctx context.Context , start , end time.Time , limit int32 , logGroups ... LogGroupInput ) (<- chan LogEvent , <- chan error ) {
129128 var evtsChan chan LogEvent
130129 var errChan chan error
131130 for _ , lgi := range logGroups {
132131 lgEvtChan := make (chan LogEvent )
133132 // Start a go routine for each log group
134133 go func (lgi LogGroupInput ) {
135134 defer close (lgEvtChan )
136- if err := QueryLogGroup (ctx , lgi , start , end , func (logEvents []LogEvent ) error {
135+ if err := QueryLogGroup (ctx , lgi , start , end , limit , func (logEvents []LogEvent ) error {
137136 for _ , event := range logEvents {
138137 lgEvtChan <- event
139138 }
@@ -145,22 +144,22 @@ func QueryLogGroups(ctx context.Context, start, end time.Time, limit int, logGro
145144 evtsChan = mergeLogEventChan (evtsChan , lgEvtChan ) // Merge sort the log events based on timestamp
146145 // take the last n events only
147146 if limit > 0 {
148- evtsChan = takeLastN (evtsChan , limit )
147+ evtsChan = takeLastN (evtsChan , int ( limit ) )
149148 }
150149 }
151150 return evtsChan , errChan
152151}
153152
154- func QueryLogGroup (ctx context.Context , input LogGroupInput , start , end time.Time , cb func ([]LogEvent ) error ) error {
153+ func QueryLogGroup (ctx context.Context , input LogGroupInput , start , end time.Time , limit int32 , cb func ([]LogEvent ) error ) error {
155154 region := region .FromArn (input .LogGroupARN )
156155 cw , err := newCloudWatchLogsClient (ctx , region )
157156 if err != nil {
158157 return err
159158 }
160- return filterLogEvents (ctx , cw , input , start , end , cb )
159+ return filterLogEvents (ctx , cw , input , start , end , limit , cb )
161160}
162161
163- func filterLogEvents (ctx context.Context , cw * cloudwatchlogs.Client , lgi LogGroupInput , start , end time.Time , cb func ([]LogEvent ) error ) error {
162+ func filterLogEvents (ctx context.Context , cw * cloudwatchlogs.Client , lgi LogGroupInput , start , end time.Time , limit int32 , cb func ([]LogEvent ) error ) error {
164163 var pattern * string
165164 if lgi .LogEventFilterPattern != "" {
166165 pattern = & lgi .LogEventFilterPattern
@@ -189,6 +188,11 @@ func filterLogEvents(ctx context.Context, cw *cloudwatchlogs.Client, lgi LogGrou
189188 params .LogStreamNamePrefix = & lgi .LogStreamNamePrefix
190189 }
191190 for {
191+ if limit > 0 {
192+ // Specifying the limit parameter only guarantees that a single page doesn't return more log events than the
193+ // specified limit, but it might return fewer events than the limit. This is the expected API behavior.
194+ params .Limit = ptr .Int32 (limit )
195+ }
192196 fleo , err := cw .FilterLogEvents (ctx , params )
193197 if err != nil {
194198 return err
@@ -209,6 +213,10 @@ func filterLogEvents(ctx context.Context, cw *cloudwatchlogs.Client, lgi LogGrou
209213 if fleo .NextToken == nil {
210214 return nil
211215 }
216+ if limit > 0 && len (events ) >= int (limit ) {
217+ return nil
218+ }
219+ limit -= int32 (len (events )) // #nosec G115 - always safe because len(events) <= limit
212220 params .NextToken = fleo .NextToken
213221 }
214222}
0 commit comments