MCPcopy Create free account
hub / github.com/prometheus/common / NewRoundTripperFromConfigWithContext

Function NewRoundTripperFromConfigWithContext

config/http_config.go:630–763  ·  view source on GitHub ↗

NewRoundTripperFromConfigWithContext returns a new HTTP RoundTripper configured for the given config.HTTPClientConfig and config.HTTPClientOption. The name is used as go-conntrack metric label.

(ctx context.Context, cfg HTTPClientConfig, name string, optFuncs ...HTTPClientOption)

Source from the content-addressed store, hash-verified

628// given config.HTTPClientConfig and config.HTTPClientOption.
629// The name is used as go-conntrack metric label.
630func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientConfig, name string, optFuncs ...HTTPClientOption) (http.RoundTripper, error) {
631 opts := defaultHTTPClientOptions
632 for _, opt := range optFuncs {
633 opt.applyToHTTPClientOptions(&opts)
634 }
635
636 var dialContext func(ctx context.Context, network, addr string) (net.Conn, error)
637
638 if opts.dialContextFunc != nil {
639 dialContext = conntrack.NewDialContextFunc(
640 conntrack.DialWithDialContextFunc((func(context.Context, string, string) (net.Conn, error))(opts.dialContextFunc)),
641 conntrack.DialWithTracing(),
642 conntrack.DialWithName(name))
643 } else {
644 dialContext = conntrack.NewDialContextFunc(
645 conntrack.DialWithTracing(),
646 conntrack.DialWithName(name))
647 }
648
649 newRT := func(tlsConfig *tls.Config) (http.RoundTripper, error) {
650 // The only timeout we care about is the configured scrape timeout.
651 // It is applied on request. So we leave out any timings here.
652 var rt http.RoundTripper = &http.Transport{
653 Proxy: cfg.Proxy(),
654 ProxyConnectHeader: cfg.GetProxyConnectHeader(),
655 MaxIdleConns: 20000,
656 MaxIdleConnsPerHost: 1000, // see https://github.com/golang/go/issues/13801
657 DisableKeepAlives: !opts.keepAlivesEnabled,
658 TLSClientConfig: tlsConfig,
659 DisableCompression: true,
660 IdleConnTimeout: opts.idleConnTimeout,
661 TLSHandshakeTimeout: 10 * time.Second,
662 ExpectContinueTimeout: 1 * time.Second,
663 DialContext: dialContext,
664 }
665 if opts.http2Enabled && cfg.EnableHTTP2 {
666 http2t, err := http2.ConfigureTransports(rt.(*http.Transport))
667 if err != nil {
668 return nil, err
669 }
670 http2t.ReadIdleTimeout = time.Minute
671 }
672
673 // If a authorization_credentials is provided, create a round tripper that will set the
674 // Authorization header correctly on each request.
675 if cfg.Authorization != nil {
676 credentialsSecret, err := toSecret(opts.secretManager, cfg.Authorization.Credentials, cfg.Authorization.CredentialsFile, cfg.Authorization.CredentialsRef)
677 if err != nil {
678 return nil, fmt.Errorf("unable to use credentials: %w", err)
679 }
680 rt = NewAuthorizationCredentialsRoundTripper(cfg.Authorization.Type, credentialsSecret, rt)
681 }
682 // Backwards compatibility, be nice with importers who would not have
683 // called Validate().
684 if len(cfg.BearerToken) > 0 || len(cfg.BearerTokenFile) > 0 {
685 bearerSecret, err := toSecret(opts.secretManager, cfg.BearerToken, cfg.BearerTokenFile, "")
686 if err != nil {
687 return nil, fmt.Errorf("unable to use bearer token: %w", err)

Callers 1

Calls 15

toSecretFunction · 0.85
SecretTypeAlias · 0.85
NewBasicAuthRoundTripperFunction · 0.85
NewOAuth2RoundTripperFunction · 0.85
NewHeadersRoundTripperFunction · 0.85
NewUserAgentRoundTripperFunction · 0.85
NewHostRoundTripperFunction · 0.85
WithSecretManagerFunction · 0.85
ProxyMethod · 0.80
GetProxyConnectHeaderMethod · 0.80

Tested by

no test coverage detected