63383ef545
* 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
263 lines
8.5 KiB
Go
263 lines
8.5 KiB
Go
package resourcegraph
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/loganalytics"
|
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/macros"
|
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
|
)
|
|
|
|
// AzureResourceGraphResponse is the json response object from the Azure Resource Graph Analytics API.
|
|
type AzureResourceGraphResponse struct {
|
|
Data types.AzureResponseTable `json:"data"`
|
|
}
|
|
|
|
// AzureResourceGraphDatasource calls the Azure Resource Graph API's
|
|
type AzureResourceGraphDatasource struct {
|
|
Proxy types.ServiceProxy
|
|
}
|
|
|
|
// AzureResourceGraphQuery is the query request that is built from the saved values for
|
|
// from the UI
|
|
type AzureResourceGraphQuery struct {
|
|
RefID string
|
|
ResultFormat string
|
|
URL string
|
|
JSON json.RawMessage
|
|
InterpolatedQuery string
|
|
TimeRange backend.TimeRange
|
|
QueryType string
|
|
}
|
|
|
|
const ArgAPIVersion = "2021-06-01-preview"
|
|
const argQueryProviderName = "/providers/Microsoft.ResourceGraph/resources"
|
|
|
|
func (e *AzureResourceGraphDatasource) ResourceRequest(rw http.ResponseWriter, req *http.Request, cli *http.Client) {
|
|
e.Proxy.Do(rw, req, cli)
|
|
}
|
|
|
|
// executeTimeSeriesQuery does the following:
|
|
// 1. builds the AzureMonitor url and querystring for each query
|
|
// 2. executes each query by calling the Azure Monitor API
|
|
// 3. parses the responses for each query into data frames
|
|
func (e *AzureResourceGraphDatasource) ExecuteTimeSeriesQuery(ctx context.Context, logger log.Logger, originalQueries []backend.DataQuery, dsInfo types.DatasourceInfo, client *http.Client, url string, tracer tracing.Tracer) (*backend.QueryDataResponse, error) {
|
|
result := &backend.QueryDataResponse{
|
|
Responses: map[string]backend.DataResponse{},
|
|
}
|
|
ctxLogger := logger.FromContext(ctx)
|
|
|
|
queries, err := e.buildQueries(ctxLogger, originalQueries, dsInfo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, query := range queries {
|
|
result.Responses[query.RefID] = e.executeQuery(ctx, ctxLogger, query, dsInfo, client, url, tracer)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
type argJSONQuery struct {
|
|
AzureResourceGraph struct {
|
|
Query string `json:"query"`
|
|
ResultFormat string `json:"resultFormat"`
|
|
} `json:"azureResourceGraph"`
|
|
}
|
|
|
|
func (e *AzureResourceGraphDatasource) buildQueries(logger log.Logger, queries []backend.DataQuery, dsInfo types.DatasourceInfo) ([]*AzureResourceGraphQuery, error) {
|
|
var azureResourceGraphQueries []*AzureResourceGraphQuery
|
|
|
|
for _, query := range queries {
|
|
queryJSONModel := argJSONQuery{}
|
|
err := json.Unmarshal(query.JSON, &queryJSONModel)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to decode the Azure Resource Graph query object from JSON: %w", err)
|
|
}
|
|
|
|
azureResourceGraphTarget := queryJSONModel.AzureResourceGraph
|
|
logger.Debug("AzureResourceGraph", "target", azureResourceGraphTarget)
|
|
|
|
resultFormat := azureResourceGraphTarget.ResultFormat
|
|
if resultFormat == "" {
|
|
resultFormat = "table"
|
|
}
|
|
|
|
interpolatedQuery, err := macros.KqlInterpolate(logger, query, dsInfo, azureResourceGraphTarget.Query)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
azureResourceGraphQueries = append(azureResourceGraphQueries, &AzureResourceGraphQuery{
|
|
RefID: query.RefID,
|
|
ResultFormat: resultFormat,
|
|
JSON: query.JSON,
|
|
InterpolatedQuery: interpolatedQuery,
|
|
TimeRange: query.TimeRange,
|
|
QueryType: query.QueryType,
|
|
})
|
|
}
|
|
|
|
return azureResourceGraphQueries, nil
|
|
}
|
|
|
|
func (e *AzureResourceGraphDatasource) executeQuery(ctx context.Context, logger log.Logger, query *AzureResourceGraphQuery, dsInfo types.DatasourceInfo, client *http.Client,
|
|
dsURL string, tracer tracing.Tracer) backend.DataResponse {
|
|
dataResponse := backend.DataResponse{}
|
|
|
|
params := url.Values{}
|
|
params.Add("api-version", ArgAPIVersion)
|
|
|
|
dataResponseErrorWithExecuted := func(err error) backend.DataResponse {
|
|
dataResponse = backend.DataResponse{Error: err}
|
|
frames := data.Frames{
|
|
&data.Frame{
|
|
RefID: query.RefID,
|
|
Meta: &data.FrameMeta{
|
|
ExecutedQueryString: query.InterpolatedQuery,
|
|
},
|
|
},
|
|
}
|
|
dataResponse.Frames = frames
|
|
return dataResponse
|
|
}
|
|
|
|
model, err := simplejson.NewJson(query.JSON)
|
|
if err != nil {
|
|
dataResponse.Error = err
|
|
return dataResponse
|
|
}
|
|
|
|
reqBody, err := json.Marshal(map[string]interface{}{
|
|
"subscriptions": model.Get("subscriptions").MustStringArray(),
|
|
"query": query.InterpolatedQuery,
|
|
"options": map[string]string{"resultFormat": "table"},
|
|
})
|
|
|
|
if err != nil {
|
|
dataResponse.Error = err
|
|
return dataResponse
|
|
}
|
|
|
|
req, err := e.createRequest(ctx, logger, reqBody, dsURL)
|
|
|
|
if err != nil {
|
|
dataResponse.Error = err
|
|
return dataResponse
|
|
}
|
|
|
|
req.URL.Path = path.Join(req.URL.Path, argQueryProviderName)
|
|
req.URL.RawQuery = params.Encode()
|
|
|
|
ctx, span := tracer.Start(ctx, "azure resource graph query")
|
|
span.SetAttributes("interpolated_query", query.InterpolatedQuery, attribute.Key("interpolated_query").String(query.InterpolatedQuery))
|
|
span.SetAttributes("from", query.TimeRange.From.UnixNano()/int64(time.Millisecond), attribute.Key("from").Int64(query.TimeRange.From.UnixNano()/int64(time.Millisecond)))
|
|
span.SetAttributes("until", query.TimeRange.To.UnixNano()/int64(time.Millisecond), attribute.Key("until").Int64(query.TimeRange.To.UnixNano()/int64(time.Millisecond)))
|
|
span.SetAttributes("datasource_id", dsInfo.DatasourceID, attribute.Key("datasource_id").Int64(dsInfo.DatasourceID))
|
|
span.SetAttributes("org_id", dsInfo.OrgID, attribute.Key("org_id").Int64(dsInfo.OrgID))
|
|
|
|
defer span.End()
|
|
|
|
tracer.Inject(ctx, req.Header, span)
|
|
|
|
logger.Debug("AzureResourceGraph", "Request ApiURL", req.URL.String())
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
return dataResponseErrorWithExecuted(err)
|
|
}
|
|
|
|
defer func() {
|
|
err := res.Body.Close()
|
|
if err != nil {
|
|
logger.Warn("failed to close response body", "error", err)
|
|
}
|
|
}()
|
|
|
|
argResponse, err := e.unmarshalResponse(logger, res)
|
|
if err != nil {
|
|
return dataResponseErrorWithExecuted(err)
|
|
}
|
|
|
|
frame, err := loganalytics.ResponseTableToFrame(&argResponse.Data, query.RefID, query.InterpolatedQuery, dataquery.AzureQueryType(query.QueryType), dataquery.ResultFormat(query.ResultFormat))
|
|
if err != nil {
|
|
return dataResponseErrorWithExecuted(err)
|
|
}
|
|
if frame == nil {
|
|
// empty response
|
|
return dataResponse
|
|
}
|
|
|
|
azurePortalUrl, err := loganalytics.GetAzurePortalUrl(dsInfo.Cloud)
|
|
if err != nil {
|
|
return dataResponseErrorWithExecuted(err)
|
|
}
|
|
|
|
url := azurePortalUrl + "/#blade/HubsExtension/ArgQueryBlade/query/" + url.PathEscape(query.InterpolatedQuery)
|
|
frameWithLink := loganalytics.AddConfigLinks(*frame, url, nil)
|
|
if frameWithLink.Meta == nil {
|
|
frameWithLink.Meta = &data.FrameMeta{}
|
|
}
|
|
frameWithLink.Meta.ExecutedQueryString = req.URL.RawQuery
|
|
|
|
dataResponse.Frames = data.Frames{&frameWithLink}
|
|
return dataResponse
|
|
}
|
|
|
|
func (e *AzureResourceGraphDatasource) createRequest(ctx context.Context, logger log.Logger, reqBody []byte, url string) (*http.Request, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(reqBody))
|
|
if err != nil {
|
|
logger.Debug("Failed to create request", "error", err)
|
|
return nil, fmt.Errorf("%v: %w", "failed to create request", err)
|
|
}
|
|
req.URL.Path = "/"
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func (e *AzureResourceGraphDatasource) unmarshalResponse(logger log.Logger, res *http.Response) (AzureResourceGraphResponse, error) {
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return AzureResourceGraphResponse{}, err
|
|
}
|
|
defer func() {
|
|
if err := res.Body.Close(); err != nil {
|
|
logger.Warn("Failed to close response body", "err", err)
|
|
}
|
|
}()
|
|
|
|
if res.StatusCode/100 != 2 {
|
|
logger.Debug("Request failed", "status", res.Status, "body", string(body))
|
|
return AzureResourceGraphResponse{}, fmt.Errorf("%s. Azure Resource Graph error: %s", res.Status, string(body))
|
|
}
|
|
|
|
var data AzureResourceGraphResponse
|
|
d := json.NewDecoder(bytes.NewReader(body))
|
|
d.UseNumber()
|
|
err = d.Decode(&data)
|
|
if err != nil {
|
|
logger.Debug("Failed to unmarshal azure resource graph response", "error", err, "status", res.Status, "body", string(body))
|
|
return AzureResourceGraphResponse{}, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|