Example #1
0
func findLogStreamsSince(logService *cloudwatchlogs.CloudWatchLogs, startTime time.Time, endTime time.Time, output chan<- stream) {
	var (
		startTimestamp = startTime.UnixNano() / 1000000
		endTimestamp   = endTime.UnixNano() / 1000000
	)

	go func() {
		defer close(output)

		var nextToken *string

		for {
			logStreams, err := logService.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
				Descending:   pointer.Bool(true),
				LogGroupName: pointer.String(logGroupName),
				NextToken:    nextToken,
				OrderBy:      pointer.String("LastEventTime"),
			})
			if err != nil {
				panic(err)
			}

			for _, logStream := range logStreams.LogStreams {
				fmt.Fprintf(os.Stderr, "%s...%s %s\n", time.Unix(0, *logStream.FirstEventTimestamp*1000000), time.Unix(0, *logStream.LastEventTimestamp*1000000), *logStream.LogStreamName)

				if endTimestamp > 0 && *logStream.FirstEventTimestamp > endTimestamp {
					continue
				}

				if *logStream.LastEventTimestamp < startTimestamp {
					return
				}

				output <- stream{
					name: *logStream.LogStreamName,
				}
			}

			nextToken = logStreams.NextToken
		}
	}()

	return
}
Example #2
0
func load(logService *cloudwatchlogs.CloudWatchLogs, initial chan<- string, follow chan<- string, limit int, startTime time.Time, endTime time.Time, instanceId string, terminated bool) {
	initialParams := &cloudwatchlogs.GetLogEventsInput{
		LogGroupName:  pointer.String(logGroupName),
		LogStreamName: &instanceId,
	}

	if endTime.IsZero() {
		if !startTime.IsZero() {
			initialParams.StartFromHead = pointer.Bool(true)
			initialParams.StartTime = pointer.Int64(startTime.UnixNano() / int64(time.Millisecond))
		} else {
			initialParams.EndTime = pointer.Int64(time.Now().UnixNano() / int64(time.Millisecond))
		}

		initialParams.Limit = pointer.Int64(int64(limit))
	} else {
		initialParams.StartFromHead = pointer.Bool(true)
		initialParams.StartTime = pointer.Int64(startTime.UnixNano() / int64(time.Millisecond))
		initialParams.EndTime = pointer.Int64(endTime.UnixNano() / int64(time.Millisecond))
	}

	logEvents, err := logService.GetLogEvents(initialParams)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", instanceId, err)
		initial <- ""
		return
	}

	for _, e := range logEvents.Events {
		if *e.Message != "" {
			initial <- formatMessage(e)
		}
	}

	initial <- ""

	if follow == nil || terminated {
		return
	}

	token := logEvents.NextForwardToken

	for {
		logEvents, err := logService.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
			LogGroupName:  pointer.String(logGroupName),
			LogStreamName: &instanceId,
			NextToken:     token,
			StartFromHead: pointer.Bool(true),
		})
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", instanceId, err)
			continue
		}

		for _, e := range logEvents.Events {
			follow <- formatMessage(e)
		}

		token = logEvents.NextForwardToken

		time.Sleep(pollInterval)
	}
}