11import datetime
22import json
3- import random
3+ import logging
44import os
5+ import random
56
67from opencensus .common .schedule import PeriodicTask
78
9+ logger = logging .getLogger (__name__ )
10+
811
912def _fmt (timestamp ):
1013 return timestamp .strftime ('%Y-%m-%dT%H%M%S.%f' )
@@ -22,25 +25,23 @@ class LocalFileBlob(object):
2225 def __init__ (self , fullpath ):
2326 self .fullpath = fullpath
2427
25- def delete (self , silent = False ):
28+ def delete (self ):
2629 try :
2730 os .remove (self .fullpath )
2831 except Exception :
29- if not silent :
30- raise
32+ pass # keep silent
3133
32- def get (self , silent = False ):
34+ def get (self ):
3335 try :
3436 with open (self .fullpath , 'r' ) as file :
3537 return tuple (
3638 json .loads (line .strip ())
3739 for line in file .readlines ()
3840 )
3941 except Exception :
40- if not silent :
41- raise
42+ pass # keep silent
4243
43- def put (self , data , lease_period = 0 , silent = False ):
44+ def put (self , data , lease_period = 0 ):
4445 try :
4546 fullpath = self .fullpath + '.tmp'
4647 with open (fullpath , 'w' ) as file :
@@ -56,8 +57,7 @@ def put(self, data, lease_period=0, silent=False):
5657 os .rename (fullpath , self .fullpath )
5758 return self
5859 except Exception :
59- if not silent :
60- raise
60+ pass # keep silent
6161
6262 def lease (self , period ):
6363 timestamp = _now () + _seconds (period )
@@ -77,7 +77,7 @@ class LocalFileStorage(object):
7777 def __init__ (
7878 self ,
7979 path ,
80- max_size = 100 * 1024 * 1024 , # 100MB
80+ max_size = 50 * 1024 * 1024 , # 50MiB
8181 maintenance_period = 60 , # 1 minute
8282 retention_period = 7 * 24 * 60 * 60 , # 7 days
8383 write_timeout = 60 , # 1 minute
@@ -87,11 +87,11 @@ def __init__(
8787 self .maintenance_period = maintenance_period
8888 self .retention_period = retention_period
8989 self .write_timeout = write_timeout
90- self ._maintenance_routine (silent = False )
90+ # Run maintenance routine once upon instantiating
91+ self ._maintenance_routine ()
9192 self ._maintenance_task = PeriodicTask (
9293 interval = self .maintenance_period ,
9394 function = self ._maintenance_routine ,
94- kwargs = {'silent' : True },
9595 )
9696 self ._maintenance_task .daemon = True
9797 self ._maintenance_task .start ()
@@ -106,19 +106,18 @@ def __enter__(self):
106106 def __exit__ (self , type , value , traceback ):
107107 self .close ()
108108
109- def _maintenance_routine (self , silent = False ):
109+ def _maintenance_routine (self ):
110110 try :
111111 if not os .path .isdir (self .path ):
112112 os .makedirs (self .path )
113113 except Exception :
114- if not silent :
115- raise
114+ # Race case will throw OSError which we can ignore
115+ pass
116116 try :
117117 for blob in self .gets ():
118118 pass
119119 except Exception :
120- if not silent :
121- raise
120+ pass # keep silent
122121
123122 def gets (self ):
124123 now = _now ()
@@ -161,12 +160,39 @@ def get(self):
161160 pass
162161 return None
163162
164- def put (self , data , lease_period = 0 , silent = False ):
163+ def put (self , data , lease_period = 0 ):
164+ if not self ._check_storage_size ():
165+ return None
165166 blob = LocalFileBlob (os .path .join (
166167 self .path ,
167168 '{}-{}.blob' .format (
168169 _fmt (_now ()),
169170 '{:08x}' .format (random .getrandbits (32 )), # thread-safe random
170171 ),
171172 ))
172- return blob .put (data , lease_period = lease_period , silent = silent )
173+ return blob .put (data , lease_period = lease_period )
174+
175+ def _check_storage_size (self ):
176+ size = 0
177+ for dirpath , dirnames , filenames in os .walk (self .path ):
178+ for f in filenames :
179+ fp = os .path .join (dirpath , f )
180+ # skip if it is symbolic link
181+ if not os .path .islink (fp ):
182+ try :
183+ size += os .path .getsize (fp )
184+ except OSError :
185+ logger .error (
186+ "Path %s does not exist or is inaccessible." , fp
187+ )
188+ continue
189+ if size >= self .max_size :
190+ logger .warning (
191+ "Persistent storage max capacity has been "
192+ "reached. Currently at %fKB. Telemetry will be "
193+ "lost. Please consider increasing the value of "
194+ "'storage_max_size' in exporter config." ,
195+ format (size / 1024 )
196+ )
197+ return False
198+ return True
0 commit comments