Skip to content

Commit 215970e

Browse files
authored
Add monitored resource samples (GoogleCloudPlatform#797)
1 parent 379835e commit 215970e

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

monitoring/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
"src/delete_metric.php",
2020
"src/delete_uptime_check.php",
2121
"src/get_descriptor.php",
22+
"src/get_resource.php",
2223
"src/get_uptime_check.php",
2324
"src/list_descriptors.php",
25+
"src/list_resources.php",
2426
"src/list_uptime_check_ips.php",
2527
"src/list_uptime_checks.php",
2628
"src/read_timeseries_align.php",

monitoring/monitoring.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@
8888
);
8989
});
9090

91+
$application->add(new Command('get-resource'))
92+
->setDefinition(clone $inputDefinition)
93+
->addArgument('resource_type', InputArgument::REQUIRED, 'The monitored resource type')
94+
->setDescription('Gets a monitored resource.')
95+
->setCode(function ($input, $output) {
96+
get_resource(
97+
$input->getArgument('project_id'),
98+
$input->getArgument('resource_type')
99+
);
100+
});
101+
91102
$application->add(new Command('get-uptime-check'))
92103
->setDefinition(clone $inputDefinition)
93104
->addArgument('config_name', InputArgument::REQUIRED, 'The uptime check config name')
@@ -108,6 +119,15 @@
108119
);
109120
});
110121

122+
$application->add(new Command('list-resources'))
123+
->setDefinition(clone $inputDefinition)
124+
->setDescription('List monitored resources.')
125+
->setCode(function ($input, $output) {
126+
list_resources(
127+
$input->getArgument('project_id')
128+
);
129+
});
130+
111131
$application->add(new Command('list-uptime-check-ips'))
112132
->setDefinition($inputDefinition)
113133
->setDescription('Lists Uptime Check IPs.')

monitoring/src/get_resource.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/monitoring/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Monitoring;
25+
26+
// [START monitoring_get_resource]
27+
use Google\Cloud\Monitoring\V3\MetricServiceClient;
28+
29+
/**
30+
* Example:
31+
* ```
32+
* get_resource('your-project-id', 'gcs_bucket');
33+
* ```
34+
*
35+
* @param string $projectId Your project ID
36+
* @param string $resourceType The resource type of the monitored resource.
37+
*/
38+
function get_resource($projectId, $resourceType)
39+
{
40+
$metrics = new MetricServiceClient([
41+
'projectId' => $projectId,
42+
]);
43+
44+
$metricName = $metrics->monitoredResourceDescriptorName($projectId, $resourceType);
45+
$resource = $metrics->getMonitoredResourceDescriptor($metricName);
46+
47+
printf('Name: %s' . PHP_EOL, $resource->getName());
48+
printf('Type: %s' . PHP_EOL, $resource->getType());
49+
printf('Display Name: %s' . PHP_EOL, $resource->getDisplayName());
50+
printf('Description: %s' . PHP_EOL, $resource->getDescription());
51+
printf('Labels:' . PHP_EOL);
52+
foreach ($resource->getLabels() as $labels) {
53+
printf(' %s (%s) - %s' . PHP_EOL,
54+
$labels->getKey(),
55+
$labels->getValueType(),
56+
$labels->getDescription());
57+
}
58+
}
59+
// [END monitoring_get_resource]

monitoring/src/list_resources.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/monitoring/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Monitoring;
25+
26+
// [START monitoring_list_resources]
27+
use Google\Cloud\Monitoring\V3\MetricServiceClient;
28+
29+
/**
30+
* Example:
31+
* ```
32+
* list_resources('your-project-id');
33+
* ```
34+
*
35+
* @param string $projectId Your project ID
36+
*/
37+
function list_resources($projectId)
38+
{
39+
$metrics = new MetricServiceClient([
40+
'projectId' => $projectId,
41+
]);
42+
$projectName = $metrics->projectName($projectId);
43+
$descriptors = $metrics->listMonitoredResourceDescriptors($projectName);
44+
foreach ($descriptors->iterateAllElements() as $descriptor) {
45+
print($descriptor->getType() . PHP_EOL);
46+
}
47+
}
48+
// [END monitoring_list_resources]

monitoring/test/monitoringTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,23 @@ public function testDeleteMetric()
152152
}, self::RETRY_COUNT, true);
153153
}
154154

155+
public function testGetResource()
156+
{
157+
$output = $this->runCommand('get-resource', [
158+
'project_id' => self::$projectId,
159+
'resource_type' => 'gcs_bucket',
160+
]);
161+
$this->assertContains('A Google Cloud Storage (GCS) bucket.', $output);
162+
}
163+
164+
public function testListResources()
165+
{
166+
$output = $this->runCommand('list-resources', [
167+
'project_id' => self::$projectId,
168+
]);
169+
$this->assertContains('gcs_bucket', $output);
170+
}
171+
155172
public function testWriteTimeseries()
156173
{
157174
// Catch all exceptions as this method occasionally throws an Internal error.

0 commit comments

Comments
 (0)