@@ -23,6 +23,15 @@ class GoogleCloudLoggingSender(LogSender):
2323 0 : "EMERGENCY" ,
2424 }
2525
26+ # A bit on the safe side, not exactly 256KB but this
27+ # is an approximation anyway
28+ # according to https://cloud.google.com/logging/quotas
29+ _LOG_ENTRY_QUOTA = 250 * 1024
30+
31+ # Somewhat arbitrary maximum message size choosen, this gives a 56K
32+ # headroom for the other fields in the LogEntry
33+ _MAX_MESSAGE_SIZE = 200 * 1024
34+
2635 def __init__ (self , * , config , googleapiclient_request_builder = None , ** kwargs ):
2736 super ().__init__ (config = config , max_send_interval = config .get ("max_send_interval" , 0.3 ), ** kwargs )
2837 credentials = None
@@ -62,6 +71,18 @@ def send_messages(self, *, messages, cursor):
6271 for message in messages :
6372 msg_str = message .decode ("utf8" )
6473 msg = json .loads (msg_str )
74+
75+ # This might not measure exactly 256K but should be a good enough approximation to handle this error.
76+ # We try truncating the message if it isn't possible then it is skip.
77+ if len (message ) > self ._LOG_ENTRY_QUOTA :
78+ DEFAULT_MESSAGE = "Log entry can't be logged because its size is greater than GCP logging quota of 256K"
79+ if "MESSAGE" in msg :
80+ msg ["MESSAGE" ] = f'{ msg ["MESSAGE" ][:self ._MAX_MESSAGE_SIZE ]} [MESSAGE TRUNCATED]'
81+ messsage_size = len (json .dumps (msg , ensure_ascii = False ).encode ("utf-8" ))
82+ if messsage_size > self ._LOG_ENTRY_QUOTA :
83+ msg = {"MESSAGE" : DEFAULT_MESSAGE }
84+ else :
85+ msg = {"MESSAGE" : DEFAULT_MESSAGE }
6586 timestamp = msg .pop ("timestamp" , None )
6687 journald_priority = msg .pop ("PRIORITY" , None )
6788
@@ -75,7 +96,7 @@ def send_messages(self, *, messages, cursor):
7596 if timestamp is not None :
7697 entry ["timestamp" ] = timestamp [:26 ] + "Z" # assume timestamp to be UTC
7798 if journald_priority is not None :
78- severity = GoogleCloudLoggingSender ._SEVERITY_MAPPING .get (journald_priority , "DEFAULT" )
99+ severity = self ._SEVERITY_MAPPING .get (journald_priority , "DEFAULT" )
79100 entry ["severity" ] = severity
80101 body ["entries" ].append (entry )
81102
0 commit comments