Datasource/Cloudwatch: Adds support for Cloudwatch Logs (#23566)
* Datasource/Cloudwatch: Adds support for Cloudwatch Logs * Fix rebase leftover * Use jsurl for AWS url serialization * WIP: Temporary workaround for CLIQ metrics * Only allow up to 20 log groups to be selected * WIP additional changes * More changes based on feedback * More changes based on PR feedback * Fix strict null errors
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
package cloudwatch
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
//***
|
||||
// LogQuery tests
|
||||
//***
|
||||
|
||||
func TestLogsResultsToDataframes(t *testing.T) {
|
||||
fakeCloudwatchResponse := &cloudwatchlogs.GetQueryResultsOutput{
|
||||
Results: [][]*cloudwatchlogs.ResultField{
|
||||
{
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@ptr"),
|
||||
Value: aws.String("fake ptr"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@timestamp"),
|
||||
Value: aws.String("2020-03-02 15:04:05.000"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("line"),
|
||||
Value: aws.String("test message 1"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@logStream"),
|
||||
Value: aws.String("fakelogstream"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@log"),
|
||||
Value: aws.String("fakelog"),
|
||||
},
|
||||
},
|
||||
{
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@ptr"),
|
||||
Value: aws.String("fake ptr"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@timestamp"),
|
||||
Value: aws.String("2020-03-02 16:04:05.000"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("line"),
|
||||
Value: aws.String("test message 2"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@logStream"),
|
||||
Value: aws.String("fakelogstream"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@log"),
|
||||
Value: aws.String("fakelog"),
|
||||
},
|
||||
},
|
||||
{
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@ptr"),
|
||||
Value: aws.String("fake ptr"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@timestamp"),
|
||||
Value: aws.String("2020-03-02 17:04:05.000"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("line"),
|
||||
Value: aws.String("test message 3"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@logStream"),
|
||||
Value: aws.String("fakelogstream"),
|
||||
},
|
||||
&cloudwatchlogs.ResultField{
|
||||
Field: aws.String("@log"),
|
||||
Value: aws.String("fakelog"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: aws.String("ok"),
|
||||
Statistics: &cloudwatchlogs.QueryStatistics{
|
||||
BytesScanned: aws.Float64(2000),
|
||||
RecordsMatched: aws.Float64(3),
|
||||
RecordsScanned: aws.Float64(5000),
|
||||
},
|
||||
}
|
||||
|
||||
dataframes, _ := logsResultsToDataframes(fakeCloudwatchResponse)
|
||||
timeA, _ := time.Parse("2006-01-02 15:04:05.000", "2020-03-02 15:04:05.000")
|
||||
timeB, _ := time.Parse("2006-01-02 15:04:05.000", "2020-03-02 16:04:05.000")
|
||||
timeC, _ := time.Parse("2006-01-02 15:04:05.000", "2020-03-02 17:04:05.000")
|
||||
timeVals := []*time.Time{
|
||||
&timeA, &timeB, &timeC,
|
||||
}
|
||||
timeField := data.NewField("@timestamp", nil, timeVals)
|
||||
timeField.SetConfig(&data.FieldConfig{Title: "Time"})
|
||||
|
||||
lineField := data.NewField("line", nil, []*string{
|
||||
aws.String("test message 1"),
|
||||
aws.String("test message 2"),
|
||||
aws.String("test message 3"),
|
||||
})
|
||||
|
||||
logStreamField := data.NewField("@logStream", nil, []*string{
|
||||
aws.String("fakelogstream"),
|
||||
aws.String("fakelogstream"),
|
||||
aws.String("fakelogstream"),
|
||||
})
|
||||
logStreamField.SetConfig(&data.FieldConfig{
|
||||
Custom: map[string]interface{}{
|
||||
"Hidden": true,
|
||||
},
|
||||
})
|
||||
|
||||
logField := data.NewField("@log", nil, []*string{
|
||||
aws.String("fakelog"),
|
||||
aws.String("fakelog"),
|
||||
aws.String("fakelog"),
|
||||
})
|
||||
logField.SetConfig(&data.FieldConfig{
|
||||
Custom: map[string]interface{}{
|
||||
"Hidden": true,
|
||||
},
|
||||
})
|
||||
|
||||
expectedDataframe := &data.Frame{
|
||||
Name: "CloudWatchLogsResponse",
|
||||
Fields: []*data.Field{
|
||||
timeField,
|
||||
lineField,
|
||||
logStreamField,
|
||||
logField,
|
||||
},
|
||||
RefID: "",
|
||||
Meta: &data.FrameMeta{
|
||||
Custom: map[string]interface{}{
|
||||
"Status": "ok",
|
||||
"Statistics": cloudwatchlogs.QueryStatistics{
|
||||
BytesScanned: aws.Float64(2000),
|
||||
RecordsMatched: aws.Float64(3),
|
||||
RecordsScanned: aws.Float64(5000),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Splitting these assertions up so it's clearer what's wrong should the test
|
||||
// fail in the future
|
||||
assert.Equal(t, expectedDataframe.Name, dataframes.Name)
|
||||
assert.Equal(t, expectedDataframe.RefID, dataframes.RefID)
|
||||
assert.Equal(t, expectedDataframe.Meta, dataframes.Meta)
|
||||
assert.ElementsMatch(t, expectedDataframe.Fields, dataframes.Fields)
|
||||
}
|
||||
Reference in New Issue
Block a user