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

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft

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 0/8 passing
SOAK 70/79 passing

return;
}

if (NULL == retval_ptr) {

Check failure

Code scanning / CodeQL

Redundant null check due to previous dereference High

This null check is redundant because
the value is dereferenced
in any case.

Copilot Autofix AI 1 day ago

To fix the problem, we need to move the null check for retval_ptr before its dereference. This will ensure that we do not dereference a null pointer, which could lead to undefined behavior or a crash. Specifically, we should move the null check from line 324 to before line 364 where retval_ptr is first dereferenced.

Suggested changeset 1
agent/lib_aws_sdk_php.c

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/agent/lib_aws_sdk_php.c b/agent/lib_aws_sdk_php.c
--- a/agent/lib_aws_sdk_php.c
+++ b/agent/lib_aws_sdk_php.c
@@ -323,7 +323,2 @@
 
-  if (NULL == retval_ptr) {
-    /* Do not instrument when an exception has happened */
-    return;
-  }
-
 #define AWS_COMMAND_IS(CMD) \
@@ -360,2 +355,7 @@
 
+  if (NULL == retval_ptr) {
+    /* Do not instrument when an exception has happened */
+    return;
+  }
+
   /* end the segment */
EOF
@@ -323,7 +323,2 @@

if (NULL == retval_ptr) {
/* Do not instrument when an exception has happened */
return;
}

#define AWS_COMMAND_IS(CMD) \
@@ -360,2 +355,7 @@

if (NULL == retval_ptr) {
/* Do not instrument when an exception has happened */
return;
}

/* end the segment */
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

/* 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

Comment on lines +393 to +404
/* verify arguments */
if (NULL == call_args || IS_ARRAY != Z_TYPE_P(call_args)) {
return;
}
zval* lambda_args = nr_php_zend_hash_index_find(Z_ARRVAL_P(call_args), 0);
if (NULL == lambda_args || IS_ARRAY != Z_TYPE_P(lambda_args)) {
return;
}
zval* lambda_name = nr_php_zend_hash_find(Z_ARRVAL_P(lambda_args), "FunctionName");
if (NULL == lambda_name || IS_STRING != Z_TYPE_P(lambda_name)) {
return;
}
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.

This function will already do all the checking for you and if 'FunctionName' has a valid string value, it will return the value to you .
https://github.com/newrelic/newrelic-php-agent/blob/main/agent/lib_aws_sdk_php.h#L78

accountID = NRINI(aws_account_id);
}
if (nr_strempty(region)) {
region_zval = nr_php_call(this_obj, "getRegion");
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.

Since the region property exists on the object, instead of doing an nr_php_call here (which has higher overhead), you could use something like:

  region_zval
      = nr_php_get_zval_object_property(this_obj, "region");
  if (!nr_php_is_zval_non_empty_string(region_zval)) {
 ... 
}

nr_aws_lambda_invoke(NR_EXECUTE_ORIG_ARGS, &cloud_attrs);
if (!cloud_attrs.cloud_resource_id) {
/* we do not want to instrument if we cannot reconstruct the ARN */
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's still fine to instrument in this case.
It won't create the relationship, but the external segment can still be created. The cloud attributes are extra attributes, not core external segment attributes.

external_params.status = Z_LVAL_P(status_code);
}
zval* metadata = nr_php_zend_hash_find(Z_ARRVAL_P(data), "@metadata");
if (NULL != metadata && IS_REFERENCE == Z_TYPE_P(metadata)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could simplify this a bit by something like just adding this check:

  if (IS_REFERENCE == Z_TYPE_P(metadata)) {
    metadata = Z_REFVAL_P(metadata);
  }

Then the code could look something like:

if (NULL != metadata && IS_REFERENCE == Z_TYPE_P(metadata)) {
    metadata = Z_REFVAL_P(metadata);
}
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_non_empty_string(uri)) {
        external_params.uri = Z_STRVAL_P(uri);
      }
    }

char* accountID = NULL;

/* verify arguments */
if (NULL == call_args || IS_ARRAY != Z_TYPE_P(call_args)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

General comment, there are a lot of helper functions for zval here: https://github.com/newrelic/newrelic-php-agent/blob/main/agent/php_zval.h
of the form nr_php_is_zval_* that will already do the NULL and type checking for you for most zval types.

STD_PHP_INI_ENTRY_EX("newrelic.cloud.aws.account_id",
"1",
NR_PHP_REQUEST,
nr_string_mh,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this call a function that verifies the string is always 12 chars long (according to the spec).

Comment on lines +1360 to +1362
; Info : AWS account id that is used to map entities called
; via the AWS SDK.
;
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

3 participants