query.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. var toReturn interface{}
  152. err = json.Unmarshal(body, &toReturn)
  153. if err != nil {
  154. return nil, warnings, fmt.Errorf("query error: '%s' fetching query '%s'", err.Error(), query)
  155. }
  156. return toReturn, warnings, nil
  157. }
  158. func (ctx *Context) QueryRange(query string, start, end time.Time, step time.Duration) QueryResultsChan {
  159. resCh := make(QueryResultsChan)
  160. go runQueryRange(query, start, end, step, ctx, resCh, "")
  161. return resCh
  162. }
  163. func (ctx *Context) ProfileQueryRange(query string, start, end time.Time, step time.Duration, profileLabel string) QueryResultsChan {
  164. resCh := make(QueryResultsChan)
  165. go runQueryRange(query, start, end, step, ctx, resCh, profileLabel)
  166. return resCh
  167. }
  168. func (ctx *Context) QueryRangeSync(query string, start, end time.Time, step time.Duration) ([]*QueryResult, prometheus.Warnings, error) {
  169. raw, warnings, err := ctx.queryRange(query, start, end, step)
  170. if err != nil {
  171. return nil, warnings, err
  172. }
  173. results := NewQueryResults(query, raw)
  174. if results.Error != nil {
  175. return nil, warnings, results.Error
  176. }
  177. return results.Results, warnings, nil
  178. }
  179. // QueryRangeURL returns the URL used to query_range Prometheus
  180. func (ctx *Context) QueryRangeURL() *url.URL {
  181. return ctx.Client.URL(epQueryRange, nil)
  182. }
  183. // runQueryRange executes the prometheus queryRange asynchronously, collects results and
  184. // errors, and passes them through the results channel.
  185. func runQueryRange(query string, start, end time.Time, step time.Duration, ctx *Context, resCh QueryResultsChan, profileLabel string) {
  186. defer errors.HandlePanic()
  187. startQuery := time.Now()
  188. raw, warnings, requestError := ctx.queryRange(query, start, end, step)
  189. results := NewQueryResults(query, raw)
  190. // report all warnings, request, and parse errors (nils will be ignored)
  191. ctx.errorCollector.Report(query, warnings, requestError, results.Error)
  192. if profileLabel != "" {
  193. log.Profile(startQuery, profileLabel)
  194. }
  195. resCh <- results
  196. }
  197. func (ctx *Context) queryRange(query string, start, end time.Time, step time.Duration) (interface{}, prometheus.Warnings, error) {
  198. u := ctx.Client.URL(epQueryRange, nil)
  199. q := u.Query()
  200. q.Set("query", query)
  201. q.Set("start", start.Format(time.RFC3339Nano))
  202. q.Set("end", end.Format(time.RFC3339Nano))
  203. q.Set("step", strconv.FormatFloat(step.Seconds(), 'f', 3, 64))
  204. u.RawQuery = q.Encode()
  205. req, err := http.NewRequest(http.MethodPost, u.String(), nil)
  206. if err != nil {
  207. return nil, nil, err
  208. }
  209. resp, body, warnings, err := ctx.Client.Do(context.Background(), req)
  210. for _, w := range warnings {
  211. // NoStoreAPIWarning is a warning that we would consider an error. It returns partial data relating only to the
  212. // store apis which were reachable. In order to ensure integrity of data across all clusters, we'll need to identify
  213. // this warning and convert it to an error.
  214. if IsNoStoreAPIWarning(w) {
  215. return nil, warnings, NewCommError(fmt.Sprintf("Error: %s, Body: %s, Query: %s", w, body, query))
  216. }
  217. log.Warningf("fetching query '%s': %s", query, w)
  218. }
  219. if err != nil {
  220. if resp == nil {
  221. return nil, warnings, fmt.Errorf("Error: %s, Body: %s Query: %s", err.Error(), body, query)
  222. }
  223. 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)
  224. }
  225. // Unsuccessful Status Code, log body and status
  226. statusCode := resp.StatusCode
  227. statusText := http.StatusText(statusCode)
  228. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  229. return nil, warnings, fmt.Errorf("%d (%s) Headers: %s, Body: %s Query: %s", statusCode, statusText, util.HeaderString(resp.Header), body, query)
  230. }
  231. var toReturn interface{}
  232. err = json.Unmarshal(body, &toReturn)
  233. if err != nil {
  234. 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)
  235. }
  236. return toReturn, warnings, nil
  237. }