MCPcopy Index your code
hub / github.com/cortexproject/cortex / GetPrometheusRules

Method GetPrometheusRules

integration/e2ecortex/client.go:729–783  ·  view source on GitHub ↗

GetPrometheusRules fetches the rules from the Prometheus endpoint /api/v1/rules.

(filter RuleFilter)

Source from the content-addressed store, hash-verified

727
728// GetPrometheusRules fetches the rules from the Prometheus endpoint /api/v1/rules.
729func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, string, error) {
730 // Create HTTP request
731
732 req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/prom/api/v1/rules", c.rulerAddress), nil)
733 if err != nil {
734 return nil, "", err
735 }
736 req.Header.Set("X-Scope-OrgID", c.orgID)
737
738 urlValues := req.URL.Query()
739 addQueryParams(urlValues, "file[]", filter.Namespaces...)
740 addQueryParams(urlValues, "rule_name[]", filter.RuleNames...)
741 addQueryParams(urlValues, "rule_group[]", filter.RuleGroupNames...)
742 addQueryParams(urlValues, "type", filter.RuleType)
743 addQueryParams(urlValues, "exclude_alerts", filter.ExcludeAlerts)
744 if filter.MaxRuleGroup > 0 {
745 addQueryParams(urlValues, "group_limit", strconv.Itoa(filter.MaxRuleGroup))
746 }
747 if filter.NextToken != "" {
748 addQueryParams(urlValues, "group_next_token", filter.NextToken)
749 }
750 req.URL.RawQuery = urlValues.Encode()
751
752 ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
753 defer cancel()
754
755 // Execute HTTP request
756 res, err := c.httpClient.Do(req.WithContext(ctx))
757 if err != nil {
758 return nil, "", err
759 }
760 defer res.Body.Close()
761
762 body, err := io.ReadAll(res.Body)
763 if err != nil {
764 return nil, "", err
765 }
766
767 // Decode the response.
768 type response struct {
769 Status string `json:"status"`
770 Data ruler.RuleDiscovery `json:"data"`
771 }
772
773 decoded := &response{}
774 if err := json.Unmarshal(body, decoded); err != nil {
775 return nil, "", err
776 }
777
778 if decoded.Status != "success" {
779 return nil, "", fmt.Errorf("unexpected response status '%s'", decoded.Status)
780 }
781
782 return decoded.Data.RuleGroups, decoded.Data.GroupNextToken, nil
783}
784
785// GetRuleGroups gets the configured rule groups from the ruler.
786func (c *Client) GetRuleGroups() (map[string][]rulefmt.RuleGroup, error) {

Calls 7

addQueryParamsFunction · 0.85
SetMethod · 0.65
QueryMethod · 0.65
EncodeMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
UnmarshalMethod · 0.45