Skip to content

Commit d6901f9

Browse files
committed
Add an option configuration to configure the underlying MetricServiceClient
1 parent 413ac1f commit d6901f9

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

Spanner/src/SpannerClient.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ class SpannerClient
199199
* @type int $isolationLevel The level of Isolation for the transactions executed by this Client's instance.
200200
* **Defaults to** IsolationLevel::ISOLATION_LEVEL_UNSPECIFIED
201201
* @type CacheItemPoolInterface $cacheItemPool
202+
* @type bool $disableBuiltInMetrics If true, built-in metrics collection will be disabled.
203+
* **Defaults to** false.
204+
* @type array $metricsOptions Configuration options for the internal `MetricServiceClient`
205+
* used to export metrics.
206+
* @type MetricServiceClient $metricServiceClient An explicit instance of
207+
* `MetricServiceClient` to use for exporting metrics.
202208
* }
203209
* @throws GoogleException If the gRPC extension is not enabled.
204210
*/
@@ -287,7 +293,7 @@ public function __construct(array $options = [])
287293
$this->instanceAdminClient->addMiddleware($middleware);
288294
$this->databaseAdminClient->addMiddleware($middleware);
289295

290-
$this->configureBuiltinMetrics($options['disableBuiltInMetrics']);
296+
$this->configureBuiltinMetrics($options);
291297

292298
$this->projectName = InstanceAdminClient::projectName($this->projectId);
293299
$this->cacheItemPool = $options['cacheItemPool'];
@@ -1040,13 +1046,38 @@ private function configureKeepAlive(array $config): array
10401046
return $config;
10411047
}
10421048

1043-
private function configureBuiltinMetrics(bool $disabled): void
1049+
private function configureBuiltinMetrics(array &$options): void
10441050
{
1045-
if ($disabled) {
1051+
$metricsClient = $this->pluck('metricServiceClient', $options, false);
1052+
$metricsOptions = $this->pluck('metricsOptions', $options, false) ?: [];
1053+
1054+
if ($this->pluck('disableBuiltInMetrics', $options, false)) {
10461055
return;
10471056
}
10481057

1049-
$metricsClient = new MetricServiceClient();
1058+
if (!$metricsClient) {
1059+
$metricsOptions += [
1060+
'projectId' => $this->projectId,
1061+
'keyFile' => $options['keyFile'] ?? null,
1062+
'keyFilePath' => $options['keyFilePath'] ?? null,
1063+
'credentials' => $options['credentials'] ?? null,
1064+
'credentialsConfig' => $options['credentialsConfig'] ?? null,
1065+
'universeDomain' => $options['universeDomain'] ?? null,
1066+
'transport' => $options['transport'] ?? null,
1067+
'transportConfig' => $options['transportConfig'] ?? null,
1068+
];
1069+
1070+
try {
1071+
$metricsClient = new MetricServiceClient($metricsOptions);
1072+
} catch (ValidationException $e) {
1073+
return;
1074+
}
1075+
}
1076+
1077+
if (!$metricsClient instanceof MetricServiceClient) {
1078+
throw new ValidationException('The "metricServiceClient" option must be a MetricServiceClient instance.');
1079+
}
1080+
10501081
$metricsClientId = RUUID::uuid4()->toString() . '-' . getmypid();
10511082
$exporter = new BuiltInMetricsExporter($metricsClient, $this->projectId, $metricsClientId);
10521083
$reader = new ExportingReader($exporter);

0 commit comments

Comments
 (0)