Skip to content

Commit

Permalink
chore: upgrade monitoring samples to new client surface (#1949)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jan 8, 2024
1 parent 8c79d1a commit 2522245
Show file tree
Hide file tree
Showing 29 changed files with 234 additions and 135 deletions.
2 changes: 1 addition & 1 deletion monitoring/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-monitoring": "^1.0.0"
"google/cloud-monitoring": "^1.9"
}
}
10 changes: 7 additions & 3 deletions monitoring/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
# Imports the Google Cloud client library
use Google\Api\Metric;
use Google\Api\MonitoredResource;
use Google\Cloud\Monitoring\V3\MetricServiceClient;
use Google\Cloud\Monitoring\V3\Client\MetricServiceClient;
use Google\Cloud\Monitoring\V3\CreateTimeSeriesRequest;
use Google\Cloud\Monitoring\V3\Point;
use Google\Cloud\Monitoring\V3\TimeInterval;
use Google\Cloud\Monitoring\V3\TimeSeries;
Expand All @@ -37,7 +38,7 @@

try {
$client = new MetricServiceClient();
$formattedProjectName = $client->projectName($projectId);
$formattedProjectName = 'projects/' . $projectId;
$labels = [
'instance_id' => $instanceId,
'zone' => $zone,
Expand Down Expand Up @@ -69,8 +70,11 @@
$timeSeries->setMetric($m);
$timeSeries->setResource($r);
$timeSeries->setPoints($points);
$createTimeSeriesRequest = (new CreateTimeSeriesRequest())
->setName($formattedProjectName)
->setTimeSeries([$timeSeries]);

$client->createTimeSeries($formattedProjectName, [$timeSeries]);
$client->createTimeSeries($createTimeSeriesRequest);
print('Successfully submitted a time series' . PHP_EOL);
} finally {
$client->close();
Expand Down
16 changes: 11 additions & 5 deletions monitoring/src/alert_backup_policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
namespace Google\Cloud\Samples\Monitoring;

// [START monitoring_alert_backup_policies]
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;
use Google\Cloud\Monitoring\V3\ListNotificationChannelsRequest;

/**
* Back up alert policies.
Expand All @@ -40,18 +42,22 @@ function alert_backup_policies($projectId)
$channelClient = new NotificationChannelServiceClient([
'projectId' => $projectId,
]);
$projectName = $alertClient->projectName($projectId);
$projectName = 'projects/' . $projectId;

$record = [
'project_name' => $projectName,
'policies' => [],
'channels' => [],
];
$policies = $alertClient->listAlertPolicies($projectName);
$listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
->setName($projectName);
$policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
foreach ($policies->iterateAllElements() as $policy) {
$record['policies'][] = json_decode($policy->serializeToJsonString());
}
$channels = $channelClient->listNotificationChannels($projectName);
$listNotificationChannelsRequest = (new ListNotificationChannelsRequest())
->setName($projectName);
$channels = $channelClient->listNotificationChannels($listNotificationChannelsRequest);
foreach ($channels->iterateAllElements() as $channel) {
$record['channels'][] = json_decode($channel->serializeToJsonString());
}
Expand Down
10 changes: 7 additions & 3 deletions monitoring/src/alert_create_channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Monitoring;

# [START monitoring_alert_create_channel]
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\CreateNotificationChannelRequest;
use Google\Cloud\Monitoring\V3\NotificationChannel;

/**
Expand All @@ -35,14 +36,17 @@ function alert_create_channel(string $projectId): void
$channelClient = new NotificationChannelServiceClient([
'projectId' => $projectId,
]);
$projectName = $channelClient->projectName($projectId);
$projectName = 'projects/' . $projectId;

$channel = new NotificationChannel();
$channel->setDisplayName('Test Notification Channel');
$channel->setType('email');
$channel->setLabels(['email_address' => '[email protected]']);
$createNotificationChannelRequest = (new CreateNotificationChannelRequest())
->setName($projectName)
->setNotificationChannel($channel);

$channel = $channelClient->createNotificationChannel($projectName, $channel);
$channel = $channelClient->createNotificationChannel($createNotificationChannelRequest);
printf('Created notification channel %s' . PHP_EOL, $channel->getName());
}
# [END monitoring_alert_create_channel]
Expand Down
12 changes: 8 additions & 4 deletions monitoring/src/alert_create_policy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
namespace Google\Cloud\Samples\Monitoring;

# [START monitoring_alert_create_policy]
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\AlertPolicy;
use Google\Cloud\Monitoring\V3\ComparisonType;
use Google\Cloud\Monitoring\V3\AlertPolicy\Condition;
use Google\Cloud\Monitoring\V3\AlertPolicy\Condition\MetricThreshold;
use Google\Cloud\Monitoring\V3\AlertPolicy\ConditionCombinerType;
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\ComparisonType;
use Google\Cloud\Monitoring\V3\CreateAlertPolicyRequest;
use Google\Protobuf\Duration;

/**
Expand All @@ -40,7 +41,7 @@ function alert_create_policy($projectId)
$alertClient = new AlertPolicyServiceClient([
'projectId' => $projectId,
]);
$projectName = $alertClient->projectName($projectId);
$projectName = 'projects/' . $projectId;

$policy = new AlertPolicy();
$policy->setDisplayName('Test Alert Policy');
Expand All @@ -55,8 +56,11 @@ function alert_create_policy($projectId)
'comparison' => ComparisonType::COMPARISON_LT,
])
])]);
$createAlertPolicyRequest = (new CreateAlertPolicyRequest())
->setName($projectName)
->setAlertPolicy($policy);

$policy = $alertClient->createAlertPolicy($projectName, $policy);
$policy = $alertClient->createAlertPolicy($createAlertPolicyRequest);
printf('Created alert policy %s' . PHP_EOL, $policy->getName());
}
# [END monitoring_alert_create_policy]
Expand Down
7 changes: 5 additions & 2 deletions monitoring/src/alert_delete_channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Monitoring;

# [START monitoring_alert_delete_channel]
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\DeleteNotificationChannelRequest;

/**
* @param string $projectId Your project ID
Expand All @@ -36,8 +37,10 @@ function alert_delete_channel(string $projectId, string $channelId): void
'projectId' => $projectId,
]);
$channelName = $channelClient->notificationChannelName($projectId, $channelId);
$deleteNotificationChannelRequest = (new DeleteNotificationChannelRequest())
->setName($channelName);

$channelClient->deleteNotificationChannel($channelName);
$channelClient->deleteNotificationChannel($deleteNotificationChannelRequest);
printf('Deleted notification channel %s' . PHP_EOL, $channelName);
}
# [END monitoring_alert_delete_channel]
Expand Down
20 changes: 12 additions & 8 deletions monitoring/src/alert_enable_policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
namespace Google\Cloud\Samples\Monitoring;

// [START monitoring_alert_enable_policies]
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;
use Google\Cloud\Monitoring\V3\UpdateAlertPolicyRequest;
use Google\Protobuf\FieldMask;

/**
Expand All @@ -40,11 +42,12 @@ function alert_enable_policies($projectId, $enable = true, $filter = null)
$alertClient = new AlertPolicyServiceClient([
'projectId' => $projectId,
]);
$projectName = $alertClient->projectName($projectId);
$projectName = 'projects/' . $projectId;
$listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
->setName($projectName)
->setFilter($filter);

$policies = $alertClient->listAlertPolicies($projectName, [
'filter' => $filter
]);
$policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
foreach ($policies->iterateAllElements() as $policy) {
$isEnabled = $policy->getEnabled()->getValue();
if ($enable == $isEnabled) {
Expand All @@ -56,9 +59,10 @@ function alert_enable_policies($projectId, $enable = true, $filter = null)
$policy->getEnabled()->setValue((bool) $enable);
$mask = new FieldMask();
$mask->setPaths(['enabled']);
$alertClient->updateAlertPolicy($policy, [
'updateMask' => $mask
]);
$updateAlertPolicyRequest = (new UpdateAlertPolicyRequest())
->setAlertPolicy($policy)
->setUpdateMask($mask);
$alertClient->updateAlertPolicy($updateAlertPolicyRequest);
printf('%s %s' . PHP_EOL,
$enable ? 'Enabled' : 'Disabled',
$policy->getName()
Expand Down
10 changes: 6 additions & 4 deletions monitoring/src/alert_list_channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@
namespace Google\Cloud\Samples\Monitoring;

// [START monitoring_alert_list_channels]
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\ListNotificationChannelsRequest;

/**
* @param string $projectId Your project ID
*/
function alert_list_channels($projectId)
{
$projectName = 'projects/' . $projectId;
$channelClient = new NotificationChannelServiceClient([
'projectId' => $projectId,
]);
$listNotificationChannelsRequest = (new ListNotificationChannelsRequest())
->setName($projectName);

$channels = $channelClient->listNotificationChannels(
$channelClient->projectName($projectId)
);
$channels = $channelClient->listNotificationChannels($listNotificationChannelsRequest);
foreach ($channels->iterateAllElements() as $channel) {
printf('Name: %s (%s)' . PHP_EOL, $channel->getDisplayName(), $channel->getName());
}
Expand Down
10 changes: 6 additions & 4 deletions monitoring/src/alert_list_policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Monitoring;

// [START monitoring_alert_list_policies]
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;

/**
* Adds a new column to the Albums table in the example database.
Expand All @@ -37,13 +38,14 @@
*/
function alert_list_policies($projectId)
{
$projectName = 'projects/' . $projectId;
$alertClient = new AlertPolicyServiceClient([
'projectId' => $projectId,
]);
$listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
->setName($projectName);

$policies = $alertClient->listAlertPolicies(
$alertClient->projectName($projectId)
);
$policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
foreach ($policies->iterateAllElements() as $policy) {
printf('Name: %s (%s)' . PHP_EOL, $policy->getDisplayName(), $policy->getName());
}
Expand Down
12 changes: 7 additions & 5 deletions monitoring/src/alert_replace_channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
namespace Google\Cloud\Samples\Monitoring;

// [START monitoring_alert_replace_channels]
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\AlertPolicy;
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\UpdateAlertPolicyRequest;
use Google\Protobuf\FieldMask;

/**
Expand All @@ -53,9 +54,10 @@ function alert_replace_channels(string $projectId, string $alertPolicyId, array
$policy->setNotificationChannels($newChannels);
$mask = new FieldMask();
$mask->setPaths(['notification_channels']);
$updatedPolicy = $alertClient->updateAlertPolicy($policy, [
'updateMask' => $mask,
]);
$updateAlertPolicyRequest = (new UpdateAlertPolicyRequest())
->setAlertPolicy($policy)
->setUpdateMask($mask);
$updatedPolicy = $alertClient->updateAlertPolicy($updateAlertPolicyRequest);
printf('Updated %s' . PHP_EOL, $updatedPolicy->getName());
}
// [END monitoring_alert_replace_channels]
Expand Down
33 changes: 22 additions & 11 deletions monitoring/src/alert_restore_policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
# [START monitoring_alert_restore_policies]
# [START monitoring_alert_update_channel]
# [START monitoring_alert_enable_channel]
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\NotificationChannelServiceClient;
use Google\ApiCore\ApiException;
use Google\Cloud\Monitoring\V3\AlertPolicy;
use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
use Google\Cloud\Monitoring\V3\Client\NotificationChannelServiceClient;
use Google\Cloud\Monitoring\V3\CreateAlertPolicyRequest;
use Google\Cloud\Monitoring\V3\CreateNotificationChannelRequest;
use Google\Cloud\Monitoring\V3\NotificationChannel;
use Google\Cloud\Monitoring\V3\NotificationChannel\VerificationStatus;
use Google\ApiCore\ApiException;
use Google\Cloud\Monitoring\V3\UpdateAlertPolicyRequest;
use Google\Cloud\Monitoring\V3\UpdateNotificationChannelRequest;

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

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

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

if ($isSameProject) {
try {
$channelClient->updateNotificationChannel($channel);
$updateNotificationChannelRequest = (new UpdateNotificationChannelRequest())
->setNotificationChannel($channel);
$channelClient->updateNotificationChannel($updateNotificationChannelRequest);
$updated = true;
} catch (ApiException $e) {
# The channel was deleted. Create it below.
Expand All @@ -96,10 +102,10 @@ function alert_restore_policies(string $projectId): void
# The channel no longer exists. Recreate it.
$oldName = $channel->getName();
$channel->setName('');
$newChannel = $channelClient->createNotificationChannel(
$projectName,
$channel
);
$createNotificationChannelRequest = (new CreateNotificationChannelRequest())
->setName($projectName)
->setNotificationChannel($channel);
$newChannel = $channelClient->createNotificationChannel($createNotificationChannelRequest);
$channelNameMap[$oldName] = $newChannel->getName();
}
}
Expand All @@ -123,7 +129,9 @@ function alert_restore_policies(string $projectId): void
$updated = false;
if ($isSameProject) {
try {
$alertClient->updateAlertPolicy($policy);
$updateAlertPolicyRequest = (new UpdateAlertPolicyRequest())
->setAlertPolicy($policy);
$alertClient->updateAlertPolicy($updateAlertPolicyRequest);
$updated = true;
} catch (ApiException $e) {
# The policy was deleted. Create it below.
Expand All @@ -140,7 +148,10 @@ function alert_restore_policies(string $projectId): void
foreach ($policy->getConditions() as $condition) {
$condition->setName('');
}
$policy = $alertClient->createAlertPolicy($projectName, $policy);
$createAlertPolicyRequest = (new CreateAlertPolicyRequest())
->setName($projectName)
->setAlertPolicy($policy);
$policy = $alertClient->createAlertPolicy($createAlertPolicyRequest);
}
printf('Updated %s' . PHP_EOL, $policy->getName());
}
Expand Down
Loading

0 comments on commit 2522245

Please sign in to comment.