query.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/httputil"
  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. // RawQuery is a direct query to the prometheus client and returns the body of the response
  135. func (ctx *Context) RawQuery(query string) ([]byte, error) {
  136. u := ctx.Client.URL(epQuery, nil)
  137. q := u.Query()
  138. q.Set("query", query)
  139. u.RawQuery = q.Encode()
  140. req, err := http.NewRequest(http.MethodPost, u.String(), nil)
  141. if err != nil {
  142. return nil, err
  143. }
  144. // Set QueryContext name if non empty
  145. if ctx.name != "" {
  146. req = httputil.SetName(req, ctx.name)
  147. }
  148. req = httputil.SetQuery(req, query)
  149. // Note that the warnings return value from client.Do() is always nil using this
  150. // version of the prometheus client library. We parse the warnings out of the response
  151. // body after json decodidng completes.
  152. resp, body, _, err := ctx.Client.Do(context.Background(), req)
  153. if err != nil {
  154. if resp == nil {
  155. return nil, fmt.Errorf("query error: '%s' fetching query '%s'", err.Error(), query)
  156. }
  157. return nil, fmt.Errorf("query error %d: '%s' fetching query '%s'", resp.StatusCode, err.Error(), query)
  158. }
  159. // Unsuccessful Status Code, log body and status
  160. statusCode := resp.StatusCode
  161. statusText := http.StatusText(statusCode)
  162. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  163. return nil, CommErrorf("%d (%s) URL: '%s', Request Headers: '%s', Headers: '%s', Body: '%s' Query: '%s'", statusCode, statusText, req.URL, req.Header, httputil.HeaderString(resp.Header), body, query)
  164. }
  165. return body, err
  166. }
  167. func (ctx *Context) query(query string) (interface{}, prometheus.Warnings, error) {
  168. body, err := ctx.RawQuery(query)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. var toReturn interface{}
  173. err = json.Unmarshal(body, &toReturn)
  174. if err != nil {
  175. return nil, nil, fmt.Errorf("Unmarshal Error: %s\nQuery: %s", err, query)
  176. }
  177. warnings := warningsFrom(toReturn)
  178. for _, w := range warnings {
  179. // NoStoreAPIWarning is a warning that we would consider an error. It returns partial data relating only to the
  180. // store apis which were reachable. In order to ensure integrity of data across all clusters, we'll need to identify
  181. // this warning and convert it to an error.
  182. if IsNoStoreAPIWarning(w) {
  183. return nil, warnings, CommErrorf("Error: %s, Body: %s, Query: %s", w, body, query)
  184. }
  185. log.Warningf("fetching query '%s': %s", query, w)
  186. }
  187. return toReturn, warnings, nil
  188. }
  189. func (ctx *Context) QueryRange(query string, start, end time.Time, step time.Duration) QueryResultsChan {
  190. resCh := make(QueryResultsChan)
  191. go runQueryRange(query, start, end, step, ctx, resCh, "")
  192. return resCh
  193. }
  194. func (ctx *Context) ProfileQueryRange(query string, start, end time.Time, step time.Duration, profileLabel string) QueryResultsChan {
  195. resCh := make(QueryResultsChan)
  196. go runQueryRange(query, start, end, step, ctx, resCh, profileLabel)
  197. return resCh
  198. }
  199. func (ctx *Context) QueryRangeSync(query string, start, end time.Time, step time.Duration) ([]*QueryResult, prometheus.Warnings, error) {
  200. raw, warnings, err := ctx.queryRange(query, start, end, step)
  201. if err != nil {
  202. return nil, warnings, err
  203. }
  204. results := NewQueryResults(query, raw)
  205. if results.Error != nil {
  206. return nil, warnings, results.Error
  207. }
  208. return results.Results, warnings, nil
  209. }
  210. // QueryRangeURL returns the URL used to query_range Prometheus
  211. func (ctx *Context) QueryRangeURL() *url.URL {
  212. return ctx.Client.URL(epQueryRange, nil)
  213. }
  214. // runQueryRange executes the prometheus queryRange asynchronously, collects results and
  215. // errors, and passes them through the results channel.
  216. func runQueryRange(query string, start, end time.Time, step time.Duration, ctx *Context, resCh QueryResultsChan, profileLabel string) {
  217. defer errors.HandlePanic()
  218. startQuery := time.Now()
  219. raw, warnings, requestError := ctx.queryRange(query, start, end, step)
  220. results := NewQueryResults(query, raw)
  221. // report all warnings, request, and parse errors (nils will be ignored)
  222. ctx.errorCollector.Report(query, warnings, requestError, results.Error)
  223. if profileLabel != "" {
  224. log.Profile(startQuery, profileLabel)
  225. }
  226. resCh <- results
  227. }
  228. // RawQuery is a direct query to the prometheus client and returns the body of the response
  229. func (ctx *Context) RawQueryRange(query string, start, end time.Time, step time.Duration) ([]byte, error) {
  230. u := ctx.Client.URL(epQueryRange, nil)
  231. q := u.Query()
  232. q.Set("query", query)
  233. q.Set("start", start.Format(time.RFC3339Nano))
  234. q.Set("end", end.Format(time.RFC3339Nano))
  235. q.Set("step", strconv.FormatFloat(step.Seconds(), 'f', 3, 64))
  236. u.RawQuery = q.Encode()
  237. req, err := http.NewRequest(http.MethodPost, u.String(), nil)
  238. if err != nil {
  239. return nil, err
  240. }
  241. // Set QueryContext name if non empty
  242. if ctx.name != "" {
  243. req = httputil.SetName(req, ctx.name)
  244. }
  245. req = httputil.SetQuery(req, query)
  246. // Note that the warnings return value from client.Do() is always nil using this
  247. // version of the prometheus client library. We parse the warnings out of the response
  248. // body after json decodidng completes.
  249. resp, body, _, err := ctx.Client.Do(context.Background(), req)
  250. if err != nil {
  251. if resp == nil {
  252. return nil, fmt.Errorf("Error: %s, Body: %s Query: %s", err.Error(), body, query)
  253. }
  254. return nil, fmt.Errorf("%d (%s) Headers: %s Error: %s Body: %s Query: %s", resp.StatusCode, http.StatusText(resp.StatusCode), httputil.HeaderString(resp.Header), body, err.Error(), query)
  255. }
  256. // Unsuccessful Status Code, log body and status
  257. statusCode := resp.StatusCode
  258. statusText := http.StatusText(statusCode)
  259. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  260. return nil, CommErrorf("%d (%s) Headers: %s, Body: %s Query: %s", statusCode, statusText, httputil.HeaderString(resp.Header), body, query)
  261. }
  262. return body, err
  263. }
  264. func (ctx *Context) queryRange(query string, start, end time.Time, step time.Duration) (interface{}, prometheus.Warnings, error) {
  265. body, err := ctx.RawQueryRange(query, start, end, step)
  266. if err != nil {
  267. return nil, nil, err
  268. }
  269. var toReturn interface{}
  270. err = json.Unmarshal(body, &toReturn)
  271. if err != nil {
  272. return nil, nil, fmt.Errorf("Unmarshal Error: %s\nQuery: %s", err, query)
  273. }
  274. warnings := warningsFrom(toReturn)
  275. for _, w := range warnings {
  276. // NoStoreAPIWarning is a warning that we would consider an error. It returns partial data relating only to the
  277. // store apis which were reachable. In order to ensure integrity of data across all clusters, we'll need to identify
  278. // this warning and convert it to an error.
  279. if IsNoStoreAPIWarning(w) {
  280. return nil, warnings, CommErrorf("Error: %s, Body: %s, Query: %s", w, body, query)
  281. }
  282. log.Warningf("fetching query '%s': %s", query, w)
  283. }
  284. return toReturn, warnings, nil
  285. }
  286. // Extracts the warnings from the resulting json if they exist (part of the prometheus response api).
  287. func warningsFrom(result interface{}) prometheus.Warnings {
  288. var warnings prometheus.Warnings
  289. if resultMap, ok := result.(map[string]interface{}); ok {
  290. if warningProp, ok := resultMap["warnings"]; ok {
  291. if w, ok := warningProp.([]string); ok {
  292. warnings = w
  293. }
  294. }
  295. }
  296. return warnings
  297. }