-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws_logs.go
105 lines (89 loc) · 2.92 KB
/
aws_logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package collector
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
"github.com/kpettijohn/cwlogstream_exporter/types"
)
// AWSLogsGatherer interface implments methods to gather data on a AWS CloudWatch log group and its log streams
type AWSLogsGatherer interface {
GetLogGroups(context.Context) ([]*types.AWSLogGroup, error)
GetLogStreams(ctx context.Context, group *types.AWSLogGroup) (*types.AWSLogGroupStreams, error)
}
// AWSLogsClient contains an AWS CloudWatch Logs cloudwatchlogsiface client
type AWSLogsClient struct {
client cloudwatchlogsiface.CloudWatchLogsAPI
logGroupNamePrefix *string
logHistory int64
}
// NewAWSLogsClient creates a new AWS CloudWatch Logs API client
func NewAWSLogsClient(awsRegion string, logGroupNamePrefix *string) (*AWSLogsClient, error) {
// Create AWS session
s, err := session.NewSession(&aws.Config{Region: aws.String(awsRegion)})
if err != nil {
return nil, fmt.Errorf("error creating aws session")
}
return &AWSLogsClient{
client: cloudwatchlogs.New(s),
logGroupNamePrefix: logGroupNamePrefix,
}, nil
}
// GetLogGroups returns all log groups under a perfix
func (c *AWSLogsClient) GetLogGroups(ctx context.Context) ([]*types.AWSLogGroup, error) {
params := &cloudwatchlogs.DescribeLogGroupsInput{}
if len(*c.logGroupNamePrefix) > 0 {
params.LogGroupNamePrefix = c.logGroupNamePrefix
}
// Get log groups
resp, err := c.client.DescribeLogGroupsWithContext(ctx, params)
if err != nil {
return nil, err
}
lg := []*types.AWSLogGroup{}
for _, l := range resp.LogGroups {
respLogGroup := &types.AWSLogGroup{
ID: aws.StringValue(l.Arn),
Name: aws.StringValue(l.LogGroupName),
}
lg = append(lg, respLogGroup)
}
return lg, nil
}
// GetLogStreams will return all log streams for a group
func (c *AWSLogsClient) GetLogStreams(ctx context.Context, group *types.AWSLogGroup) (*types.AWSLogGroupStreams, error) {
var err error
logStreams := &types.AWSLogGroupStreams{
Group: group,
}
desc := true
orderBy := "LastEventTime"
limit := int64(50)
params := &cloudwatchlogs.DescribeLogStreamsInput{
Descending: &desc,
LogGroupName: &group.Name,
OrderBy: &orderBy,
Limit: &limit,
}
/* Only iterate over 3 pages (150 log streams). Streams are sorted by last event time,
so unless you are running more than 150 instances it should include all of the
active log streams. */
count := 0
err = c.client.DescribeLogStreamsPagesWithContext(ctx, params,
func(page *cloudwatchlogs.DescribeLogStreamsOutput, lastPage bool) bool {
for _, s := range page.LogStreams {
logStreams.Streams = append(logStreams.Streams, s)
}
count++
if count > 3 {
return false
}
return true
})
if err != nil {
return nil, err
}
return logStreams, nil
}