query.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package prom
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "github.com/kubecost/cost-model/pkg/errors"
  10. "github.com/kubecost/cost-model/pkg/log"
  11. "github.com/kubecost/cost-model/pkg/util"
  12. "github.com/kubecost/cost-model/pkg/util/json"
  13. prometheus "github.com/prometheus/client_golang/api"
  14. )
  15. const (
  16. apiPrefix = "/api/v1"
  17. epQuery = apiPrefix + "/query"
  18. epQueryRange = apiPrefix + "/query_range"
  19. )
  20. // Context wraps a Prometheus client and provides methods for querying and
  21. // parsing query responses and errors.
  22. type Context struct {
  23. Client prometheus.Client
  24. errorCollector *QueryErrorCollector
  25. }
  26. // NewContext creates a new Promethues querying context from the given client
  27. func NewContext(client prometheus.Client) *Context {
  28. var ec QueryErrorCollector
  29. return &Context{
  30. Client: client,
  31. errorCollector: &ec,
  32. }
  33. }
  34. // Warnings returns the warnings collected from the Context's ErrorCollector
  35. func (ctx *Context) Warnings() []*QueryWarning {
  36. return ctx.errorCollector.Warnings()
  37. }
  38. // HasWarnings returns true if the ErrorCollector has warnings.
  39. func (ctx *Context) HasWarnings() bool {
  40. return ctx.errorCollector.IsWarning()
  41. }
  42. // Errors returns the errors collected from the Context's ErrorCollector.
  43. func (ctx *Context) Errors() []*QueryError {
  44. return ctx.errorCollector.Errors()
  45. }
  46. // HasErrors returns true if the ErrorCollector has errors
  47. func (ctx *Context) HasErrors() bool {
  48. return ctx.errorCollector.IsError()
  49. }
  50. // ErrorCollection returns the aggregation of errors if there exists errors. Otherwise,
  51. // nil is returned
  52. func (ctx *Context) ErrorCollection() error {
  53. if ctx.errorCollector.IsError() {
  54. // errorCollector implements the error interface
  55. return ctx.errorCollector
  56. }
  57. return nil
  58. }
  59. // Query returns a QueryResultsChan, then runs the given query and sends the
  60. // results on the provided channel. Receiver is responsible for closing the
  61. // channel, preferably using the Read method.
  62. func (ctx *Context) Query(query string) QueryResultsChan {
  63. resCh := make(QueryResultsChan)
  64. go runQuery(query, ctx, resCh, "")
  65. return resCh
  66. }
  67. // ProfileQuery returns a QueryResultsChan, then runs the given query with a profile
  68. // label and sends the results on the provided channel. Receiver is responsible for closing the
  69. // channel, preferably using the Read method.
  70. func (ctx *Context) ProfileQuery(query string, profileLabel string) QueryResultsChan {
  71. resCh := make(QueryResultsChan)
  72. go runQuery(query, ctx, resCh, profileLabel)
  73. return resCh
  74. }
  75. // QueryAll returns one QueryResultsChan for each query provided, then runs
  76. // each query concurrently and returns results on each channel, respectively,
  77. // in the order they were provided; i.e. the response to queries[1] will be
  78. // sent on channel resChs[1].
  79. func (ctx *Context) QueryAll(queries ...string) []QueryResultsChan {
  80. resChs := []QueryResultsChan{}
  81. for _, q := range queries {
  82. resChs = append(resChs, ctx.Query(q))
  83. }
  84. return resChs
  85. }
  86. // ProfileQueryAll returns one QueryResultsChan for each query provided, then runs
  87. // each ProfileQuery concurrently and returns results on each channel, respectively,
  88. // in the order they were provided; i.e. the response to queries[1] will be
  89. // sent on channel resChs[1].
  90. func (ctx *Context) ProfileQueryAll(queries ...string) []QueryResultsChan {
  91. resChs := []QueryResultsChan{}
  92. for _, q := range queries {
  93. resChs = append(resChs, ctx.ProfileQuery(q, fmt.Sprintf("Query #%d", len(resChs)+1)))
  94. }
  95. return resChs
  96. }
  97. func (ctx *Context) QuerySync(query string) ([]*QueryResult, prometheus.Warnings, error) {
  98. raw, warnings, err := ctx.query(query)
  99. if err != nil {
  100. return nil, warnings, err
  101. }
  102. results := NewQueryResults(query, raw)
  103. if results.Error != nil {
  104. return nil, warnings, results.Error
  105. }
  106. return results.Results, warnings, nil
  107. }
  108. // QueryURL returns the URL used to query Prometheus
  109. func (ctx *Context) QueryURL() *url.URL {
  110. return ctx.Client.URL(epQuery, nil)
  111. }
  112. // runQuery executes the prometheus query asynchronously, collects results and
  113. // errors, and passes them through the results channel.
  114. func runQuery(query string, ctx *Context, resCh QueryResultsChan, profileLabel string) {
  115. defer errors.HandlePanic()
  116. startQuery := time.Now()
  117. raw, warnings, requestError := ctx.query(query)
  118. results := NewQueryResults(query, raw)
  119. // report all warnings, request, and parse errors (nils will be ignored)
  120. ctx.errorCollector.Report(query, warnings, requestError, results.Error)
  121. if profileLabel != "" {
  122. log.Profile(startQuery, profileLabel)
  123. }
  124. resCh <- results
  125. }
  126. func (ctx *Context) query(query string) (interface{}, prometheus.Warnings, error) {
  127. u := ctx.Client.URL(epQuery, nil)
  128. q := u.Query()
  129. q.Set("query", query)
  130. u.RawQuery = q.Encode()
  131. req, err := http.NewRequest(http.MethodPost, u.String(), nil)
  132. if err != nil {
  133. return nil, nil, err
  134. }
  135. resp, body, warnings, err := ctx.Client.Do(context.Background(), req)
  136. for _, w := range warnings {
  137. // NoStoreAPIWarning is a warning that we would consider an error. It returns partial data relating only to the
  138. // store apis which were reachable. In order to ensure integrity of data across all clusters, we'll need to identify
  139. // this warning and convert it to an error.
  140. if IsNoStoreAPIWarning(w) {
  141. return nil, warnings, NewCommError(fmt.Sprintf("Error: %s, Body: %s, Query: %s", w, body, query))
  142. }
  143. log.Warningf("fetching query '%s': %s", query, w)
  144. }
  145. if err != nil {
  146. if resp == nil {
  147. return nil, warnings, fmt.Errorf("query error: '%s' fetching query '%s'", err.Error(), query)
  148. }
  149. return nil, warnings, fmt.Errorf("query error %d: '%s' fetching query '%s'", resp.StatusCode, err.Error(), query)
  150. }
  151. // Unsuccessful Status Code, log body and status
  152. statusCode := resp.StatusCode
  153. statusText := http.StatusText(statusCode)
  154. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  155. return nil, warnings, CommErrorf("%d (%s) URL: '%s', Request Headers: '%s', Headers: '%s', Body: '%s' Query: '%s'", statusCode, statusText, req.URL, req.Header, util.HeaderString(resp.Header), body, query)
  156. }
  157. var toReturn interface{}
  158. err = json.Unmarshal(body, &toReturn)
  159. if err != nil {
  160. return nil, warnings, fmt.Errorf("query error: '%s' fetching query '%s'", err.Error(), query)
  161. }
  162. return toReturn, warnings, nil
  163. }
  164. func (ctx *Context) QueryRange(query string, start, end time.Time, step time.Duration) QueryResultsChan {
  165. resCh := make(QueryResultsChan)
  166. go runQueryRange(query, start, end, step, ctx, resCh, "")
  167. return resCh
  168. }
  169. func (ctx *Context) ProfileQueryRange(query string, start, end time.Time, step time.Duration, profileLabel string) QueryResultsChan {
  170. resCh := make(QueryResultsChan)
  171. go runQueryRange(query, start, end, step, ctx, resCh, profileLabel)
  172. return resCh
  173. }
  174. func (ctx *Context) QueryRangeSync(query string, start, end time.Time, step time.Duration) ([]*QueryResult, prometheus.Warnings, error) {
  175. raw, warnings, err := ctx.queryRange(query, start, end, step)
  176. if err != nil {
  177. return nil, warnings, err
  178. }
  179. results := NewQueryResults(query, raw)
  180. if results.Error != nil {
  181. return nil, warnings, results.Error
  182. }
  183. return results.Results, warnings, nil
  184. }
  185. // QueryRangeURL returns the URL used to query_range Prometheus
  186. func (ctx *Context) QueryRangeURL() *url.URL {
  187. return ctx.Client.URL(epQueryRange, nil)
  188. }
  189. // runQueryRange executes the prometheus queryRange asynchronously, collects results and
  190. // errors, and passes them through the results channel.
  191. func runQueryRange(query string, start, end time.Time, step time.Duration, ctx *Context, resCh QueryResultsChan, profileLabel string) {
  192. defer errors.HandlePanic()
  193. startQuery := time.Now()
  194. raw, warnings, requestError := ctx.queryRange(query, start, end, step)
  195. results := NewQueryResults(query, raw)
  196. // report all warnings, request, and parse errors (nils will be ignored)
  197. ctx.errorCollector.Report(query, warnings, requestError, results.Error)
  198. if profileLabel != "" {
  199. log.Profile(startQuery, profileLabel)
  200. }
  201. resCh <- results
  202. }
  203. func (ctx *Context) queryRange(query string, start, end time.Time, step time.Duration) (interface{}, prometheus.Warnings, error) {
  204. u := ctx.Client.URL(epQueryRange, nil)
  205. q := u.Query()
  206. q.Set("query", query)
  207. q.Set("start", start.Format(time.RFC3339Nano))
  208. q.Set("end", end.Format(time.RFC3339Nano))
  209. q.Set("step", strconv.FormatFloat(step.Seconds(), 'f', 3, 64))
  210. u.RawQuery = q.Encode()
  211. req, err := http.NewRequest(http.MethodPost, u.String(), nil)
  212. if err != nil {
  213. return nil, nil, err
  214. }
  215. resp, body, warnings, err := ctx.Client.Do(context.Background(), req)
  216. for _, w := range warnings {
  217. // NoStoreAPIWarning is a warning that we would consider an error. It returns partial data relating only to the
  218. // store apis which were reachable. In order to ensure integrity of data across all clusters, we'll need to identify
  219. // this warning and convert it to an error.
  220. if IsNoStoreAPIWarning(w) {
  221. return nil, warnings, NewCommError(fmt.Sprintf("Error: %s, Body: %s, Query: %s", w, body, query))
  222. }
  223. log.Warningf("fetching query '%s': %s", query, w)
  224. }
  225. if err != nil {
  226. if resp == nil {
  227. return nil, warnings, fmt.Errorf("Error: %s, Body: %s Query: %s", err.Error(), body, query)
  228. }
  229. return nil, warnings, fmt.Errorf("%d (%s) Headers: %s Error: %s Body: %s Query: %s", resp.StatusCode, http.StatusText(resp.StatusCode), util.HeaderString(resp.Header), body, err.Error(), query)
  230. }
  231. // Unsuccessful Status Code, log body and status
  232. statusCode := resp.StatusCode
  233. statusText := http.StatusText(statusCode)
  234. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  235. return nil, warnings, CommErrorf("%d (%s) Headers: %s, Body: %s Query: %s", statusCode, statusText, util.HeaderString(resp.Header), body, query)
  236. }
  237. var toReturn interface{}
  238. err = json.Unmarshal(body, &toReturn)
  239. if err != nil {
  240. return nil, warnings, fmt.Errorf("%d (%s) Headers: %s Error: %s Body: %s Query: %s", statusCode, statusText, util.HeaderString(resp.Header), err.Error(), body, query)
  241. }
  242. return toReturn, warnings, nil
  243. }