Skip to content
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

chore: move samples from python-docs-sample #18

Merged
merged 54 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
5d21b4d
Add Error Reporting Code
Apr 21, 2016
70fe529
Updating doc links [(#324)](https://github.com/GoogleCloudPlatform/py…
May 4, 2016
3e9f7ef
Commenting noxfile, renaming a test file.
Jul 15, 2016
22db3f1
Update requirements. [(#436)](https://github.com/GoogleCloudPlatform/…
Aug 3, 2016
830d8a2
Fix import order lint errors
Aug 19, 2016
94862ce
bump
Aug 19, 2016
2ac4388
Merge pull request #468 from GoogleCloudPlatform/nox-lint-per-sample
dpebot Aug 19, 2016
287db39
Auto-update dependencies. [(#537)](https://github.com/GoogleCloudPlat…
dpebot Sep 20, 2016
af085d9
Update samples to support latest Google Cloud Python [(#656)](https:/…
Nov 15, 2016
36c51b1
Switch Error Reporting to Google Cloud Python [(#663)](https://github…
waprin Nov 17, 2016
d0e673d
Auto-update dependencies. [(#715)](https://github.com/GoogleCloudPlat…
dpebot Dec 13, 2016
f7a9060
Auto-update dependencies. [(#914)](https://github.com/GoogleCloudPlat…
dpebot Apr 24, 2017
529716d
Re-generate all readmes
Apr 27, 2017
ba62075
Auto-update dependencies. [(#922)](https://github.com/GoogleCloudPlat…
dpebot May 1, 2017
6a57c0d
Fix README rst links [(#962)](https://github.com/GoogleCloudPlatform/…
waprin May 24, 2017
f6d676e
Auto-update dependencies. [(#1004)](https://github.com/GoogleCloudPla…
dpebot Jun 27, 2017
671e2e1
Auto-update dependencies. [(#1011)](https://github.com/GoogleCloudPla…
dpebot Jul 13, 2017
f92e1fa
Auto-update dependencies. [(#1055)](https://github.com/GoogleCloudPla…
dpebot Aug 7, 2017
1669f65
Auto-update dependencies. [(#1093)](https://github.com/GoogleCloudPla…
dpebot Aug 29, 2017
42fdeca
Update all generated readme auth instructions [(#1121)](https://githu…
Sep 18, 2017
a73c2cd
Added Link to Python Setup Guide [(#1158)](https://github.com/GoogleC…
Oct 12, 2017
9889694
Auto-update dependencies. [(#1186)](https://github.com/GoogleCloudPla…
dpebot Nov 1, 2017
677bb94
Add error reporting sample for manual reporting
andrewsg Nov 3, 2017
06e8623
Readd fluentd samples
andrewsg Nov 4, 2017
afe2b33
rename dir
andrewsg Nov 4, 2017
6dbdada
Merge pull request #1198 from andrewsg/error-reporting
andrewsg Nov 6, 2017
46365c6
Auto-update dependencies. [(#1205)](https://github.com/GoogleCloudPla…
dpebot Nov 7, 2017
9cf0532
Auto-update dependencies. [(#1215)](https://github.com/GoogleCloudPla…
dpebot Nov 13, 2017
060977c
Auto-update dependencies. [(#1245)](https://github.com/GoogleCloudPla…
dpebot Dec 4, 2017
0f75778
Auto-update dependencies. [(#1248)](https://github.com/GoogleCloudPla…
dpebot Dec 6, 2017
daf3bdf
Added "Open in Cloud Shell" buttons to README files [(#1254)](https:/…
Dec 7, 2017
011a719
Auto-update dependencies. [(#1276)](https://github.com/GoogleCloudPla…
dpebot Dec 14, 2017
d66bb14
Auto-update dependencies. [(#1282)](https://github.com/GoogleCloudPla…
dpebot Jan 2, 2018
71c29cd
Auto-update dependencies. [(#1317)](https://github.com/GoogleCloudPla…
dpebot Jan 16, 2018
fbac152
Auto-update dependencies. [(#1320)](https://github.com/GoogleCloudPla…
dpebot Feb 2, 2018
f844f2a
Auto-update dependencies. [(#1359)](https://github.com/GoogleCloudPla…
dpebot Feb 26, 2018
1b6b9a2
Auto-update dependencies. [(#1377)](https://github.com/GoogleCloudPla…
dpebot Mar 5, 2018
e782c59
Regenerate the README files and fix the Open in Cloud Shell link for …
Apr 7, 2018
232d0fd
Update READMEs to fix numbering and add git clone [(#1464)](https://g…
frankyn Apr 26, 2018
fb658e9
Auto-update dependencies. [(#1658)](https://github.com/GoogleCloudPla…
dpebot Aug 28, 2018
0a360ec
Put in new region code error_reporting_quickstart [(#1842)](https://g…
engelke Nov 15, 2018
d0c50a8
New region tags [(#1843)](https://github.com/GoogleCloudPlatform/pyth…
engelke Nov 15, 2018
952704d
Auto-update dependencies. [(#1980)](https://github.com/GoogleCloudPla…
dpebot Feb 6, 2019
52e97d2
Adds updates including compute [(#2436)](https://github.com/GoogleClo…
gguuss Oct 23, 2019
e4b17e3
Auto-update dependencies. [(#2005)](https://github.com/GoogleCloudPla…
dpebot Dec 21, 2019
d25ffa7
chore(deps): update dependency fluent-logger to v0.9.5 [(#3004)](http…
renovate-bot Feb 28, 2020
232f678
chore(deps): update dependency fluent-logger to v0.9.6 [(#3080)](http…
renovate-bot Mar 10, 2020
7002564
Simplify noxfile setup. [(#2806)](https://github.com/GoogleCloudPlatf…
kurtisvg Apr 2, 2020
ea66147
chore(deps): update dependency google-cloud-error-reporting to v0.34.…
renovate-bot May 18, 2020
65c7657
chore(deps): update dependency pytest to v5.4.3 [(#4279)](https://git…
renovate-bot Jul 12, 2020
2c0785f
chore(deps): update dependency mock to v4 [(#4287)](https://github.co…
renovate-bot Jul 13, 2020
705ade3
Update dependency pytest to v6 [(#4390)](https://github.com/GoogleClo…
renovate-bot Aug 1, 2020
397c0f8
Merge branch 'snippets-samples' into add-snippets-samples
arithmetic1728 Aug 12, 2020
59853ee
chore: update templates
arithmetic1728 Aug 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions samples/snippets/api/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
.. This file is automatically generated. Do not edit this file directly.

Stackdriver Error Reporting Python Samples
===============================================================================

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=error_reporting/api/README.rst


This directory contains samples for Stackdriver Error Reporting. `Stackdriver Error Reporting`_ aggregates and displays errors produced in
your running cloud services.




.. _Stackdriver Error Reporting: https://cloud.google.com/error-reporting/docs/

Setup
-------------------------------------------------------------------------------


Authentication
++++++++++++++

This sample requires you to have authentication setup. Refer to the
`Authentication Getting Started Guide`_ for instructions on setting up
credentials for applications.

.. _Authentication Getting Started Guide:
https://cloud.google.com/docs/authentication/getting-started

Install Dependencies
++++++++++++++++++++

#. Clone python-docs-samples and change directory to the sample directory you want to use.

.. code-block:: bash

$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.

.. _Python Development Environment Setup Guide:
https://cloud.google.com/python/setup

#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

.. code-block:: bash

$ virtualenv env
$ source env/bin/activate

#. Install the dependencies needed to run the samples.

.. code-block:: bash

$ pip install -r requirements.txt

.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

Report Exception
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=error_reporting/api/report_exception.py,error_reporting/api/README.rst




To run this sample:

.. code-block:: bash

$ python report_exception.py




The client library
-------------------------------------------------------------------------------

This sample uses the `Google Cloud Client Library for Python`_.
You can read the documentation for more details on API usage and use GitHub
to `browse the source`_ and `report issues`_.

.. _Google Cloud Client Library for Python:
https://googlecloudplatform.github.io/google-cloud-python/
.. _browse the source:
https://github.com/GoogleCloudPlatform/google-cloud-python
.. _report issues:
https://github.com/GoogleCloudPlatform/google-cloud-python/issues


.. _Google Cloud SDK: https://cloud.google.com/sdk/
21 changes: 21 additions & 0 deletions samples/snippets/api/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is used to generate README.rst

product:
name: Stackdriver Error Reporting
short_name: Error Reporting
url: https://cloud.google.com/error-reporting/docs/
description: >
`Stackdriver Error Reporting`_ aggregates and displays errors produced in
your running cloud services.

setup:
- auth
- install_deps

samples:
- name: Report Exception
file: report_exception.py

cloud_client_library: true

folder: error_reporting/api
46 changes: 46 additions & 0 deletions samples/snippets/api/report_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.


# [START error_reporting]
# [START error_reporting_quickstart]
# [START error_reporting_setup_python]
def simulate_error():
from google.cloud import error_reporting

client = error_reporting.Client()
try:
# simulate calling a method that's not defined
raise NameError
except Exception:
client.report_exception()
# [END error_reporting_setup_python]
# [END error_reporting_quickstart]
# [END error_reporting]


# [START error_reporting_manual]
# [START error_reporting_setup_python_manual]
def report_manual_error():
from google.cloud import error_reporting

client = error_reporting.Client()
client.report("An error has occurred.")
# [START error_reporting_setup_python_manual]
# [END error_reporting_manual]


if __name__ == '__main__':
simulate_error()
report_manual_error()
23 changes: 23 additions & 0 deletions samples/snippets/api/report_exception_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

import report_exception


def test_error_sends():
report_exception.simulate_error()


def test_manual_error_sends():
report_exception.report_manual_error()
1 change: 1 addition & 0 deletions samples/snippets/api/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==6.0.1
1 change: 1 addition & 0 deletions samples/snippets/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-error-reporting==0.34.0
35 changes: 35 additions & 0 deletions samples/snippets/fluent_on_compute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Google Error Reorting Samples Samples

[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=error_reporting/fluent_on_compute/README.md

This section contains samples for [Google Cloud Error Reporting](https://cloud.google.com/error-reporting).

A startup script has been provided to demonstrated how to properly provision a GCE
instance with fluentd configured. Note the intallation of fluentd, the addition of the config file,
and the restarting of the fluetnd service. You can start an instance using
it like this:

gcloud compute instances create example-instance --metadata-from-file startup-script=startup_script.sh

or simply use it as reference when creating your own instance.

After fluentd is configured, main.py could be used to simulate an error:

gcloud compute copy-files main.py example-instance:~/main.py

Then,

gcloud compute ssh example-instance
python ~/main.py

And you will see the message in the Errors Console.

<!-- auto-doc-link -->
These samples are used on the following documentation page:

> https://cloud.google.com/error-reporting/docs/setting-up-on-compute-engine

<!-- end-auto-doc-link -->
42 changes: 42 additions & 0 deletions samples/snippets/fluent_on_compute/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

# [START error_reporting]
import traceback

import fluent.event
import fluent.sender


def simulate_error():
fluent.sender.setup('myapp', host='localhost', port=24224)

def report(ex):
data = {}
data['message'] = '{0}'.format(ex)
data['serviceContext'] = {'service': 'myapp'}
# ... add more metadata
fluent.event.Event('errors', data)

# report exception data using:
try:
# simulate calling a method that's not defined
raise NameError
except Exception:
report(traceback.format_exc())
# [END error_reporting]


if __name__ == '__main__':
simulate_error()
23 changes: 23 additions & 0 deletions samples/snippets/fluent_on_compute/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

import mock

import main


@mock.patch("fluent.event")
def test_error_sends(event_mock):
main.simulate_error()
event_mock.Event.assert_called_once_with(mock.ANY, mock.ANY)
2 changes: 2 additions & 0 deletions samples/snippets/fluent_on_compute/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==6.0.1
mock==4.0.2
1 change: 1 addition & 0 deletions samples/snippets/fluent_on_compute/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fluent-logger==0.9.6
35 changes: 35 additions & 0 deletions samples/snippets/fluent_on_compute/startup_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# 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.

set -v

curl -sSO "https://dl.google.com/cloudagents/install-logging-agent.sh"
chmod +x install-logging-agent.sh
./install-logging-agent.sh
mkdir -p /etc/google-fluentd/config.d/
cat <<EOF > /etc/google-fluentd/config.d/forward.conf
<source>
type forward
port 24224
</source>
EOF
service google-fluentd restart

apt-get update
apt-get install -yq \
git build-essential supervisor python python-dev python-pip libffi-dev \
libssl-dev
pip install fluent-logger