Example #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
}
Example #2
0
func streams(svc *cloudwatchlogs.CloudWatchLogs, g string) ([]string, error) {
	var streams []string

	params := &cloudwatchlogs.DescribeLogStreamsInput{
		LogGroupName: aws.String(g),
	}
	resp, err := svc.DescribeLogStreams(params)
	if err != nil {
		return streams, err
	}

	for _, l := range resp.LogStreams {
		streams = append(streams, *l.LogStreamName)
	}

	return streams, nil
}
Example #3
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
}
func lookupCloudWatchLogStream(conn *cloudwatchlogs.CloudWatchLogs,
	name string, logStreamName string, nextToken *string) (*cloudwatchlogs.LogStream, bool, error) {
	input := &cloudwatchlogs.DescribeLogStreamsInput{
		LogStreamNamePrefix: aws.String(name),
		LogGroupName:        aws.String(logStreamName),
		NextToken:           nextToken,
	}
	resp, err := conn.DescribeLogStreams(input)
	if err != nil {
		return nil, true, err
	}

	for _, ls := range resp.LogStreams {
		if *ls.LogStreamName == name {
			return ls, true, nil
		}
	}

	if resp.NextToken != nil {
		return lookupCloudWatchLogStream(conn, name, logStreamName, resp.NextToken)
	}

	return nil, false, nil
}