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 46 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 75/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.63%. Comparing base (11b67fa) to head (df096d7).
Report is 1 commits behind head on dev.

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.58%   77.63%   +0.05%     
==========================================
  Files         198      198              
  Lines       27715    27786      +71     
==========================================
+ Hits        21503    21573      +70     
- Misses       6212     6213       +1     
Flag Coverage Δ
agent-for-php-7.2 77.78% <97.26%> (+0.05%) ⬆️
agent-for-php-7.3 77.80% <97.26%> (+0.05%) ⬆️
agent-for-php-7.4 77.51% <97.26%> (+0.05%) ⬆️
agent-for-php-8.0 76.90% <97.26%> (+0.05%) ⬆️
agent-for-php-8.1 77.40% <97.26%> (+0.05%) ⬆️
agent-for-php-8.2 77.01% <97.26%> (+0.05%) ⬆️
agent-for-php-8.3 77.01% <97.26%> (+0.05%) ⬆️
agent-for-php-8.4 77.03% <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 added 24 commits February 25, 2025 10:47
The function nr_txn_begin() now accepts a nrobj_t *log_labels
parameter which contains the logging labels to be associated
with log events.
mfulb added 22 commits February 25, 2025 10:47
- added PHP5 tests - these are not for PHP 5 but are intended
  to run on PHP5+!
nrl_debug(NRL_INIT, "unable to begin transaction: app '%.128s' is unknown",
appnames ? appnames : "");
return NR_FAILURE;
}

attribute_config = nr_php_create_attribute_config(TSRMLS_C);
NRPRG(txn) = nr_txn_begin(NRPRG(app), &opts, attribute_config);
log_forwarding_labels
= nr_php_txn_get_log_forwarding_labels(NRPRG(app)->info.labels);
Copy link
Contributor

@zsistla zsistla Feb 25, 2025

Choose a reason for hiding this comment

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

Why not just have log_forwarding_labels be part of NRPRG(app)->info? Why does it need to be passed separately?

It could just be added as a var here: https://github.com/newrelic/newrelic-php-agent/pull/1027/files#diff-f5f8c5da8db458c336e8505b331bcb42a571175d89cdd0e65fbc39c1121a5960L881

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 considered that but it would be odd to put it in "appinfo" but send it to the daemon in the "transaction" payload. Also I believe the information needed to make the filtered log labels list is not available when "appinfo" is initialized.

Copy link
Member

Choose a reason for hiding this comment

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

Does the label filtering need to happen at the start of the transaction? Could it happen in cmd_txndata_transmit? The advantage of the latter is that it doesn't affect transaction latency because cmd_txndata_transmit happens after response has been sent to the client.

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.

Why wouldn't the info needed to make the filtered list not be available when appinfo is initialized?
Isn't the info is already available from the INI?

Otherwise, all of this logic is being done and added to the overhead of the beginning of every txn when it's a known value that won't change and only needs to be done once even if you still send it via the txn payload.

You could compute it once here after the labels are added: https://github.com/newrelic/newrelic-php-agent/pull/1027/files#diff-f5f8c5da8db458c336e8505b331bcb42a571175d89cdd0e65fbc39c1121a5960L881.
Then you could keep your same logic to send it in the txn payload
by just doing

log_forwarding_labels
      = NRPRG(app)->info.log_labels;

The above would keep all the sending labels in the txn payload logic, but would save the agent from having to do all the string manipulation and exclude comparisons and hash creations/looksups/etc needed to recompute the log labels (which never change since they are set in the INI) in every txn.

Alternatively, you could also just theoretically send it with the appinfo payload once, then it could be set somewhere on the daemon side where the daemon could consult the appinfo for any log labels before sending a logevent.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, you could also just theoretically send it with the appinfo payload once, then it could be set somewhere on the daemon side where the daemon could consult the appinfo for any log labels before sending a logevent.

I also like this idea.

@mfulb mfulb force-pushed the feat/apm_logging_labels branch from bc18e12 to df096d7 Compare February 25, 2025 18:06
Comment on lines +1311 to +1314
; Info : Toggles whether the agent adds labels as attributes to log records for
; sending to New Relic. The labels can be defined by the newrelic.labels
; configuration value, and filtered using the
; newrelic.application_logging.forwawrding.labels.exclude configuration value.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
; Info : Toggles whether the agent adds labels as attributes to log records for
; sending to New Relic. The labels can be defined by the newrelic.labels
; configuration value, and filtered using the
; newrelic.application_logging.forwawrding.labels.exclude configuration value.
; Info : Toggles whether the agent adds labels as attributes to log records which
; are sent to New Relic. The labels can be defined by the newrelic.labels
; configuration value, and filtered using the
; newrelic.application_logging.forwarding.labels.exclude configuration value.

; 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.

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.

@@ -666,6 +777,11 @@ static void nr_php_txn_send_metrics_once(nrtxn_t* txn TSRMLS_DC) {
nrm_force_add(NRTXN(unscoped_metrics), metname, 0);
nr_free(metname);

metname = nr_formatf("Supportability/Logging/Labels/PHP/%s",
Copy link
Contributor

Choose a reason for hiding this comment

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

Has this been added to angler?

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 will create a PR associated with the next release to add this. I will also create a ticket as part of the epic to track this.

@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?

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