|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Client for interacting with the Stackdriver Logging API""" |
| 17 | + |
| 18 | +import traceback |
| 19 | + |
| 20 | +import gcloud.logging.client |
| 21 | + |
| 22 | + |
| 23 | +class Client(object): |
| 24 | + """Error Reporting client. Currently Error Reporting is done by creating |
| 25 | + a Logging client. |
| 26 | +
|
| 27 | + :type project: string |
| 28 | + :param project: the project which the client acts on behalf of. If not |
| 29 | + passed falls back to the default inferred from the |
| 30 | + environment. |
| 31 | +
|
| 32 | + :type credentials: :class:`oauth2client.client.OAuth2Credentials` or |
| 33 | + :class:`NoneType` |
| 34 | + :param credentials: The OAuth2 Credentials to use for the connection |
| 35 | + owned by this client. If not passed (and if no ``http`` |
| 36 | + object is passed), falls back to the default inferred |
| 37 | + from the environment. |
| 38 | +
|
| 39 | + :type http: :class:`httplib2.Http` or class that defines ``request()``. |
| 40 | + :param http: An optional HTTP object to make requests. If not passed, an |
| 41 | + ``http`` object is created that is bound to the |
| 42 | + ``credentials`` for the current object. |
| 43 | +
|
| 44 | + :type service: str |
| 45 | + :param service: An identifier of the service, such as the name of the |
| 46 | + executable, job, or Google App Engine module name. This |
| 47 | + field is expected to have a low number of values that are |
| 48 | + relatively stable over time, as opposed to version, |
| 49 | + which can be changed whenever new code is deployed. |
| 50 | +
|
| 51 | +
|
| 52 | + :type version: str |
| 53 | + :param version: Represents the source code version that the developer |
| 54 | + provided, which could represent a version label or a Git |
| 55 | + SHA-1 hash, for example. If the developer did not provide |
| 56 | + a version, the value is set to default. |
| 57 | +
|
| 58 | + :raises: :class:`ValueError` if the project is neither passed in nor |
| 59 | + set in the environment. |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self, project=None, |
| 63 | + credentials=None, |
| 64 | + http=None, |
| 65 | + service=None, |
| 66 | + version=None): |
| 67 | + self.logging_client = gcloud.logging.client.Client( |
| 68 | + project, credentials, http) |
| 69 | + self.service = service |
| 70 | + self.version = version |
| 71 | + |
| 72 | + DEFAULT_SERVICE = 'python' |
| 73 | + DEFAULT_VERSION = 'default' |
| 74 | + |
| 75 | + def _get_default_service(self): |
| 76 | + """Returns the service to use on method calls that don't specify an |
| 77 | + override. |
| 78 | +
|
| 79 | + :rtype: string |
| 80 | + :returns: The default service for error reporting calls |
| 81 | + """ |
| 82 | + if self.service: |
| 83 | + return self.service |
| 84 | + else: |
| 85 | + return self.DEFAULT_SERVICE |
| 86 | + |
| 87 | + def _get_default_version(self): |
| 88 | + """Returns the service to use on method calls that don't specify an |
| 89 | + override. |
| 90 | +
|
| 91 | + :rtype: string |
| 92 | + :returns: The default version for error reporting calls. |
| 93 | + """ |
| 94 | + if self.version: |
| 95 | + return self.version |
| 96 | + else: |
| 97 | + return self.DEFAULT_VERSION |
| 98 | + |
| 99 | + def report_error(self, message="", service=None, version=None): |
| 100 | + """ Reports the details of the latest exceptions to Stackdriver Error |
| 101 | + Reporting. |
| 102 | +
|
| 103 | + https://cloud.google.com/error-reporting/docs/formatting-error-messages |
| 104 | +
|
| 105 | + :type message: str |
| 106 | + :param message: An optional message to include with the exception |
| 107 | + detail |
| 108 | +
|
| 109 | + :type service: str |
| 110 | + :param service: An identifier of the service, such as the name of |
| 111 | + the executable, job, or Google App Engine module |
| 112 | + name. This field is expected to have a low number |
| 113 | + of values that are relatively stable over time, |
| 114 | + as opposed to version, which can be changed |
| 115 | + whenever new code is deployed. |
| 116 | +
|
| 117 | + :type version: str |
| 118 | + :param version: Represents the source code version that the |
| 119 | + developer provided, which could represent a |
| 120 | + version label or a Git SHA-1 hash, for example. If |
| 121 | + the developer did not provide a version, the value |
| 122 | + is set to default. |
| 123 | +
|
| 124 | +
|
| 125 | + Example:: |
| 126 | +
|
| 127 | + >>> try: |
| 128 | + >>> raise NameError |
| 129 | + >>> except Exception: |
| 130 | + >>> client.report_error("Something went wrong!") |
| 131 | + """ |
| 132 | + if not service: |
| 133 | + service = self._get_default_service() |
| 134 | + if not version: |
| 135 | + version = self._get_default_version() |
| 136 | + payload = { |
| 137 | + 'serviceContext': { |
| 138 | + 'service': service, |
| 139 | + 'version': version |
| 140 | + }, |
| 141 | + 'message': '{0} : {1}'.format(message, traceback.format_exc()) |
| 142 | + } |
| 143 | + payload['serviceContext']['version'] = version |
| 144 | + logger = self.logging_client.logger('errors') |
| 145 | + logger.log_struct(payload) |
0 commit comments