forked from lablabs/cloudflare-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
TRAC-469: add support for wae metrics #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| const waeAPIBase = "https://api.cloudflare.com/client/v4/accounts" | ||
|
|
||
| // WAEClient queries the Workers Analytics Engine SQL API. | ||
| type WAEClient struct { | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| // WAEResponse is the JSON response from the WAE SQL API. | ||
| // This is NOT the standard Cloudflare v4 API envelope. | ||
| type WAEResponse struct { | ||
| Meta []WAEColumnMeta `json:"meta"` | ||
| Data []map[string]interface{} `json:"data"` | ||
| Rows int `json:"rows"` | ||
| } | ||
|
|
||
| type WAEColumnMeta struct { | ||
| Name string `json:"name"` | ||
| Type string `json:"type"` | ||
| } | ||
|
|
||
| func NewWAEClient(httpClient *http.Client) *WAEClient { | ||
| if httpClient == nil { | ||
| httpClient = http.DefaultClient | ||
| } | ||
| return &WAEClient{httpClient: httpClient} | ||
| } | ||
|
|
||
| // Query executes a SQL query against the WAE API for the given account. | ||
| func (w *WAEClient) Query(ctx context.Context, accountID, query string) (*WAEResponse, error) { | ||
| url := fmt.Sprintf("%s/%s/analytics_engine/sql", waeAPIBase, accountID) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(query)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating WAE request: %w", err) | ||
| } | ||
|
|
||
| log.Debugf("WAE query (account=%s): %s", accountID, query) | ||
|
|
||
| resp, err := w.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("executing WAE query: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading WAE response: %w", err) | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("WAE query failed (HTTP %d): %s", resp.StatusCode, body) | ||
| } | ||
|
|
||
| var result WAEResponse | ||
| if err := json.Unmarshal(body, &result); err != nil { | ||
| return nil, fmt.Errorf("parsing WAE response: %w", err) | ||
| } | ||
|
|
||
| return &result, nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍹 I would also say that it may be inaccurate as well. We've seen where KV reports "bad" metrics, as in high latency, due to a high-ish aggregation window.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, honestly we'll see how it goes. we can always make this value configurable in case we need to tweak it w/o code change