SetRuleGroup configures the provided rulegroup to the ruler.
(rulegroup rulefmt.RuleGroup, namespace string)
| 818 | |
| 819 | // SetRuleGroup configures the provided rulegroup to the ruler. |
| 820 | func (c *Client) SetRuleGroup(rulegroup rulefmt.RuleGroup, namespace string) error { |
| 821 | // Create write request |
| 822 | data, err := yaml.Marshal(rulegroup) |
| 823 | if err != nil { |
| 824 | return err |
| 825 | } |
| 826 | |
| 827 | // Create HTTP request |
| 828 | req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/api/prom/rules/%s", c.rulerAddress, url.PathEscape(namespace)), bytes.NewReader(data)) |
| 829 | if err != nil { |
| 830 | return err |
| 831 | } |
| 832 | |
| 833 | req.Header.Set("Content-Type", "application/yaml") |
| 834 | req.Header.Set("X-Scope-OrgID", c.orgID) |
| 835 | |
| 836 | ctx, cancel := context.WithTimeout(context.Background(), c.timeout) |
| 837 | defer cancel() |
| 838 | |
| 839 | // Execute HTTP request |
| 840 | res, err := c.httpClient.Do(req.WithContext(ctx)) |
| 841 | if err != nil { |
| 842 | return err |
| 843 | } |
| 844 | |
| 845 | defer res.Body.Close() |
| 846 | |
| 847 | if res.StatusCode != 202 { |
| 848 | return fmt.Errorf("unexpected status code: %d", res.StatusCode) |
| 849 | } |
| 850 | |
| 851 | return nil |
| 852 | } |
| 853 | |
| 854 | // GetRuleGroup gets a rule group. |
| 855 | func (c *Client) GetRuleGroup(namespace string, groupName string) (*http.Response, error) { |