Skip to content

Commit

Permalink
Add support for timeout offset (#647)
Browse files Browse the repository at this point in the history
* Add support for timout offset

Allow the MongoDB timeout to be configured relative to the Prometheus
scrape timeout. This ensures the Prometheus scrape will get a response even
if the exporter cannot connect to MongoDB.

* Add check to ensure TimeoutOffset is greater than 0

* Empty commit
  • Loading branch information
mikael-lindstrom authored Oct 12, 2023
1 parent e02fe4f commit c17b537
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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|
|--version|Show version and exit|
2 changes: 2 additions & 0 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Opts struct {
DiscoveringMode bool
GlobalConnPool bool
ProfileTimeTS int
TimeoutOffset int

CollectAll bool
EnableDBStats bool
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit c17b537

Please sign in to comment.