|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package providers |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "net/http" |
| 26 | + "strconv" |
| 27 | + "time" |
| 28 | + |
| 29 | + flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + nerdGraphDefaultHost = "https://api.newrelic.com/graphql" |
| 34 | + nerdGraphAPIKeySecretKey = "newrelic_api_key" |
| 35 | + nerdGraphAccountIDSecretKey = "newrelic_account_id" |
| 36 | + nerdGraphAPIKeyHeaderKey = "Api-Key" |
| 37 | + nerdGraphContentTypeHeader = "application/json" |
| 38 | + |
| 39 | + nerdGraphRunQueryTemplate = ` |
| 40 | + query ($query: Nrql!) { |
| 41 | + actor { |
| 42 | + account(id: %s) { |
| 43 | + nrql(query: $query) { |
| 44 | + results |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }` |
| 49 | +) |
| 50 | + |
| 51 | +// NerdGraphProvider executes New Relic NerdGraph (GraphQL) queries |
| 52 | +type NerdGraphProvider struct { |
| 53 | + endpoint string |
| 54 | + timeout time.Duration |
| 55 | + apiKey string |
| 56 | + accountID string |
| 57 | + fromDelta int64 |
| 58 | +} |
| 59 | + |
| 60 | +// nerdGraphPayload is the JSON payload for a GraphQL request |
| 61 | +type nerdGraphPayload struct { |
| 62 | + Query string `json:"query"` |
| 63 | + Variables map[string]any `json:"variables,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +// nerdGraphGQLResponse is the generic wrapper for a GraphQL response |
| 67 | +type nerdGraphGQLResponse struct { |
| 68 | + Data map[string]any `json:"data"` |
| 69 | + Errors []nerdGraphError `json:"errors"` |
| 70 | +} |
| 71 | + |
| 72 | +// nerdGraphError represents a single error in a GraphQL response |
| 73 | +type nerdGraphError struct { |
| 74 | + Message string `json:"message"` |
| 75 | +} |
| 76 | + |
| 77 | +// NewNerdGraphProvider takes a canary spec, a provider spec and the credentials map, and |
| 78 | +// returns a NewRelic client ready to execute queries against the NerdGraph API |
| 79 | +func NewNerdGraphProvider( |
| 80 | + metricInterval string, |
| 81 | + provider flaggerv1.MetricTemplateProvider, |
| 82 | + credentials map[string][]byte, |
| 83 | +) (*NerdGraphProvider, error) { |
| 84 | + address := provider.Address |
| 85 | + if address == "" { |
| 86 | + address = nerdGraphDefaultHost |
| 87 | + } |
| 88 | + |
| 89 | + apiKey, ok := credentials[nerdGraphAPIKeySecretKey] |
| 90 | + if !ok { |
| 91 | + return nil, fmt.Errorf("newrelic credentials does not contain the key '%s'", nerdGraphAPIKeySecretKey) |
| 92 | + } |
| 93 | + |
| 94 | + accountIDBytes, ok := credentials[nerdGraphAccountIDSecretKey] |
| 95 | + if !ok { |
| 96 | + return nil, fmt.Errorf("newrelic credentials does not contain the key '%s'", nerdGraphAccountIDSecretKey) |
| 97 | + } |
| 98 | + accountID := string(accountIDBytes) |
| 99 | + if _, err := strconv.Atoi(accountID); err != nil { |
| 100 | + return nil, fmt.Errorf("newrelic account ID '%s' is not a valid integer: %w", accountID, err) |
| 101 | + } |
| 102 | + |
| 103 | + md, err := time.ParseDuration(metricInterval) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("error parsing metric interval: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + return &NerdGraphProvider{ |
| 109 | + timeout: 5 * time.Second, |
| 110 | + endpoint: address, |
| 111 | + apiKey: string(apiKey), |
| 112 | + accountID: accountID, |
| 113 | + fromDelta: int64(md.Seconds()), |
| 114 | + }, nil |
| 115 | +} |
| 116 | + |
| 117 | +// RunQuery executes the NerdGraph query and returns the first numeric result |
| 118 | +func (p *NerdGraphProvider) RunQuery(query string) (float64, error) { |
| 119 | + since := strconv.FormatInt(p.fromDelta, 10) |
| 120 | + fullQuery := fmt.Sprintf("%s SINCE %s SECONDS ago", query, since) |
| 121 | + queryTemplate := fmt.Sprintf(nerdGraphRunQueryTemplate, p.accountID) |
| 122 | + |
| 123 | + payload := nerdGraphPayload{ |
| 124 | + Query: queryTemplate, |
| 125 | + Variables: map[string]any{ |
| 126 | + "query": fullQuery, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + payloadBytes, err := json.Marshal(payload) |
| 131 | + if err != nil { |
| 132 | + return 0, fmt.Errorf("error marshalling payload: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + req, err := p.newNerdGraphRequest(payloadBytes) |
| 136 | + if err != nil { |
| 137 | + return 0, err |
| 138 | + } |
| 139 | + |
| 140 | + ctx, cancel := context.WithTimeout(req.Context(), p.timeout) |
| 141 | + defer cancel() |
| 142 | + r, err := http.DefaultClient.Do(req.WithContext(ctx)) |
| 143 | + if err != nil { |
| 144 | + return 0, fmt.Errorf("request failed: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + defer r.Body.Close() |
| 148 | + b, err := io.ReadAll(r.Body) |
| 149 | + if err != nil { |
| 150 | + return 0, fmt.Errorf("error reading body: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + if r.StatusCode != http.StatusOK { |
| 154 | + return 0, fmt.Errorf("error response: %s", string(b)) |
| 155 | + } |
| 156 | + |
| 157 | + var res nerdGraphGQLResponse |
| 158 | + if err := json.Unmarshal(b, &res); err != nil { |
| 159 | + return 0, fmt.Errorf("error unmarshaling result: %w, '%s'", err, string(b)) |
| 160 | + } |
| 161 | + |
| 162 | + if len(res.Errors) > 0 { |
| 163 | + return 0, fmt.Errorf("nerdgraph query failed: %s", res.Errors[0].Message) |
| 164 | + } |
| 165 | + |
| 166 | + if res.Data == nil { |
| 167 | + return 0, fmt.Errorf("invalid response, no data found: %s", string(b)) |
| 168 | + } |
| 169 | + |
| 170 | + val, err := findResultValue(res.Data) |
| 171 | + if err != nil { |
| 172 | + return 0, fmt.Errorf("error parsing nerdgraph response: %w, body: '%s'", err, string(b)) |
| 173 | + } |
| 174 | + |
| 175 | + return val, nil |
| 176 | +} |
| 177 | + |
| 178 | +// IsOnline checks if the NerdGraph API is reachable and credentials are valid |
| 179 | +func (p *NerdGraphProvider) IsOnline() (bool, error) { |
| 180 | + pingQuery := "{ actor { user { name } } }" |
| 181 | + payload := nerdGraphPayload{Query: pingQuery} |
| 182 | + payloadBytes, err := json.Marshal(payload) |
| 183 | + if err != nil { |
| 184 | + return false, fmt.Errorf("error marshaling ping query: %w", err) |
| 185 | + } |
| 186 | + |
| 187 | + req, err := p.newNerdGraphRequest(payloadBytes) |
| 188 | + if err != nil { |
| 189 | + return false, fmt.Errorf("error creating request: %w", err) |
| 190 | + } |
| 191 | + |
| 192 | + ctx, cancel := context.WithTimeout(req.Context(), p.timeout) |
| 193 | + defer cancel() |
| 194 | + r, err := http.DefaultClient.Do(req.WithContext(ctx)) |
| 195 | + if err != nil { |
| 196 | + return false, fmt.Errorf("request failed: %w", err) |
| 197 | + } |
| 198 | + defer r.Body.Close() |
| 199 | + |
| 200 | + b, err := io.ReadAll(r.Body) |
| 201 | + if err != nil { |
| 202 | + return false, fmt.Errorf("error reading body: %w", err) |
| 203 | + } |
| 204 | + |
| 205 | + if r.StatusCode != http.StatusOK { |
| 206 | + return false, fmt.Errorf("error response: %s", string(b)) |
| 207 | + } |
| 208 | + |
| 209 | + var res nerdGraphGQLResponse |
| 210 | + if err := json.Unmarshal(b, &res); err != nil { |
| 211 | + return false, fmt.Errorf("error unmarshaling response: %w, '%s'", err, string(b)) |
| 212 | + } |
| 213 | + |
| 214 | + if len(res.Errors) > 0 { |
| 215 | + return false, fmt.Errorf("nerdgraph query failed: %s", res.Errors[0].Message) |
| 216 | + } |
| 217 | + |
| 218 | + return true, nil |
| 219 | +} |
| 220 | + |
| 221 | +// newNerdGraphRequest creates a new HTTP POST request for the NerdGraph API |
| 222 | +func (p *NerdGraphProvider) newNerdGraphRequest(payload []byte) (*http.Request, error) { |
| 223 | + req, err := http.NewRequest("POST", p.endpoint, bytes.NewBuffer(payload)) |
| 224 | + if err != nil { |
| 225 | + return nil, fmt.Errorf("error creating http.NewRequest: %w", err) |
| 226 | + } |
| 227 | + |
| 228 | + req.Header.Set(nerdGraphAPIKeyHeaderKey, p.apiKey) |
| 229 | + req.Header.Set("Content-Type", nerdGraphContentTypeHeader) |
| 230 | + |
| 231 | + return req, nil |
| 232 | +} |
| 233 | + |
| 234 | +// findResultValue recursively searches a map for the first 'results' array, |
| 235 | +// then returns the first float64 value from the first object in that array. |
| 236 | +func findResultValue(data map[string]any) (float64, error) { |
| 237 | + for key, val := range data { |
| 238 | + if key == "results" { |
| 239 | + if results, ok := val.([]any); ok && len(results) > 0 { |
| 240 | + if firstResult, ok := results[0].(map[string]any); ok { |
| 241 | + for _, resultVal := range firstResult { |
| 242 | + if f, ok := resultVal.(float64); ok { |
| 243 | + return f, nil |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + if subMap, ok := val.(map[string]any); ok { |
| 251 | + if res, err := findResultValue(subMap); err == nil { |
| 252 | + return res, nil |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + return 0, ErrNoValuesFound |
| 258 | +} |
0 commit comments