github.com/grafana/pyroscope@v1.18.0/pkg/objstore/providers/cos/config.go (about)

     1  package cos
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  	"time"
    10  
    11  	"github.com/grafana/dskit/flagext"
    12  )
    13  
    14  // Config encapsulates the necessary config values to instantiate an cos client.
    15  type Config struct {
    16  	Bucket    string         `yaml:"bucket"`
    17  	Region    string         `yaml:"region"`
    18  	AppID     string         `yaml:"app_id"`
    19  	Endpoint  string         `yaml:"endpoint"`
    20  	SecretKey flagext.Secret `yaml:"secret_key"`
    21  	SecretID  string         `yaml:"secret_id"`
    22  	HTTP      HTTPConfig     `yaml:"http"`
    23  }
    24  
    25  // Validate validates cos client config and returns error on failure
    26  func (c *Config) Validate() error {
    27  	if len(c.Endpoint) != 0 {
    28  		if _, err := url.Parse(c.Endpoint); err != nil {
    29  			return fmt.Errorf("cos config: failed to parse endpoint: %w", err)
    30  		}
    31  
    32  		if empty(c.SecretKey.String()) || empty(c.SecretID) {
    33  			return errors.New("secret id and secret key cannot be empty")
    34  		}
    35  		return nil
    36  	}
    37  
    38  	if empty(c.Bucket) || empty(c.AppID) || empty(c.Region) || empty(c.SecretID) || empty(c.SecretKey.String()) {
    39  		return errors.New("invalid cos configuration, bucket, app_id, region, secret_id and secret_key must be set")
    40  	}
    41  	return nil
    42  }
    43  
    44  func empty(s string) bool {
    45  	return len(s) == 0
    46  }
    47  
    48  // RegisterFlags registers the flags for COS storage
    49  func (c *Config) RegisterFlags(f *flag.FlagSet) {
    50  	c.RegisterFlagsWithPrefix("", f)
    51  }
    52  
    53  // RegisterFlagsWithPrefix register the flags for COS storage with provided prefix
    54  func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
    55  	f.StringVar(&c.Bucket, prefix+"cos.bucket", "", "COS bucket name")
    56  	f.StringVar(&c.Region, prefix+"cos.region", "", "COS region name")
    57  	f.StringVar(&c.AppID, prefix+"cos.app-id", "", "COS app id")
    58  	f.StringVar(&c.Endpoint, prefix+"cos.endpoint", "", "COS storage endpoint")
    59  	f.StringVar(&c.SecretID, prefix+"cos.secret-id", "", "COS secret id")
    60  	f.Var(&c.SecretKey, prefix+"cos.secret-key", "COS secret key")
    61  	c.HTTP.RegisterFlagsWithPrefix(prefix, f)
    62  }
    63  
    64  // HTTPConfig stores the http.Transport configuration for the COS client.
    65  type HTTPConfig struct {
    66  	IdleConnTimeout       time.Duration `yaml:"idle_conn_timeout" category:"advanced"`
    67  	ResponseHeaderTimeout time.Duration `yaml:"response_header_timeout" category:"advanced"`
    68  	InsecureSkipVerify    bool          `yaml:"insecure_skip_verify" category:"advanced"`
    69  	TLSHandshakeTimeout   time.Duration `yaml:"tls_handshake_timeout" category:"advanced"`
    70  	ExpectContinueTimeout time.Duration `yaml:"expect_continue_timeout" category:"advanced"`
    71  	MaxIdleConns          int           `yaml:"max_idle_connections" category:"advanced"`
    72  	MaxIdleConnsPerHost   int           `yaml:"max_idle_connections_per_host" category:"advanced"`
    73  	MaxConnsPerHost       int           `yaml:"max_connections_per_host" category:"advanced"`
    74  
    75  	// Allow upstream callers to inject a round tripper
    76  	Transport http.RoundTripper `yaml:"-"`
    77  }
    78  
    79  // RegisterFlagsWithPrefix registers the flags for COS storage with the provided prefix
    80  func (cfg *HTTPConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
    81  	f.DurationVar(&cfg.IdleConnTimeout, prefix+"cos.http.idle-conn-timeout", 90*time.Second, "The time an idle connection will remain idle before closing.")
    82  	f.DurationVar(&cfg.ResponseHeaderTimeout, prefix+"cos.http.response-header-timeout", 2*time.Minute, "The amount of time the client will wait for a servers response headers.")
    83  	f.BoolVar(&cfg.InsecureSkipVerify, prefix+"cos.http.insecure-skip-verify", false, "If the client connects to COS via HTTPS and this option is enabled, the client will accept any certificate and hostname.")
    84  	f.DurationVar(&cfg.TLSHandshakeTimeout, prefix+"cos.tls-handshake-timeout", 10*time.Second, "Maximum time to wait for a TLS handshake. 0 means no limit.")
    85  	f.DurationVar(&cfg.ExpectContinueTimeout, prefix+"cos.expect-continue-timeout", 1*time.Second, "The time to wait for a server's first response headers after fully writing the request headers if the request has an Expect header. 0 to send the request body immediately.")
    86  	f.IntVar(&cfg.MaxIdleConns, prefix+"cos.max-idle-connections", 100, "Maximum number of idle (keep-alive) connections across all hosts. 0 means no limit.")
    87  	f.IntVar(&cfg.MaxIdleConnsPerHost, prefix+"cos.max-idle-connections-per-host", 100, "Maximum number of idle (keep-alive) connections to keep per-host. If 0, a built-in default value is used.")
    88  	f.IntVar(&cfg.MaxConnsPerHost, prefix+"cos.max-connections-per-host", 0, "Maximum number of connections per host. 0 means no limit.")
    89  }