Skip to content

Commit 6ae0728

Browse files
feat(Spanner): Add the built in metrics feature (#9055)
1 parent f1b4cf9 commit 6ae0728

18 files changed

Lines changed: 2020 additions & 10 deletions

.github/run-package-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ for DIR in ${DIRS}; do
7070
"PubSub,cloud-pubsub"
7171
"Storage,cloud-storage,2.100"
7272
"ShoppingCommonProtos,shopping-common-protos"
73-
"GeoCommonProtos,geo-common-protos,0.1"
73+
"GeoCommonProtos,geo-common-protos,0.1",
74+
"Monitoring,cloud-monitoring"
7475
)
7576
for i in "${PACKAGE_DEPENDENCIES[@]}"; do
7677
IFS="," read -r PKG_DIR PKG_NAME PKG_VERSION <<< "$i"

Spanner/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ $database = $spanner
167167
[lock-interface]: https://github.com/googleapis/google-cloud-php/blob/main/Core/src/Lock/LockInterface.php
168168
[symfony-lock]: https://symfony.com/doc/current/components/lock.html
169169

170+
### Client-Side Metrics
171+
172+
The Cloud Spanner client library supports exporting built-in client-side metrics to Google Cloud Monitoring using OpenTelemetry. These metrics provide detailed observability into operations, RPC attempts, and Google Front End (GFE) or Spanner API Frontend (AFE) latencies.
173+
174+
To enable client-side metrics:
175+
176+
```php
177+
use Google\Cloud\Spanner\SpannerClient;
178+
179+
$spanner = new SpannerClient([
180+
'enableBuiltInMetrics' => true,
181+
// optional: timeout in milliseconds for metric export calls
182+
'metricsTimeoutMillis' => 100
183+
]);
184+
```
185+
186+
All captured metrics are aggregated in-memory and exported synchronously during the PHP process shutdown phase.
187+
188+
> [!WARNING]
189+
> **Performance Caveat**: Because PHP has a share-nothing architecture, exporting metrics synchronously at shutdown will add to the total request execution latency of the PHP process. For this reason, it is advised to enable built-in metrics primarily for debugging and active performance monitoring.
190+
>
191+
> To minimize this performance overhead, installing the PECL [`ext-opentelemetry`](https://pecl.php.net/package/opentelemetry) extension is **highly recommended**.
192+
193+
#### Prerequisites
194+
195+
1. **IAM Permissions**: The credentials used by your application require `monitoring.timeSeries.create` permission to publish metrics. Predefined Spanner roles (such as `roles/spanner.databaseAdmin`, `roles/spanner.databaseUser`, and `roles/spanner.databaseReader`) already include this permission by default. If you are using a custom IAM role, ensure this permission is added.
196+
2. **OpenTelemetry Extension**: Installing the [`ext-opentelemetry`](https://pecl.php.net/package/opentelemetry) PHP extension is highly advised to optimize metrics collection and export performance.
197+
170198
### Debugging
171199

172200
Please see our [Debugging guide](https://github.com/googleapis/google-cloud-php/blob/main/DEBUG.md)

Spanner/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"php": "^8.1",
88
"ext-grpc": "*",
99
"google/cloud-core": "^1.68",
10-
"google/gax": "^1.40.0"
10+
"google/gax": "^1.41.0",
11+
"google/cloud-monitoring": "^2.2",
12+
"open-telemetry/sdk": "^1.13"
1113
},
1214
"require-dev": {
1315
"phpunit/phpunit": "^9.6",
@@ -23,6 +25,7 @@
2325
},
2426
"suggest": {
2527
"ext-protobuf": "Provides a significant increase in throughput over the pure PHP protobuf implementation. See https://cloud.google.com/php/grpc for installation instructions.",
28+
"ext-opentelemetry": "Provides a significant increase in performance for OpenTelemetry metrics collection.",
2629
"brick/math": "Perform arithmetic on NUMERIC values."
2730
},
2831
"extra": {

0 commit comments

Comments
 (0)