blob: df981190158bcc77adc6a3dc03c906e77e546111 [file] [log] [blame]
Xixuan Wu6f117e92017-10-27 10:51:58 -07001# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module for stackdriver related functions."""
6
7import os
8
9
Xixuan Wu69a5e782017-11-06 14:32:05 -080010# The format of loggings.
Xixuan Wu6f117e92017-10-27 10:51:58 -070011LOGGING_FORMAT = ('%(time)s %(levelname)-5.5s|%(module)'
12 '18.18s:%(lineno)4.4s| %(message)s')
13
14
15def form_logging_client_request(project_id, utc_start_time, utc_end_time,
16 resource_name, order_by='timestamp asc'):
17 """Form a request that can be used by stackdriver logger API.
18
19 Args:
20 project_id: the GAE project id.
21 utc_start_time: A string representing UTC time, in format of
22 converter.STACKDRIVER_TIME_FORMAT.
23 utc_end_time: A string representing UTC time, in format of
24 converter.STACKDRIVER_TIME_FORMAT.
25 resource_name: the resouce of the logs to fetch.
26 order_by: the order of the logs. By default it's asc in time.
27
28 Returns:
29 A dict representing formalized request.
30 """
31 request = {}
32 request['projectIds'] = [project_id]
33 request['orderBy'] = order_by
34 request['filter'] = (
35 'protoPayload.resource = "%s" AND '
36 'protoPayload.startTime > "%s" AND '
37 'protoPayload.endTime < "%s"' % (
38 resource_name, utc_start_time, utc_end_time))
39 return request
40
41
42def parse_logging_client_response(logs):
43 """Parse json object returned by stackDriver client.
44
45 Args:
46 logs: A json object includes all logs.
47
48 Returns:
49 A string includes all lines of logs, where each line is split by
50 a newline.
51 """
52 response = ''
53 if 'entries' not in logs:
54 return response
55
56 for entry in logs['entries']:
57 if 'line' not in entry['protoPayload']:
58 continue
59
60 for line in entry['protoPayload']['line']:
61 response += _form_logging_line(line) + '\n'
62
63 return response
64
65
66def _form_logging_line(line):
67 """Form each line of logs with specified format.
68
69 Args:
70 line: A json object including all required information of a logging line.
71
72 Returns:
73 A string of logs formatted by LOGGING_FORMAT.
74 """
75 module, lineno = _parse_file_info(line)
76 formatted_line = LOGGING_FORMAT % {'time': line['time'],
77 'levelname': line['severity'],
78 'module': module,
79 'lineno': lineno,
80 'message': line['logMessage']}
81 return formatted_line
82
83
84def _parse_file_info(line):
85 """Parse the file information from line object.
86
87 Args:
88 line: A json object including file location and line number.
89
90 Returns:
91 A tuple of file location & line number in file.
92 """
93 try:
94 source = line['sourceLocation']
95 return os.path.basename(source['file']), source['line']
96 except KeyError:
97 return '', ''