Content-Length: 719816 | pFad | http://github.com/googleapis/google-cloud-python/commit/2ee9e38c05f8aadd573edc5c2a3dd8bfb1d8891d

E1 Add GAX to Error Reporting · googleapis/google-cloud-python@2ee9e38 · GitHub
Skip to content

Commit 2ee9e38

Browse files
author
Bill Prin
committed
Add GAX to Error Reporting
1 parent 80d2bee commit 2ee9e38

File tree

3 files changed

+188
-17
lines changed

3 files changed

+188
-17
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""GAX wrapper for Error Reporting API requests."""
16+
17+
from google.cloud.gapic.errorreporting.v1beta1 import report_errors_service_api
18+
from google.devtools.clouderrorreporting.v1beta1 import report_errors_service_pb2
19+
from google.protobuf.json_format import ParseDict
20+
21+
22+
def make_report_error_api(project):
23+
"""Create an instance of the GAX Logging API."""
24+
api = report_errors_service_api.ReportErrorsServiceApi()
25+
return _ErrorReportingGaxApi(api, project)
26+
27+
28+
class _ErrorReportingGaxApi(object):
29+
"""Helper mapping Error Reporting-related APIs
30+
31+
:type gax_api:
32+
:class:`google.cloud.gapic.errorreporting.v1beta1
33+
.report_errors_service_api.report_errors_service_api`
34+
:param gax_api: API object used to make GAX requests.
35+
"""
36+
def __init__(self, gax_api, project):
37+
self._gax_api = gax_api
38+
self._project = project
39+
40+
def report_error_event(self, error_report):
41+
"""Uses the GAX client to report the error.
42+
43+
:type project: str
44+
:param: project: Project ID to report the error to
45+
46+
:type error: dict:
47+
:param: error: dict payload of the error report formatted
48+
according to
49+
https://cloud.google.com/error-reporting/docs/formatting-error-messages
50+
This object should be built using
51+
Use :meth:~`google.cloud.error_reporting.client._build_error_report`
52+
"""
53+
project_name = self._gax_api.project_path(self._project)
54+
error_report_payload = report_errors_service_pb2.ReportedErrorEvent()
55+
ParseDict(error_report, error_report_payload)
56+
self._gax_api.report_error_event(project_name, error_report_payload)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Interact with Stackdriver Error Reporting via Logging API.
16+
17+
It's possible to report Stackdriver Error Reporting errors by formatting
18+
structured log messages in Stackdriver Logging in a given format. This
19+
client provides a mechanism to report errors using that technique.
20+
"""
21+
22+
import google.cloud.logging.client
23+
24+
25+
class _ErrorReportingLoggingAPI(object):
26+
"""Report to Stackdriver Error Reporting via Logging API
27+
"""
28+
def __init__(self, project, credentials, http):
29+
self.logging_client = google.cloud.logging.client.Client(
30+
project, credentials, http)
31+
32+
def report_error_event(self, project, error_report):
33+
logger = self.logging_client.logger('errors')
34+
logger.log_struct(error_report)

error_reporting/google/cloud/error_reporting/client.py

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@
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
1718
import traceback
1819

19-
import google.cloud.logging.client
20+
try:
21+
from google.cloud.error_reporting._gax import make_report_error_api
22+
except ImportError: # pragma: NO COVER
23+
_HAVE_GAX = False
24+
else:
25+
_HAVE_GAX = True
26+
27+
from google.cloud._helpers import _determine_default_project
28+
from google.cloud.error_reporting._logging import _ErrorReportingLoggingAPI
29+
from google.cloud.environment_vars import DISABLE_GRPC
30+
2031
import six
2132

33+
_DISABLE_GAX = os.getenv(DISABLE_GRPC, False)
34+
_USE_GAX = _HAVE_GAX and not _DISABLE_GAX
35+
2236

