forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Error Reporting GAPIC as dependency (googleapis#2894)
* Add Error Reporting GAPIC as dependency This both adds the GAPIC to be used directly via this package, as well as hooking it up to the existing helpers to provide gRPC support.
- Loading branch information
Showing
8 changed files
with
399 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""GAX wrapper for Error Reporting API requests.""" | ||
|
||
from google.cloud._helpers import make_secure_channel | ||
from google.cloud._http import DEFAULT_USER_AGENT | ||
|
||
from google.cloud.gapic.errorreporting.v1beta1 import ( | ||
report_errors_service_client) | ||
from google.cloud.grpc.devtools.clouderrorreporting.v1beta1 import ( | ||
report_errors_service_pb2) | ||
from google.protobuf.json_format import ParseDict | ||
|
||
|
||
def make_report_error_api(client): | ||
"""Create an instance of the GAX Logging API. | ||
:type client::class:`google.cloud.error_reporting.Client` | ||
:param client: Error Reporting client. | ||
:rtype: :class:_ErrorReportingGaxApi | ||
:returns: An Error Reporting API instance. | ||
""" | ||
channel = make_secure_channel( | ||
client._connection.credentials, | ||
DEFAULT_USER_AGENT, | ||
report_errors_service_client.ReportErrorsServiceClient.SERVICE_ADDRESS) | ||
gax_client = report_errors_service_client.ReportErrorsServiceClient( | ||
channel=channel) | ||
return _ErrorReportingGaxApi(gax_client, client.project) | ||
|
||
|
||
class _ErrorReportingGaxApi(object): | ||
"""Helper mapping Error Reporting-related APIs | ||
:type gax_api: | ||
:class:`v1beta1.report_errors_service_client.ReportErrorsServiceClient` | ||
:param gax_api: API object used to make GAX requests. | ||
:type project: str | ||
:param project: Google Cloud Project ID | ||
""" | ||
|
||
def __init__(self, gax_api, project): | ||
self._gax_api = gax_api | ||
self._project = project | ||
|
||
def report_error_event(self, error_report): | ||
"""Uses the GAX client to report the error. | ||
:type error_report: dict | ||
:param error_report: | ||
payload of the error report formatted according to | ||
https://cloud.google.com/error-reporting/docs/formatting-error-messages | ||
This object should be built using | ||
Use | ||
:meth:~`google.cloud.error_reporting.client._build_error_report` | ||
""" | ||
project_name = self._gax_api.project_path(self._project) | ||
error_report_payload = report_errors_service_pb2.ReportedErrorEvent() | ||
ParseDict(error_report, error_report_payload) | ||
self._gax_api.report_error_event(project_name, error_report_payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Interact with Stackdriver Error Reporting via Logging API. | ||
It's possible to report Stackdriver Error Reporting errors by formatting | ||
structured log messages in Stackdriver Logging in a given format. This | ||
client provides a mechanism to report errors using that technique. | ||
""" | ||
|
||
import google.cloud.logging.client | ||
|
||
|
||
class _ErrorReportingLoggingAPI(object): | ||
"""Report to Stackdriver Error Reporting via Logging API | ||
:type project: str | ||
:param project: the project which the client acts on behalf of. If not | ||
passed falls back to the default inferred from the | ||
environment. | ||
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or | ||
:class:`NoneType` | ||
:param credentials: The OAuth2 Credentials to use for the connection | ||
owned by this client. If not passed (and if no ``http`` | ||
object is passed), falls back to the default inferred | ||
from the environment. | ||
:type http: :class:`httplib2.Http` or class that defines ``request()``. | ||
:param http: An optional HTTP object to make requests. If not passed, an | ||
``http`` object is created that is bound to the | ||
``credentials`` for the current object. | ||
""" | ||
def __init__(self, project, credentials=None, http=None): | ||
self.logging_client = google.cloud.logging.client.Client( | ||
project, credentials, http) | ||
|
||
def report_error_event(self, error_report): | ||
"""Report error payload. | ||
:type error_report: dict | ||
:param: error_report: | ||
dict payload of the error report formatted according to | ||
https://cloud.google.com/error-reporting/docs/formatting-error-messages | ||
This object should be built using | ||
:meth:~`google.cloud.error_reporting.client._build_error_report` | ||
""" | ||
logger = self.logging_client.logger('errors') | ||
logger.log_struct(error_report) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2017 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
import mock | ||
|
||
|
||
class Test_make_report_error_api(unittest.TestCase): | ||
|
||
def test_make_report_error_api(self): | ||
from google.cloud.error_reporting._gax import make_report_error_api | ||
client = mock.Mock() | ||
client.project = mock.Mock() | ||
report_error_client = make_report_error_api(client) | ||
self.assertEqual(report_error_client._project, client.project) | ||
|
||
|
||
class Test_ErrorReportingGaxApi(unittest.TestCase): | ||
|
||
PROJECT = 'PROJECT' | ||
|
||
def _call_fut(self, gax_api, project): | ||
from google.cloud.error_reporting._gax import _ErrorReportingGaxApi | ||
return _ErrorReportingGaxApi(gax_api, project) | ||
|
||
def test_constructor(self): | ||
gax_api = mock.Mock() | ||
gax_client_wrapper = self._call_fut(gax_api, self.PROJECT) | ||
|
||
self.assertEqual(gax_client_wrapper._project, self.PROJECT) | ||
self.assertEqual(gax_client_wrapper._gax_api, gax_api) | ||
|
||
@mock.patch("google.cloud.error_reporting._gax.ParseDict") | ||
def test_report_error_event(self, _): | ||
gax_api = mock.Mock() | ||
gax_client_wrapper = self._call_fut(gax_api, self.PROJECT) | ||
|
||
mock_error_report = mock.Mock() | ||
gax_client_wrapper.report_error_event(mock_error_report) | ||
self.assertTrue(gax_api.report_error_event.called_with, | ||
mock_error_report) |
Oops, something went wrong.