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