ScrapeAndCompare calls a remote exporter's endpoint which is expected to return some metrics in plain text format. Then it compares it with the results that the `expected` would return. If the `metricNames` is not empty it would filter the comparison only to the given metric names. NOTE: Be mindful
(url string, expected io.Reader, metricNames ...string)
| 164 | // NOTE: Be mindful of accidental discrepancies between expected and metricNames; metricNames filter |
| 165 | // both expected and scraped metrics. See https://github.com/prometheus/client_golang/issues/1351. |
| 166 | func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) error { |
| 167 | resp, err := http.Get(url) |
| 168 | if err != nil { |
| 169 | return fmt.Errorf("scraping metrics failed: %w", err) |
| 170 | } |
| 171 | defer resp.Body.Close() |
| 172 | |
| 173 | if resp.StatusCode != http.StatusOK { |
| 174 | return fmt.Errorf("the scraping target returned a status code other than 200: %d", |
| 175 | resp.StatusCode) |
| 176 | } |
| 177 | |
| 178 | scraped, err := convertReaderToMetricFamily(resp.Body) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | wanted, err := convertReaderToMetricFamily(expected) |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | |
| 188 | return compareMetricFamilies(scraped, wanted, metricNames...) |
| 189 | } |
| 190 | |
| 191 | // CollectAndCompare collects the metrics identified by `metricNames` and compares them in the Prometheus text |
| 192 | // exposition format to the data read from expected. |