Skip to content

Commit 124117f

Browse files
authored
Merge pull request #2412 from grafana/fix/add-server-timeouts
fix(server): Add read and write timeouts
2 parents 5d387b0 + cd460fe commit 124117f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

docs/developer/cli-arguments.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ Flags:
6767
--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.
6868
--port int Port to expose metrics on. (default 8080)
6969
--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"
70+
--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)
71+
--server-read-header-timeout duration The maximum duration for reading the header of requests. (default 5s)
72+
--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)
73+
--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)
7074
--shard int32 The instances shard nominal (zero indexed) within the total number of shards. (default 0)
7175
--skip_headers If true, avoid header prefixes in the log messages
7276
--skip_log_headers If true, avoid headers when opening log files (no effect when -logtostderr=true)

pkg/app/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
325325
metricsServerListenAddress := net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port))
326326
metricsServer := http.Server{
327327
Handler: metricsMux,
328-
ReadHeaderTimeout: 5 * time.Second,
328+
ReadHeaderTimeout: opts.ServerReadHeaderTimeout,
329+
ReadTimeout: opts.ServerReadTimeout,
330+
WriteTimeout: opts.ServerWriteTimeout,
331+
IdleTimeout: opts.ServerIdleTimeout,
329332
}
330333
metricsFlags := web.FlagConfig{
331334
WebListenAddresses: &[]string{metricsServerListenAddress},
@@ -401,7 +404,6 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
401404
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
402405

403406
mux.Handle(metricsPath, promhttp.InstrumentHandlerDuration(durationObserver, m))
404-
405407
// Add healthzPath
406408
mux.HandleFunc(healthzPath, func(w http.ResponseWriter, _ *http.Request) {
407409
w.WriteHeader(http.StatusOK)

pkg/options/options.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ import (
2121
"fmt"
2222
"os"
2323
"strings"
24+
"time"
2425

2526
"github.com/prometheus/common/version"
2627
"github.com/spf13/cobra"
2728
"k8s.io/klog/v2"
2829
)
2930

31+
var (
32+
// Align with the default scrape interval from Prometheus: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config
33+
defaultServerReadTimeout = 60 * time.Second
34+
defaultServerWriteTimeout = 60 * time.Second
35+
// ServerIdleTimeout is set to 5 minutes to match the default idle timeout of Prometheus scrape clients
36+
// https://github.com/prometheus/common/blob/318309999517402ad522877ac7e55fa650a11114/config/http_config.go#L55
37+
defaultServerIdleTimeout = 5 * time.Minute
38+
defaultServerReadHeaderTimeout = 5 * time.Second
39+
)
40+
3041
// Options are the configurable parameters for kube-state-metrics.
3142
type Options struct {
3243
AnnotationsAllowList LabelsAllowList `yaml:"annotations_allow_list"`
@@ -55,6 +66,10 @@ type Options struct {
5566
TelemetryPort int `yaml:"telemetry_port"`
5667
TotalShards int `yaml:"total_shards"`
5768
UseAPIServerCache bool `yaml:"use_api_server_cache"`
69+
ServerReadTimeout time.Duration `yaml:"server_read_timeout"`
70+
ServerWriteTimeout time.Duration `yaml:"server_write_timeout"`
71+
ServerIdleTimeout time.Duration `yaml:"server_idle_timeout"`
72+
ServerReadHeaderTimeout time.Duration `yaml:"server_read_header_timeout"`
5873

5974
Config string
6075

@@ -146,6 +161,11 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
146161
o.cmd.Flags().Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
147162
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.")
148163
o.cmd.Flags().Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources))
164+
165+
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. ")
166+
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..")
167+
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.")
168+
o.cmd.Flags().DurationVar(&o.ServerReadHeaderTimeout, "server-read-header-timeout", defaultServerReadHeaderTimeout, "The maximum duration for reading the header of requests.")
149169
}
150170

151171
// Parse parses the flag definitions from the argument list.

0 commit comments

Comments
 (0)