Skip to content

Commit

Permalink
Merge pull request #2412 from grafana/fix/add-server-timeouts
Browse files Browse the repository at this point in the history
fix(server): Add read and write timeouts
  • Loading branch information
k8s-ci-robot committed Jun 17, 2024
2 parents 5d387b0 + cd460fe commit 124117f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/developer/cli-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Flags:
--pod-namespace string Name of the namespace of the pod specified by --pod. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.
--port int Port to expose metrics on. (default 8080)
--resources string Comma-separated list of Resources to be enabled. Defaults to "certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,horizontalpodautoscalers,ingresses,jobs,leases,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments"
--server-idle-timeout duration The maximum amount of time to wait for the next request when keep-alives are enabled. Align with the idletimeout of your scrape clients. (default 5m0s)
--server-read-header-timeout duration The maximum duration for reading the header of requests. (default 5s)
--server-read-timeout duration The maximum duration for reading the entire request, including the body. Align with the scrape interval or timeout of scraping clients. (default 1m0s)
--server-write-timeout duration The maximum duration before timing out writes of the response. Align with the scrape interval or timeout of scraping clients.. (default 1m0s)
--shard int32 The instances shard nominal (zero indexed) within the total number of shards. (default 0)
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
Expand Down
6 changes: 4 additions & 2 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
metricsServerListenAddress := net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port))
metricsServer := http.Server{
Handler: metricsMux,
ReadHeaderTimeout: 5 * time.Second,
ReadHeaderTimeout: opts.ServerReadHeaderTimeout,
ReadTimeout: opts.ServerReadTimeout,
WriteTimeout: opts.ServerWriteTimeout,
IdleTimeout: opts.ServerIdleTimeout,
}
metricsFlags := web.FlagConfig{
WebListenAddresses: &[]string{metricsServerListenAddress},
Expand Down Expand Up @@ -401,7 +404,6 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))

mux.Handle(metricsPath, promhttp.InstrumentHandlerDuration(durationObserver, m))

// Add healthzPath
mux.HandleFunc(healthzPath, func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down
20 changes: 20 additions & 0 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/prometheus/common/version"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)

var (
// Align with the default scrape interval from Prometheus: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config
defaultServerReadTimeout = 60 * time.Second
defaultServerWriteTimeout = 60 * time.Second
// ServerIdleTimeout is set to 5 minutes to match the default idle timeout of Prometheus scrape clients
// https://github.com/prometheus/common/blob/318309999517402ad522877ac7e55fa650a11114/config/http_config.go#L55
defaultServerIdleTimeout = 5 * time.Minute
defaultServerReadHeaderTimeout = 5 * time.Second
)

// Options are the configurable parameters for kube-state-metrics.
type Options struct {
AnnotationsAllowList LabelsAllowList `yaml:"annotations_allow_list"`
Expand Down Expand Up @@ -55,6 +66,10 @@ type Options struct {
TelemetryPort int `yaml:"telemetry_port"`
TotalShards int `yaml:"total_shards"`
UseAPIServerCache bool `yaml:"use_api_server_cache"`
ServerReadTimeout time.Duration `yaml:"server_read_timeout"`
ServerWriteTimeout time.Duration `yaml:"server_write_timeout"`
ServerIdleTimeout time.Duration `yaml:"server_idle_timeout"`
ServerReadHeaderTimeout time.Duration `yaml:"server_read_header_timeout"`

Config string

Expand Down Expand Up @@ -146,6 +161,11 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
o.cmd.Flags().Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
o.cmd.Flags().Var(&o.NamespacesDenylist, "namespaces-denylist", "Comma-separated list of namespaces not to be enabled. If namespaces and namespaces-denylist are both set, only namespaces that are excluded in namespaces-denylist will be used.")
o.cmd.Flags().Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources))

o.cmd.Flags().DurationVar(&o.ServerReadTimeout, "server-read-timeout", defaultServerReadTimeout, "The maximum duration for reading the entire request, including the body. Align with the scrape interval or timeout of scraping clients. ")
o.cmd.Flags().DurationVar(&o.ServerWriteTimeout, "server-write-timeout", defaultServerWriteTimeout, "The maximum duration before timing out writes of the response. Align with the scrape interval or timeout of scraping clients..")
o.cmd.Flags().DurationVar(&o.ServerIdleTimeout, "server-idle-timeout", defaultServerIdleTimeout, "The maximum amount of time to wait for the next request when keep-alives are enabled. Align with the idletimeout of your scrape clients.")
o.cmd.Flags().DurationVar(&o.ServerReadHeaderTimeout, "server-read-header-timeout", defaultServerReadHeaderTimeout, "The maximum duration for reading the header of requests.")
}

// Parse parses the flag definitions from the argument list.
Expand Down

0 comments on commit 124117f

Please sign in to comment.