2323from blitzortung .gis .constants import grid , global_grid
2424from blitzortung .gis .local_grid import LocalGrid
2525from blitzortung .service .cache import ServiceCache
26+ from blitzortung .service .metrics import StatsDMetrics
2627from blitzortung .util import TimeConstraint
2728
2829try :
4748
4849is_pypy = platform .python_implementation () == 'PyPy'
4950
50- statsd_client = statsd .StatsClient ('localhost' , 8125 , prefix = 'org.blitzortung.service' )
51-
5251FORBIDDEN_IPS = {}
5352
5453USER_AGENT_PREFIX = 'bo-android-'
@@ -93,6 +92,8 @@ def __init__(self, db_connection_pool, log_directory):
9392 self .next_memory_info = 0.0
9493 self .minute_constraints = TimeConstraint (self .DEFAULT_MINUTE_LENGTH , self .MAX_MINUTES_PER_DAY )
9594
95+ self .metrics = StatsDMetrics (statsd_client )
96+
9697 addSlash = True
9798
9899 def __get_epoch (self , timestamp ):
@@ -143,7 +144,7 @@ def get_strikes_grid(self, minute_length, grid_baselength, minute_offset, region
143144 time_interval = create_time_interval (minute_length , minute_offset )
144145
145146 grid_result , state = self .strike_grid_query .create (grid_parameters , time_interval , self .connection_pool ,
146- statsd_client )
147+ self . metrics . statsd )
147148
148149 histogram_result = self .get_histogram (time_interval , envelope = grid_parameters .grid ) \
149150 if minute_length > self .HISTOGRAM_MINUTE_THRESHOLD else succeed ([])
@@ -160,7 +161,7 @@ def get_global_strikes_grid(self, minute_length, grid_baselength, minute_offset,
160161 time_interval = create_time_interval (minute_length , minute_offset )
161162
162163 grid_result , state = self .global_strike_grid_query .create (grid_parameters , time_interval , self .connection_pool ,
163- statsd_client )
164+ self . metrics . statsd )
164165
165166 histogram_result = self .get_histogram (
166167 time_interval ) if minute_length > self .HISTOGRAM_MINUTE_THRESHOLD else succeed ([])
@@ -179,7 +180,7 @@ def get_local_strikes_grid(self, x, y, grid_baselength, minute_length, minute_of
179180 time_interval = create_time_interval (minute_length , minute_offset )
180181
181182 grid_result , state = self .strike_grid_query .create (grid_parameters , time_interval , self .connection_pool ,
182- statsd_client )
183+ self . metrics . statsd )
183184
184185 histogram_result = self .get_histogram (time_interval , envelope = grid_parameters .grid ) \
185186 if minute_length > self .HISTOGRAM_MINUTE_THRESHOLD else succeed ([])
@@ -237,12 +238,7 @@ def jsonrpc_get_global_strikes_grid(self, request, minute_length, grid_base_leng
237238 minute_offset ,
238239 0 , count_threshold , client , user_agent ))
239240
240- statsd_client .incr ('strikes_grid.total_count' )
241- statsd_client .incr ('global_strikes_grid.total_count' )
242- statsd_client .gauge ('global_strikes_grid.cache_hits' , cache .get_ratio ())
243- if minute_length == 10 :
244- statsd_client .incr ('strikes_grid.bg_count' )
245- statsd_client .incr ('global_strikes_grid.bg_count' )
241+ self .metrics .for_global_strikes (minute_length , cache .get_ratio ())
246242
247243 return response
248244
@@ -289,13 +285,7 @@ def jsonrpc_get_local_strikes_grid(self, request, x, y, grid_base_length=10000,
289285 minute_offset ,
290286 - 1 , count_threshold , client , user_agent , x , y , data_area ))
291287
292- statsd_client .incr ('strikes_grid.total_count' )
293- statsd_client .incr ('local_strikes_grid.total_count' )
294- statsd_client .incr (f'local_strikes_grid.data_area.{ data_area } ' )
295- statsd_client .gauge ('local_strikes_grid.cache_hits' , cache .get_ratio ())
296- if minute_length == 10 :
297- statsd_client .incr ('strikes_grid.bg_count' )
298- statsd_client .incr ('local_strikes_grid.bg_count' )
288+ self .metrics .for_local_strikes (minute_length , data_area , cache .get_ratio ())
299289
300290 return response
301291
@@ -340,12 +330,7 @@ def jsonrpc_get_strikes_grid(self, request, minute_length, grid_base_length=1000
340330 region ,
341331 count_threshold , client , user_agent ))
342332
343- statsd_client .incr ('strikes_grid.total_count' )
344- statsd_client .incr ('strikes_grid.total_count.{}' .format (region ))
345- statsd_client .gauge ('strikes_grid.cache_hits' , cache .get_ratio ())
346- if minute_length == 10 :
347- statsd_client .incr ('strikes_grid.bg_count' )
348- statsd_client .incr ('strikes_grid.bg_count.{}' .format (region ))
333+ self .metrics .for_strikes (minute_length , region , cache .get_ratio ())
349334
350335 return response
351336
0 commit comments