@@ -23,6 +23,15 @@ class GoogleCloudLoggingSender(LogSender):
23
23
0 : "EMERGENCY" ,
24
24
}
25
25
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
+
26
35
def __init__ (self , * , config , googleapiclient_request_builder = None , ** kwargs ):
27
36
super ().__init__ (config = config , max_send_interval = config .get ("max_send_interval" , 0.3 ), ** kwargs )
28
37
credentials = None
@@ -62,6 +71,18 @@ def send_messages(self, *, messages, cursor):
62
71
for message in messages :
63
72
msg_str = message .decode ("utf8" )
64
73
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 }
65
86
timestamp = msg .pop ("timestamp" , None )
66
87
journald_priority = msg .pop ("PRIORITY" , None )
67
88
@@ -75,7 +96,7 @@ def send_messages(self, *, messages, cursor):
75
96
if timestamp is not None :
76
97
entry ["timestamp" ] = timestamp [:26 ] + "Z" # assume timestamp to be UTC
77
98
if journald_priority is not None :
78
- severity = GoogleCloudLoggingSender ._SEVERITY_MAPPING .get (journald_priority , "DEFAULT" )
99
+ severity = self ._SEVERITY_MAPPING .get (journald_priority , "DEFAULT" )
79
100
entry ["severity" ] = severity
80
101
body ["entries" ].append (entry )
81
102
0 commit comments