|
| 1 | +Using the API |
| 2 | +============= |
| 3 | + |
| 4 | + |
| 5 | +Authentication and Configuration |
| 6 | +-------------------------------- |
| 7 | + |
| 8 | +- For an overview of authentication in ``gcloud-python``, |
| 9 | + see :doc:`gcloud-auth`. |
| 10 | + |
| 11 | +- In addition to any authentication configuration, you should also set the |
| 12 | + :envvar:`GCLOUD_PROJECT` environment variable for the project you'd like |
| 13 | + to interact with. If you are Google App Engine or Google Compute Engine |
| 14 | + this will be detected automatically. |
| 15 | + |
| 16 | +- After configuring your environment, create a |
| 17 | + :class:`Client <gcloud.error_reporting.client.Client>` |
| 18 | + |
| 19 | + .. doctest:: |
| 20 | + |
| 21 | + >>> from gcloud import error_reporting |
| 22 | + >>> client = error_reporting.Client() |
| 23 | + |
| 24 | + or pass in ``credentials`` and ``project`` explicitly |
| 25 | + |
| 26 | + .. doctest:: |
| 27 | + |
| 28 | + >>> from gcloud import error_reporting |
| 29 | + >>> client = error_reporting.Client(project='my-project', credentials=creds) |
| 30 | + |
| 31 | + Error Reporting associates errors with a service, which is an identifier for an executable, |
| 32 | + App Engine service, or job. The default service is "python", but a default can be specified |
| 33 | + for the client on construction time. You can also optionally specify a version for that service, |
| 34 | + which defaults to "default." |
| 35 | + |
| 36 | + |
| 37 | + .. doctest:: |
| 38 | + |
| 39 | + >>> from gcloud import error_reporting |
| 40 | + >>> client = error_reporting.Client(project='my-project', |
| 41 | + ... service="login_service", |
| 42 | + ... version="0.1.0") |
| 43 | + |
| 44 | +Reporting an exception |
| 45 | +----------------------- |
| 46 | + |
| 47 | +Report a stacktrace to Stackdriver Error Reporting after an exception |
| 48 | + |
| 49 | +.. doctest:: |
| 50 | + |
| 51 | + >>> from gcloud import error_reporting |
| 52 | + >>> client = error_reporting.Client() |
| 53 | + >>> try: |
| 54 | + >>> raise NameError |
| 55 | + >>> except Exception: |
| 56 | + >>> client.report_exception() |
| 57 | + |
| 58 | + |
| 59 | +By default, the client will report the error using the service specified in the client's |
| 60 | +constructor, or the default service of "python". |
| 61 | + |
| 62 | +The user and HTTP context can also be included in the exception. The HTTP context |
| 63 | +can be constructed using :class:`gcloud.error_reporting.HTTPContext`. This will |
| 64 | +be used by Stackdriver Error Reporting to help group exceptions. |
| 65 | + |
| 66 | +.. doctest:: |
| 67 | + |
| 68 | + >>> from gcloud import error_reporting |
| 69 | + >>> client = error_reporting.Client() |
| 70 | + |
| 71 | + >>> http_context = HTTPContext(method='GET', url='/', userAgent='test agent', |
| 72 | + ... referrer='example.com', responseStatusCode=500, |
| 73 | + ... remote_ip='1.2.3.4') |
| 74 | + >>> try: |
| 75 | + >>> raise NameError |
| 76 | + >>> except Exception: |
| 77 | + >>> client.report_exception(http_context=http_context, user=user)) |
| 78 | + |
| 79 | +Reporting an error without an exception |
| 80 | +----------------------------------------- |
| 81 | + |
| 82 | +Errors can also be reported to Stackdriver Error Reporting outside the context of an exception. |
| 83 | +The library will include the file path, function name, and line number of the location where the |
| 84 | +error was reported. |
| 85 | + |
| 86 | +.. doctest:: |
| 87 | + |
| 88 | + >>> from gcloud import error_reporting |
| 89 | + >>> client = error_reporting.Client() |
| 90 | + >>> error_reporting.report("Found an error!") |
| 91 | + |
| 92 | +Similarly to reporting an exception, the user and HTTP context can be provided: |
| 93 | + |
| 94 | +.. doctest:: |
| 95 | + |
| 96 | + >>> from gcloud import error_reporting |
| 97 | + >>> client = error_reporting.Client() |
| 98 | + |
| 99 | + >>> http_context = HTTPContext(method='GET', url='/', userAgent='test agent', |
| 100 | + ... referrer='example.com', responseStatusCode=500, |
| 101 | + ... remote_ip='1.2.3.4') |
| 102 | + >>> error_reporting.report("Found an error!", http_context=http_context, user=user)) |
0 commit comments