1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- """Client for interacting with the Stackdriver Logging API"""
15+ """Client for interacting with the Stackdriver Error Reporting API"""
1616
17+ import os
1718import traceback
1819
19- import google .cloud .logging .client
20+ try :
21+ from google .cloud .error_reporting ._gax import make_report_error_api
22+ _HAVE_GAX = True
23+ except ImportError : # pragma: NO COVER
24+ _HAVE_GAX = False
25+
26+ from google .cloud ._helpers import _determine_default_project
27+ from google .cloud .error_reporting ._logging import _ErrorReportingLoggingAPI
28+ from google .cloud .environment_vars import DISABLE_GRPC
29+
2030import six
2131
32+ _DISABLE_GAX = os .getenv (DISABLE_GRPC , False )
33+ _USE_GAX = _HAVE_GAX and not _DISABLE_GAX
34+
2235
2336class HTTPContext (object ):
2437 """HTTPContext defines an object that captures the parameter for the
@@ -96,6 +109,12 @@ class Client(object):
96109 SHA-1 hash, for example. If the developer did not provide
97110 a version, the value is set to default.
98111
112+ :type use_gax: bool
113+ :param use_gax: (Optional) Explicitly specifies whether
114+ to use the gRPC transport (via GAX) or HTTP. If unset,
115+ falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
116+ variable.
117+
99118 :raises: :class:`ValueError` if the project is neither passed in nor
100119 set in the environment.
101120 """
@@ -104,24 +123,56 @@ def __init__(self, project=None,
104123 credentials = None ,
105124 http = None ,
106125 service = None ,
107- version = None ):
108- self .logging_client = google .cloud .logging .client .Client (
109- project , credentials , http )
126+ version = None ,
127+ use_gax = None ):
128+ if project is None :
129+ self ._project = _determine_default_project ()
130+ else :
131+ self ._project = project
132+ self ._credentials = credentials
133+ self ._http = http
134+
135+ self ._report_errors_api = None
136+
110137 self .service = service if service else self .DEFAULT_SERVICE
111138 self .version = version
139+ if use_gax is None :
140+ self ._use_gax = _USE_GAX
141+ else :
142+ self ._use_gax = use_gax
112143
113144 DEFAULT_SERVICE = 'python'
114145
115- def _send_error_report ( self , message ,
116- report_location = None , http_context = None , user = None ):
117- """Makes the call to the Error Reporting API via the log stream .
146+ @ property
147+ def report_errors_api ( self ):
148+ """Helper for logging-related API calls .
118149
119- This is the lower-level interface to build the payload, generally
120- users will use either report() or report_exception() to automatically
121- gather the parameters for this method.
150+ See:
151+ https://cloud.google.com/logging/docs/api/reference/rest/v2/entries
152+ https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.logs
122153
123- Currently this method sends the Error Report by formatting a structured
124- log message according to
154+ :rtype:
155+ :class:`_gax._ErrorReportingGaxApi`
156+ or
157+ :class:`._logging._ErrorReportingLoggingAPI`
158+ :returns: A class that implements the report errors API.
159+ """
160+ if self ._report_errors_api is None :
161+ if self ._use_gax :
162+ self ._report_errors_api = make_report_error_api (self ._project )
163+ else :
164+ self ._report_errors_api = _ErrorReportingLoggingAPI (
165+ self ._project , self ._credentials , self ._http )
166+ return self ._report_errors_api
167+
168+ def _build_error_report (self ,
169+ message ,
170+ report_location = None ,
171+ http_context = None ,
172+ user = None ):
173+ """Builds the Error Reporting object to report.
174+
175+ This builds the object according to
125176
126177 https://cloud.google.com/error-reporting/docs/formatting-error-messages
127178
@@ -151,7 +202,10 @@ def _send_error_report(self, message,
151202 logged in. In this case the Error Reporting system will
152203 use other data, such as remote IP address,
153204 to distinguish affected users.
154- """
205+ :rtype: dict
206+ :returns: A dict payload ready to be serialized to JSON and sent to
207+ the API.
208+ """
155209 payload = {
156210 'serviceContext' : {
157211 'service' : self .service ,
@@ -178,9 +232,49 @@ def _send_error_report(self, message,
178232
179233 if user :
180234 payload ['context' ]['user' ] = user
235+ return payload
236+
237+ def _send_error_report (self ,
238+ message ,
239+ report_location = None ,
240+ http_context = None ,
241+ user = None ):
242+ """Makes the call to the Error Reporting API.
243+
244+ This is the lower-level interface to build and send the payload,
245+ generally users will use either report() or report_exception() to
246+ automatically gather the parameters for this method.
181247
182- logger = self .logging_client .logger ('errors' )
183- logger .log_struct (payload )
248+ :type message: str
249+ :param message: The stack trace that was reported or logged by the
250+ service.
251+
252+ :type report_location: dict
253+ :param report_location: The location in the source code where the
254+ decision was made to report the error, usually the place
255+ where it was logged. For a logged exception this would be the
256+ source line where the exception is logged, usually close to
257+ the place where it was caught.
258+
259+ This should be a Python dict that contains the keys 'filePath',
260+ 'lineNumber', and 'functionName'
261+
262+ :type http_context: :class`google.cloud.error_reporting.HTTPContext`
263+ :param http_context: The HTTP request which was processed when the
264+ error was triggered.
265+
266+ :type user: str
267+ :param user: The user who caused or was affected by the crash. This can
268+ be a user ID, an email address, or an arbitrary token that
269+ uniquely identifies the user. When sending an error
270+ report, leave this field empty if the user was not
271+ logged in. In this case the Error Reporting system will
272+ use other data, such as remote IP address,
273+ to distinguish affected users.
274+ """
275+ error_report = self ._build_error_report (message , report_location ,
276+ http_context , user )
277+ self .report_errors_api .report_error_event (error_report )
184278
185279 def report (self , message , http_context = None , user = None ):
186280 """ Reports a message to Stackdriver Error Reporting
0 commit comments