Skip to content

Commit

Permalink
fix after review
Browse files Browse the repository at this point in the history
Signed-off-by: Rakshit Gondwal <[email protected]>
  • Loading branch information
rakshitgondwal committed Aug 23, 2023
1 parent b1ac310 commit 0f06f37
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewAPIClient(config apiConfig, options ...APIClientOption) *apiClient {
// Do sends and API request to the Dynatrace API and returns its result as a string containing the raw response payload
func (client *apiClient) Do(ctx context.Context, path, method string, payload []byte) ([]byte, int, error) {
if err := client.auth(ctx); err != nil {
return nil, http.StatusInternalServerError, err
return nil, http.StatusUnauthorized, err
}
api := fmt.Sprintf("%s%s", client.config.serverURL, path)
req, err := http.NewRequestWithContext(ctx, method, api, bytes.NewBuffer(payload))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAPIClientAuthError(t *testing.T) {

require.ErrorIs(t, err, ErrRequestFailed)
require.Empty(t, resp)
require.Equal(t, http.StatusInternalServerError, code)
require.Equal(t, http.StatusUnauthorized, code)
}

func TestAPIClientAuthNoToken(t *testing.T) {
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestAPIClientAuthNoToken(t *testing.T) {

require.ErrorIs(t, err, ErrAuthenticationFailed)
require.Empty(t, resp)
require.Equal(t, http.StatusInternalServerError, code)
require.Equal(t, http.StatusUnauthorized, code)
}

func TestAPIClientRequestError(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,7 @@ func NewKeptnDynatraceDQLProvider(k8sClient client.Client, opts ...KeptnDynatrac
}

func (d *keptnDynatraceDQLProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) {
if err := d.ensureDTClientIsSetUp(ctx, provider); err != nil {
return "", nil, err
}

b, status, err := d.postDQL(ctx, metric)
if err != nil {
d.log.Error(err, "Error while posting the DQL query", "query", metric.Spec.Query)
return "", nil, err
}

results, err := d.parseDQLResults(b, status)
results, err := d.getResults(ctx, metric, provider)
if err != nil {
return "", nil, err
}
Expand All @@ -117,7 +107,7 @@ func (d *keptnDynatraceDQLProvider) EvaluateQuery(ctx context.Context, metric me
}

r := fmt.Sprintf("%f", results.Records[0].Value.Avg)
b, err = json.Marshal(results)
b, err := json.Marshal(results)
if err != nil {
d.log.Error(err, "Error marshaling DQL results")
}
Expand All @@ -126,28 +116,36 @@ func (d *keptnDynatraceDQLProvider) EvaluateQuery(ctx context.Context, metric me
}

func (d *keptnDynatraceDQLProvider) EvaluateQueryForStep(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) ([]string, []byte, error) {
if err := d.ensureDTClientIsSetUp(ctx, provider); err != nil {
results, err := d.getResults(ctx, metric, provider)
if err != nil {
return nil, nil, err
}

b, status, err := d.postDQL(ctx, metric)
r := d.getResultSlice(results)
b, err := json.Marshal(results)
if err != nil {
d.log.Error(err, "Error while posting the DQL query", "query", metric.Spec.Query)
return nil, nil, err
d.log.Error(err, "Error marshaling DQL results")
}

results, err := d.parseDQLResults(b, status)
if err != nil {
return nil, nil, err
return r, b, nil
}

func (d *keptnDynatraceDQLProvider) getResults(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (*DQLResult, error) {
if err := d.ensureDTClientIsSetUp(ctx, provider); err != nil {
return nil, err
}

r := d.getResultSlice(results)
b, err = json.Marshal(results)
b, status, err := d.postDQL(ctx, metric)
if err != nil {
d.log.Error(err, "Error marshaling DQL results")
d.log.Error(err, "Error while posting the DQL query", "query", metric.Spec.Query)
return nil, err
}

return r, b, nil
results, err := d.parseDQLResults(b, status)
if err != nil {
return nil, err
}
return results, nil
}

func (d *keptnDynatraceDQLProvider) parseDQLResults(b []byte, status int) (*DQLResult, error) {
Expand Down

0 comments on commit 0f06f37

Please sign in to comment.