mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Fix interpolation of log groups when fetching fields (#98054)
This commit is contained in:
parent
67a7731ca9
commit
105251425f
@ -7,6 +7,7 @@ import { CompletionItemProvider } from '../../monarch/CompletionItemProvider';
|
||||
import { LinkedToken } from '../../monarch/LinkedToken';
|
||||
import { TRIGGER_SUGGEST } from '../../monarch/commands';
|
||||
import { SuggestionKind, CompletionItemPriority, StatementPosition } from '../../monarch/types';
|
||||
import { fetchLogGroupFields } from '../../utils';
|
||||
import {
|
||||
ASC,
|
||||
BY,
|
||||
@ -268,7 +269,12 @@ export class LogsSQLCompletionItemProvider extends CompletionItemProvider {
|
||||
break;
|
||||
|
||||
case SuggestionKind.Field:
|
||||
const fields = await this.fetchFields(this.queryContext.logGroups || [], this.queryContext.region);
|
||||
const fields = await fetchLogGroupFields(
|
||||
this.queryContext.logGroups || [],
|
||||
this.queryContext.region,
|
||||
this.templateSrv,
|
||||
this.resources
|
||||
);
|
||||
fields.forEach((field) => {
|
||||
if (field !== '') {
|
||||
addSuggestion(field, {
|
||||
@ -295,20 +301,4 @@ export class LogsSQLCompletionItemProvider extends CompletionItemProvider {
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
private fetchFields = async (logGroups: LogGroup[], region: string): Promise<string[]> => {
|
||||
if (logGroups.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
logGroups.map((logGroup) =>
|
||||
this.resources
|
||||
.getLogGroupFields({ logGroupName: logGroup.name, arn: logGroup.arn, region })
|
||||
.then((fields) => fields.filter((f) => f).map((f) => f.value.name ?? ''))
|
||||
)
|
||||
);
|
||||
// Deduplicate fields
|
||||
return [...new Set(results.flat())];
|
||||
};
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import { CompletionItemGroup, SearchFunctionType, Token, TypeaheadInput, Typeahe
|
||||
|
||||
import { CloudWatchDatasource } from '../../datasource';
|
||||
import { CloudWatchQuery, LogGroup } from '../../types';
|
||||
import { interpolateStringArrayUsingSingleOrMultiValuedVariable } from '../../utils/templateVariableUtils';
|
||||
import { fetchLogGroupFields } from '../utils';
|
||||
|
||||
import syntax, {
|
||||
AGGREGATION_FUNCTIONS_STATS,
|
||||
@ -131,24 +131,6 @@ export class CloudWatchLogsLanguageProvider extends LanguageProvider {
|
||||
};
|
||||
}
|
||||
|
||||
private fetchFields = async (logGroups: LogGroup[], region: string): Promise<string[]> => {
|
||||
const interpolatedLogGroups = interpolateStringArrayUsingSingleOrMultiValuedVariable(
|
||||
this.templateSrv,
|
||||
logGroups.map((lg) => lg.name),
|
||||
{},
|
||||
'text'
|
||||
);
|
||||
const results = await Promise.all(
|
||||
interpolatedLogGroups.map((logGroupName) =>
|
||||
this.datasource.resources
|
||||
.getLogGroupFields({ logGroupName, region })
|
||||
.then((fields) => fields.filter((f) => f).map((f) => f.value.name ?? ''))
|
||||
)
|
||||
);
|
||||
|
||||
return results.flat();
|
||||
};
|
||||
|
||||
private handleKeyword = async (context?: TypeaheadContext): Promise<TypeaheadOutput> => {
|
||||
const suggs = await this.getFieldCompletionItems(context?.logGroups, context?.region || 'default');
|
||||
const functionSuggestions: CompletionItemGroup[] = [
|
||||
@ -312,7 +294,7 @@ export class CloudWatchLogsLanguageProvider extends LanguageProvider {
|
||||
return { suggestions: [] };
|
||||
}
|
||||
|
||||
const fields = await this.fetchFields(logGroups, region);
|
||||
const fields = await fetchLogGroupFields(logGroups, region, this.templateSrv, this.datasource.resources);
|
||||
return {
|
||||
suggestions: [
|
||||
{
|
||||
|
@ -7,6 +7,7 @@ import { CompletionItemProvider } from '../../monarch/CompletionItemProvider';
|
||||
import { LinkedToken } from '../../monarch/LinkedToken';
|
||||
import { TRIGGER_SUGGEST } from '../../monarch/commands';
|
||||
import { CompletionItem, CompletionItemPriority, StatementPosition, SuggestionKind } from '../../monarch/types';
|
||||
import { fetchLogGroupFields } from '../../utils';
|
||||
import {
|
||||
BOOLEAN_LITERALS,
|
||||
CONDITION_FUNCTIONS,
|
||||
@ -252,7 +253,12 @@ export class PPLCompletionItemProvider extends CompletionItemProvider {
|
||||
): Promise<void> {
|
||||
if (this.queryContext.logGroups && this.queryContext.logGroups.length > 0) {
|
||||
try {
|
||||
let fields = await this.fetchFields(this.queryContext.logGroups, this.queryContext.region);
|
||||
let fields = await fetchLogGroupFields(
|
||||
this.queryContext.logGroups,
|
||||
this.queryContext.region,
|
||||
this.templateSrv,
|
||||
this.resources
|
||||
);
|
||||
fields.forEach((field) => {
|
||||
if (field !== '') {
|
||||
addSuggestion(field, {
|
||||
@ -269,16 +275,4 @@ export class PPLCompletionItemProvider extends CompletionItemProvider {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchFields(logGroups: LogGroup[], region: string): Promise<string[]> {
|
||||
const results = await Promise.all(
|
||||
logGroups.map((logGroup) =>
|
||||
this.resources
|
||||
.getLogGroupFields({ logGroupName: logGroup.name, arn: logGroup.arn, region })
|
||||
.then((fields) => fields.filter((f) => f).map((f) => f.value.name ?? ''))
|
||||
)
|
||||
);
|
||||
// Deduplicate fields
|
||||
return [...new Set(results.flat())];
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import { CompletionItemProvider } from '../../monarch/CompletionItemProvider';
|
||||
import { LinkedToken } from '../../monarch/LinkedToken';
|
||||
import { TRIGGER_SUGGEST } from '../../monarch/commands';
|
||||
import { CompletionItem, CompletionItemPriority, StatementPosition, SuggestionKind } from '../../monarch/types';
|
||||
import { fetchLogGroupFields } from '../../utils';
|
||||
import { LOGS_COMMANDS, LOGS_FUNCTION_OPERATORS, SORT_DIRECTION_KEYWORDS } from '../language';
|
||||
|
||||
import { getStatementPosition } from './statementPosition';
|
||||
@ -86,7 +87,12 @@ export class LogsCompletionItemProvider extends CompletionItemProvider {
|
||||
});
|
||||
|
||||
if (this.queryContext.logGroups && this.queryContext.logGroups.length > 0) {
|
||||
let fields = await this.fetchFields(this.queryContext.logGroups, this.queryContext.region);
|
||||
let fields = await fetchLogGroupFields(
|
||||
this.queryContext.logGroups,
|
||||
this.queryContext.region,
|
||||
this.templateSrv,
|
||||
this.resources
|
||||
);
|
||||
fields.push('@log');
|
||||
fields.forEach((field) => {
|
||||
if (field !== '') {
|
||||
@ -133,16 +139,4 @@ export class LogsCompletionItemProvider extends CompletionItemProvider {
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
private fetchFields = async (logGroups: LogGroup[], region: string): Promise<string[]> => {
|
||||
const results = await Promise.all(
|
||||
logGroups.map((logGroup) =>
|
||||
this.resources
|
||||
.getLogGroupFields({ logGroupName: logGroup.name, arn: logGroup.arn, region })
|
||||
.then((fields) => fields.filter((f) => f).map((f) => f.value.name ?? ''))
|
||||
)
|
||||
);
|
||||
// Deduplicate fields
|
||||
return [...new Set(results.flat())];
|
||||
};
|
||||
}
|
||||
|
33
public/app/plugins/datasource/cloudwatch/language/utils.ts
Normal file
33
public/app/plugins/datasource/cloudwatch/language/utils.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { TemplateSrv } from '@grafana/runtime';
|
||||
|
||||
import { LogGroup } from '../dataquery.gen';
|
||||
import { ResourcesAPI } from '../resources/ResourcesAPI';
|
||||
import { interpolateStringArrayUsingSingleOrMultiValuedVariable } from '../utils/templateVariableUtils';
|
||||
|
||||
export const fetchLogGroupFields = async (
|
||||
logGroups: LogGroup[],
|
||||
region: string,
|
||||
templateSrv: TemplateSrv,
|
||||
resources: ResourcesAPI
|
||||
): Promise<string[]> => {
|
||||
if (logGroups.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const interpolatedLogGroups = interpolateStringArrayUsingSingleOrMultiValuedVariable(
|
||||
templateSrv,
|
||||
logGroups.map((lg) => lg.name),
|
||||
{},
|
||||
'text'
|
||||
);
|
||||
|
||||
const results = await Promise.all(
|
||||
interpolatedLogGroups.map((logGroupName) =>
|
||||
resources
|
||||
.getLogGroupFields(region, logGroupName)
|
||||
.then((fields) => fields.filter((f) => f).map((f) => f.value.name ?? ''))
|
||||
)
|
||||
);
|
||||
// Deduplicate fields
|
||||
return [...new Set(results.flat())];
|
||||
};
|
@ -12,7 +12,6 @@ import {
|
||||
ResourceResponse,
|
||||
DescribeLogGroupsRequest,
|
||||
LogGroupResponse,
|
||||
GetLogGroupFieldsRequest,
|
||||
GetMetricsRequest,
|
||||
GetDimensionKeysRequest,
|
||||
GetDimensionValuesRequest,
|
||||
@ -79,15 +78,10 @@ export class ResourcesAPI extends CloudWatchRequest {
|
||||
});
|
||||
}
|
||||
|
||||
getLogGroupFields({
|
||||
region,
|
||||
arn,
|
||||
logGroupName,
|
||||
}: GetLogGroupFieldsRequest): Promise<Array<ResourceResponse<LogGroupField>>> {
|
||||
getLogGroupFields(region: string, logGroupName: string): Promise<Array<ResourceResponse<LogGroupField>>> {
|
||||
return this.memoizedGetRequest<Array<ResourceResponse<LogGroupField>>>('log-group-fields', {
|
||||
region: this.templateSrv.replace(this.getActualRegion(region)),
|
||||
logGroupName: this.templateSrv.replace(logGroupName, {}),
|
||||
logGroupArn: this.templateSrv.replace(arn),
|
||||
logGroupName: logGroupName,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -12,11 +12,6 @@ export interface ResourceRequest {
|
||||
accountId?: string;
|
||||
}
|
||||
|
||||
export interface GetLogGroupFieldsRequest extends ResourceRequest {
|
||||
arn?: string;
|
||||
logGroupName: string;
|
||||
}
|
||||
|
||||
export interface GetDimensionKeysRequest extends ResourceRequest {
|
||||
metricName?: string;
|
||||
namespace?: string;
|
||||
|
Loading…
Reference in New Issue
Block a user