Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Graphite Web HTTP request timeout option #321

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

atj
Copy link
Contributor

@atj atj commented Feb 1, 2024

If the Graphite Web server is unreachable, all requests for frontend pages containing graphs hang until the backend HTTP request times out, resulting in a very poor UX.

The Guzzle documentation states that the default behaviour is to wait indefinitely, however in our testing the cURL handler has an internal default of 30 seconds:

https://docs.guzzlephp.org/en/stable/request-options.html#timeout

This commit makes the HTTP request timeout configurable and sets a reasonable default of 10 seconds.

@cla-bot cla-bot bot added the cla/signed label Feb 1, 2024
Copy link
Member

@nilmerg nilmerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I've just left you some suggestions to solve two problems:

  • The type safety as reported by phpstan
  • The fallback for configs without a timeout, so that the new default of 10 seconds applies to them as well

library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
@nilmerg nilmerg added this to the v1.2.5 milestone Feb 1, 2024
@atj
Copy link
Contributor Author

atj commented Feb 1, 2024

@nilmerg Thanks for the suggestions. I had tried to fix the type safety issues reported by PHPStan but couldn't understand why the following calls weren't triggering the same errors:

->setUser($graphite->user)
->setPassword($graphite->password)
->setInsecure($graphite->insecure)

I applied the suggestions locally and PHPStan raised the same error with a slightly different expected type:

$ vendor/bin/phpstan analyse library/ application/
Note: Using configuration file icingaweb2-module-graphite/phpstan.neon.
 42/42 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ -------------------------------------------------------------------------------------------------------------------------------- 
  Line   library/Graphite/Graphing/GraphingTrait.php (in context of class Icinga\Module\Graphite\Clicommands\Icinga2Command)             
 ------ -------------------------------------------------------------------------------------------------------------------------------- 
  :74    Parameter #1 $timeout of method Icinga\Module\Graphite\Graphing\GraphiteWebClient::setTimeout() expects int|null, mixed given.  
 ------ -------------------------------------------------------------------------------------------------------------------------------- 

@nilmerg
Copy link
Member

nilmerg commented Feb 1, 2024

The problem is that the config section type has no idea of its contained properties and hence phpstan complains rightfully about the unsafe handling. The config might as well contain timeout = "20" which is a string.

To fix this, I'd suggest to use this in GraphingTrait:

setTimeout(isset($graphite->timeout) ? (int) $graphite->timeout : null)

@nilmerg
Copy link
Member

nilmerg commented Feb 14, 2024

Extending the baseline isn't an option.

@atj
Copy link
Contributor Author

atj commented Feb 14, 2024

Extending the baseline isn't an option.

PHPStan is not throwing type errors for the other config fields because they are being ignored via entries in the baseline:

-
message: "#^Parameter \\#1 \\$insecure of method Icinga\\\\Module\\\\Graphite\\\\Graphing\\\\GraphiteWebClient\\:\\:setInsecure\\(\\) expects bool, mixed given\\.$#"
count: 1
path: application/controllers/GraphController.php
-
message: "#^Parameter \\#1 \\$password of method Icinga\\\\Module\\\\Graphite\\\\Graphing\\\\GraphiteWebClient\\:\\:setPassword\\(\\) expects string\\|null, mixed given\\.$#"
count: 1
path: application/controllers/GraphController.php

As the Icinga\Data\ConfigObject accessors are returning mixed values, I don't believe it's possible to make the call to setTimeout type safe, so it's the only option.

Copy link
Member

@nilmerg nilmerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rebase, phpstan shouldn't complain anymore.

We've introduced phpstan to our CI toolstack just recently and didn't adjust most of what it complained about. Hence why the baselines are rather large at the moment. Though, adding additional entries to those isn't helpful in any way as it renders phpstan useless after all.

I've played around with generics just recently and now added them to Icinga Web's ConfigObject. This allows us to define here what our config contains. I've did that now in the default branch and you don't have to do anything, other than rebasing. So please drop the changes in the baseline.

library/Graphite/Graphing/GraphiteWebClient.php Outdated Show resolved Hide resolved
If the Graphite Web server is unreachable, all requests for frontend
pages containing graphs hang until the backend HTTP request times out,
resulting in a very poor UX.

The Guzzle documentation states that the default behaviour is to wait
indefinitely, however in our testing the cURL handler has an internal
default of 30 seconds:

https://docs.guzzlephp.org/en/stable/request-options.html#timeout

This commit makes the HTTP request timeout configurable and sets a
reasonable default of 10 seconds.
@atj
Copy link
Contributor Author

atj commented Feb 15, 2024

Thanks for your help on this. I've removed the baseline changes and ensured that Guzzle isn't passed a null value for timeout.

@nilmerg nilmerg added the enhancement New feature or improvement label Feb 20, 2024
@nilmerg nilmerg merged commit 5e0d57c into Icinga:main Feb 20, 2024
7 checks passed
@atj atj deleted the add-timeout-option branch February 20, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed enhancement New feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants