Skip to content

Commit

Permalink
samples: migrate code from googleapis/nodejs-monitoring (#2807)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrarmonsur authored Nov 10, 2022
1 parent bef7132 commit be12932
Show file tree
Hide file tree
Showing 26 changed files with 2,189 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/monitoring-snippets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: monitoring-snippets
on:
push:
branches:
- main
paths:
- 'monitoring/snippets/**'
pull_request:
paths:
- 'monitoring/snippets/**'
pull_request_target:
types: [labeled]
schedule:
- cron: '0 0 * * 0'
jobs:
test:
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: 'write'
pull-requests: 'write'
id-token: 'write'
steps:
- uses: actions/checkout@v3.1.0
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- uses: 'google-github-actions/auth@v0.8.3'
with:
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com'
create_credentials_file: 'true'
access_token_lifetime: 600s
- uses: actions/setup-node@v3.5.1
with:
node-version: 16
- run: npm install
working-directory: monitoring/snippets
- run: npm test
working-directory: monitoring/snippets
env:
MOCHA_REPORTER_SUITENAME: monitoring_snippets
MOCHA_REPORTER_OUTPUT: monitoring_snippets_sponge_log.xml
MOCHA_REPORTER: xunit
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
await github.rest.issues.removeLabel({
name: 'actions:force-run',
owner: 'GoogleCloudPlatform',
repo: 'nodejs-docs-samples',
issue_number: context.payload.pull_request.number
});
} catch (e) {
if (!e.message.includes('Label does not exist')) {
throw e;
}
}
- if: ${{ github.event_name == 'schedule'}}
run: |
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
chmod +x ./flakybot
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
1 change: 1 addition & 0 deletions .github/workflows/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"mediatranslation",
"monitoring/opencensus",
"monitoring/prometheus",
"monitoring/snippets",
"datacatalog/cloud-client",
"datacatalog/quickstart",
"datastore/functions",
Expand Down
3 changes: 3 additions & 0 deletions monitoring/snippets/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rules:
no-console: off
2 changes: 2 additions & 0 deletions monitoring/snippets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
policies_backup.json
package-lock.json
60 changes: 60 additions & 0 deletions monitoring/snippets/alerts.backupPolicies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2018 Google LLC
//
// 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.

'use strict';

function main(projectId) {
// [START monitoring_alert_backup_policies]
const fs = require('fs');

// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.AlertPolicyServiceClient();

async function backupPolicies() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';

const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
};

let [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);

// filter out any policies created by tests for this sample
policies = policies.filter(policy => {
return !policy.displayName.startsWith('gcloud-tests-');
});

fs.writeFileSync(
'./policies_backup.json',
JSON.stringify(policies, null, 2),
'utf-8'
);

console.log('Saved policies to ./policies_backup.json');
// [END monitoring_alert_backup_policies]
}
backupPolicies();
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
60 changes: 60 additions & 0 deletions monitoring/snippets/alerts.deleteChannels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2018 Google LLC
//
// 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.

'use strict';

function main(projectId, filter) {
// [START monitoring_alert_delete_channel]
// [START monitoring_alert_list_channels]

// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.NotificationChannelServiceClient();

async function deleteChannels() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';

const request = {
name: client.projectPath(projectId),
filter,
};
const channels = await client.listNotificationChannels(request);
console.log(channels);
for (const channel of channels[0]) {
console.log(`Deleting channel ${channel.displayName}`);
try {
await client.deleteNotificationChannel({
name: channel.name,
});
} catch (err) {
// ignore error
}
}
}
deleteChannels();
// [END monitoring_alert_delete_channel]
// [END monitoring_alert_list_channels]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
71 changes: 71 additions & 0 deletions monitoring/snippets/alerts.enablePolicies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2018 Google LLC
//
// 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.

'use strict';

function main(projectId, enabled, filter) {
enabled = enabled === 'true';

// [START monitoring_alert_enable_policies]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.AlertPolicyServiceClient();

async function enablePolicies() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const enabled = true;
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';

const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
// See https://cloud.google.com/monitoring/alerting/docs/sorting-and-filtering
filter: filter,
};

const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
const responses = [];
for (const policy of policies) {
responses.push(
await client.updateAlertPolicy({
updateMask: {
paths: ['enabled'],
},
alertPolicy: {
name: policy.name,
enabled: {
value: enabled,
},
},
})
);
}
responses.forEach(response => {
const alertPolicy = response[0];
console.log(`${enabled ? 'Enabled' : 'Disabled'} ${alertPolicy.name}.`);
});
}
enablePolicies();
// [END monitoring_alert_enable_policies]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
51 changes: 51 additions & 0 deletions monitoring/snippets/alerts.listPolicies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018 Google LLC
//
// 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.

'use strict';

function main(projectId) {
// [START monitoring_alert_list_policies]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.AlertPolicyServiceClient();

async function listPolicies() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';

const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
};
const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
console.log('Policies:');
policies.forEach(policy => {
console.log(` Display name: ${policy.displayName}`);
if (policy.documentation && policy.documentation.content) {
console.log(` Documentation: ${policy.documentation.content}`);
}
});
}
listPolicies();
// [END monitoring_alert_list_policies]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Loading

0 comments on commit be12932

Please sign in to comment.