2017-11-20 07:28:49 +01:00
|
|
|
package operations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2017-11-23 06:33:36 +01:00
|
|
|
"sync"
|
2017-11-21 01:37:41 +01:00
|
|
|
"time"
|
2017-11-20 07:28:49 +01:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2017-11-20 22:44:23 +01:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
|
|
|
"github.com/golang/glog"
|
2017-11-20 07:28:49 +01:00
|
|
|
"github.com/pkg/errors"
|
2017-11-20 22:44:23 +01:00
|
|
|
|
2017-11-20 07:28:49 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/tokens"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
|
|
|
)
|
|
|
|
|
2017-11-26 18:57:41 +01:00
|
|
|
// TODO[pulumi/pulumi#54] This should be factored out behind an OperationsProvider RPC interface and versioned with the
|
|
|
|
// `pulumi-aws` repo instead of statically linked into the engine.
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
// AWSOperationsProvider creates an OperationsProvider capable of answering operational queries based on the
|
2017-11-26 18:57:41 +01:00
|
|
|
// underlying resources of the `@pulumi/aws` implementation.
|
2017-11-20 22:31:20 +01:00
|
|
|
func AWSOperationsProvider(
|
2017-11-20 07:28:49 +01:00
|
|
|
config map[tokens.ModuleMember]string,
|
|
|
|
component *Resource) (Provider, error) {
|
|
|
|
|
2017-11-20 22:44:23 +01:00
|
|
|
awsRegion, ok := config[regionKey]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no AWS region found")
|
|
|
|
}
|
|
|
|
|
|
|
|
awsConnection, err := getAWSConnection(awsRegion)
|
2017-11-20 07:28:49 +01:00
|
|
|
if err != nil {
|
2017-11-20 22:44:23 +01:00
|
|
|
return nil, err
|
2017-11-20 07:28:49 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
prov := &awsOpsProvider{
|
2017-11-20 22:44:23 +01:00
|
|
|
awsConnection: awsConnection,
|
2017-11-20 07:28:49 +01:00
|
|
|
component: component,
|
|
|
|
}
|
|
|
|
return prov, nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
type awsOpsProvider struct {
|
2017-11-20 07:28:49 +01:00
|
|
|
awsConnection *awsConnection
|
|
|
|
component *Resource
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
var _ Provider = (*awsOpsProvider)(nil)
|
2017-11-20 07:28:49 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
// AWS config keys
|
|
|
|
regionKey = "aws:config:region"
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
// AWS resource types
|
|
|
|
awsFunctionType = tokens.Type("aws:lambda/function:Function")
|
2017-11-20 23:55:09 +01:00
|
|
|
awsLogGroupType = tokens.Type("aws:cloudwatch/logGroup:LogGroup")
|
2017-11-20 07:28:49 +01:00
|
|
|
)
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
func (ops *awsOpsProvider) GetLogs(query LogQuery) (*[]LogEntry, error) {
|
2017-11-21 01:37:41 +01:00
|
|
|
if query.Query != nil {
|
|
|
|
contract.Failf("not yet implemented - Query")
|
2017-11-20 07:28:49 +01:00
|
|
|
}
|
|
|
|
switch ops.component.state.Type {
|
2017-11-20 22:31:20 +01:00
|
|
|
case awsFunctionType:
|
|
|
|
functionName := ops.component.state.Outputs["name"].StringValue()
|
2017-11-21 01:37:41 +01:00
|
|
|
logResult := ops.awsConnection.getLogsForLogGroupsConcurrently(
|
|
|
|
[]string{functionName},
|
|
|
|
[]string{"/aws/lambda/" + functionName},
|
|
|
|
query.StartTime,
|
|
|
|
query.EndTime,
|
|
|
|
)
|
2017-11-20 07:28:49 +01:00
|
|
|
sort.SliceStable(logResult, func(i, j int) bool { return logResult[i].Timestamp < logResult[j].Timestamp })
|
|
|
|
return &logResult, nil
|
2017-11-20 23:55:09 +01:00
|
|
|
case awsLogGroupType:
|
|
|
|
name := ops.component.state.Outputs["name"].StringValue()
|
2017-11-21 01:37:41 +01:00
|
|
|
logResult := ops.awsConnection.getLogsForLogGroupsConcurrently(
|
|
|
|
[]string{name},
|
|
|
|
[]string{name},
|
|
|
|
query.StartTime,
|
|
|
|
query.EndTime,
|
|
|
|
)
|
2017-11-20 23:55:09 +01:00
|
|
|
sort.SliceStable(logResult, func(i, j int) bool { return logResult[i].Timestamp < logResult[j].Timestamp })
|
|
|
|
return &logResult, nil
|
2017-11-20 07:28:49 +01:00
|
|
|
default:
|
|
|
|
// Else this resource kind does not produce any logs.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
func (ops *awsOpsProvider) ListMetrics() []MetricName {
|
2017-11-20 07:28:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:31:20 +01:00
|
|
|
func (ops *awsOpsProvider) GetMetricStatistics(metric MetricRequest) ([]MetricDataPoint, error) {
|
2017-11-20 07:28:49 +01:00
|
|
|
return nil, fmt.Errorf("Not yet implmeneted: GetMetricStatistics")
|
|
|
|
}
|
2017-11-20 22:44:23 +01:00
|
|
|
|
|
|
|
type awsConnection struct {
|
|
|
|
sess *session.Session
|
|
|
|
logSvc *cloudwatchlogs.CloudWatchLogs
|
|
|
|
metricSvc *cloudwatch.CloudWatch
|
|
|
|
}
|
|
|
|
|
|
|
|
var awsConnectionCache = map[string]*awsConnection{}
|
2017-11-23 06:33:36 +01:00
|
|
|
var awsConnectionCacheMutex = sync.RWMutex{}
|
2017-11-20 22:44:23 +01:00
|
|
|
|
|
|
|
func getAWSConnection(awsRegion string) (*awsConnection, error) {
|
2017-11-23 06:33:36 +01:00
|
|
|
awsConnectionCacheMutex.RLock()
|
2017-11-20 22:44:23 +01:00
|
|
|
connection, ok := awsConnectionCache[awsRegion]
|
2017-11-23 06:33:36 +01:00
|
|
|
awsConnectionCacheMutex.RUnlock()
|
2017-11-20 22:44:23 +01:00
|
|
|
if !ok {
|
|
|
|
awsConfig := aws.NewConfig()
|
|
|
|
awsConfig.Region = aws.String(awsRegion)
|
|
|
|
sess, err := session.NewSession(awsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create AWS session")
|
|
|
|
}
|
|
|
|
connection = &awsConnection{
|
|
|
|
sess: sess,
|
|
|
|
logSvc: cloudwatchlogs.New(sess),
|
|
|
|
metricSvc: cloudwatch.New(sess),
|
|
|
|
}
|
2017-11-23 06:33:36 +01:00
|
|
|
awsConnectionCacheMutex.Lock()
|
2017-11-20 22:44:23 +01:00
|
|
|
awsConnectionCache[awsRegion] = connection
|
2017-11-23 06:33:36 +01:00
|
|
|
awsConnectionCacheMutex.Unlock()
|
2017-11-20 22:44:23 +01:00
|
|
|
}
|
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:08:19 +01:00
|
|
|
func (p *awsConnection) getLogsForLogGroupsConcurrently(
|
|
|
|
names []string,
|
|
|
|
logGroups []string,
|
|
|
|
startTime *time.Time,
|
|
|
|
endTime *time.Time) []LogEntry {
|
2017-11-20 22:44:23 +01:00
|
|
|
|
|
|
|
// Create a channel for collecting log event outputs
|
|
|
|
ch := make(chan []*cloudwatchlogs.FilteredLogEvent)
|
|
|
|
|
2017-11-21 01:37:41 +01:00
|
|
|
var startMilli *int64
|
|
|
|
if startTime != nil {
|
|
|
|
startMilli = aws.Int64(aws.TimeUnixMilli(*startTime))
|
|
|
|
}
|
|
|
|
var endMilli *int64
|
|
|
|
if endTime != nil {
|
|
|
|
endMilli = aws.Int64(aws.TimeUnixMilli(*endTime))
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:44:23 +01:00
|
|
|
// Run FilterLogEvents for each log group in parallel
|
|
|
|
for _, logGroup := range logGroups {
|
|
|
|
go func(logGroup string) {
|
2017-11-21 08:18:47 +01:00
|
|
|
var ret []*cloudwatchlogs.FilteredLogEvent
|
2017-11-21 08:23:11 +01:00
|
|
|
err := p.logSvc.FilterLogEventsPages(&cloudwatchlogs.FilterLogEventsInput{
|
|
|
|
LogGroupName: aws.String(logGroup),
|
|
|
|
StartTime: startMilli,
|
|
|
|
EndTime: endMilli,
|
|
|
|
}, func(resp *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool {
|
2017-11-21 08:18:47 +01:00
|
|
|
ret = append(ret, resp.Events...)
|
2017-11-21 08:23:11 +01:00
|
|
|
if !lastPage {
|
2017-11-21 08:18:47 +01:00
|
|
|
fmt.Printf("Getting more logs for %v...\n", logGroup)
|
|
|
|
}
|
2017-11-21 08:23:11 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
glog.V(5).Infof("[getLogs] Error getting logs: %v %v\n", logGroup, err)
|
2017-11-20 22:44:23 +01:00
|
|
|
}
|
2017-11-21 08:18:47 +01:00
|
|
|
ch <- ret
|
2017-11-20 22:44:23 +01:00
|
|
|
}(logGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect responses on the channel and append logs into combined log array
|
|
|
|
var logs []LogEntry
|
|
|
|
for i := 0; i < len(logGroups); i++ {
|
|
|
|
logEvents := <-ch
|
|
|
|
for _, event := range logEvents {
|
|
|
|
logs = append(logs, LogEntry{
|
|
|
|
ID: names[i],
|
|
|
|
Message: aws.StringValue(event.Message),
|
|
|
|
Timestamp: aws.Int64Value(event.Timestamp),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return logs
|
|
|
|
}
|