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

feat(agent): Drupal hook attribute instrumentation #1030

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from

Conversation

bduranleau-nr
Copy link
Contributor

Adds support for Drupal Attribute Hooks added in Drupal 11.1.

@newrelic-php-agent-bot
Copy link

newrelic-php-agent-bot commented Feb 25, 2025

Test Suite Status Result
Multiverse 8/8 passing
SOAK 79/79 passing

@codecov-commenter
Copy link

codecov-commenter commented Feb 25, 2025

Codecov Report

Attention: Patch coverage is 34.14634% with 27 lines in your changes missing coverage. Please review.

Project coverage is 77.87%. Comparing base (8ddc8af) to head (ea0e4cf).
Report is 1 commits behind head on dev.

Files with missing lines Patch % Lines
agent/fw_drupal8.c 34.14% 27 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1030      +/-   ##
==========================================
+ Coverage   77.58%   77.87%   +0.28%     
==========================================
  Files         198      198              
  Lines       27715    27754      +39     
==========================================
+ Hits        21503    21613     +110     
+ Misses       6212     6141      -71     
Flag Coverage Δ
agent-for-php-7.2 77.97% <0.00%> (+0.24%) ⬆️
agent-for-php-7.3 77.99% <0.00%> (+0.24%) ⬆️
agent-for-php-7.4 77.75% <34.14%> (+0.29%) ⬆️
agent-for-php-8.0 77.14% <34.14%> (+0.29%) ⬆️
agent-for-php-8.1 77.64% <34.14%> (+0.29%) ⬆️
agent-for-php-8.2 77.25% <34.14%> (+0.29%) ⬆️
agent-for-php-8.3 77.25% <34.14%> (?)
agent-for-php-8.4 77.27% <34.14%> (+0.29%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

hook_attribute_instrumentation
= nr_drupal_hook_attribute_instrument(*retval_ptr);

if (!hook_attribute_instrumentation) {
Copy link
Member

Choose a reason for hiding this comment

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

nr_drupal_hook_attribute_instrument will only return true if it's able to walk the entire hookImplementationsMap. What will happen if it fails mid way? Which hook instrumentation will be used? Does nr_drupal_hook_attribute_instrument cleanup its partial work if it fails to walk the entire hookImplementationsMap? Could you add a test for this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If walking the map fails halfway through, we exit the function and return false. The wraprecs that are already created remain, and we revert back to the old method of wrapping hooks.

Which hook instrumentation will be used?

The answer would be "both". The attribute based instrumentation would persist for anything it managed to wrap, and the other methods act as a fallback to hopefully plug the gaps. I don't see anticipate any negative side-effects from this, do you?

Could you add a test for this case?

This would be difficult and doesn't fit neatly into our established test paradigms, The dependency here is a lot of Drupal code, from Drupal::moduleHandler to the hookImplementationsMap. Is there a specific concern you have with this failing partway though walking the map?

Copy link
Member

Choose a reason for hiding this comment

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

Given #1030 (comment), I would feel more comfortable having a test with mocked implementation of Drupal\Core\Extension\ModuleHandlerInterface that has corrupted hookImplementationsMap property in every possible way - invalid type of the property (not an array), first level has invalid key and value, second level has invalid key and value, third level has invalid key and value.

Copy link
Member

@lavarou lavarou Feb 27, 2025

Choose a reason for hiding this comment

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

Take a look here and here for inspiration how to mock implementation of Drupal\Core\Extension\ModuleHandlerInterface and use it in integration test.

hook_val) {
if ((NULL == hook_key) || (0 == nr_php_is_zval_valid_array(hook_val))) {
nrl_warning(NRL_FRAMEWORK,
"hookImplementationsMap[hook = %s]: invalid value",
Copy link
Member

Choose a reason for hiding this comment

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

This can happen when NULL == hook_key, so more appropriate message would be:

Suggested change
"hookImplementationsMap[hook = %s]: invalid value",
"hookImplementationsMap[hook = %s]: invalid key or value",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

value here was intended to be generic, as in "an invalid value was returned" and not the specifc map value for a key, but I've fixed it here ea0e4cf

if ((NULL == hook_key) || (0 == nr_php_is_zval_valid_array(hook_val))) {
nrl_warning(NRL_FRAMEWORK,
"hookImplementationsMap[hook = %s]: invalid value",
NRSAFESTR(ZEND_STRING_VALUE(hook_key)));
Copy link
Member

Choose a reason for hiding this comment

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

This will seg fault if hook_key is NULL - ZEND_STRING_VALUE dereferences pointer it is passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The warnings have been fixed here d876e0e

zval* module_val = NULL;

char* hookpath = NULL;

Copy link
Contributor

Choose a reason for hiding this comment

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

Could check module_handler for NULL and exit early.

return false;
}

if (!nr_php_is_zval_valid_array(hook_implementation_map)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nr_php_is_zval_valid_array will already check hook_implementation_map for NULL ; it doesn't need the extra check right above.

if ((NULL == hook_key) || (0 == nr_php_is_zval_valid_array(hook_val))) {
nrl_warning(NRL_FRAMEWORK,
"hookImplementationsMap[hook]: invalid key or value");
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to return here or just skip this round of the loop?
What if the first hook_key or hook_val is invalid, but the next one is good?

if ((NULL == class_key) || (0 == nr_php_is_zval_valid_array(class_val))) {
nrl_warning(NRL_FRAMEWORK,
"hookImplementationsMap[class]: invalid key or value");
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to https://github.com/newrelic/newrelic-php-agent/pull/1030/files#r1974532294, do we want to exit the whole function or just skip this round of the loop.

|| (0 == nr_php_is_zval_valid_string(module_val))) {
nrl_warning(NRL_FRAMEWORK,
"hookImplementationsMap[method]: invalid key or value");
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/newrelic/newrelic-php-agent/pull/1030/files#r1974532294
skip this round of the loop or completely exit?

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.

5 participants