Skip to content

Commit

Permalink
Add dimensional stats table to stats engine
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed May 11, 2023
1 parent 5e1cc9d commit 6caf71e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
15 changes: 6 additions & 9 deletions newrelic/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ def __init__(self, app_name, linked_applications=None):
self._stats_custom_lock = threading.RLock()
self._stats_custom_engine = StatsEngine()

self._stats_dimensional_lock = threading.RLock()
self._stats_dimensional_engine = StatsEngine()

self._agent_commands_lock = threading.Lock()
self._data_samplers_lock = threading.Lock()
self._data_samplers_started = False
Expand Down Expand Up @@ -513,8 +510,8 @@ def connect_to_data_collector(self, activate_agent):
with self._stats_custom_lock:
self._stats_custom_engine.reset_stats(configuration)

with self._stats_dimensional_lock:
self._stats_dimensional_engine.reset_stats(configuration)
with self._stats_lock:
self._stats_engine.reset_stats(configuration)

# Record an initial start time for the reporting period and
# clear record of last transaction processed.
Expand Down Expand Up @@ -882,9 +879,9 @@ def record_dimensional_metric(self, name, value, tags=None):
if not self._active_session:
return

with self._stats_dimensional_lock:
with self._stats_lock:
self._global_events_account += 1
self._stats_dimensional_engine.record_dimensional_metric(name, value, tags)
self._stats_engine.record_dimensional_metric(name, value, tags)

def record_dimensional_metrics(self, metrics):
"""Record a set of dimensional metrics against the application
Expand All @@ -902,13 +899,13 @@ def record_dimensional_metrics(self, metrics):
if not self._active_session:
return

with self._stats_dimensional_lock:
with self._stats_lock:
for metric in metrics:
name, value = metric[:2]
tags = metric[2] if len(metric) >= 3 else None

self._global_events_account += 1
self._stats_dimensional_engine.record_dimensional_metric(name, value, tags)
self._stats_engine.record_dimensional_metric(name, value, tags)

def record_custom_event(self, event_type, params):
if not self._active_session:
Expand Down
18 changes: 15 additions & 3 deletions newrelic/core/stats_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class DimensionalMetrics(CustomMetrics):
def __contains__(self, key):
if not isinstance(key[1], frozenset):
# Convert tags dict to a frozen set for proper comparisons
create_metric_identity(*key)
key = create_metric_identity(*key)
return key in self.__stats_table

def record_dimensional_metric(self, name, value, tags=None):
Expand All @@ -259,6 +259,16 @@ def record_dimensional_metric(self, name, value, tags=None):
key = create_metric_identity(name, tags)
self.record_custom_metric(key, value)

class DimensionalStatsTable(dict):

"""Extends dict to coerce a set of tags to a hashable identity."""

def __contains__(self, key):
if key[1] is not None and not isinstance(key[1], frozenset):
# Convert tags dict to a frozen set for proper comparisons
key = create_metric_identity(*key)
return super(DimensionalStatsTable, self).__contains__(key)


class SlowSqlStats(list):
def __init__(self):
Expand Down Expand Up @@ -458,7 +468,7 @@ class StatsEngine(object):
def __init__(self):
self.__settings = None
self.__stats_table = {}
self.__dimensional_stats_table = {}
self.__dimensional_stats_table = DimensionalStatsTable()
self._transaction_events = SampledDataSet()
self._error_events = SampledDataSet()
self._custom_events = SampledDataSet()
Expand Down Expand Up @@ -908,7 +918,7 @@ def record_dimensional_metric(self, name, value, tags=None):
else:
new_stats = TimeStats(1, value, value, value, value, value**2)

key = create_metric_identity(name, tags=None)
key = create_metric_identity(name, tags)
stats = self.__dimensional_stats_table.get(key)
if stats is None:
self.__dimensional_stats_table[key] = new_stats
Expand Down Expand Up @@ -1568,6 +1578,7 @@ def reset_stats(self, settings, reset_stream=False):

self.__settings = settings
self.__stats_table = {}
self.__dimensional_stats_table = {}
self.__sql_stats_table = {}
self.__slow_transaction = None
self.__slow_transaction_map = {}
Expand All @@ -1594,6 +1605,7 @@ def reset_metric_stats(self):
"""

self.__stats_table = {}
self.__dimensional_stats_table = {}

def reset_transaction_events(self):
"""Resets the accumulated statistics back to initial state for
Expand Down

0 comments on commit 6caf71e

Please sign in to comment.