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 60 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 74/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 95.94595% with 3 lines in your changes missing coverage. Please review.

Project coverage is 77.98%. Comparing base (f02c410) to head (65f9ba2).

Files with missing lines Patch % Lines
agent/php_txn.c 96.36% 2 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    27785      +72     
==========================================
+ Hits        21599    21669      +70     
- Misses       6114     6116       +2     
Flag Coverage Δ
agent-for-php-7.2 78.14% <95.94%> (+0.05%) ⬆️
agent-for-php-7.3 78.16% <95.94%> (+0.05%) ⬆️
agent-for-php-7.4 77.87% <95.94%> (+0.05%) ⬆️
agent-for-php-8.0 77.26% <95.94%> (+0.05%) ⬆️
agent-for-php-8.1 77.76% <95.94%> (+0.05%) ⬆️
agent-for-php-8.2 ?
agent-for-php-8.3 77.37% <95.94%> (+0.05%) ⬆️
agent-for-php-8.4 77.39% <95.94%> (+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.

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

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

@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 mfulb force-pushed the feat/apm_logging_labels branch from df096d7 to c9bea37 Compare February 27, 2025 19:26
agent/php_txn.c Outdated
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.

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've added the NRSAFESTR macro just in case although with the change in c13d6c0 the value of key should not be NULL here.
See a238c67

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

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 the exclude rule is empty we need to copy all the labels into the log labels structure so I don't see how we could exit early.

Copy link
Contributor

@zsistla zsistla Mar 7, 2025

Choose a reason for hiding this comment

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

Why do we need to copy all the log labels if there are no excludes?
We know that we want to include everything, so if the exclude list is the an empty string, couldn't we just could exit early here and return labels?

agent/php_txn.c Outdated
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);
const char* value = nro_get_hash_string(labels, key, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

The value is not needed until it is certain the label has not been filtered out. If it is filtered out, there's no need to retrieve it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for this observation - I've incorporated this into 90955f0

Comment on lines +396 to +397
const nr_attribute_config_t* attribute_config,
const nrobj_t* log_forwarding_labels);
Copy link
Member

Choose a reason for hiding this comment

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

These two arguments are not documented by doc string. I don't know if this is something we care about or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point - I addressed this omission with 7a02270

lavarou
lavarou previously approved these changes Mar 6, 2025
Copy link
Member

@lavarou lavarou left a comment

Choose a reason for hiding this comment

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

Looks good considering all limitations of agent<->daemon/integration_runner design.

mfulb added 8 commits March 6, 2025 12:35
Takes advantage of return value from nrp_get_hash_boolean() being -1 if the key
lookup did not suceed to simplify this section of code.  Also is smarter about retrieving
data until it is known it is needed.
Checks if log forwarding is disabled then log forwarding labels is also disabled, even if
the log forwarding labels config is set to true.
@mfulb
Copy link
Contributor Author

mfulb commented Mar 6, 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?

I think commit 65f9ba2 will address your points 1 and 3 above.

I did run the integration tests (no multiverse tests for log labels (yet)) with valgrind. A couple of CLM related tests passed valgrind but failed because an additional span ("name": "Custom/Monolog\Logger::pushHandler") was detected. I'm suspecting this is because it ran slower with valgrind than w/o so it was included whereas it is not when valgrind is not used?

@zsistla
Copy link
Contributor

zsistla commented Mar 7, 2025

65f9ba2

Awesome, thank you!

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