Skip to content

Commit

Permalink
Merge pull request #1013 from GregoireW/testing
Browse files Browse the repository at this point in the history
Add dynatrace metric provider
  • Loading branch information
stefanprodan authored Sep 17, 2021
2 parents 418853f + 13a2a50 commit db72fe3
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 0 deletions.
1 change: 1 addition & 0 deletions charts/flagger/crds/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ spec:
- cloudwatch
- newrelic
- graphite
- dynatrace
address:
description: API address of this provider
type: string
Expand Down
48 changes: 48 additions & 0 deletions docs/gitbook/usage/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,51 @@ spec:
|> count()
|> yield(name: "count")
```
## Dynatrace
You can create custom metric checks using the Dynatrace provider.
Create a secret with your Dynatrace token:
```yaml
apiVersion: v1
kind: Secret
metadata:
name: dynatrace
namespace: istio-system
data:
dynatrace_token: ZHQwYz...
```
Dynatrace metric template example:
```yaml
apiVersion: flagger.app/v1beta1
kind: MetricTemplate
metadata:
name: response-time-95pct
namespace: istio-system
spec:
provider:
type: dynatrace
address: https://xxxxxxxx.live.dynatrace.com
secretRef:
name: dynatrace
query: |
builtin:service.response.time:filter(eq(dt.entity.service,SERVICE-ABCDEFG0123456789)):percentile(95)
```
Reference the template in the canary analysis:
```yaml
analysis:
metrics:
- name: "response-time-95pct"
templateRef:
name: response-time-95pct
namespace: istio-system
thresholdRange:
max: 1000
interval: 1m
```
1 change: 1 addition & 0 deletions kustomize/base/flagger/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ spec:
- cloudwatch
- newrelic
- graphite
- dynatrace
address:
description: API address of this provider
type: string
Expand Down
181 changes: 181 additions & 0 deletions pkg/metrics/providers/dynatrace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
Copyright 2020 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package providers

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"

flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1"
)

// https://www.dynatrace.com/support/help/dynatrace-api/environment-api/metric-v2/get-all-metrics/
const (
dynatraceMetricsQueryPath = "/api/v2/metrics/query"
dynatraceValidationPath = "/api/v2/metrics?pageSize=1"

dynatraceAPITokenSecretKey = "dynatrace_token"
dynatraceAuthorizationHeaderKey = "Authorization"
dynatraceAuthorizationHeaderType = "Api-Token"

dynatraceDeltaMultiplierOnMetricInterval = 10
)

// DynatraceProvider executes dynatrace queries
type DynatraceProvider struct {
metricsQueryEndpoint string
apiValidationEndpoint string

timeout time.Duration
token string
fromDelta int64
}

type dynatraceResponse struct {
Result []struct {
Data []struct {
Timestamps []int64 `json:"timestamps"`
Values []float64 `json:"values"`
} `json:"data"`
} `json:"result"`
}

// NewDynatraceProvider takes a canary spec, a provider spec and the credentials map, and
// returns a Dynatrace client ready to execute queries against the API
func NewDynatraceProvider(metricInterval string,
provider flaggerv1.MetricTemplateProvider,
credentials map[string][]byte) (*DynatraceProvider, error) {

address := provider.Address
if address == "" {
return nil, fmt.Errorf("dynatrace endpoint is not set")
}

dt := DynatraceProvider{
timeout: 5 * time.Second,
metricsQueryEndpoint: address + dynatraceMetricsQueryPath,
apiValidationEndpoint: address + dynatraceValidationPath,
}

if b, ok := credentials[dynatraceAPITokenSecretKey]; ok {
dt.token = string(b)
} else {
return nil, fmt.Errorf("dynatrace credentials does not contain dynatrace_token")
}

md, err := time.ParseDuration(metricInterval)
if err != nil {
return nil, fmt.Errorf("error parsing metric interval: %w", err)
}

dt.fromDelta = int64(dynatraceDeltaMultiplierOnMetricInterval * md.Milliseconds())
return &dt, nil
}

// RunQuery executes the dynatrace query against DynatraceProvider.metricsQueryEndpoint
// and returns the the first result as float64
func (p *DynatraceProvider) RunQuery(query string) (float64, error) {

req, err := http.NewRequest("GET", p.metricsQueryEndpoint, nil)
if err != nil {
return 0, fmt.Errorf("error http.NewRequest: %w", err)
}

req.Header.Set(dynatraceAuthorizationHeaderKey, fmt.Sprintf("%s %s", dynatraceAuthorizationHeaderType, p.token))

now := time.Now().Unix() * 1000
q := req.URL.Query()
q.Add("metricSelector", query)
q.Add("resolution", "Inf")
q.Add("from", strconv.FormatInt(now-p.fromDelta, 10))
q.Add("to", strconv.FormatInt(now, 10))
req.URL.RawQuery = q.Encode()

ctx, cancel := context.WithTimeout(req.Context(), p.timeout)
defer cancel()
r, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return 0, fmt.Errorf("request failed: %w", err)
}

defer r.Body.Close()
b, err := io.ReadAll(r.Body)
if err != nil {
return 0, fmt.Errorf("error reading body: %w", err)
}

if r.StatusCode != http.StatusOK {
return 0, fmt.Errorf("error response: %s: %w", string(b), err)
}

var res dynatraceResponse
if err := json.Unmarshal(b, &res); err != nil {
return 0, fmt.Errorf("error unmarshaling result: %w, '%s'", err, string(b))
}

if len(res.Result) < 1 {
return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound)
}

data := res.Result[0].Data
if len(data) < 1 {
return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound)
}

vs := data[len(data)-1]
if len(vs.Values) < 1 {
return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound)
}

return vs.Values[0], nil
}

// IsOnline calls the Dynatrace's metrics endpoint with token
// and returns an error if the endpoint fails
func (p *DynatraceProvider) IsOnline() (bool, error) {
req, err := http.NewRequest("GET", p.apiValidationEndpoint, nil)
if err != nil {
return false, fmt.Errorf("error http.NewRequest: %w", err)
}

req.Header.Set(dynatraceAuthorizationHeaderKey, fmt.Sprintf("%s %s", dynatraceAuthorizationHeaderType, p.token))

ctx, cancel := context.WithTimeout(req.Context(), p.timeout)
defer cancel()
r, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return false, fmt.Errorf("request failed: %w", err)
}

defer r.Body.Close()

b, err := io.ReadAll(r.Body)
if err != nil {
return false, fmt.Errorf("error reading body: %w", err)
}

if r.StatusCode != http.StatusOK {
return false, fmt.Errorf("error response: %s", string(b))
}

return true, nil
}
Loading

0 comments on commit db72fe3

Please sign in to comment.