Files
grafana/public/app/plugins/datasource/azuremonitor/components/ResourcePicker/utils.ts
Andreas Christou 63383ef545 AzureMonitor: Application Insights Traces (#64859)
* Build out barebones Traces editor

- Add Traces query type and operation ID prop to query type
- Add necessary header types
- Update resource picker to appropriately work with traces query type
- Build out TracesQueryEditor component
- Include logic to retrieve operationId's for AI Workspaces
- Add backend route mapping
- Update macro to use timestamp as default time field for traces

* AzureMonitor: Traces - Response parsing (#65442)

* Update FormatAsField component

- Add trace ResultFormat type
- Generalise FormatAsField component
- Add component to TracesQueryEditor
- Remove duplicate code in setQueryValue

* Add custom filter function to improve performance

* Add basic conversion for logs to trace

- Add serviceTags converter
- Pass through required parameters (queryType and resultFormat)
- Appropriately set visualisation

* Update parsing to also fill trace tags

- Add constant values for each table schema (include legacy mapping for now if needed)
- Add constant for list of table tags
- Set the foundation for dynamic query building
- Update query to build tags value
- Appropriately set operationName
- Update tagsConverter to filter empty values

* Fix lint and test issues

* AzureMonitor: Traces - Data links (#65566)

* Add portal link for traces

- Pull out necessary values (itemId and itemType)
- Appropriately construct
- Fix ordering

* Set default format as value

- Also set default visualisation

* Fix event schema

* Set default formatAsField value

* Include logs link on traces results

- Adapt config links to allow custom title to be set

* Correctly set operationId for query

* Update backend types

- Include OperationID in query
- Pass forward datasource name and UID

* Ensure setTime doesn't consistently get called if operationID is defined

* Add explore link

- Update util functions to allow setting custom datalinks

* Fix tests

* AzureMonitor: Traces - Query and Editor updates (#66076)

* Add initial query

- Will query the resource as soon as a resource has been selected
- Updates the data links for the query without operationId
- Remove initial operationId query and timeRange dependency
- Update query building

* Add entirely separate traces query property

- Update shared types (also including future types for Azure traces)
- Update backend log analytics datasource to accept both azureLogAnalytics and azureTraces queries
- Update backend specific types
- Update frontend datasource for new properties
- Update mock query

* Update FormatAsField to be entirely generic

* Update query building to be done in backend

- Add required mappings in backend
- Update frontend querying

* Fix query and explore data link

* Add trace type selection

* Better method for setting explore link

* Fix operationId updating

* Run go mod tidy

* Unnecessary changes

* Fix tests

* AzureMonitor: Traces - Add correlation API support (#65855)

Add correlation API support

- Add necessary types
- Add correlation API request when conditions are met
- Update query

* Fix property from merge

* AzureMonitor: Traces - Filtering (#66303)

* Add initial query

- Will query the resource as soon as a resource has been selected
- Updates the data links for the query without operationId
- Remove initial operationId query and timeRange dependency
- Update query building

* Add entirely separate traces query property

- Update shared types (also including future types for Azure traces)
- Update backend log analytics datasource to accept both azureLogAnalytics and azureTraces queries
- Update backend specific types
- Update frontend datasource for new properties
- Update mock query

* Update FormatAsField to be entirely generic

* Update query building to be done in backend

- Add required mappings in backend
- Update frontend querying

* Fix query and explore data link

* Add trace type selection

* Better method for setting explore link

* Fix operationId updating

* Run go mod tidy

* Unnecessary changes

* Fix tests

* Start building out Filters component

- Configure component to query for Filter property values when a filter property is set
- Add setFilters function
- Add typing to tablesSchema
- Use component in TracesQueryEditor

* Update Filters

- Asynchronously pull property options
- Setup list of Filter components

* Update filters component

- Remove unused imports
- Have local filters state and query filters
- Correctly set filters values
- Don't update query every time a filter property changes (not performant)

* Update properties query

- Use current timeRange
- Get count to provide informative labels

* Reset map when time changes

* Add operation selection

* Reset filters when property changes

* Appropriate label name for empty values

* Add filtering to query

* Update filter components

- Fix rendering issue
- Correctly compare and update timeRange
- Split out files for simplicity

* Add checkbox option to multiselect

- Add custom option component
- Correctly call onChange
- Add variableOptionGroup for template variable selection

* Fix adding template vars

* Improve labels and refresh labels on query prop changes

* AzureMonitor: Traces - Testing (#66474)

* Select ds for template variable interpolation

* Update az logs ds tests

- Add templateVariables test
- Add filter test
- Update mock
- Remove anys

* Update QueryEditor test

- Update mocks with timeSrv for log analytics datasource
- Fix query mock
- Use appropriate and consistent selectors

* Add TracesQueryEditor test

- Update resourcePickerRows mock to include app insights resources
- Remove comments and extra new line

* Add FormatAsField test

- Remove unneeded condition

* Update resourcePicker utils test

* Don't hide selected options in filters

* Fix multi-selection on filters

* Add TraceTypeField test

- Add test file
- Update selectors (remove copy/paste mistake)
- Update placeholder text for select and add label

* Add basic filters test

* Begin filters test

* Update filters test

* Add final tests and simplify/generalise addFilter helper

* Minor update to datasource test

* Update macros test

* Update selectors in tests

* Add response-table-frame tests

* Add datasource tests

- Use sorting where JSON models are inconsistent
- Update filters clause
- Dedupe tags
- Correct operationId conditions

* Don't set a default value for blurInputOnSelect

* Simplify datasource test

* Update to use CheckGoldenJSON utils

- Update with generated frame files
- Remove redundant expected frame code
- Update all usages

* Fix lint

* AzureMonitor: Traces feedback (#67292)

* Filter traces if the visualisation is set to trace

- Update build query logic
- Added additional test cases
- Return an error if the traces type is set by itself with the trace visualisation
- Add descriptions to event types
- Update tests

* Fix bug for error displaying traces

* Update mappings and add error field

- Update tests
- Remove unnecessary comments

* Switch location of Operation ID field

* Re-order fields

* Update link title

* Update label for event type selection

* Update correct link title

* Update logs datalink to link to Azure Logs in explore

* Fix lint
2023-04-27 20:24:11 +01:00

206 lines
6.7 KiB
TypeScript

import produce from 'immer';
import { getTemplateSrv } from '@grafana/runtime';
import UrlBuilder from '../../azure_monitor/url_builder';
import { ResourcePickerQueryType } from '../../resourcePicker/resourcePickerData';
import { AzureMonitorResource, AzureMonitorQuery } from '../../types';
import { ResourceRow, ResourceRowGroup } from './types';
// This regex matches URIs representing:
// - subscriptions: /subscriptions/44693801-6ee6-49de-9b2d-9106972f9572
// - resource groups: /subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources
// - resources: /subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Compute/virtualMachines/GithubTestDataVM
const RESOURCE_URI_REGEX =
/\/subscriptions\/(?<subscription>[^/]+)(?:\/resourceGroups\/(?<resourceGroup>[^/]+)(?:\/providers\/(?<metricNamespaceAndResource>.+))?)?/;
type RegexGroups = Record<string, string | undefined>;
function parseNamespaceAndName(metricNamespaceAndName?: string) {
if (!metricNamespaceAndName) {
return {};
}
const stringArray = metricNamespaceAndName.split('/');
// The first two groups belong to the namespace (e.g. Microsoft.Storage/storageAccounts)
const namespaceArray = stringArray.splice(0, 2);
// The next element belong to the resource name (e.g. storageAcc1)
const resourceNameArray = stringArray.splice(0, 1);
// If there are more elements, keep adding them to the namespace and resource name, alternatively
// e.g (blobServices/default)
while (stringArray.length) {
const nextElem = stringArray.shift()!;
stringArray.length % 2 === 0 ? resourceNameArray.push(nextElem) : namespaceArray.push(nextElem);
}
return { metricNamespace: namespaceArray.join('/'), resourceName: resourceNameArray.join('/') };
}
export function parseResourceURI(resourceURI: string): AzureMonitorResource {
const matches = RESOURCE_URI_REGEX.exec(resourceURI);
const groups: RegexGroups = matches?.groups ?? {};
const { subscription, resourceGroup, metricNamespaceAndResource } = groups;
const { metricNamespace, resourceName } = parseNamespaceAndName(metricNamespaceAndResource);
return { subscription, resourceGroup, metricNamespace, resourceName };
}
export function parseMultipleResourceDetails(resources: Array<string | AzureMonitorResource>, location?: string) {
return resources.map((resource) => {
return parseResourceDetails(resource, location);
});
}
export function parseResourceDetails(resource: string | AzureMonitorResource, location?: string) {
if (typeof resource === 'string') {
const res = parseResourceURI(resource);
if (location) {
res.region = location;
}
return res;
}
return resource;
}
export function resourcesToStrings(resources: Array<string | AzureMonitorResource>) {
return resources.map((resource) => resourceToString(resource));
}
export function resourceToString(resource?: string | AzureMonitorResource) {
return resource
? typeof resource === 'string'
? resource
: UrlBuilder.buildResourceUri(getTemplateSrv(), resource)
: '';
}
export function isGUIDish(input: string) {
return !!input.match(/^[A-Z0-9]+/i);
}
function compareNamespaceAndName(
rowNamespace?: string,
rowName?: string,
resourceNamespace?: string,
resourceName?: string
) {
// StorageAccounts subresources are not listed independently
if (resourceNamespace?.startsWith('microsoft.storage/storageaccounts')) {
resourceNamespace = 'microsoft.storage/storageaccounts';
if (resourceName?.endsWith('/default')) {
resourceName = resourceName.slice(0, -'/default'.length);
}
}
return rowNamespace === resourceNamespace && rowName === resourceName;
}
export function matchURI(rowURI: string, resourceURI: string) {
const targetParams = parseResourceDetails(resourceURI);
const rowParams = parseResourceDetails(rowURI);
return (
rowParams?.subscription === targetParams?.subscription &&
rowParams?.resourceGroup?.toLowerCase() === targetParams?.resourceGroup?.toLowerCase() &&
compareNamespaceAndName(
rowParams?.metricNamespace?.toLowerCase(),
rowParams?.resourceName,
targetParams?.metricNamespace?.toLowerCase(),
targetParams?.resourceName
)
);
}
export function findRows(rows: ResourceRowGroup, uris: string[]): ResourceRow[] {
const result: ResourceRow[] = [];
uris.forEach((uri) => {
const row = findRow(rows, uri);
if (row) {
result.push(row);
}
});
return result;
}
export function findRow(rows: ResourceRowGroup, uri: string): ResourceRow | undefined {
for (const row of rows) {
if (matchURI(row.uri, uri)) {
return row;
}
if (row.children) {
const result = findRow(row.children, uri);
if (result) {
return result;
}
}
}
return undefined;
}
export function addResources(rows: ResourceRowGroup, targetParentId: string, newResources: ResourceRowGroup) {
return produce(rows, (draftState) => {
const draftRow = findRow(draftState, targetParentId);
// we can't find the selected resource in our list of resources,
// probably means user has either mistyped in the input field
// or is using template variables.
// either way no need to throw, just show that none of the resources are checked
if (!draftRow) {
return;
}
draftRow.children = newResources;
});
}
export function setResources(
query: AzureMonitorQuery,
type: ResourcePickerQueryType,
resources: Array<string | AzureMonitorResource>
): AzureMonitorQuery {
if (type === 'logs') {
// Resource URI for LogAnalytics
return {
...query,
azureLogAnalytics: {
...query.azureLogAnalytics,
resources: resourcesToStrings(resources).filter((resource) => resource !== ''),
},
};
}
if (type === 'traces') {
// Resource URI for Traces
return {
...query,
azureTraces: {
...query.azureTraces,
resources: resourcesToStrings(resources).filter((resource) => resource !== ''),
},
};
}
// Resource object for metrics
const parsedResource = resources.length ? parseResourceDetails(resources[0]) : {};
return {
...query,
subscription: parsedResource.subscription,
azureMonitor: {
...query.azureMonitor,
metricNamespace: parsedResource.metricNamespace?.toLocaleLowerCase(),
region: parsedResource.region,
resources: parseMultipleResourceDetails(resources).filter(
(resource) =>
resource.resourceName !== '' &&
resource.metricNamespace !== '' &&
resource.subscription !== '' &&
resource.resourceGroup !== ''
),
metricName: undefined,
aggregation: undefined,
timeGrain: '',
dimensionFilters: [],
},
};
}