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): Adds label forwarding to log events #1027

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

Conversation

mfulb
Copy link
Contributor

@mfulb mfulb commented Feb 20, 2025

This PR adds the ability for labels to be forwarded with any log messages forwarded by the PHP agent.

@newrelic-php-agent-bot
Copy link

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

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

@mfulb mfulb changed the base branch from main to dev February 20, 2025 21:25
@codecov-commenter
Copy link

codecov-commenter commented Feb 24, 2025

Codecov Report

Attention: Patch coverage is 97.26027% with 2 lines in your changes missing coverage. Please review.

Project coverage is 77.98%. Comparing base (f02c410) to head (277d034).

Files with missing lines Patch % Lines
agent/php_txn.c 98.14% 1 Missing ⚠️
axiom/cmd_txndata_transmit.c 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1027      +/-   ##
==========================================
+ Coverage   77.93%   77.98%   +0.04%     
==========================================
  Files         198      198              
  Lines       27713    27784      +71     
==========================================
+ Hits        21599    21667      +68     
- Misses       6114     6117       +3     
Flag Coverage Δ
agent-for-php-7.2 78.14% <97.26%> (+0.05%) ⬆️
agent-for-php-7.3 ?
agent-for-php-7.4 77.87% <97.26%> (+0.05%) ⬆️
agent-for-php-8.0 77.26% <97.26%> (+0.05%) ⬆️
agent-for-php-8.1 77.76% <97.26%> (+0.05%) ⬆️
agent-for-php-8.2 77.37% <97.26%> (+0.05%) ⬆️
agent-for-php-8.3 77.37% <97.26%> (+0.05%) ⬆️
agent-for-php-8.4 77.39% <97.26%> (+0.05%) ⬆️

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.

@mfulb mfulb marked this pull request as ready for review February 25, 2025 15:29
@mfulb mfulb force-pushed the feat/apm_logging_labels branch from bc18e12 to df096d7 Compare February 25, 2025 18:06
; Scope : per-directory
; Default: ""
; Info : A list of labels to NOT add as attributes to logs which are forwarded
; to New Relic. This list can be separated by commas.
Copy link
Member

Choose a reason for hiding this comment

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

Please update description to state that label matching is case insensitive and multiple list elements MUST be separated by commas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in commit 277d034.

Comment on lines +85 to +87
"common": {
"attributes": { }
},
Copy link
Member

Choose a reason for hiding this comment

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

For other reviewers - sending "common" object with empty "attributes" is an existing functionality.

@@ -95,7 +95,6 @@ test_segment
test_segment_children
test_segment_datastore
test_segment_external
test_segment_message
Copy link
Member

Choose a reason for hiding this comment

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

I think this has been removed by a mistake during rebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had it in this PR, removed it, put it in #1026 and then rebased. Weird it didn't complain about a conflict - I'll look into it.

@zsistla
Copy link
Contributor

zsistla commented Feb 27, 2025

Thanks for adding the new tests_monolog_label* tests.
I did a first look through and overall they look good.
A few questions/requests:

  1. Could you add additional tests where:
  • Labels list is not set, but exclude log labels are
  • Log forwarding disabled, but exclude log labels are set (I.e., not default)
  • Exclude label list includes a label that matches multiple case sensitive labels; expected behavior is they are all removed.
  • Exclude label list has same entry more than once
  1. Did you run the new integration/multiverse label tests with valgrind?
  2. What is expected behavior if a label has a comma? Can you add a test case, so we can document the expected behavior that way?

mfulb added 20 commits February 27, 2025 14:23
- added PHP5 tests - these are not for PHP 5 but are intended
  to run on PHP5+!
@mfulb mfulb force-pushed the feat/apm_logging_labels branch from df096d7 to c9bea37 Compare February 27, 2025 19:26
bool exclude = false;
nr_status_t rv;

exclude = nro_get_hash_boolean(exclude_labels_hash, lower_key, &rv);
Copy link
Contributor

@zsistla zsistla Feb 27, 2025

Choose a reason for hiding this comment

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

exclude is set as a bool, but nro_get_hash_boolean returns an int, not a bool.

int nro_get_hash_boolean(const nrobj_t* obj,
                         const char* key,
                         nr_status_t* errp)

It returns -1 on failure/error cases and 0/1 to indicate a setting of false/true.

Consider saving some steps/checks and removing the need for a rv by taking advantage of the int return value.

It could then be simplified a bit, so that instead of:

   bool exclude = false;
    nr_status_t rv;

    exclude = nro_get_hash_boolean(exclude_labels_hash, lower_key, &rv);
if (NR_SUCCESS != rv) {
      exclude = false;
    }
    if (!exclude) {
      nro_set_hash_string(log_labels, key, value);
    } else {
      nrl_verbosedebug(NRL_TXN, "%s: Excluding label %s", __FUNCTION__, key);
    }

It could simplified to be:

if (nro_get_hash_boolean(exclude_labels_hash, lower_key, NULL) != 1) {
      nro_set_hash_string(log_labels, key, value);
    } else {
      nrl_verbosedebug(NRL_TXN, "%s: Excluding label %s", __FUNCTION__, NRSAFESTR(key));
    }

if (!exclude) {
nro_set_hash_string(log_labels, key, value);
} else {
nrl_verbosedebug(NRL_TXN, "%s: Excluding label %s", __FUNCTION__, key);
Copy link
Contributor

@zsistla zsistla Feb 27, 2025

Choose a reason for hiding this comment

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

Key was not checked for NULL, this could generate a segfault. Check for NULL earlier, and/or wrap in NRSAFESTR.

*/
log_labels = nro_new(NR_OBJECT_HASH);
for (int i = 0; i < nro_getsize(label_keys); i++) {
const char* key = nro_get_array_string(label_keys, i + 1, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

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

A NULL check for the key could help skip this round of the loop.

exclude = false;
}
if (!exclude) {
nro_set_hash_string(log_labels, key, value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Also would be a useless call if passing in a NULL key.

__FUNCTION__);
return NULL;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

A check if log_forwarding_labels_exclude is an empty string (most common case) would allow an early exit from the function.

events := NewLogEvents(10)
id := AgentRunID(`12345`)
log_data := []byte(`{"message":"test log event"}`)
label_data := []byte(`[{"label_type":"type1","label_value":"value1"},{"label_type":"type2","label_value":"value2"}]`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add a test where label name has spaces?

@@ -118,7 +118,8 @@ table Transaction {
sampling_priority: double; // added in the 8.2 PHP agent release
span_events: [Event];
log_events: [Event]; // added in the 10.1 PHP agent release
php_packages: Event; // added in the ??? PHP agent release
php_packages: Event; // added in the 10.17 PHP agent release
log_forwarding_labels: Event; // added in the ??? PHP agent release
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 you can go ahead and put 11.7 in there instead of ???

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