|
| 1 | +package providers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + newrelicInsightsDefaultHost = "https://insights-api.newrelic.com" |
| 16 | + |
| 17 | + newrelicQueryKeySecretKey = "newrelic_query_key" |
| 18 | + newrelicAccountIdSecretKey = "newrelic_account_id" |
| 19 | + |
| 20 | + newrelicQueryKeyHeaderKey = "X-Query-Key" |
| 21 | +) |
| 22 | + |
| 23 | +// NewRelicProvider executes newrelic queries |
| 24 | +type NewRelicProvider struct { |
| 25 | + insightsQueryEndpoint string |
| 26 | + |
| 27 | + timeout time.Duration |
| 28 | + queryKey string |
| 29 | + fromDelta int64 |
| 30 | +} |
| 31 | + |
| 32 | +type newRelicResponse struct { |
| 33 | + Results []struct { |
| 34 | + Result *float64 `json:"result"` |
| 35 | + } `json:"results"` |
| 36 | +} |
| 37 | + |
| 38 | +// NewNewRelicProvider takes a canary spec, a provider spec and the credentials map, and |
| 39 | +// returns a NewRelic client ready to execute queries against the Insights API |
| 40 | +func NewNewRelicProvider( |
| 41 | + metricInterval string, |
| 42 | + provider flaggerv1.MetricTemplateProvider, |
| 43 | + credentials map[string][]byte, |
| 44 | +) (*NewRelicProvider, error) { |
| 45 | + address := provider.Address |
| 46 | + if address == "" { |
| 47 | + address = newrelicInsightsDefaultHost |
| 48 | + } |
| 49 | + |
| 50 | + accountId, ok := credentials[newrelicAccountIdSecretKey] |
| 51 | + if !ok { |
| 52 | + return nil, fmt.Errorf("newrelic credentials does not contain " + newrelicAccountIdSecretKey) |
| 53 | + } |
| 54 | + |
| 55 | + queryEndpoint := fmt.Sprintf("%s/v1/accounts/%s/query", address, accountId) |
| 56 | + nr := NewRelicProvider{ |
| 57 | + timeout: 5 * time.Second, |
| 58 | + insightsQueryEndpoint: queryEndpoint, |
| 59 | + } |
| 60 | + |
| 61 | + if b, ok := credentials[newrelicQueryKeySecretKey]; ok { |
| 62 | + nr.queryKey = string(b) |
| 63 | + } else { |
| 64 | + return nil, fmt.Errorf("newrelic credentials does not contain " + newrelicQueryKeySecretKey) |
| 65 | + } |
| 66 | + |
| 67 | + md, err := time.ParseDuration(metricInterval) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("error parsing metric interval: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + nr.fromDelta = int64(md.Seconds()) |
| 73 | + return &nr, nil |
| 74 | +} |
| 75 | + |
| 76 | +// RunQuery executes the new relic query against the New Relic Insights API |
| 77 | +// and returns the the first result |
| 78 | +func (p *NewRelicProvider) RunQuery(query string) (float64, error) { |
| 79 | + req, err := p.newInsightsRequest(query) |
| 80 | + if err != nil { |
| 81 | + return 0, err |
| 82 | + } |
| 83 | + |
| 84 | + ctx, cancel := context.WithTimeout(req.Context(), p.timeout) |
| 85 | + defer cancel() |
| 86 | + r, err := http.DefaultClient.Do(req.WithContext(ctx)) |
| 87 | + if err != nil { |
| 88 | + return 0, fmt.Errorf("request failed: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + defer r.Body.Close() |
| 92 | + b, err := ioutil.ReadAll(r.Body) |
| 93 | + if err != nil { |
| 94 | + return 0, fmt.Errorf("error reading body: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + if r.StatusCode != http.StatusOK { |
| 98 | + return 0, fmt.Errorf("error response: %s: %w", string(b), err) |
| 99 | + } |
| 100 | + |
| 101 | + var res newRelicResponse |
| 102 | + if err := json.Unmarshal(b, &res); err != nil { |
| 103 | + return 0, fmt.Errorf("error unmarshaling result: %w, '%s'", err, string(b)) |
| 104 | + } |
| 105 | + |
| 106 | + if len(res.Results) != 1 { |
| 107 | + return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound) |
| 108 | + } |
| 109 | + |
| 110 | + if res.Results[0].Result == nil { |
| 111 | + return 0, fmt.Errorf("invalid response: %s: %w", string(b), ErrNoValuesFound) |
| 112 | + } |
| 113 | + |
| 114 | + return *res.Results[0].Result, nil |
| 115 | +} |
| 116 | + |
| 117 | +// IsOnline calls the NewRelic's insights API with |
| 118 | +// and returns an error if the request is rejected |
| 119 | +func (p *NewRelicProvider) IsOnline() (bool, error) { |
| 120 | + req, err := p.newInsightsRequest("SELECT * FROM Metric") |
| 121 | + if err != nil { |
| 122 | + return false, fmt.Errorf("error http.NewRequest: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + ctx, cancel := context.WithTimeout(req.Context(), p.timeout) |
| 126 | + defer cancel() |
| 127 | + r, err := http.DefaultClient.Do(req.WithContext(ctx)) |
| 128 | + if err != nil { |
| 129 | + return false, fmt.Errorf("request failed: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + defer r.Body.Close() |
| 133 | + |
| 134 | + b, err := ioutil.ReadAll(r.Body) |
| 135 | + if err != nil { |
| 136 | + return false, fmt.Errorf("error reading body: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + if r.StatusCode != http.StatusOK { |
| 140 | + return false, fmt.Errorf("error response: %s", string(b)) |
| 141 | + } |
| 142 | + |
| 143 | + return true, nil |
| 144 | +} |
| 145 | + |
| 146 | +func (p *NewRelicProvider) newInsightsRequest(query string) (*http.Request, error) { |
| 147 | + req, err := http.NewRequest("GET", p.insightsQueryEndpoint, nil) |
| 148 | + if err != nil { |
| 149 | + return nil, fmt.Errorf("error http.NewRequest: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + req.Header.Set(newrelicQueryKeyHeaderKey, p.queryKey) |
| 153 | + |
| 154 | + q := req.URL.Query() |
| 155 | + q.Add("nrql", fmt.Sprintf("%s SINCE %d seconds ago", query, p.fromDelta)) |
| 156 | + req.URL.RawQuery = q.Encode() |
| 157 | + |
| 158 | + return req, nil |
| 159 | +} |
0 commit comments