diff --git a/REFERENCE.md b/REFERENCE.md index a78f7edc7..b5edb81b4 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -14,6 +14,7 @@ |--web.listen-address|Address to listen on for web interface and telemetry|--web.listen-address=":9216"| |--web.telemetry-path|Metrics expose path|--web.telemetry-path="/metrics"| |--web.config|Path to the file having Prometheus TLS config for basic auth|--web.config=STRING| +|--web.timeout-offset|Offset to subtract from the timeout in seconds|--web.timeout-offset=1| |--log.level|Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]|--log.level="error"| |--collector.diagnosticdata|Enable collecting metrics from getDiagnosticData| |--collector.replicasetstatus|Enable collecting metrics from replSetGetStatus| @@ -27,4 +28,4 @@ |--collector.profile-time-ts=30|Set time for scrape slow queries| This interval must be synchronized with the Prometheus scrape interval| |--collector.profile|Enable collecting metrics from profile| |--metrics.overridedescendingindex| Enable descending index name override to replace -1 with _DESC || -|--version|Show version and exit| \ No newline at end of file +|--version|Show version and exit| diff --git a/exporter/exporter.go b/exporter/exporter.go index ce11f0667..53d793de1 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -56,6 +56,7 @@ type Opts struct { DiscoveringMode bool GlobalConnPool bool ProfileTimeTS int + TimeoutOffset int CollectAll bool EnableDBStats bool @@ -271,6 +272,7 @@ func (e *Exporter) Handler() http.Handler { if err != nil { seconds = 10 } + seconds -= e.opts.TimeoutOffset var client *mongo.Client ctx, cancel := context.WithTimeout(r.Context(), time.Duration(seconds)*time.Second) diff --git a/main.go b/main.go index 83948ff27..260c1e809 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ type GlobalFlags struct { WebListenAddress string `name:"web.listen-address" help:"Address to listen on for web interface and telemetry" default:":9216"` WebTelemetryPath string `name:"web.telemetry-path" help:"Metrics expose path" default:"/metrics"` TLSConfigPath string `name:"web.config" help:"Path to the file having Prometheus TLS config for basic auth"` + TimeoutOffset int `name:"web.timeout-offset" help:"Offset to subtract from the request timeout in seconds" default:"1"` LogLevel string `name:"log.level" help:"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]" enum:"debug,info,warn,error,fatal" default:"error"` ConnectTimeoutMS int `name:"mongodb.connect-timeout-ms" help:"Connection timeout in milliseconds" default:"5000"` @@ -112,6 +113,11 @@ func main() { ctx.Fatalf("No MongoDB hosts were specified. You must specify the host(s) with the --mongodb.uri command argument or the MONGODB_URI environment variable") } + if opts.TimeoutOffset <= 0 { + log.Warn("Timeout offset needs to be greater than \"0\", falling back to \"1\". You can specify the timout offset with --web.timeout-offset command argument") + opts.TimeoutOffset = 1 + } + serverOpts := &exporter.ServerOpts{ Path: opts.WebTelemetryPath, MultiTargetPath: "/scrape", @@ -135,6 +141,7 @@ func buildExporter(opts GlobalFlags, uri string, log *logrus.Logger) *exporter.E GlobalConnPool: opts.GlobalConnPool, DirectConnect: opts.DirectConnect, ConnectTimeoutMS: opts.ConnectTimeoutMS, + TimeoutOffset: opts.TimeoutOffset, EnableDiagnosticData: opts.EnableDiagnosticData, EnableReplicasetStatus: opts.EnableReplicasetStatus,