Chore: Update cloud monitoring and Azure data sources to support contextual logs (#57844)
* update cloud monitoring to use log from context * update azure monitor to use contextual logger
This commit is contained in:
@@ -17,9 +17,9 @@ import (
|
||||
"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/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/azlog"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/loganalytics"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/macros"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||
@@ -57,19 +57,19 @@ func (e *AzureResourceGraphDatasource) ResourceRequest(rw http.ResponseWriter, r
|
||||
// 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, originalQueries []backend.DataQuery, dsInfo types.DatasourceInfo, client *http.Client,
|
||||
url string, tracer tracing.Tracer) (*backend.QueryDataResponse, error) {
|
||||
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(originalQueries, dsInfo)
|
||||
queries, err := e.buildQueries(ctxLogger, originalQueries, dsInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, query := range queries {
|
||||
result.Responses[query.RefID] = e.executeQuery(ctx, query, dsInfo, client, url, tracer)
|
||||
result.Responses[query.RefID] = e.executeQuery(ctx, ctxLogger, query, dsInfo, client, url, tracer)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
@@ -82,7 +82,7 @@ type argJSONQuery struct {
|
||||
} `json:"azureResourceGraph"`
|
||||
}
|
||||
|
||||
func (e *AzureResourceGraphDatasource) buildQueries(queries []backend.DataQuery, dsInfo types.DatasourceInfo) ([]*AzureResourceGraphQuery, error) {
|
||||
func (e *AzureResourceGraphDatasource) buildQueries(logger log.Logger, queries []backend.DataQuery, dsInfo types.DatasourceInfo) ([]*AzureResourceGraphQuery, error) {
|
||||
var azureResourceGraphQueries []*AzureResourceGraphQuery
|
||||
|
||||
for _, query := range queries {
|
||||
@@ -93,14 +93,14 @@ func (e *AzureResourceGraphDatasource) buildQueries(queries []backend.DataQuery,
|
||||
}
|
||||
|
||||
azureResourceGraphTarget := queryJSONModel.AzureResourceGraph
|
||||
azlog.Debug("AzureResourceGraph", "target", azureResourceGraphTarget)
|
||||
logger.Debug("AzureResourceGraph", "target", azureResourceGraphTarget)
|
||||
|
||||
resultFormat := azureResourceGraphTarget.ResultFormat
|
||||
if resultFormat == "" {
|
||||
resultFormat = "table"
|
||||
}
|
||||
|
||||
interpolatedQuery, err := macros.KqlInterpolate(query, dsInfo, azureResourceGraphTarget.Query)
|
||||
interpolatedQuery, err := macros.KqlInterpolate(logger, query, dsInfo, azureResourceGraphTarget.Query)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -118,7 +118,7 @@ func (e *AzureResourceGraphDatasource) buildQueries(queries []backend.DataQuery,
|
||||
return azureResourceGraphQueries, nil
|
||||
}
|
||||
|
||||
func (e *AzureResourceGraphDatasource) executeQuery(ctx context.Context, query *AzureResourceGraphQuery, dsInfo types.DatasourceInfo, client *http.Client,
|
||||
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{}
|
||||
|
||||
@@ -156,7 +156,7 @@ func (e *AzureResourceGraphDatasource) executeQuery(ctx context.Context, query *
|
||||
return dataResponse
|
||||
}
|
||||
|
||||
req, err := e.createRequest(ctx, dsInfo, reqBody, dsURL)
|
||||
req, err := e.createRequest(ctx, logger, reqBody, dsURL)
|
||||
|
||||
if err != nil {
|
||||
dataResponse.Error = err
|
||||
@@ -177,13 +177,13 @@ func (e *AzureResourceGraphDatasource) executeQuery(ctx context.Context, query *
|
||||
|
||||
tracer.Inject(ctx, req.Header, span)
|
||||
|
||||
azlog.Debug("AzureResourceGraph", "Request ApiURL", req.URL.String())
|
||||
logger.Debug("AzureResourceGraph", "Request ApiURL", req.URL.String())
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return dataResponseErrorWithExecuted(err)
|
||||
}
|
||||
|
||||
argResponse, err := e.unmarshalResponse(res)
|
||||
argResponse, err := e.unmarshalResponse(logger, res)
|
||||
if err != nil {
|
||||
return dataResponseErrorWithExecuted(err)
|
||||
}
|
||||
@@ -224,10 +224,10 @@ func AddConfigLinks(frame data.Frame, dl string) data.Frame {
|
||||
return frame
|
||||
}
|
||||
|
||||
func (e *AzureResourceGraphDatasource) createRequest(ctx context.Context, dsInfo types.DatasourceInfo, reqBody []byte, url string) (*http.Request, error) {
|
||||
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 {
|
||||
azlog.Debug("Failed to create request", "error", err)
|
||||
logger.Debug("Failed to create request", "error", err)
|
||||
return nil, fmt.Errorf("%v: %w", "failed to create request", err)
|
||||
}
|
||||
req.URL.Path = "/"
|
||||
@@ -237,19 +237,19 @@ func (e *AzureResourceGraphDatasource) createRequest(ctx context.Context, dsInfo
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (e *AzureResourceGraphDatasource) unmarshalResponse(res *http.Response) (AzureResourceGraphResponse, error) {
|
||||
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 {
|
||||
azlog.Warn("Failed to close response body", "err", err)
|
||||
logger.Warn("Failed to close response body", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if res.StatusCode/100 != 2 {
|
||||
azlog.Debug("Request failed", "status", res.Status, "body", string(body))
|
||||
logger.Debug("Request failed", "status", res.Status, "body", string(body))
|
||||
return AzureResourceGraphResponse{}, fmt.Errorf("%s. Azure Resource Graph error: %s", res.Status, string(body))
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ func (e *AzureResourceGraphDatasource) unmarshalResponse(res *http.Response) (Az
|
||||
d.UseNumber()
|
||||
err = d.Decode(&data)
|
||||
if err != nil {
|
||||
azlog.Debug("Failed to unmarshal azure resource graph response", "error", err, "status", res.Status, "body", string(body))
|
||||
logger.Debug("Failed to unmarshal azure resource graph response", "error", err, "status", res.Status, "body", string(body))
|
||||
return AzureResourceGraphResponse{}, err
|
||||
}
|
||||
|
||||
|
||||
@@ -14,12 +14,16 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||
)
|
||||
|
||||
var logger = log.New("test")
|
||||
|
||||
func TestBuildingAzureResourceGraphQueries(t *testing.T) {
|
||||
datasource := &AzureResourceGraphDatasource{}
|
||||
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
||||
@@ -70,7 +74,7 @@ func TestBuildingAzureResourceGraphQueries(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
queries, err := datasource.buildQueries(tt.queryModel, types.DatasourceInfo{})
|
||||
queries, err := datasource.buildQueries(logger, tt.queryModel, types.DatasourceInfo{})
|
||||
tt.Err(t, err)
|
||||
if diff := cmp.Diff(tt.azureResourceGraphQueries, queries, cmpopts.IgnoreUnexported(simplejson.Json{})); diff != "" {
|
||||
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
||||
@@ -82,7 +86,6 @@ func TestBuildingAzureResourceGraphQueries(t *testing.T) {
|
||||
func TestAzureResourceGraphCreateRequest(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
url := "http://ds"
|
||||
dsInfo := types.DatasourceInfo{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -104,7 +107,7 @@ func TestAzureResourceGraphCreateRequest(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ds := AzureResourceGraphDatasource{}
|
||||
req, err := ds.createRequest(ctx, dsInfo, []byte{}, url)
|
||||
req, err := ds.createRequest(ctx, logger, []byte{}, url)
|
||||
tt.Err(t, err)
|
||||
if req.URL.String() != tt.expectedURL {
|
||||
t.Errorf("Expecting %s, got %s", tt.expectedURL, req.URL.String())
|
||||
@@ -157,7 +160,7 @@ func TestGetAzurePortalUrl(t *testing.T) {
|
||||
|
||||
func TestUnmarshalResponse400(t *testing.T) {
|
||||
datasource := &AzureResourceGraphDatasource{}
|
||||
res, err := datasource.unmarshalResponse(&http.Response{
|
||||
res, err := datasource.unmarshalResponse(logger, &http.Response{
|
||||
StatusCode: 400,
|
||||
Status: "400 Bad Request",
|
||||
Body: io.NopCloser(strings.NewReader(("Azure Error Message"))),
|
||||
@@ -171,7 +174,7 @@ func TestUnmarshalResponse400(t *testing.T) {
|
||||
|
||||
func TestUnmarshalResponse200Invalid(t *testing.T) {
|
||||
datasource := &AzureResourceGraphDatasource{}
|
||||
res, err := datasource.unmarshalResponse(&http.Response{
|
||||
res, err := datasource.unmarshalResponse(logger, &http.Response{
|
||||
StatusCode: 200,
|
||||
Status: "OK",
|
||||
Body: io.NopCloser(strings.NewReader(("Azure Data"))),
|
||||
@@ -186,7 +189,7 @@ func TestUnmarshalResponse200Invalid(t *testing.T) {
|
||||
|
||||
func TestUnmarshalResponse200(t *testing.T) {
|
||||
datasource := &AzureResourceGraphDatasource{}
|
||||
res, err2 := datasource.unmarshalResponse(&http.Response{
|
||||
res, err2 := datasource.unmarshalResponse(logger, &http.Response{
|
||||
StatusCode: 200,
|
||||
Status: "OK",
|
||||
Body: io.NopCloser(strings.NewReader("{}")),
|
||||
|
||||
Reference in New Issue
Block a user