Skip to content

Commit 6caf71e

Browse files
committed
Add dimensional stats table to stats engine
1 parent 5e1cc9d commit 6caf71e

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

newrelic/core/application.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ def __init__(self, app_name, linked_applications=None):
106106
self._stats_custom_lock = threading.RLock()
107107
self._stats_custom_engine = StatsEngine()
108108

109-
self._stats_dimensional_lock = threading.RLock()
110-
self._stats_dimensional_engine = StatsEngine()
111-
112109
self._agent_commands_lock = threading.Lock()
113110
self._data_samplers_lock = threading.Lock()
114111
self._data_samplers_started = False
@@ -513,8 +510,8 @@ def connect_to_data_collector(self, activate_agent):
513510
with self._stats_custom_lock:
514511
self._stats_custom_engine.reset_stats(configuration)
515512

516-
with self._stats_dimensional_lock:
517-
self._stats_dimensional_engine.reset_stats(configuration)
513+
with self._stats_lock:
514+
self._stats_engine.reset_stats(configuration)
518515

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

885-
with self._stats_dimensional_lock:
882+
with self._stats_lock:
886883
self._global_events_account += 1
887-
self._stats_dimensional_engine.record_dimensional_metric(name, value, tags)
884+
self._stats_engine.record_dimensional_metric(name, value, tags)
888885

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

905-
with self._stats_dimensional_lock:
902+
with self._stats_lock:
906903
for metric in metrics:
907904
name, value = metric[:2]
908905
tags = metric[2] if len(metric) >= 3 else None
909906

910907
self._global_events_account += 1
911-
self._stats_dimensional_engine.record_dimensional_metric(name, value, tags)
908+
self._stats_engine.record_dimensional_metric(name, value, tags)
912909

913910
def record_custom_event(self, event_type, params):
914911
if not self._active_session:

newrelic/core/stats_engine.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class DimensionalMetrics(CustomMetrics):
247247
def __contains__(self, key):
248248
if not isinstance(key[1], frozenset):
249249
# Convert tags dict to a frozen set for proper comparisons
250-
create_metric_identity(*key)
250+
key = create_metric_identity(*key)
251251
return key in self.__stats_table
252252

253253
def record_dimensional_metric(self, name, value, tags=None):
@@ -259,6 +259,16 @@ def record_dimensional_metric(self, name, value, tags=None):
259259
key = create_metric_identity(name, tags)
260260
self.record_custom_metric(key, value)
261261

262+
class DimensionalStatsTable(dict):
263+
264+
"""Extends dict to coerce a set of tags to a hashable identity."""
265+
266+
def __contains__(self, key):
267+
if key[1] is not None and not isinstance(key[1], frozenset):
268+
# Convert tags dict to a frozen set for proper comparisons
269+
key = create_metric_identity(*key)
270+
return super(DimensionalStatsTable, self).__contains__(key)
271+
262272

263273
class SlowSqlStats(list):
264274
def __init__(self):
@@ -458,7 +468,7 @@ class StatsEngine(object):
458468
def __init__(self):
459469
self.__settings = None
460470
self.__stats_table = {}
461-
self.__dimensional_stats_table = {}
471+
self.__dimensional_stats_table = DimensionalStatsTable()
462472
self._transaction_events = SampledDataSet()
463473
self._error_events = SampledDataSet()
464474
self._custom_events = SampledDataSet()
@@ -908,7 +918,7 @@ def record_dimensional_metric(self, name, value, tags=None):
908918
else:
909919
new_stats = TimeStats(1, value, value, value, value, value**2)
910920

911-
key = create_metric_identity(name, tags=None)
921+
key = create_metric_identity(name, tags)
912922
stats = self.__dimensional_stats_table.get(key)
913923
if stats is None:
914924
self.__dimensional_stats_table[key] = new_stats
@@ -1568,6 +1578,7 @@ def reset_stats(self, settings, reset_stream=False):
15681578

15691579
self.__settings = settings
15701580
self.__stats_table = {}
1581+
self.__dimensional_stats_table = {}
15711582
self.__sql_stats_table = {}
15721583
self.__slow_transaction = None
15731584
self.__slow_transaction_map = {}
@@ -1594,6 +1605,7 @@ def reset_metric_stats(self):
15941605
"""
15951606

15961607
self.__stats_table = {}
1608+
self.__dimensional_stats_table = {}
15971609

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

0 commit comments

Comments
 (0)