Skip to content

Commit a1f9052

Browse files
authored
CloudWatch Logs: Move query response stats to appropriate FrameMeta property (grafana#26732)
* CloudWatch Logs: Move query response stats to appropriate FrameMeta property
1 parent 96babf1 commit a1f9052

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

pkg/tsdb/cloudwatch/log_actions_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,19 @@ func TestQuery_GetQueryResults(t *testing.T) {
489489
expFrame.Meta = &data.FrameMeta{
490490
Custom: map[string]interface{}{
491491
"Status": "Complete",
492-
"Statistics": cloudwatchlogs.QueryStatistics{
493-
BytesScanned: aws.Float64(512),
494-
RecordsMatched: aws.Float64(256),
495-
RecordsScanned: aws.Float64(1024),
492+
},
493+
Stats: []data.QueryStat{
494+
{
495+
FieldConfig: data.FieldConfig{DisplayName: "Bytes scanned"},
496+
Value: 512,
497+
},
498+
{
499+
FieldConfig: data.FieldConfig{DisplayName: "Records scanned"},
500+
Value: 1024,
501+
},
502+
{
503+
FieldConfig: data.FieldConfig{DisplayName: "Records matched"},
504+
Value: 256,
496505
},
497506
},
498507
PreferredVisualization: "logs",

pkg/tsdb/cloudwatch/log_query.go

+40-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
)
1313

1414
func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*data.Frame, error) {
15+
if response == nil {
16+
return nil, fmt.Errorf("response is nil, cannot convert log results to data frames")
17+
}
18+
1519
nonEmptyRows := make([][]*cloudwatchlogs.ResultField, 0)
1620
// Sometimes CloudWatch can send empty rows
1721
for _, row := range response.Results {
@@ -94,12 +98,44 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d
9498
}
9599
}
96100

101+
queryStats := make([]data.QueryStat, 0)
102+
if response.Statistics != nil {
103+
if response.Statistics.BytesScanned != nil {
104+
queryStats = append(queryStats, data.QueryStat{
105+
FieldConfig: data.FieldConfig{DisplayName: "Bytes scanned"},
106+
Value: *response.Statistics.BytesScanned,
107+
})
108+
}
109+
110+
if response.Statistics.RecordsScanned != nil {
111+
queryStats = append(queryStats, data.QueryStat{
112+
FieldConfig: data.FieldConfig{DisplayName: "Records scanned"},
113+
Value: *response.Statistics.RecordsScanned,
114+
})
115+
}
116+
117+
if response.Statistics.RecordsMatched != nil {
118+
queryStats = append(queryStats, data.QueryStat{
119+
FieldConfig: data.FieldConfig{DisplayName: "Records matched"},
120+
Value: *response.Statistics.RecordsMatched,
121+
})
122+
}
123+
}
124+
97125
frame := data.NewFrame("CloudWatchLogsResponse", newFields...)
98126
frame.Meta = &data.FrameMeta{
99-
Custom: map[string]interface{}{
100-
"Status": *response.Status,
101-
"Statistics": *response.Statistics,
102-
},
127+
Stats: nil,
128+
Custom: nil,
129+
}
130+
131+
if len(queryStats) > 0 {
132+
frame.Meta.Stats = queryStats
133+
}
134+
135+
if response.Status != nil {
136+
frame.Meta.Custom = map[string]interface{}{
137+
"Status": *response.Status,
138+
}
103139
}
104140

105141
// Results aren't guaranteed to come ordered by time (ascending), so we need to sort

pkg/tsdb/cloudwatch/log_query_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,19 @@ func TestLogsResultsToDataframes(t *testing.T) {
195195
Meta: &data.FrameMeta{
196196
Custom: map[string]interface{}{
197197
"Status": "ok",
198-
"Statistics": cloudwatchlogs.QueryStatistics{
199-
BytesScanned: aws.Float64(2000),
200-
RecordsMatched: aws.Float64(3),
201-
RecordsScanned: aws.Float64(5000),
198+
},
199+
Stats: []data.QueryStat{
200+
{
201+
FieldConfig: data.FieldConfig{DisplayName: "Bytes scanned"},
202+
Value: 2000,
203+
},
204+
{
205+
FieldConfig: data.FieldConfig{DisplayName: "Records scanned"},
206+
Value: 5000,
207+
},
208+
{
209+
FieldConfig: data.FieldConfig{DisplayName: "Records matched"},
210+
Value: 3,
202211
},
203212
},
204213
},

public/app/plugins/datasource/cloudwatch/datasource.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ export class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery, CloudWa
248248
map(frames => {
249249
let moreRecordsMatched = false;
250250
for (const frame of frames) {
251-
const recordsMatched = frame.meta?.custom?.['Statistics']['RecordsMatched'];
251+
const recordsMatched = frame.meta?.stats?.find(stat => stat.displayName === 'Records matched')
252+
?.value!;
252253
if (recordsMatched > (prevRecordsMatched[frame.refId!] ?? 0)) {
253254
moreRecordsMatched = true;
254255
}

public/app/plugins/datasource/cloudwatch/specs/datasource.test.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ describe('CloudWatchDatasource', () => {
167167
it('should stop querying when no more data retrieved past max attempts', async () => {
168168
const fakeFrames = genMockFrames(10);
169169
for (let i = 7; i < fakeFrames.length; i++) {
170-
fakeFrames[i].meta!.custom!['Statistics']['RecordsMatched'] = fakeFrames[6].meta!.custom!['Statistics'][
171-
'RecordsMatched'
170+
fakeFrames[i].meta!.stats = [
171+
{
172+
displayName: 'Records matched',
173+
value: fakeFrames[6].meta!.stats?.find(stat => stat.displayName === 'Records matched')?.value!,
174+
},
172175
];
173176
}
174177

@@ -193,6 +196,7 @@ describe('CloudWatchDatasource', () => {
193196
...fakeFrames[MAX_ATTEMPTS - 1].meta!.custom,
194197
Status: 'Complete',
195198
},
199+
stats: fakeFrames[MAX_ATTEMPTS - 1].meta!.stats,
196200
},
197201
},
198202
];
@@ -1101,10 +1105,13 @@ function genMockFrames(numResponses: number): DataFrame[] {
11011105
meta: {
11021106
custom: {
11031107
Status: i === numResponses - 1 ? CloudWatchLogsQueryStatus.Complete : CloudWatchLogsQueryStatus.Running,
1104-
Statistics: {
1105-
RecordsMatched: (i + 1) * recordIncrement,
1106-
},
11071108
},
1109+
stats: [
1110+
{
1111+
displayName: 'Records matched',
1112+
value: (i + 1) * recordIncrement,
1113+
},
1114+
],
11081115
},
11091116
length: 0,
11101117
});

0 commit comments

Comments
 (0)