Skip to content
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

testutils/promrated: add prom query to collect pubkey to cluster information #1562

Merged
merged 7 commits into from
Dec 15, 2022

Conversation

LukeHackett12
Copy link
Contributor

Adding in prometheus query to get all the necessary cluster information and associated private keys.

category: misc
ticket: #1540

@codecov
Copy link

codecov bot commented Dec 14, 2022

Codecov Report

Base: 54.28% // Head: 54.10% // Decreases project coverage by -0.18% ⚠️

Coverage data is based on head (71aa332) compared to base (b3fe553).
Patch coverage: 38.66% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1562      +/-   ##
==========================================
- Coverage   54.28%   54.10%   -0.19%     
==========================================
  Files         153      156       +3     
  Lines       19436    19579     +143     
==========================================
+ Hits        10551    10593      +42     
- Misses       7455     7549      +94     
- Partials     1430     1437       +7     
Impacted Files Coverage Δ
testutil/promrated/promrated.go 0.00% <0.00%> (ø)
testutil/promrated/prometheus.go 42.02% <53.70%> (ø)
cmd/bootnode/p2p.go 38.06% <0.00%> (-3.85%) ⬇️
app/vmock.go 73.05% <0.00%> (-1.04%) ⬇️
cmd/bootnode/bootnode.go 56.31% <0.00%> (-0.43%) ⬇️
cmd/bootnode/metrics.go 33.33% <0.00%> (ø)
core/qbft/qbft.go 83.82% <0.00%> (+0.42%) ⬆️
core/priority/prioritiser.go 61.69% <0.00%> (+2.82%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

)

const (
promEndpoint = "https://vm.monitoring.gcp.obol.tech"
promQuery = "group%20by%20%28cluster_name%2C%20ccluster_hash%2C%20cluster_peer%2C%20cluster_network%2C%20pubkey_full%29%20%28core_scheduler_validator_balance_gwei%29"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write normal text, http libraries should handle escaping

)

const (
promEndpoint = "https://vm.monitoring.gcp.obol.tech"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best avoid globals, make this a flag with a default value

promQuery = "group%20by%20%28cluster_name%2C%20ccluster_hash%2C%20cluster_peer%2C%20cluster_network%2C%20pubkey_full%29%20%28core_scheduler_validator_balance_gwei%29"
)

type ClusterInfo struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always unexport names by default.

return nil, err
}

if res.StatusCode == 200 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


func getPromClusters(ctx context.Context, promAuth string) (*http.Response, error) {
client := new(http.Client)
url := fmt.Sprintf("%s/query?query=%s", promEndpoint, promQuery)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the url package to build the url (avoid unstructured untyped logic)


req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
log.Error(ctx, "HTTP Request malformed for prom query", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't log and return errors, rather just return errors and log at the root (the loop or processor that is doing the scheduling).

return res, nil
}

func readPromJSON(ctx context.Context, res *http.Response) (*PromResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is two function calls and pretty common go code, can inline. Also you need to close the request body.

return nil, errors.New("error processing prom metrics", z.Str("url", res.Request.RequestURI))
}

func getPromClusters(ctx context.Context, promAuth string) (*http.Response, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning a http response isn't a good abstraction, rather colocate http wrangling stuff and return proper business logic types

@@ -41,6 +81,73 @@ func serveMonitoring(addr string) error {
return errors.Wrap(server.ListenAndServe(), "failed to serve prometheus metrics")
}

func getPubkeyToClusterInfo(ctx context.Context, promAuth string) (map[string]prometheus.Labels, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPubkeyToClusterInfo name can improve, what about getClusters or getClustersFromProm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add godoc please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather return a proper type instead of map of strings. Also remember that clusters contain multiple validators. Suggest:

type Validator struct {
  PubkeyHex string
  ClusterName string
  ClusterHash string
  ClusterPeer string
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPubkeyToClusterInfo name can improve, what about getClusters or getClustersFromProm

Yeah this naming is what I was used to for map types, can change it

Metric PromMetric `json:"metric"`
}

type PromData struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for all these types, you can just define a single nested type since it is only used for unmarshalling. Also, only include fields that you are actually using.

type promResponse struct {
  Data struct {
      Result []struct{
           ...
      } `json:result`
  }  `json:"data"`
}

}

type PromMetric struct {
*ClusterInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid pointers

@LukeHackett12 LukeHackett12 added the merge when ready Indicates bulldozer bot may merge when all checks pass label Dec 15, 2022
@obol-bulldozer obol-bulldozer bot merged commit 532b2be into main Dec 15, 2022
@obol-bulldozer obol-bulldozer bot deleted the luke/prom-metrics branch December 15, 2022 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge when ready Indicates bulldozer bot may merge when all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants