-
Notifications
You must be signed in to change notification settings - Fork 721
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
[tmpnet] Add check for collection of logs and metrics to custom github action #3740
Open
maru-ava
wants to merge
4
commits into
tmpnet-managed-collectors
Choose a base branch
from
tmpnet-check-metrics-collection
base: tmpnet-managed-collectors
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+346
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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,249 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package tmpnet | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/api" | ||
"github.com/prometheus/client_golang/api/prometheus/v1" | ||
"github.com/prometheus/common/model" | ||
"go.uber.org/zap" | ||
|
||
"github.com/ava-labs/avalanchego/utils/logging" | ||
) | ||
|
||
type getCountFunc func() (int, error) | ||
|
||
// waitForCount waits until the provided function returns greater than zero. | ||
func waitForCount(ctx context.Context, log logging.Logger, name string, getCount getCountFunc) error { | ||
if err := pollUntilContextCancel(ctx, func(_ context.Context) (bool, error) { | ||
count, err := getCount() | ||
if err != nil { | ||
log.Warn("failed to query for "+name, | ||
zap.Error(err), | ||
) | ||
return false, nil | ||
} | ||
if count > 0 { | ||
log.Info(name+" exist", | ||
zap.Int("count", count), | ||
) | ||
} | ||
return count > 0, nil | ||
}); err != nil { | ||
return fmt.Errorf("%s not found before timeout: %w", name, err) | ||
} | ||
return nil | ||
} | ||
|
||
// CheckLogsExist checks if logs exist for the given network. Github labels are also | ||
// included if provided as env vars (GH_*). | ||
func CheckLogsExist(ctx context.Context, log logging.Logger, networkUUID string) error { | ||
username, password, err := getCollectorCredentials(promtailCmd) | ||
if err != nil { | ||
return fmt.Errorf("failed to get collector credentials: %w", err) | ||
} | ||
|
||
url := getLokiURL() | ||
|
||
selectors, err := getSelectors(networkUUID) | ||
if err != nil { | ||
return err | ||
} | ||
query := fmt.Sprintf("sum(count_over_time({%s}[1h]))", selectors) | ||
|
||
log.Info("checking if logs exist", | ||
zap.String("url", url), | ||
zap.String("query", query), | ||
) | ||
|
||
return waitForCount(ctx, log, "logs", func() (int, error) { | ||
return queryLoki(ctx, url, username, password, query) | ||
}) | ||
} | ||
|
||
func queryLoki( | ||
ctx context.Context, | ||
lokiURL string, | ||
username string, | ||
password string, | ||
query string, | ||
) (int, error) { | ||
// Compose the URL | ||
params := url.Values{} | ||
params.Add("query", query) | ||
reqURL := fmt.Sprintf("%s/loki/api/v1/query?%s", lokiURL, params.Encode()) | ||
|
||
// Create request | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | ||
req.Header.Set("Authorization", "Basic "+auth) | ||
|
||
// Execute request | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to execute request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Read and parse response | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to read response: %w", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return 0, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body)) | ||
} | ||
|
||
// Parse JSON response | ||
var result struct { | ||
Status string `json:"status"` | ||
Data struct { | ||
Result []struct { | ||
Value []interface{} `json:"value"` | ||
} `json:"result"` | ||
} `json:"data"` | ||
} | ||
|
||
if err := json.Unmarshal(body, &result); err != nil { | ||
return 0, fmt.Errorf("failed to parse response: %w", err) | ||
} | ||
|
||
// Extract count value | ||
if len(result.Data.Result) == 0 { | ||
return 0, nil | ||
} | ||
if len(result.Data.Result[0].Value) != 2 { | ||
return 0, errors.New("unexpected value format in response") | ||
} | ||
// Convert value to a string | ||
valueStr, ok := result.Data.Result[0].Value[1].(string) | ||
if !ok { | ||
return 0, errors.New("value is not a string") | ||
} | ||
// Convert string to float64 first to handle scientific notation | ||
floatVal, err := strconv.ParseFloat(valueStr, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("parsing count value: %w", err) | ||
} | ||
// Round to nearest integer | ||
return int(floatVal + 0.5), nil | ||
} | ||
|
||
// CheckMetricsExist checks if metrics exist for the given network. Github labels are also | ||
// included if provided as env vars (GH_*). | ||
func CheckMetricsExist(ctx context.Context, log logging.Logger, networkUUID string) error { | ||
username, password, err := getCollectorCredentials(prometheusCmd) | ||
if err != nil { | ||
return fmt.Errorf("failed to get collector credentials: %w", err) | ||
} | ||
|
||
url := getPrometheusURL() | ||
|
||
selectors, err := getSelectors(networkUUID) | ||
if err != nil { | ||
return err | ||
} | ||
query := fmt.Sprintf("count({%s})", selectors) | ||
|
||
log.Info("checking if metrics exist", | ||
zap.String("url", url), | ||
zap.String("query", query), | ||
) | ||
|
||
return waitForCount(ctx, log, "metrics", func() (int, error) { | ||
return queryPrometheus(ctx, log, url, username, password, query) | ||
}) | ||
} | ||
|
||
func queryPrometheus( | ||
ctx context.Context, | ||
log logging.Logger, | ||
url string, | ||
username string, | ||
password string, | ||
query string, | ||
) (int, error) { | ||
// Create client with basic auth | ||
client, err := api.NewClient(api.Config{ | ||
Address: url, | ||
RoundTripper: &basicAuthRoundTripper{ | ||
username: username, | ||
password: password, | ||
rt: api.DefaultRoundTripper, | ||
}, | ||
}) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to create client: %w", err) | ||
} | ||
|
||
// Query Prometheus | ||
result, warnings, err := v1.NewAPI(client).QueryRange(ctx, query, v1.Range{ | ||
Start: time.Now().Add(-time.Hour), | ||
End: time.Now(), | ||
Step: time.Minute, | ||
}) | ||
if err != nil { | ||
return 0, fmt.Errorf("query failed: %w", err) | ||
} | ||
if len(warnings) > 0 { | ||
log.Warn("prometheus query warnings", | ||
zap.Strings("warnings", warnings), | ||
) | ||
} | ||
|
||
if matrix, ok := result.(model.Matrix); !ok { | ||
return 0, fmt.Errorf("unexpected result type: %s", result.Type()) | ||
} else if len(matrix) > 0 { | ||
return int(matrix[0].Values[len(matrix[0].Values)-1].Value), nil | ||
} | ||
|
||
return 0, nil | ||
} | ||
|
||
type basicAuthRoundTripper struct { | ||
username, password string | ||
rt http.RoundTripper | ||
} | ||
|
||
func (b *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
req.SetBasicAuth(b.username, b.password) | ||
return b.rt.RoundTrip(req) | ||
} | ||
|
||
// getSelectors returns the comma-separated list of selectors. | ||
func getSelectors(networkUUID string) (string, error) { | ||
selectors := []string{} | ||
if len(networkUUID) > 0 { | ||
selectors = append(selectors, fmt.Sprintf(`network_uuid="%s"`, networkUUID)) | ||
} | ||
githubLabels := githubLabelsFromEnv() | ||
for label := range githubLabels { | ||
value, err := githubLabels.GetStringVal(label) | ||
if err != nil { | ||
return "", err | ||
} | ||
if len(value) == 0 { | ||
continue | ||
} | ||
selectors = append(selectors, fmt.Sprintf(`%s="%s"`, label, value)) | ||
} | ||
return strings.Join(selectors, ","), nil | ||
} |
This file contains 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
Oops, something went wrong.
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.
Why is
go.sum
changed with no corresponding change?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 think it's because of my use of the prometheus client. I didn't add any deps but the go mod job failed and this was the result of running 'go mod tidy'.