Skip to content

Commit

Permalink
Add 4096 char truncation for ML events.
Browse files Browse the repository at this point in the history
  • Loading branch information
umaannamalai committed Oct 18, 2023
1 parent 51e7362 commit c55326d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion newrelic/api/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ def record_ml_event(self, event_type, params):
if not settings.ml_insights_events.enabled:
return

event = create_custom_event(event_type, params)
event = create_custom_event(event_type, params, is_ml_event=True)
if event:
self._ml_events.add(event, priority=self.priority)

Expand Down
1 change: 1 addition & 0 deletions newrelic/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@

MAX_NUM_USER_ATTRIBUTES = 128
MAX_ATTRIBUTE_LENGTH = 255
MAX_ML_ATTRIBUTE_LENGTH = 4096
MAX_64_BIT_INT = 2**63 - 1
MAX_LOG_MESSAGE_LENGTH = 32768

Expand Down
12 changes: 9 additions & 3 deletions newrelic/core/custom_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from newrelic.core.attribute import (check_name_is_string, check_name_length,
process_user_attribute, NameIsNotStringException, NameTooLongException,
MAX_NUM_USER_ATTRIBUTES)
MAX_NUM_USER_ATTRIBUTES, MAX_ML_ATTRIBUTE_LENGTH)


_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,7 +72,8 @@ def process_event_type(name):
else:
return name

def create_custom_event(event_type, params):

def create_custom_event(event_type, params, is_ml_event=False):
"""Creates a valid custom event.
Ensures that the custom event has a valid name, and also checks
Expand All @@ -83,6 +84,8 @@ def create_custom_event(event_type, params):
Args:
event_type (str): The type (name) of the custom event.
params (dict): Attributes to add to the event.
is_ml_event (bool): Boolean indicating whether create_custom_event was called from
record_ml_event for truncation purposes
Returns:
Custom event (list of 2 dicts), if successful.
Expand All @@ -99,7 +102,10 @@ def create_custom_event(event_type, params):

try:
for k, v in params.items():
key, value = process_user_attribute(k, v)
if is_ml_event:
key, value = process_user_attribute(k, v, max_length=MAX_ML_ATTRIBUTE_LENGTH)
else:
key, value = process_user_attribute(k, v)
if key:
if len(attributes) >= MAX_NUM_USER_ATTRIBUTES:
_logger.debug('Maximum number of attributes already '
Expand Down
10 changes: 10 additions & 0 deletions tests/agent_features/test_ml_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ def _test():
_test()


@reset_core_stats_engine()
def test_record_ml_event_truncation():
@validate_ml_events([(_intrinsics, {"a": "a" * 4096})])
@background_task()
def _test():
record_ml_event("LabelEvent", {"a": "a" * 4100})

_test()


@pytest.mark.parametrize(
"params,expected",
[
Expand Down

0 comments on commit c55326d

Please sign in to comment.