-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathLoggingClient.php
More file actions
653 lines (624 loc) · 26.5 KB
/
LoggingClient.php
File metadata and controls
653 lines (624 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<?php
/**
* 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.
*/
namespace Google\Cloud\Logging;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\Batch\BatchRunner;
use Google\Cloud\Core\Batch\ClosureSerializerInterface;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\Iterator\ItemIterator;
use Google\Cloud\Core\Iterator\PageIterator;
use Google\Cloud\Core\Report\MetadataProviderInterface;
use Google\Cloud\Logging\Connection\ConnectionInterface;
use Google\Cloud\Logging\Connection\Grpc;
use Google\Cloud\Logging\Connection\Rest;
use Psr\Cache\CacheItemPoolInterface;
/**
* Google Stackdriver Logging allows you to store, search, analyze, monitor, and
* alert on log data and events from Google Cloud Platform and Amazon Web
* Services. Find more information at the
* [Google Stackdriver Logging docs](https://cloud.google.com/logging/docs/).
*
* This client supports transport over
* [REST](https://cloud.google.com/logging/docs/api/reference/rest/) or
* gRPC.
*
* In order to enable gRPC support please make sure to install and enable
* the gRPC extension through PECL:
*
* ```sh
* $ pecl install grpc
* ```
*
* NOTE: Support for gRPC is currently at an Alpha quality level, meaning it is still
* a work in progress and is more likely to get backwards-incompatible updates.
*
* When using gRPC in production environments, it is highly recommended that you make use of the
* Protobuf PHP extension for improved performance. Protobuf can be installed
* via [PECL](https://pecl.php.net).
*
* ```
* $ pecl install protobuf
* ```
*
* Example:
* ```
* use Google\Cloud\Logging\LoggingClient;
*
* $logging = new LoggingClient();
* ```
*/
class LoggingClient
{
use ArrayTrait;
use ClientTrait;
const VERSION = '1.34.4';
const FULL_CONTROL_SCOPE = 'https://www.googleapis.com/auth/logging.admin';
const READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/logging.read';
const WRITE_ONLY_SCOPE = 'https://www.googleapis.com/auth/logging.write';
/**
* @var ConnectionInterface Represents a connection to Stackdriver Logging.
* @internal
*/
protected $connection;
/**
* @var string The formatted name used in API requests.
*/
private $formattedProjectName;
/**
* @var array The config given to the constructor.
*/
private $config;
/**
* Create a PsrLogger with batching enabled.
*
* @param string $name The name of the log to write entries to.
* @param array $options [optional] {
* Configuration options.
*
* @type string $apiEndpoint A hostname with optional port to use in
* place of the service's default endpoint.
* @type string $messageKey The key in the `jsonPayload` used to contain
* the logged message. **Defaults to** `message`.
* @type array $resource The
* [monitored resource](https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource)
* to associate log entries with. **Defaults to** type global.
* @type array $labels A set of user-defined (key, value) data that
* provides additional information about the log entry.
* @type MetadataProviderInterface $metadataProvider **Defaults to** An
* automatically chosen provider, based on detected environment
* settings.
* @type bool $debugOutput Whether or not to output debug information.
* Please note debug output currently only applies in CLI based
* applications. **Defaults to** `false`.
* @type array $batchOptions A set of options for a BatchJob.
* {@see \Google\Cloud\Core\Batch\BatchJob::__construct()} for
* more details.
* **Defaults to** ['batchSize' => 1000,
* 'callPeriod' => 2.0,
* 'numWorkers' => 2].
* @type array $clientConfig Configuration options for the Logging client
* used to handle processing of batch items. For valid options
* please see
* {@see \Google\Cloud\Logging\LoggingClient::__construct()}.
* @type BatchRunner $batchRunner A BatchRunner object. Mainly used for
* the tests to inject a mock. **Defaults to** a newly created
* BatchRunner.
* @type ClosureSerializerInterface $closureSerializer An implementation
* responsible for serializing closures used in the
* `$clientConfig`. This is especially important when using the
* batch daemon. **Defaults to**
* {@see \Google\Cloud\Core\Batch\OpisClosureSerializer} if the
* `opis/closure` library is installed.
* }
* @return PsrLogger
* @experimental The experimental flag means that while we believe this method
* or class is ready for use, it may change before release in backwards-
* incompatible ways. Please use with caution, and test thoroughly when
* upgrading.
**/
public static function psrBatchLogger($name, array $options = [])
{
$client = array_key_exists('clientConfig', $options)
? new self($options['clientConfig'])
: new self();
// Force enabling batch.
$options['batchEnabled'] = true;
return $client->psrLogger($name, $options);
}
/**
* Create a Logging client.
*
* @param array $config [optional] {
* Configuration options.
*
* @type string $projectId The project ID from the Google Developer's
* Console.
* @type CacheItemPoolInterface $authCache A cache for storing access
* tokens. **Defaults to** a simple in memory implementation.
* @type array $authCacheOptions Cache configuration options.
* @type callable $authHttpHandler A handler used to deliver Psr7
* requests specifically for authentication.
* @type FetchAuthTokenInterface $credentialsFetcher A credentials
* fetcher instance.
* @type callable $httpHandler A handler used to deliver Psr7 requests.
* Only valid for requests sent over REST.
* @type array $keyFile [DEPRECATED]
* This option is being deprecated because of a potential security risk.
* This option does not validate the credential configuration. The security
* risk occurs when a credential configuration is accepted from a source
* that is not under your control and used without validation on your side.
* If you know that you will be loading credential configurations of a
* specific type, it is recommended to create the credentials directly and
* configure them using the `credentialsFetcher` option instead.
* ```
* use Google\Auth\Credentials\ServiceAccountCredentials;
* $credentialsFetcher = new ServiceAccountCredentials($scopes, $json);
* $creds = new LoggingClient(['credentialsFetcher' => $creds]);
* ```
* This will ensure that an unexpected credential type with potential for
* malicious intent is not loaded unintentionally. You might still have to do
* validation for certain credential types.
* If you are loading your credential configuration from an untrusted source and have
* not mitigated the risks (e.g. by validating the configuration yourself), make
* these changes as soon as possible to prevent security risks to your environment.
* Regardless of the method used, it is always your responsibility to validate
* configurations received from external sources.
* @see https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
* @type string $keyFilePath [DEPRECATED]
* This option is being deprecated because of a potential security risk.
* This option does not validate the credential configuration. The security
* risk occurs when a credential configuration is accepted from a source
* that is not under your control and used without validation on your side.
* If you know that you will be loading credential configurations of a
* specific type, it is recommended to create the credentials directly and
* configure them using the `credentialsFetcher` option instead.
* ```
* use Google\Auth\Credentials\ServiceAccountCredentials;
* $credentialsFetcher = new ServiceAccountCredentials($scopes, $json);
* $creds = new LoggingClient(['credentialsFetcher' => $creds]);
* ```
* This will ensure that an unexpected credential type with potential for
* malicious intent is not loaded unintentionally. You might still have to do
* validation for certain credential types.
* If you are loading your credential configuration from an untrusted source and have
* not mitigated the risks (e.g. by validating the configuration yourself), make
* these changes as soon as possible to prevent security risks to your environment.
* Regardless of the method used, it is always your responsibility to validate
* configurations received from external sources.
* @see https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
* @type float $requestTimeout Seconds to wait before timing out the
* request. **Defaults to** `0` with REST and `60` with gRPC.
* @type int $retries Number of retries for a failed request.
* **Defaults to** `3`.
* @type array $scopes Scopes to be used for the request.
* @type string $quotaProject Specifies a user project to bill for
* access charges associated with the request.
* @type string $transport The transport type used for requests. May be
* either `grpc` or `rest`. **Defaults to** `grpc` if gRPC support
* is detected on the system.
* }
*/
public function __construct(array $config = [])
{
$this->config = $config;
$connectionType = $this->getConnectionType($config);
$config += [
'scopes' => [self::FULL_CONTROL_SCOPE],
'projectIdRequired' => true
];
$this->connection = $connectionType === 'grpc'
? new Grpc($this->configureAuthentication($config))
: new Rest($this->configureAuthentication($config));
$this->formattedProjectName = "projects/$this->projectId";
}
/**
* Create a sink.
*
* Example:
* ```
* $sink = $logging->createSink('my-sink', 'storage.googleapis.com/my-bucket');
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.sinks/create projects.sinks create API documentation.
* @codingStandardsIgnoreEnd
*
* @param string $name The name of the sink.
* @param string $destination The export destination. Please see
* [Exporting Logs With Sinks](https://cloud.google.com/logging/docs/api/tasks/exporting-logs#about_sinks)
* for more information and examples.
* @param array $options [optional] {
* Configuration options.
*
* @type string $filter An [advanced logs filter](https://cloud.google.com/logging/docs/view/advanced_filters).
* @type string $outputVersionFormat The log entry version to use for
* this sink's exported log entries. This version does not have
* to correspond to the version of the log entry when it was
* written to Stackdriver Logging. May be either `V1` or `V2`.
* **Defaults to** `V2`.
* }
* @return Sink
*/
public function createSink($name, $destination, array $options = [])
{
$response = $this->connection->createSink($options + [
'parent' => $this->formattedProjectName,
'name' => $name,
'destination' => $destination,
'outputVersionFormat' => 'VERSION_FORMAT_UNSPECIFIED'
]);
return new Sink($this->connection, $name, $this->projectId, $response);
}
/**
* Lazily instantiates a sink. There are no network requests made at this
* point. To see the operations that can be performed on a sink please see
* {@see \Google\Cloud\Logging\Sink}.
*
* Example:
* ```
* $sink = $logging->sink('my-sink');
* echo $sink->name();
* ```
*
* @param string $name The name of the sink.
* @return Sink
*/
public function sink($name)
{
return new Sink($this->connection, $name, $this->projectId);
}
/**
* Fetches sinks associated with your project.
*
* Example:
* ```
* $sinks = $logging->sinks();
*
* foreach ($sinks as $sink) {
* echo $sink->name() . PHP_EOL;
* }
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.sinks/list projects.sinks list API documentation.
* @codingStandardsIgnoreEnd
*
* @param array $options [optional] {
* Configuration options.
*
* @type int $pageSize The maximum number of results to return per
* request.
* @type int $resultLimit Limit the number of results returned in total.
* **Defaults to** `0` (return all results).
* @type string $pageToken A previously-returned page token used to
* resume the loading of results from a specific point.
* }
* @return ItemIterator<Sink>
*/
public function sinks(array $options = [])
{
$resultLimit = $this->pluck('resultLimit', $options, false);
return new ItemIterator(
new PageIterator(
function (array $sink) {
return new Sink($this->connection, $sink['name'], $this->projectId, $sink);
},
[$this->connection, 'listSinks'],
$options + ['parent' => $this->formattedProjectName],
[
'itemsKey' => 'sinks',
'resultLimit' => $resultLimit
]
)
);
}
/**
* Create a metric.
*
* Example:
* ```
* $metric = $logging->createMetric(
* 'my-metric',
* 'logName = projects/my-project/logs/my-log'
* );
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.metrics/create projects.metrics create API documentation.
* @codingStandardsIgnoreEnd
*
* @param string $name The name of the metric.
* @param string $filter An [advanced logs filter](https://cloud.google.com/logging/docs/view/advanced_filters).
* @param array $options [optional] {
* Configuration Options.
*
* @type string $description A description of the metric.
* }
* @return Metric
*/
public function createMetric($name, $filter, array $options = [])
{
$response = $this->connection->createMetric($options + [
'parent' => $this->formattedProjectName,
'name' => $name,
'filter' => $filter
]);
return new Metric($this->connection, $name, $this->projectId, $response);
}
/**
* Lazily instantiates a metric. There are no network requests made at this
* point. To see the operations that can be performed on a metric please see
* {@see \Google\Cloud\Logging\Metric}.
*
* Example:
* ```
* $metric = $logging->metric('my-metric');
* echo $metric->name();
* ```
*
* @param string $name The name of the metric.
* @return Metric
*/
public function metric($name)
{
return new Metric($this->connection, $name, $this->projectId);
}
/**
* Fetches metrics associated with your project.
*
* Example:
* ```
* $metrics = $logging->metrics();
*
* foreach ($metrics as $metric) {
* echo $metric->name() . PHP_EOL;
* }
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.metrics/list projects.metrics list API documentation.
* @codingStandardsIgnoreEnd
*
* @param array $options [optional] {
* Configuration options.
*
* @type int $pageSize The maximum number of results to return per
* request.
* @type int $resultLimit Limit the number of results returned in total.
* **Defaults to** `0` (return all results).
* @type string $pageToken A previously-returned page token used to
* resume the loading of results from a specific point.
* }
* @return ItemIterator<Metric>
*/
public function metrics(array $options = [])
{
$resultLimit = $this->pluck('resultLimit', $options, false);
return new ItemIterator(
new PageIterator(
function (array $metric) {
return new Metric($this->connection, $metric['name'], $this->projectId, $metric);
},
[$this->connection, 'listMetrics'],
$options + ['parent' => $this->formattedProjectName],
[
'itemsKey' => 'metrics',
'resultLimit' => $resultLimit
]
)
);
}
/**
* Fetches log entries.
*
* Example:
* ```
* $entries = $logging->entries();
*
* foreach ($entries as $entry) {
* echo $entry->info()['textPayload'] . PHP_EOL;
* }
* ```
*
* ```
* // Use an advanced logs filter to fetch only entries from a specified log.
* $entries = $logging->entries([
* 'filter' => 'logName = projects/my-project/logs/my-log'
* ]);
*
* foreach ($entries as $entry) {
* echo $entry->info()['textPayload'] . PHP_EOL;
* }
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/logging/docs/api/reference/rest/v2/entries/list Entries list API documentation.
* @codingStandardsIgnoreEnd
*
* @param array $options [optional] {
* Configuration options.
*
* @type string[] $projectIds A list of projectIds to fetch
* entries from in addition to entries found in the project bound
* to this client.
* @type string[] $resourceNames One or more cloud resources from which
* to retrieve log entries. Projects listed in projectIds are
* added to this list.
* Example: "projects/my-project-1A", "projects/1234567890".
* @type string $filter An [advanced logs filter](https://cloud.google.com/logging/docs/view/advanced_filters).
* @type string $orderBy How the results should be sorted. Presently,
* the only permitted values are `timestamp asc` and
* `timestamp desc`. **Defaults to** `"timestamp asc"`.
* @type int $pageSize The maximum number of results to return per
* request.
* @type int $resultLimit Limit the number of results returned in total.
* **Defaults to** `0` (return all results).
* @type string $pageToken A previously-returned page token used to
* resume the loading of results from a specific point.
* }
* @return ItemIterator<Entry>
*/
public function entries(array $options = [])
{
$resultLimit = $this->pluck('resultLimit', $options, false);
$resourceNames = ['projects/' . $this->projectId];
if (isset($options['projectIds'])) {
foreach ($options['projectIds'] as $projectId) {
$resourceNames[] = 'projects/' . $projectId;
}
unset($options['projectIds']);
}
if (isset($options['resourceNames'])) {
$options['resourceNames'] = array_merge($resourceNames, $options['resourceNames']);
} else {
$options['resourceNames'] = $resourceNames;
}
return new ItemIterator(
new PageIterator(
function (array $entry) {
return new Entry($entry);
},
[$this->connection, 'listEntries'],
$options,
[
'itemsKey' => 'entries',
'resultLimit' => $resultLimit
]
)
);
}
/**
* Fetches a logger which will write log entries to Stackdriver Logging and
* implements the PSR-3 specification.
*
* Example:
* ```
* $psrLogger = $logging->psrLogger('my-log');
* ```
*
* ```
* // Write entries with background batching.
* $psrLogger = $logging->psrLogger('my-log', [
* 'batchEnabled' => true
* ]);
* ```
*
* @param string $name The name of the log to write entries to.
* @param array $options [optional] {
* Configuration options.
*
* @type string $messageKey The key in the `jsonPayload` used to contain
* the logged message. **Defaults to** `message`.
* @type array $resource The
* [monitored resource](https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource)
* to associate log entries with. **Defaults to** type global.
* @type array $labels A set of user-defined (key, value) data that
* provides additional information about the log entry.
* @type MetadataProviderInterface $metadataProvider **Defaults to** An
* automatically chosen provider, based on detected environment
* settings.
* @type bool $batchEnabled Determines whether or not to use background
* batching. **Defaults to** `false`.
* @type bool $debugOutput Whether or not to output debug information.
* Please note debug output currently only applies in CLI based
* applications. **Defaults to** `false`. Applies only when
* `batchEnabled` is set to `true`.
* @type resource $debugOutputResource A resource to output debug output
* to. Applies only when `batchEnabled` is set to `true`.
* @type array $batchOptions A set of options for a BatchJob.
* {@see \Google\Cloud\Core\Batch\BatchJob::__construct()} for
* more details.
* **Defaults to** ['batchSize' => 1000,
* 'callPeriod' => 2.0,
* 'numWorkers' => 2]. Applies only when
* `batchEnabled` is set to `true`. Note that this option is
* currently considered **experimental** and is subject to change.
* @type array $clientConfig Configuration options for the Logging client
* used to handle processing of batch items. For valid options
* please see
* {@see \Google\Cloud\Logging\LoggingClient::__construct()}.
* **Defaults to** the options provided to the current client.
* Applies only when `batchEnabled` is set to `true`.
* @type BatchRunner $batchRunner A BatchRunner object. Mainly used for
* the tests to inject a mock. **Defaults to** a newly created
* BatchRunner. Applies only when `batchEnabled` is set to `true`.
* @type ClosureSerializerInterface $closureSerializer An implementation
* responsible for serializing closures used in the
* `$clientConfig`. This is especially important when using the
* batch daemon. **Defaults to**
* {@see \Google\Cloud\Core\Batch\OpisClosureSerializer} if the
* `opis/closure` library is installed.
* }
* @return PsrLogger
*/
public function psrLogger($name, array $options = [])
{
$messageKey = null;
if (isset($options['messageKey'])) {
$messageKey = $options['messageKey'];
unset($options['messageKey']);
}
$psrLoggerOptions = $this->pluckArray([
'metadataProvider',
'batchEnabled',
'debugOutput',
'batchOptions',
'clientConfig',
'batchRunner',
'closureSerializer',
'debugOutputResource'
], $options);
return new PsrLogger(
$this->logger($name, $options),
$messageKey,
$psrLoggerOptions + [
'clientConfig' => $this->config
]
);
}
/**
* Fetches a logger which will write log entries to Stackdriver Logging.
*
* Example:
* ```
* $logger = $logging->logger('my-log');
* ```
*
* @param string $name The name of the log to write entries to.
* @param array $options [optional] {
* Configuration options.
*
* @type array $resource The
* [monitored resource](https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource)
* to associate log entries with. **Defaults to** type global.
* @type array $labels A set of user-defined (key, value) data that
* provides additional information about the log entry.
* }
* @return Logger
*/
public function logger($name, array $options = [])
{
return new Logger(
$this->connection,
$name,
$this->projectId,
isset($options['resource']) ? $options['resource'] : null,
isset($options['labels']) ? $options['labels'] : null
);
}
}