Skip to content

Commit 12ea819

Browse files
authored
Updates error reporting samples and fixes tests (GoogleCloudPlatform#802)
1 parent c9b4af6 commit 12ea819

12 files changed

+130
-510
lines changed

error_reporting/README.md

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ exceptions, errors, and PHP fatral errors to Stackdriver Error Reporting.
1212

1313
1. To use this sample, you must first [enable the Stackdriver Error Reporting API][0]
1414
1. Next, **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md):
15-
1. Run `php composer.phar install --ignore-platform-reqs` (if composer is installed locally) or `composer install --ignore-platform-reqs`
15+
1. Run `php composer.phar install` (if composer is installed locally) or `composer install`
1616
(if composer is installed globally).
1717
```sh
18-
composer install --ignore-platform-reqs
18+
composer install
19+
```
20+
1. To use the [gRPC PHP Extension][php_grpc], which will be more performant than
21+
REST/HTTP,
22+
install and enable the gRPC extension using PECL:
23+
```sh
24+
pecl install grpc
1925
```
20-
1. If the [gRPC PHP Extension][php_grpc] is enabled for your version of PHP,
21-
install your dependencies without the `--ignore-platform-reqs` flag. **Note**
22-
some samples in `error_reporting.php` require gRPC.
2326
1. Create a service account in the [Service Account section of the Cloud Console][2]
2427
1. Download the JSON key file of the service account.
2528
1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to that file.
@@ -36,41 +39,31 @@ Run the samples:
3639

3740
```sh
3841
php quickstart.php
39-
Exception logged to Stackdriver Error Reporting
42+
Throwing a test exception. You can view the message at https://console.cloud.google.com/errors.
4043
```
4144

42-
View [Stackdriver Error Reporting][1] in the Cloud Console to see the logged
43-
exception.
45+
This example registers the Stackdriver exception handler using
46+
[PHP exception handlers][3]. View [Stackdriver Error Reporting][1] in the Cloud
47+
Console to see the logged exception.
4448

45-
# Running error_reporting.php
49+
# Running src/report_error.php
4650

47-
Run the sample:
48-
49-
```sh
50-
$ php error_reporting.php report YOUR_PROJECT_ID
51-
Reported an error to Stackdriver
52-
```
51+
This sample shows how to report an error by creating a `ReportedErrorEvent`. The
52+
`ReportedErrorEvent` object gives you more control over how the error appears
53+
and the details associated with it.
5354

54-
For an example of how to register the Stackdriver exception handler in your custom application, see
55-
[src/register_exception_handler.php](src/register_exception_handler.php). You can test this out
56-
using the samples:
55+
Run the sample:
5756

5857
```sh
59-
# Test registering an exception handler and then throwing a PHP Fatal Error
60-
$ php error_reporting.php test-exception-handler YOUR_PROJECT_ID --fatal
61-
Triggering a PHP Fatal Error by eval-ing a syntax error...
58+
$ php src/report_error.php YOUR_PROJECT_ID "This is a test message"
59+
Reported an exception to Stackdriver
6260
```
6361

64-
For more granular control over your error reporting, and better performance, you can use the gRPC
65-
library to throw errors. Follow the instructions to install and enable the
66-
[gRPC PHP Extension][php_grpc]. Now run the gRPC example in `error_reporting.php`:
67-
68-
```sh
69-
$ php error_reporting.php report-grpc YOUR_PROJECT_ID
70-
Reported an error to Stackdriver
71-
```
62+
View [Stackdriver Error Reporting][1] in the Cloud Console to see the logged
63+
exception.
7264

7365
[0]: https://console.cloud.google.com/flows/enableapi?apiid=clouderrorreporting.googleapis.com
7466
[1]: https://console.cloud.google.com/errors
7567
[2]: https://console.cloud.google.com/iam-admin/serviceaccounts/
76-
[php_grpc]: http://cloud.google.com/php/grpc
68+
[3]: http://php.net/manual/en/function.set-exception-handler.php
69+
[php_grpc]: http://cloud.google.com/php/grpc

error_reporting/composer.json

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
{
22
"require": {
3-
"google/cloud-error-reporting": "^0.8",
4-
"symfony/console": " ^3.3",
5-
"symfony/event-dispatcher": "^3.3"
3+
"google/cloud-error-reporting": "^0.12.3"
64
},
75
"require-dev": {
86
"phpunit/phpunit": "~4.8",
9-
"google/cloud-tools": "^0.6.9"
10-
},
11-
"autoload-dev": {
12-
"psr-4": {
13-
"Google\\Cloud\\Samples\\ErrorReporting\\": "test"
14-
}
7+
"google/cloud-tools": "^0.8.5"
158
}
169
}

error_reporting/error_reporting.php

Lines changed: 0 additions & 175 deletions
This file was deleted.

error_reporting/quickstart.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,30 @@
55

66
# [START error_reporting_quickstart]
77
// Imports the Cloud Client Library
8+
use Google\Cloud\ErrorReporting\Bootstrap;
89
use Google\Cloud\Logging\LoggingClient;
10+
use Google\Cloud\Core\Report\SimpleMetadataProvider;
911

1012
// These variables are set by the App Engine environment. To test locally,
1113
// ensure these are set or manually change their values.
1214
$projectId = getenv('GCLOUD_PROJECT') ?: 'YOUR_PROJECT_ID';
1315
$service = getenv('GAE_SERVICE') ?: 'error_reporting_quickstart';
14-
$version = getenv('GAE_VERSION') ?: '1.0-dev';
16+
$version = getenv('GAE_VERSION') ?: 'test';
1517

1618
// Instantiates a client
1719
$logging = new LoggingClient([
18-
'projectId' => $projectId
20+
'projectId' => $projectId,
1921
]);
22+
// Set the projectId, service, and version via the SimpleMetadataProvider
23+
$metadata = new SimpleMetadataProvider([], $projectId, $service, $version);
24+
// Create a PSR-4 compliant logger
25+
$psrLogger = $logging->psrLogger('error-log', [
26+
'metadataProvider' => $metadata,
27+
]);
28+
// Using the Error Reporting Bootstrap class, register your PSR logger as a PHP
29+
// exception hander. This will ensure all exceptions are logged to Stackdriver.
30+
Bootstrap::init($psrLogger);
2031

21-
// The name of the log to write to
22-
$logName = 'my-log';
23-
24-
// Selects the log to write to
25-
$logger = $logging->logger($logName);
26-
27-
$handlerFunction = function (Exception $e) use ($logger, $service, $version) {
28-
// Creates the log entry with the exception trace
29-
$entry = $logger->entry([
30-
'message' => sprintf('PHP Warning: %s', $e),
31-
'serviceContext' => [
32-
'service' => $service,
33-
'version' => $version,
34-
]
35-
]);
36-
// Writes the log entry
37-
$logger->write($entry);
38-
39-
print("Exception logged to Stackdriver Error Reporting" . PHP_EOL);
40-
};
41-
42-
// Sets PHP's default exception handler
43-
set_exception_handler($handlerFunction);
44-
45-
throw new Exception('This will be logged to Stackdriver Error Reporting');
32+
print("Throwing a test exception. You can view the message at https://console.cloud.google.com/errors." . PHP_EOL);
33+
throw new Exception('quickstart.php test exception');
4634
# [END error_reporting_quickstart]

error_reporting/src/register_exception_handler.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)