Skip to content

Commit 2522245

Browse files
authored
chore: upgrade monitoring samples to new client surface (GoogleCloudPlatform#1949)
1 parent 8c79d1a commit 2522245

29 files changed

+234
-135
lines changed

monitoring/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-monitoring": "^1.0.0"
3+
"google/cloud-monitoring": "^1.9"
44
}
55
}

monitoring/quickstart.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
# Imports the Google Cloud client library
2323
use Google\Api\Metric;
2424
use Google\Api\MonitoredResource;
25-
use Google\Cloud\Monitoring\V3\MetricServiceClient;
25+
use Google\Cloud\Monitoring\V3\Client\MetricServiceClient;
26+
use Google\Cloud\Monitoring\V3\CreateTimeSeriesRequest;
2627
use Google\Cloud\Monitoring\V3\Point;
2728
use Google\Cloud\Monitoring\V3\TimeInterval;
2829
use Google\Cloud\Monitoring\V3\TimeSeries;
@@ -37,7 +38,7 @@
3738

3839
try {
3940
$client = new MetricServiceClient();
40-
$formattedProjectName = $client->projectName($projectId);
41+
$formattedProjectName = 'projects/' . $projectId;
4142
$labels = [
4243
'instance_id' => $instanceId,
4344
'zone' => $zone,
@@ -69,8 +70,11 @@
6970
$timeSeries->setMetric($m);
7071
$timeSeries->setResource($r);
7172
$timeSeries->setPoints($points);
73+
$createTimeSeriesRequest = (new CreateTimeSeriesRequest())
74+
->setName($formattedProjectName)
75+
->setTimeSeries([$timeSeries]);
7276

73-
$client->createTimeSeries($formattedProjectName, [$timeSeries]);
77+
$client->createTimeSeries($createTimeSeriesRequest);
7478
print('Successfully submitted a time series' . PHP_EOL);
7579
} finally {
7680
$client->close();

monitoring/src/alert_backup_policies.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
// [START monitoring_alert_backup_policies]
27-
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
28-
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
27+
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
28+
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
29+
use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;
30+
use Google\Cloud\Monitoring\V3\ListNotificationChannelsRequest;
2931

3032
/**
3133
* Back up alert policies.
@@ -40,18 +42,22 @@ function alert_backup_policies($projectId)
4042
$channelClient = new NotificationChannelServiceClient([
4143
'projectId' => $projectId,
4244
]);
43-
$projectName = $alertClient->projectName($projectId);
45+
$projectName = 'projects/' . $projectId;
4446

4547
$record = [
4648
'project_name' => $projectName,
4749
'policies' => [],
4850
'channels' => [],
4951
];
50-
$policies = $alertClient->listAlertPolicies($projectName);
52+
$listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
53+
->setName($projectName);
54+
$policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
5155
foreach ($policies->iterateAllElements() as $policy) {
5256
$record['policies'][] = json_decode($policy->serializeToJsonString());
5357
}
54-
$channels = $channelClient->listNotificationChannels($projectName);
58+
$listNotificationChannelsRequest = (new ListNotificationChannelsRequest())
59+
->setName($projectName);
60+
$channels = $channelClient->listNotificationChannels($listNotificationChannelsRequest);
5561
foreach ($channels->iterateAllElements() as $channel) {
5662
$record['channels'][] = json_decode($channel->serializeToJsonString());
5763
}

monitoring/src/alert_create_channel.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
# [START monitoring_alert_create_channel]
27-
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
27+
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
28+
use Google\Cloud\Monitoring\V3\CreateNotificationChannelRequest;
2829
use Google\Cloud\Monitoring\V3\NotificationChannel;
2930

3031
/**
@@ -35,14 +36,17 @@ function alert_create_channel(string $projectId): void
3536
$channelClient = new NotificationChannelServiceClient([
3637
'projectId' => $projectId,
3738
]);
38-
$projectName = $channelClient->projectName($projectId);
39+
$projectName = 'projects/' . $projectId;
3940

4041
$channel = new NotificationChannel();
4142
$channel->setDisplayName('Test Notification Channel');
4243
$channel->setType('email');
4344
$channel->setLabels(['email_address' => '[email protected]']);
45+
$createNotificationChannelRequest = (new CreateNotificationChannelRequest())
46+
->setName($projectName)
47+
->setNotificationChannel($channel);
4448

45-
$channel = $channelClient->createNotificationChannel($projectName, $channel);
49+
$channel = $channelClient->createNotificationChannel($createNotificationChannelRequest);
4650
printf('Created notification channel %s' . PHP_EOL, $channel->getName());
4751
}
4852
# [END monitoring_alert_create_channel]

monitoring/src/alert_create_policy.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
# [START monitoring_alert_create_policy]
27-
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
2827
use Google\Cloud\Monitoring\V3\AlertPolicy;
29-
use Google\Cloud\Monitoring\V3\ComparisonType;
3028
use Google\Cloud\Monitoring\V3\AlertPolicy\Condition;
3129
use Google\Cloud\Monitoring\V3\AlertPolicy\Condition\MetricThreshold;
3230
use Google\Cloud\Monitoring\V3\AlertPolicy\ConditionCombinerType;
31+
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
32+
use Google\Cloud\Monitoring\V3\ComparisonType;
33+
use Google\Cloud\Monitoring\V3\CreateAlertPolicyRequest;
3334
use Google\Protobuf\Duration;
3435

3536
/**
@@ -40,7 +41,7 @@ function alert_create_policy($projectId)
4041
$alertClient = new AlertPolicyServiceClient([
4142
'projectId' => $projectId,
4243
]);
43-
$projectName = $alertClient->projectName($projectId);
44+
$projectName = 'projects/' . $projectId;
4445

4546
$policy = new AlertPolicy();
4647
$policy->setDisplayName('Test Alert Policy');
@@ -55,8 +56,11 @@ function alert_create_policy($projectId)
5556
'comparison' => ComparisonType::COMPARISON_LT,
5657
])
5758
])]);
59+
$createAlertPolicyRequest = (new CreateAlertPolicyRequest())
60+
->setName($projectName)
61+
->setAlertPolicy($policy);
5862

59-
$policy = $alertClient->createAlertPolicy($projectName, $policy);
63+
$policy = $alertClient->createAlertPolicy($createAlertPolicyRequest);
6064
printf('Created alert policy %s' . PHP_EOL, $policy->getName());
6165
}
6266
# [END monitoring_alert_create_policy]

monitoring/src/alert_delete_channel.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
# [START monitoring_alert_delete_channel]
27-
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
27+
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
28+
use Google\Cloud\Monitoring\V3\DeleteNotificationChannelRequest;
2829

2930
/**
3031
* @param string $projectId Your project ID
@@ -36,8 +37,10 @@ function alert_delete_channel(string $projectId, string $channelId): void
3637
'projectId' => $projectId,
3738
]);
3839
$channelName = $channelClient->notificationChannelName($projectId, $channelId);
40+
$deleteNotificationChannelRequest = (new DeleteNotificationChannelRequest())
41+
->setName($channelName);
3942

40-
$channelClient->deleteNotificationChannel($channelName);
43+
$channelClient->deleteNotificationChannel($deleteNotificationChannelRequest);
4144
printf('Deleted notification channel %s' . PHP_EOL, $channelName);
4245
}
4346
# [END monitoring_alert_delete_channel]

monitoring/src/alert_enable_policies.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
// [START monitoring_alert_enable_policies]
27-
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
27+
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
28+
use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;
29+
use Google\Cloud\Monitoring\V3\UpdateAlertPolicyRequest;
2830
use Google\Protobuf\FieldMask;
2931

3032
/**
@@ -40,11 +42,12 @@ function alert_enable_policies($projectId, $enable = true, $filter = null)
4042
$alertClient = new AlertPolicyServiceClient([
4143
'projectId' => $projectId,
4244
]);
43-
$projectName = $alertClient->projectName($projectId);
45+
$projectName = 'projects/' . $projectId;
46+
$listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
47+
->setName($projectName)
48+
->setFilter($filter);
4449

45-
$policies = $alertClient->listAlertPolicies($projectName, [
46-
'filter' => $filter
47-
]);
50+
$policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
4851
foreach ($policies->iterateAllElements() as $policy) {
4952
$isEnabled = $policy->getEnabled()->getValue();
5053
if ($enable == $isEnabled) {
@@ -56,9 +59,10 @@ function alert_enable_policies($projectId, $enable = true, $filter = null)
5659
$policy->getEnabled()->setValue((bool) $enable);
5760
$mask = new FieldMask();
5861
$mask->setPaths(['enabled']);
59-
$alertClient->updateAlertPolicy($policy, [
60-
'updateMask' => $mask
61-
]);
62+
$updateAlertPolicyRequest = (new UpdateAlertPolicyRequest())
63+
->setAlertPolicy($policy)
64+
->setUpdateMask($mask);
65+
$alertClient->updateAlertPolicy($updateAlertPolicyRequest);
6266
printf('%s %s' . PHP_EOL,
6367
$enable ? 'Enabled' : 'Disabled',
6468
$policy->getName()

monitoring/src/alert_list_channels.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
// [START monitoring_alert_list_channels]
27-
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
27+
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
28+
use Google\Cloud\Monitoring\V3\ListNotificationChannelsRequest;
2829

2930
/**
3031
* @param string $projectId Your project ID
3132
*/
3233
function alert_list_channels($projectId)
3334
{
35+
$projectName = 'projects/' . $projectId;
3436
$channelClient = new NotificationChannelServiceClient([
3537
'projectId' => $projectId,
3638
]);
39+
$listNotificationChannelsRequest = (new ListNotificationChannelsRequest())
40+
->setName($projectName);
3741

38-
$channels = $channelClient->listNotificationChannels(
39-
$channelClient->projectName($projectId)
40-
);
42+
$channels = $channelClient->listNotificationChannels($listNotificationChannelsRequest);
4143
foreach ($channels->iterateAllElements() as $channel) {
4244
printf('Name: %s (%s)' . PHP_EOL, $channel->getDisplayName(), $channel->getName());
4345
}

monitoring/src/alert_list_policies.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
// [START monitoring_alert_list_policies]
27-
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
27+
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
28+
use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;
2829

2930
/**
3031
* Adds a new column to the Albums table in the example database.
@@ -37,13 +38,14 @@
3738
*/
3839
function alert_list_policies($projectId)
3940
{
41+
$projectName = 'projects/' . $projectId;
4042
$alertClient = new AlertPolicyServiceClient([
4143
'projectId' => $projectId,
4244
]);
45+
$listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
46+
->setName($projectName);
4347

44-
$policies = $alertClient->listAlertPolicies(
45-
$alertClient->projectName($projectId)
46-
);
48+
$policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
4749
foreach ($policies->iterateAllElements() as $policy) {
4850
printf('Name: %s (%s)' . PHP_EOL, $policy->getDisplayName(), $policy->getName());
4951
}

monitoring/src/alert_replace_channels.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Monitoring;
2525

2626
// [START monitoring_alert_replace_channels]
27-
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
28-
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
2927
use Google\Cloud\Monitoring\V3\AlertPolicy;
28+
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
29+
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
30+
use Google\Cloud\Monitoring\V3\UpdateAlertPolicyRequest;
3031
use Google\Protobuf\FieldMask;
3132

3233
/**
@@ -53,9 +54,10 @@ function alert_replace_channels(string $projectId, string $alertPolicyId, array
5354
$policy->setNotificationChannels($newChannels);
5455
$mask = new FieldMask();
5556
$mask->setPaths(['notification_channels']);
56-
$updatedPolicy = $alertClient->updateAlertPolicy($policy, [
57-
'updateMask' => $mask,
58-
]);
57+
$updateAlertPolicyRequest = (new UpdateAlertPolicyRequest())
58+
->setAlertPolicy($policy)
59+
->setUpdateMask($mask);
60+
$updatedPolicy = $alertClient->updateAlertPolicy($updateAlertPolicyRequest);
5961
printf('Updated %s' . PHP_EOL, $updatedPolicy->getName());
6062
}
6163
// [END monitoring_alert_replace_channels]

monitoring/src/alert_restore_policies.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
# [START monitoring_alert_restore_policies]
2727
# [START monitoring_alert_update_channel]
2828
# [START monitoring_alert_enable_channel]
29-
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
30-
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
29+
use Google\ApiCore\ApiException;
3130
use Google\Cloud\Monitoring\V3\AlertPolicy;
31+
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
32+
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
33+
use Google\Cloud\Monitoring\V3\CreateAlertPolicyRequest;
34+
use Google\Cloud\Monitoring\V3\CreateNotificationChannelRequest;
3235
use Google\Cloud\Monitoring\V3\NotificationChannel;
3336
use Google\Cloud\Monitoring\V3\NotificationChannel\VerificationStatus;
34-
use Google\ApiCore\ApiException;
37+
use Google\Cloud\Monitoring\V3\UpdateAlertPolicyRequest;
38+
use Google\Cloud\Monitoring\V3\UpdateNotificationChannelRequest;
3539

3640
/**
3741
* @param string $projectId Your project ID
@@ -47,7 +51,7 @@ function alert_restore_policies(string $projectId): void
4751
]);
4852

4953
print('Loading alert policies and notification channels from backup.json.' . PHP_EOL);
50-
$projectName = $alertClient->projectName($projectId);
54+
$projectName = 'projects/' . $projectId;
5155
$record = json_decode((string) file_get_contents('backup.json'), true);
5256
$isSameProject = $projectName == $record['project_name'];
5357

@@ -82,7 +86,9 @@ function alert_restore_policies(string $projectId): void
8286

8387
if ($isSameProject) {
8488
try {
85-
$channelClient->updateNotificationChannel($channel);
89+
$updateNotificationChannelRequest = (new UpdateNotificationChannelRequest())
90+
->setNotificationChannel($channel);
91+
$channelClient->updateNotificationChannel($updateNotificationChannelRequest);
8692
$updated = true;
8793
} catch (ApiException $e) {
8894
# The channel was deleted. Create it below.
@@ -96,10 +102,10 @@ function alert_restore_policies(string $projectId): void
96102
# The channel no longer exists. Recreate it.
97103
$oldName = $channel->getName();
98104
$channel->setName('');
99-
$newChannel = $channelClient->createNotificationChannel(
100-
$projectName,
101-
$channel
102-
);
105+
$createNotificationChannelRequest = (new CreateNotificationChannelRequest())
106+
->setName($projectName)
107+
->setNotificationChannel($channel);
108+
$newChannel = $channelClient->createNotificationChannel($createNotificationChannelRequest);
103109
$channelNameMap[$oldName] = $newChannel->getName();
104110
}
105111
}
@@ -123,7 +129,9 @@ function alert_restore_policies(string $projectId): void
123129
$updated = false;
124130
if ($isSameProject) {
125131
try {
126-
$alertClient->updateAlertPolicy($policy);
132+
$updateAlertPolicyRequest = (new UpdateAlertPolicyRequest())
133+
->setAlertPolicy($policy);
134+
$alertClient->updateAlertPolicy($updateAlertPolicyRequest);
127135
$updated = true;
128136
} catch (ApiException $e) {
129137
# The policy was deleted. Create it below.
@@ -140,7 +148,10 @@ function alert_restore_policies(string $projectId): void
140148
foreach ($policy->getConditions() as $condition) {
141149
$condition->setName('');
142150
}
143-
$policy = $alertClient->createAlertPolicy($projectName, $policy);
151+
$createAlertPolicyRequest = (new CreateAlertPolicyRequest())
152+
->setName($projectName)
153+
->setAlertPolicy($policy);
154+
$policy = $alertClient->createAlertPolicy($createAlertPolicyRequest);
144155
}
145156
printf('Updated %s' . PHP_EOL, $policy->getName());
146157
}

0 commit comments

Comments
 (0)