File tree Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -29,11 +29,28 @@ def process(self, element: bytes) -> None:
29
29
# Parse the Pub/Sub data
30
30
data = json .loads (element .decode ('utf-8' ))
31
31
32
- # Rename timestamp key to dt to be understood by Better Stack
33
- if 'timestamp' in data :
34
- data ['dt' ] = data .pop ('timestamp' )
32
+ # Process data for readability
33
+ parsed_data = {}
34
+ for key in data :
35
+ if key == 'timestamp' :
36
+ # Rename timestamp key to dt to be understood by Better Stack
37
+ parsed_data ['dt' ] = data ['timestamp' ]
38
+ elif key == 'logName' :
39
+ # Parse project and log type out of logName
40
+ # Example logName: projects/excited-meercat-123456-a1/logs/dataflow.googleapis.com%2Fvm-monitor
41
+ # logProject: excited-meercat-123456-a1
42
+ # logType: dataflow.googleapis.com/vm-monitor
43
+ parsed_data ['logName' ] = data ['logName' ]
44
+ try :
45
+ log_project , log_type = data ['logName' ].split ('/logs/' )
46
+ parsed_data ['logProject' ] = log_project .split ('/' )[- 1 ]
47
+ parsed_data ['logType' ] = requests .utils .unquote (log_type )
48
+ except ValueError as e :
49
+ print (f"Could not parse log_pro and log type out of logName '{ data ['logName' ]} ': { str (e )} " )
50
+ else :
51
+ parsed_data [key ] = data [key ]
35
52
36
- self .batch .append (data )
53
+ self .batch .append (parsed_data )
37
54
38
55
# If we've reached the batch size, send the batch
39
56
if len (self .batch ) >= self .batch_size :
You can’t perform that action at this time.
0 commit comments