Skip to content

Commit 0715f6d

Browse files
authored
Merge pull request GoogleCloudPlatform#1198 from andrewsg/error-reporting
Add error reporting sample for manual reporting
2 parents eb22abb + 3825b46 commit 0715f6d

10 files changed

+148
-8
lines changed
File renamed without changes.
File renamed without changes.

error_reporting/report_exception.py renamed to error_reporting/api/report_exception.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START error_reporting]
16-
from google.cloud import error_reporting
17-
1815

16+
# [START error_reporting]
1917
def simulate_error():
18+
from google.cloud import error_reporting
19+
2020
client = error_reporting.Client()
2121
try:
2222
# simulate calling a method that's not defined
@@ -26,5 +26,15 @@ def simulate_error():
2626
# [END error_reporting]
2727

2828

29+
# [START error_reporting_manual]
30+
def report_manual_error():
31+
from google.cloud import error_reporting
32+
33+
client = error_reporting.Client()
34+
client.report("An error has occurred.")
35+
# [END error_reporting_manual]
36+
37+
2938
if __name__ == '__main__':
3039
simulate_error()
40+
report_manual_error()

error_reporting/report_exception_test.py renamed to error_reporting/api/report_exception_test.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pytest
16-
1715
import report_exception
1816

1917

20-
@pytest.mark.xfail(
21-
strict=True,
22-
reason='GoogleCloudPlatform/google-cloud-python#3263')
2318
def test_error_sends():
2419
report_exception.simulate_error()
20+
21+
22+
def test_manual_error_sends():
23+
report_exception.report_manual_error()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Google Error Reorting Samples Samples
2+
3+
This section contains samples for [Google Cloud Error Reporting](https://cloud.google.com/error-reporting).
4+
5+
A startup script has been provided to demonstrated how to properly provision a GCE
6+
instance with fluentd configured. Note the intallation of fluentd, the addition of the config file,
7+
and the restarting of the fluetnd service. You can start an instance using
8+
it like this:
9+
10+
gcloud compute instances create example-instance --metadata-from-file startup-script=startup_script.sh
11+
12+
or simply use it as reference when creating your own instance.
13+
14+
After fluentd is configured, main.py could be used to simulate an error:
15+
16+
gcloud compute copy-files main.py example-instance:~/main.py
17+
18+
Then,
19+
20+
gcloud compute ssh example-instance
21+
python ~/main.py
22+
23+
And you will see the message in the Errors Console.
24+
25+
<!-- auto-doc-link -->
26+
These samples are used on the following documentation page:
27+
28+
> https://cloud.google.com/error-reporting/docs/setting-up-on-compute-engine
29+
30+
<!-- end-auto-doc-link -->
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
# [START error_reporting]
16+
import traceback
17+
18+
import fluent.event
19+
import fluent.sender
20+
21+
22+
def simulate_error():
23+
fluent.sender.setup('myapp', host='localhost', port=24224)
24+
25+
def report(ex):
26+
data = {}
27+
data['message'] = '{0}'.format(ex)
28+
data['serviceContext'] = {'service': 'myapp'}
29+
# ... add more metadata
30+
fluent.event.Event('errors', data)
31+
32+
# report exception data using:
33+
try:
34+
# simulate calling a method that's not defined
35+
raise NameError
36+
except Exception:
37+
report(traceback.format_exc())
38+
# [END error_reporting]
39+
40+
41+
if __name__ == '__main__':
42+
simulate_error()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import mock
16+
17+
import main
18+
19+
20+
@mock.patch("fluent.event")
21+
def test_error_sends(event_mock):
22+
main.simulate_error()
23+
event_mock.Event.assert_called_once_with(mock.ANY, mock.ANY)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fluent-logger==0.4.4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
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+
set -v
17+
18+
curl -sSO "https://dl.google.com/cloudagents/install-logging-agent.sh"
19+
chmod +x install-logging-agent.sh
20+
./install-logging-agent.sh
21+
mkdir -p /etc/google-fluentd/config.d/
22+
cat <<EOF > /etc/google-fluentd/config.d/forward.conf
23+
<source>
24+
type forward
25+
port 24224
26+
</source>
27+
EOF
28+
service google-fluentd restart
29+
30+
apt-get update
31+
apt-get install -yq \
32+
git build-essential supervisor python python-dev python-pip libffi-dev \
33+
libssl-dev
34+
pip install fluent-logger
35+

0 commit comments

Comments
 (0)