Skip to content

Commit be12932

Browse files
authored
samples: migrate code from googleapis/nodejs-monitoring (#2807)
1 parent bef7132 commit be12932

26 files changed

Lines changed: 2189 additions & 0 deletions
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: monitoring-snippets
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'monitoring/snippets/**'
8+
pull_request:
9+
paths:
10+
- 'monitoring/snippets/**'
11+
pull_request_target:
12+
types: [labeled]
13+
schedule:
14+
- cron: '0 0 * * 0'
15+
jobs:
16+
test:
17+
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 60
20+
permissions:
21+
contents: 'write'
22+
pull-requests: 'write'
23+
id-token: 'write'
24+
steps:
25+
- uses: actions/[email protected]
26+
with:
27+
ref: ${{github.event.pull_request.head.ref}}
28+
repository: ${{github.event.pull_request.head.repo.full_name}}
29+
- uses: 'google-github-actions/[email protected]'
30+
with:
31+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
32+
service_account: '[email protected]'
33+
create_credentials_file: 'true'
34+
access_token_lifetime: 600s
35+
- uses: actions/[email protected]
36+
with:
37+
node-version: 16
38+
- run: npm install
39+
working-directory: monitoring/snippets
40+
- run: npm test
41+
working-directory: monitoring/snippets
42+
env:
43+
MOCHA_REPORTER_SUITENAME: monitoring_snippets
44+
MOCHA_REPORTER_OUTPUT: monitoring_snippets_sponge_log.xml
45+
MOCHA_REPORTER: xunit
46+
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
47+
uses: actions/github-script@v6
48+
with:
49+
github-token: ${{ secrets.GITHUB_TOKEN }}
50+
script: |
51+
try {
52+
await github.rest.issues.removeLabel({
53+
name: 'actions:force-run',
54+
owner: 'GoogleCloudPlatform',
55+
repo: 'nodejs-docs-samples',
56+
issue_number: context.payload.pull_request.number
57+
});
58+
} catch (e) {
59+
if (!e.message.includes('Label does not exist')) {
60+
throw e;
61+
}
62+
}
63+
- if: ${{ github.event_name == 'schedule'}}
64+
run: |
65+
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
66+
chmod +x ./flakybot
67+
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}

.github/workflows/workflows.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"mediatranslation",
4444
"monitoring/opencensus",
4545
"monitoring/prometheus",
46+
"monitoring/snippets",
4647
"datacatalog/cloud-client",
4748
"datacatalog/quickstart",
4849
"datastore/functions",

monitoring/snippets/.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
rules:
3+
no-console: off

monitoring/snippets/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
policies_backup.json
2+
package-lock.json
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(projectId) {
18+
// [START monitoring_alert_backup_policies]
19+
const fs = require('fs');
20+
21+
// Imports the Google Cloud client library
22+
const monitoring = require('@google-cloud/monitoring');
23+
24+
// Creates a client
25+
const client = new monitoring.AlertPolicyServiceClient();
26+
27+
async function backupPolicies() {
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
33+
const listAlertPoliciesRequest = {
34+
name: client.projectPath(projectId),
35+
};
36+
37+
let [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
38+
39+
// filter out any policies created by tests for this sample
40+
policies = policies.filter(policy => {
41+
return !policy.displayName.startsWith('gcloud-tests-');
42+
});
43+
44+
fs.writeFileSync(
45+
'./policies_backup.json',
46+
JSON.stringify(policies, null, 2),
47+
'utf-8'
48+
);
49+
50+
console.log('Saved policies to ./policies_backup.json');
51+
// [END monitoring_alert_backup_policies]
52+
}
53+
backupPolicies();
54+
}
55+
56+
process.on('unhandledRejection', err => {
57+
console.error(err.message);
58+
process.exitCode = 1;
59+
});
60+
main(...process.argv.slice(2));
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(projectId, filter) {
18+
// [START monitoring_alert_delete_channel]
19+
// [START monitoring_alert_list_channels]
20+
21+
// Imports the Google Cloud client library
22+
const monitoring = require('@google-cloud/monitoring');
23+
24+
// Creates a client
25+
const client = new monitoring.NotificationChannelServiceClient();
26+
27+
async function deleteChannels() {
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';
33+
34+
const request = {
35+
name: client.projectPath(projectId),
36+
filter,
37+
};
38+
const channels = await client.listNotificationChannels(request);
39+
console.log(channels);
40+
for (const channel of channels[0]) {
41+
console.log(`Deleting channel ${channel.displayName}`);
42+
try {
43+
await client.deleteNotificationChannel({
44+
name: channel.name,
45+
});
46+
} catch (err) {
47+
// ignore error
48+
}
49+
}
50+
}
51+
deleteChannels();
52+
// [END monitoring_alert_delete_channel]
53+
// [END monitoring_alert_list_channels]
54+
}
55+
56+
process.on('unhandledRejection', err => {
57+
console.error(err.message);
58+
process.exitCode = 1;
59+
});
60+
main(...process.argv.slice(2));
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(projectId, enabled, filter) {
18+
enabled = enabled === 'true';
19+
20+
// [START monitoring_alert_enable_policies]
21+
// Imports the Google Cloud client library
22+
const monitoring = require('@google-cloud/monitoring');
23+
24+
// Creates a client
25+
const client = new monitoring.AlertPolicyServiceClient();
26+
27+
async function enablePolicies() {
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const enabled = true;
33+
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';
34+
35+
const listAlertPoliciesRequest = {
36+
name: client.projectPath(projectId),
37+
// See https://cloud.google.com/monitoring/alerting/docs/sorting-and-filtering
38+
filter: filter,
39+
};
40+
41+
const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
42+
const responses = [];
43+
for (const policy of policies) {
44+
responses.push(
45+
await client.updateAlertPolicy({
46+
updateMask: {
47+
paths: ['enabled'],
48+
},
49+
alertPolicy: {
50+
name: policy.name,
51+
enabled: {
52+
value: enabled,
53+
},
54+
},
55+
})
56+
);
57+
}
58+
responses.forEach(response => {
59+
const alertPolicy = response[0];
60+
console.log(`${enabled ? 'Enabled' : 'Disabled'} ${alertPolicy.name}.`);
61+
});
62+
}
63+
enablePolicies();
64+
// [END monitoring_alert_enable_policies]
65+
}
66+
67+
process.on('unhandledRejection', err => {
68+
console.error(err.message);
69+
process.exitCode = 1;
70+
});
71+
main(...process.argv.slice(2));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(projectId) {
18+
// [START monitoring_alert_list_policies]
19+
// Imports the Google Cloud client library
20+
const monitoring = require('@google-cloud/monitoring');
21+
22+
// Creates a client
23+
const client = new monitoring.AlertPolicyServiceClient();
24+
25+
async function listPolicies() {
26+
/**
27+
* TODO(developer): Uncomment the following lines before running the sample.
28+
*/
29+
// const projectId = 'YOUR_PROJECT_ID';
30+
31+
const listAlertPoliciesRequest = {
32+
name: client.projectPath(projectId),
33+
};
34+
const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
35+
console.log('Policies:');
36+
policies.forEach(policy => {
37+
console.log(` Display name: ${policy.displayName}`);
38+
if (policy.documentation && policy.documentation.content) {
39+
console.log(` Documentation: ${policy.documentation.content}`);
40+
}
41+
});
42+
}
43+
listPolicies();
44+
// [END monitoring_alert_list_policies]
45+
}
46+
47+
process.on('unhandledRejection', err => {
48+
console.error(err.message);
49+
process.exitCode = 1;
50+
});
51+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)