query.go 11 KB

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