thanos.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package prom
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. prometheus "github.com/prometheus/client_golang/api"
  9. )
  10. // MaxSourceResulution is the query parameter key used to designate the resolution
  11. // to use when executing a query.
  12. const MaxSourceResulution = "max_source_resolution"
  13. // NewThanosClient creates a new `prometheus.Client` with the specific thanos configuration, with a
  14. // thanos client identifier.
  15. func NewThanosClient(address string, thanosConfig *OpenCostThanosConfig) (prometheus.Client, error) {
  16. config := thanosConfig.ClientConfig
  17. tc := prometheus.Config{
  18. Address: address,
  19. RoundTripper: &http.Transport{
  20. Proxy: http.ProxyFromEnvironment,
  21. DialContext: (&net.Dialer{
  22. Timeout: config.Timeout,
  23. KeepAlive: config.KeepAlive,
  24. }).DialContext,
  25. TLSHandshakeTimeout: config.TLSHandshakeTimeout,
  26. TLSClientConfig: &tls.Config{
  27. InsecureSkipVerify: config.TLSInsecureSkipVerify,
  28. },
  29. },
  30. }
  31. client, err := prometheus.NewClient(tc)
  32. if err != nil {
  33. return nil, err
  34. }
  35. // max source resolution decorator
  36. maxSourceDecorator := func(path string, queryParams url.Values) url.Values {
  37. if strings.Contains(path, "query") {
  38. queryParams.Set(MaxSourceResulution, thanosConfig.MaxSourceResulution)
  39. }
  40. return queryParams
  41. }
  42. return NewRateLimitedClient(
  43. ThanosClientID,
  44. client,
  45. config.QueryConcurrency,
  46. config.Auth,
  47. maxSourceDecorator,
  48. config.RateLimitRetryOpts,
  49. config.QueryLogFile,
  50. "",
  51. )
  52. }