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): Add AWS Lambda Relationship #1023

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

feat(agent): Add AWS Lambda Relationship #1023

wants to merge 18 commits into from

Conversation

ZNeumann
Copy link
Contributor

Instrument AWS Lambda invoke and reconstruct the ARN to facilitate a complete service map.

@newrelic-php-agent-bot
Copy link

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

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


/* Compile the regex */
if (NULL == NRPRG(aws_arn_regex)) {
NRPRG(aws_arn_regex) = nr_regex_create(AWS_ARN_REGEX, 0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Where/when does this get destroyed?

Copy link
Contributor

@zsistla zsistla Feb 13, 2025

Choose a reason for hiding this comment

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

Can we just precompile this in minit and destroy like wordpress does?
https://github.com/newrelic/newrelic-php-agent/blob/main/agent/fw_wordpress.c#L878-L886

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been moved to be compiled on first use and destroyed on mshutdown.

Copy link
Contributor

@zsistla zsistla Feb 26, 2025

Choose a reason for hiding this comment

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

Instead of first use inside an instrumented function (which would make the first instrumented call take much longer than any other call), why not have it setup either in minit or in nr_aws_sdk_php_enable if you want to wait until at least the agent knows aws-sdk is involved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In an effort to minimize the number of times that this regex is unnecessarily compiled, I would like to keep the compilation where it is.

@codecov-commenter
Copy link

codecov-commenter commented Feb 26, 2025

Codecov Report

Attention: Patch coverage is 50.87719% with 56 lines in your changes missing coverage. Please review.

Project coverage is 77.82%. Comparing base (f02c410) to head (fbf408f).

Files with missing lines Patch % Lines
agent/lib_aws_sdk_php.c 49.00% 51 Missing ⚠️
agent/php_nrini.c 70.00% 3 Missing ⚠️
axiom/nr_segment_traces.c 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1023      +/-   ##
==========================================
- Coverage   77.93%   77.82%   -0.12%     
==========================================
  Files         198      198              
  Lines       27713    27827     +114     
==========================================
+ Hits        21599    21657      +58     
- Misses       6114     6170      +56     
Flag Coverage Δ
agent-for-php-7.2 78.08% <61.53%> (-0.01%) ⬇️
agent-for-php-7.3 78.10% <61.53%> (-0.01%) ⬇️
agent-for-php-7.4 77.81% <61.53%> (-0.01%) ⬇️
agent-for-php-8.0 77.20% <61.53%> (-0.01%) ⬇️
agent-for-php-8.1 77.59% <50.87%> (-0.12%) ⬇️
agent-for-php-8.2 ?
agent-for-php-8.3 ?
agent-for-php-8.4 77.22% <50.87%> (-0.12%) ⬇️

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.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ZNeumann ZNeumann marked this pull request as ready for review February 26, 2025 19:16
}
if (nr_php_is_zval_valid_array(metadata)) {
zval* uri = nr_php_zend_hash_find(Z_ARRVAL_P(metadata), "effectiveUri");
if (nr_php_is_zval_valid_string(uri)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is an empty string acceptable, or should this be checking for a valid, nonempty string instead with nr_php_is_zval_non_empty_string?

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 it is empty, there is no harm in attaching it to the external params, but also no gain. added a check in b72ebc1

Copy link
Contributor

Choose a reason for hiding this comment

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

instead of if (nr_php_is_zval_valid_string(uri) && !nr_strempty(Z_STRVAL_P(uri)))

Just use nr_php_is_zval_non_empty_string which does both of those things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed in 50aaa3c

return;
}
zval* lambda_name = nr_php_zend_hash_find(Z_ARRVAL_P(lambda_args), "FunctionName");
if (!nr_php_is_zval_valid_string(lambda_name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is an empty string acceptable, or should this be checking for a valid, nonempty string instead with nr_php_is_zval_non_empty_string?

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 it is empty, the regex will fail to capture anything and the function will return exit at that point.

Copy link
Contributor

Choose a reason for hiding this comment

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

True, but looks like we get to at least here, which could be skipped if we used nr_php_is_zval_non_empty_string to check for both valid and not empty string instead of just valid string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed in 50aaa3c


void nr_aws_sdk_lambda_client_invoke_parse_args(NR_EXECUTE_PROTO, nr_segment_cloud_attrs_t* cloud_attrs) {
zval* call_args = nr_php_get_user_func_arg(2, NR_EXECUTE_ORIG_ARGS);
zval* this_obj = getThis();//NR_PHP_USER_FN_THIS();
Copy link
Member

Choose a reason for hiding this comment

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

Why isn't the NR_PHP_USER_FN_THIS macro used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was leftover from testing about #1023 (comment). Using NR_PHP_USER_FN_THIS() in fbf408f

Comment on lines +451 to +456
zend_class_entry* base_class = NULL;
if (NULL != execute_data->func && NULL!= execute_data->func->common.scope) {
base_class = execute_data->func->common.scope;
}
region_zval
= nr_php_get_zval_object_property_with_class(this_obj, base_class, "region");
Copy link
Member

Choose a reason for hiding this comment

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

Is this construct correct? Take a look at this example. The first argument to nr_php_get_zval_object_property_with_class is the object to extract the property from, and the second argument is the class entry for the context from which the property should be extracted. What is this code trying to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LambdaClient extends AwsClient. region exists on the AwsClient, not the LambdaClient. So if we wish to extract region from this (which is a LambdaClient), we must indicate the base class.

Copy link
Member

Choose a reason for hiding this comment

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

If the intent is to look for the "region" property either in this (which is Aws\Lambda\LambdaClient) and then fall back to looking for it in this's base class (which is Aws\AwsClient), then wouldn't it be safer to get the base_class like this: base_class = nr_php_find_class("aws\\awsclient");?

Copy link
Member

Choose a reason for hiding this comment

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

The best strategy to ensure that the agent will be able to retrieve the region is to call getRegion method on this like this: region = nr_php_call(this, "getRegion"); It doesn't rely on any assumptions but rather on publicly available API of AWS SDK for PHP.

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