Skip to content

Fetch the remote notifier config#114

Merged
mmcdaris merged 14 commits intomasterfrom
remote-config
Apr 6, 2021
Merged

Fetch the remote notifier config#114
mmcdaris merged 14 commits intomasterfrom
remote-config

Conversation

@mmcdaris
Copy link
Copy Markdown
Member

Adds support for fetching the remote notifier config when possible. The remote config feature creates a base for more flexible PHPBrake configuration and features at runtime. The remote config is cached for 10 minutes in a temp file in the system's temp directory. If fetching or caching fails or is unavailable for any reason, the default settings are used.

A few notes on Airbrake\Notifier initialization options and the remote config:

  • This Adds a new remoteConfig option to the initialization of the Airbrake\Notifer that defaults to true. If the Airbrake\Notifier is initialized with the remoteConfig set to false, the config will not be fetched from the remote and the default endpoint will be used.
  • If the host option is specified with Airbrake\Notifier initialization that will take precedence over the host fetched from the remote config.

The remote notifier config is fetched and cached for 10 minutes.
This will avoid sending errors to the service if the remote config
response specifies that errors are disabled.
When set to false the defaults are used and no remote config fetch is
attempted.
@mmcdaris mmcdaris requested a review from kyrylo March 30, 2021 18:45
Copy link
Copy Markdown
Contributor

@kyrylo kyrylo left a comment

Choose a reason for hiding this comment

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

We need to fetch the config every 10 minutes (in background). Currently, unless I am mistaken, we fetch it only once, on launch. Did I read the code correctly?

Comment thread src/RemoteConfig.php
protected $remoteConfigURL;
protected $httpClient;
protected $tempCache;
protected $tempCacheFilename = 'airbrake_cached_remote_config.json';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need to cache remote config? Couldn't we keep the config in memory?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I really wanted to do that as well. I searched a lot for a way to store data between or outside of requests without much luck. Everything that looked promising as a solution required PHP be compiled with an extra flag or installed with PECL. I couldn't really find anything I could depend on existing so I decided to use a temporary file to cache the remote config.

Comment thread src/Notifier.php Outdated
Comment thread src/Notifier.php
return $this->errorConfig;
}

protected function newRemoteConfig($projectId)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does this have to be a function? Now we assign to $remoteConfig twice. I am not sure I am following this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

my main motivation for having a newRemoteConfig method is that it made things easier to mock in tests.

We can remove the $remoteConfig assignment here though, sorry that was leftover from some local debugging. The body of this method should just be:

   return new RemoteConfig($projectId);

Since the $remoteConfig variable isn't prefixed with $this-> it will only be available in the method scope and not on the instance.

mmcdaris and others added 3 commits March 31, 2021 11:44
Co-authored-by: Kyrylo Silin <silin@kyrylo.org>
We don't need to set up or store the noticesURL during construction
since we get this information at the time the error occurs with the
buildNoticesURL function instead. This ensures we always get the
freshest error host to use from the RemoteConfig or when that isn't
available just fallback to the default error host of `api.airbrake.io`.
@mmcdaris
Copy link
Copy Markdown
Member Author

We need to fetch the config every 10 minutes (in background). Currently, unless I am mistaken, we fetch it only once, on launch. Did I read the code correctly?

Hi @kyrylo thank you for the review and questions!

I had another look at this an it turns out it was useless to set the noticesURL var in the constructor because we determine the URL when we report the error to Airbrake. I've tried to make this more clear in my last commit by removing the noticesURL var, and making it clear that we build the notice url whenever an error is reported.

As for the cache lifecycle here's how it works: When an error is being reported to Airbrake, we ask the remoteConfig for the endpoint to send it to. If the remote config isn't cached or is expired, we fetch and cache it for the next 10 minutes. If the cache is still fresh we read from it.

You mention this only getting fetched once on launch but as I understand it through my testing PHP doesn't hold onto instances of objects across requests and it would take extra effort to set up a global instance of Airbrake\Notifier that all errors are reported with. Even if that were the case the URL to use would be determined during the reporting of an error instead of during construction.

Comment thread tests/NotifierTest.php Outdated
Comment thread src/RemoteConfig.php Outdated
Comment thread src/RemoteConfig.php Outdated
Comment thread src/RemoteConfig.php Outdated
Comment thread src/RemoteConfig.php Outdated
Comment thread src/RemoteConfig.php Outdated
When we build an error we call errorHost() and errorNotifications(),
without memoization this would result in two calls to the cache instead
of just one.
@mmcdaris mmcdaris merged commit 768f8c5 into master Apr 6, 2021
@mmcdaris mmcdaris deleted the remote-config branch April 6, 2021 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants