Azure Monitor: Fix empty/errored responses for Logs variables (#59240)

This commit is contained in:
Andres Martinez Gotor 2022-11-24 11:25:40 +01:00 committed by GitHub
parent cff36ea7d8
commit 276b54fe9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 16 deletions

View File

@ -190,7 +190,10 @@ func (e *AzureLogAnalyticsDatasource) executeQuery(ctx context.Context, logger l
if err != nil {
return dataResponseErrorWithExecuted(err)
}
appendErrorNotice(frame, logResponse.Error)
frame = appendErrorNotice(frame, logResponse.Error)
if frame == nil {
return dataResponse
}
model, err := simplejson.NewJson(query.JSON)
if err != nil {
@ -222,10 +225,15 @@ func (e *AzureLogAnalyticsDatasource) executeQuery(ctx context.Context, logger l
return dataResponse
}
func appendErrorNotice(frame *data.Frame, err *AzureLogAnalyticsAPIError) {
if err != nil {
frame.AppendNotices(apiErrorToNotice(err))
func appendErrorNotice(frame *data.Frame, err *AzureLogAnalyticsAPIError) *data.Frame {
if err == nil {
return frame
}
if frame == nil {
frame = &data.Frame{}
}
frame.AppendNotices(apiErrorToNotice(err))
return frame
}
func (e *AzureLogAnalyticsDatasource) createRequest(ctx context.Context, logger log.Logger, url string) (*http.Request, error) {

View File

@ -45,12 +45,7 @@ func apiErrorToNotice(err *AzureLogAnalyticsAPIError) data.Notice {
// ResponseTableToFrame converts an AzureResponseTable to a data.Frame.
func ResponseTableToFrame(table *types.AzureResponseTable, refID string, executedQuery string) (*data.Frame, error) {
if len(table.Rows) == 0 {
return &data.Frame{
RefID: refID,
Meta: &data.FrameMeta{
ExecutedQueryString: executedQuery,
},
}, nil
return nil, nil
}
converterFrame, err := converterFrameForTable(table)

View File

@ -173,12 +173,7 @@ func TestLogTableToFrame(t *testing.T) {
name: "empty data response",
testFile: "loganalytics/11-log-analytics-response-empty.json",
expectedFrame: func() *data.Frame {
return &data.Frame{
RefID: "A",
Meta: &data.FrameMeta{
ExecutedQueryString: "query",
},
}
return nil
},
},
}

View File

@ -489,6 +489,38 @@ describe('VariableSupport', () => {
});
});
it('passes on the query error for a log query', (done) => {
const variableSupport = new VariableSupport(
createMockDatasource({
query: () =>
from(
Promise.resolve({
data: [],
error: {
message: 'boom',
},
})
),
})
);
const mockRequest = {
targets: [
{
queryType: AzureQueryType.LogAnalytics,
azureLogAnalytics: {
query: 'some log thing',
},
} as AzureMonitorQuery,
],
} as DataQueryRequest<AzureMonitorQuery>;
const observables = variableSupport.query(mockRequest);
observables.subscribe((result: DataQueryResponseData) => {
expect(result.data).toEqual([]);
expect(result.error.message).toEqual('boom');
done();
});
});
it('should handle http error', (done) => {
const error = invalidSubscriptionError();
const variableSupport = new VariableSupport(

View File

@ -94,6 +94,7 @@ export class VariableSupport extends CustomVariableSupport<DataSource, AzureMoni
const queryResp = await lastValueFrom(this.datasource.query(request));
return {
data: queryResp.data,
error: queryResp.error ? new Error(messageFromError(queryResp.error)) : undefined,
};
}
} catch (err) {