-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Error Reporting GAPIC as dependency #2894
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
28f4c98
Add Error Reporting GAPIC as dependency
df26d80
Add GAX to Error Reporting
e2eb576
fixing up stuff
e92e17d
Add tests, make Travis pass
128cc3a
Add tests
a51d687
Minor fixup
e6c3547
Fix coverage
de8daaa
More mixups
4b9c8fa
Fix coverage
602e313
Add credentials
88873ba
Fix determine
3d956a5
Fix up
fa382b3
sneeringer review
58b1e76
typos
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.