Exemplo n.º 1
1
func getLog(cw *cloudwatchlogs.CloudWatchLogs, name string) (string, error) {
	groupPrefix := aws.String("/aws/lambda/" + name)
	groups, err := cw.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{LogGroupNamePrefix: groupPrefix})
	if err != nil {
		return "", err
	}

	if len(groups.LogGroups) < 1 {
		return "", errors.New(fmt.Sprintf("No log group found for %s", name))
	}

	group := groups.LogGroups[0]
	// We don't handle the case where lambda functions may share prefixes but we get the list of groups back in non-lexicographic order. Reminder in case that ever happens.
	if *group.LogGroupName != *groupPrefix {
		log.Fatal("Got group matching prefix but not actual", groupPrefix, group.LogGroupName)
	}

	streams, err := cw.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
		LogGroupName: group.LogGroupName,
		Descending:   aws.Bool(true),
		OrderBy:      aws.String("LastEventTime"),
	})

	if err != nil {
		return "", err
	}

	if len(streams.LogStreams) < 1 {
		return "", errors.New(fmt.Sprintf("No log streams found for %s", name))
	}

	stream := streams.LogStreams[0]

	get_log_input := &cloudwatchlogs.GetLogEventsInput{
		LogStreamName: stream.LogStreamName,
		LogGroupName:  group.LogGroupName,
		StartFromHead: aws.Bool(true),
		// allow 3 minute local vs server time out of sync
		StartTime: aws.Int64(time.Now().Add(-3*time.Minute).Unix() * 1000),
	}

	events, err := cw.GetLogEvents(get_log_input)
	if err != nil {
		return "", err
	}

	var output bytes.Buffer
	for _, event := range events.Events {
		output.WriteString(*event.Message)
	}

	return output.String(), nil
}
Exemplo n.º 2
0
// Helper to get all the log messages from a stream. Takes the following args.
//  * The CloudWatch Logs connection
//  * The name of the Group
//  * The name of the stream
//  * The unix beginning time
//  * The unix finished time
//  * The color of the string to be printed
func log(svc *cloudwatchlogs.CloudWatchLogs, g string, s string, b int64, f int64, c string) (Rows, error) {
	var rows Rows

	params := &cloudwatchlogs.GetLogEventsInput{
		LogGroupName:  aws.String(g),
		LogStreamName: aws.String(s),
		StartTime:     aws.Int64(b),
		EndTime:       aws.Int64(f),
	}
	resp, err := svc.GetLogEvents(params)
	if err != nil {
		return rows, err
	}

	for _, e := range resp.Events {
		r := Row{
			Time:    *e.Timestamp,
			Color:   c,
			Message: *e.Message,
		}
		rows = append(rows, &r)
	}

	return rows, nil
}
Exemplo n.º 3
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)
	}
}