2337
class HTTPContext(object):
2438
"""HTTPContext defines an object that captures the parameter for the
@@ -96,6 +110,12 @@ class Client(object):
96110
SHA-1 hash, for example. If the developer did not provide
97111
a version, the value is set to default.
98112
113+
:type use_gax: bool
114+
:param use_gax: (Optional) Explicitly specifies whether
115+
to use the gRPC transport (via GAX) or HTTP. If unset,
116+
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
117+
variable.
118+
99119
:raises: :class:`ValueError` if the project is neither passed in nor
100120
set in the environment.
101121
"""
@@ -104,24 +124,48 @@ def __init__(self, project=None,
104124
credentials=None,
105125
http=None,
106126
service=None,
107-
version=None):
108-
self.logging_client = google.cloud.logging.client.Client(
109-
project, credentials, http)
127+
version=None,
128+
use_gax=None):
129+
if project is None:
130+
self._project = _determine_default_project()
131+
else:
132+
self._project = project
133+
self._credentials = credentials
134+
self._http = http
135+
136+
self._report_errors_api = None
137+
110138
self.service = service if service else self.DEFAULT_SERVICE
111139
self.version = version
140+
if use_gax is None:
141+
self._use_gax = _USE_GAX
142+
else:
143+
self._use_gax = use_gax
112144

113145
DEFAULT_SERVICE = 'python'
114146

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.
147+
@property
148+
def report_errors_api(self):
149+
"""Helper for logging-related API calls.
150+
151+
See:
152+
https://cloud.google.com/logging/docs/api/reference/rest/v2/entries
153+
https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.logs
154+
"""
155+
if self._report_errors_api is None:
156+
if self._use_gax:
157+
self._report_errors_api = make_report_error_api(self._project)
158+
else:
159+
self._report_errors_api = _ErrorReportingLoggingAPI(
160+
self._project, self._credentials, self._http)
161+
return self._report_errors_api
118162

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.
122163

123-
Currently this method sends the Error Report by formatting a structured
124-
log message according to
164+
def _build_error_report(self, message,
165+
report_location=None, http_context=None, user=None):
166+
"""Builds the Error Reporting object to report.
167+
168+
This builds the object according to
125169
126170
https://cloud.google.com/error-reporting/docs/formatting-error-messages
127171
@@ -151,7 +195,7 @@ def _send_error_report(self, message,
151195
logged in. In this case the Error Reporting system will
152196
use other data, such as remote IP address,
153197
to distinguish affected users.
154-
"""
198+
"""
155199
payload = {
156200
'serviceContext': {
157201
'service': self.service,
@@ -174,13 +218,50 @@ def _send_error_report(self, message,
174218
payload['context']['httpContext'] = {
175219
key: value for key, value in six.iteritems(http_context_dict)
176220
if value is not None
177-
}
221+
}
178222

179223
if user:
180224
payload['context']['user'] = user
225+
return payload
181226

182-
logger = self.logging_client.logger('errors')
183-
logger.log_struct(payload)
227+
def _send_error_report(self, message,
228+
report_location=None, http_context=None, user=None):
229+
"""Makes the call to the Error Reporting API.
230+
231+
This is the lower-level interface to build and send the payload,
232+
generally users will use either report() or report_exception() to
233+
automatically gather the parameters for this method.
234+
235+
:type message: str
236+
:param message: The stack trace that was reported or logged by the
237+
service.
238+
239+
:type report_location: dict
240+
:param report_location: The location in the source code where the
241+
decision was made to report the error, usually the place
242+
where it was logged. For a logged exception this would be the
243+
source line where the exception is logged, usually close to
244+
the place where it was caught.
245+
246+
This should be a Python dict that contains the keys 'filePath',
247+
'lineNumber', and 'functionName'
248+
249+
:type http_context: :class`google.cloud.error_reporting.HTTPContext`
250+
:param http_context: The HTTP request which was processed when the
251+
error was triggered.
252+
253+
:type user: str
254+
:param user: The user who caused or was affected by the crash. This can
255+
be a user ID, an email address, or an arbitrary token that
256+
uniquely identifies the user. When sending an error
257+
report, leave this field empty if the user was not
258+
logged in. In this case the Error Reporting system will
259+
use other data, such as remote IP address,
260+
to distinguish affected users.
261+
"""
262+
error_report = self._build_error_report(message, report_location,
263+
http_context, user)
264+
self.report_errors_api.report_error_event(error_report)
184265

185266
def report(self, message, http_context=None, user=None):
186267
""" Reports a message to Stackdriver Error Reporting

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/2ee9e38c05f8aadd573edc5c2a3dd8bfb1d8891d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy