Skip to content

Commit c8902a9

Browse files
author
Bill Prin
committed
jon wayne review
1 parent 33b2e43 commit c8902a9

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

monitoring/api/v3/cloud-client/quickstart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ def run_quickstart():
3838
print('Successfully wrote time series.')
3939
# [END monitoring_quickstart]
4040

41+
4142
if __name__ == '__main__':
4243
run_quickstart()

monitoring/api/v3/cloud-client/snippets.py

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@
1414

1515
import argparse
1616

17-
from google.cloud.monitoring import (
18-
Aligner, Client, MetricKind, Reducer, ValueType)
17+
from google.cloud import monitoring
1918

2019

21-
def create_metric_descriptor(client):
20+
def create_metric_descriptor():
21+
client = monitoring.Client()
2222
descriptor = client.metric_descriptor(
2323
'custom.googleapis.com/my_metric',
24-
metric_kind=MetricKind.GAUGE,
25-
value_type=ValueType.DOUBLE,
24+
metric_kind=monitoring.MetricKind.GAUGE,
25+
value_type=monitoring.ValueType.DOUBLE,
2626
description='This is a simple example of a custom metric.')
2727
descriptor.create()
2828

2929

30-
def delete_metric_descriptor(client, descriptor):
30+
def delete_metric_descriptor(descriptor):
31+
client = monitoring.Client()
3132
descriptor = client.metric_descriptor(
32-
'custom.googleapis.com/my_metric'
33+
descriptor
3334
)
3435
descriptor.delete()
35-
print 'Deleted metric.'
36+
print 'Deleted metric descriptor.'
3637

3738

38-
def write_time_series(client):
39+
def write_time_series():
40+
client = monitoring.Client()
3941
resource = client.resource(
4042
'gce_instance',
4143
labels={
@@ -53,47 +55,57 @@ def write_time_series(client):
5355
client.write_point(metric, resource, 3.14)
5456

5557

56-
def list_time_series(client):
58+
def list_time_series():
59+
client = monitoring.Client()
5760
metric = 'compute.googleapis.com/instance/cpu/utilization'
58-
print(list(client.query(metric, minutes=5)))
61+
query_results = client.query(metric, minutes=5)
62+
for result in query_results:
63+
print result
5964

6065

61-
def list_time_series_header(client):
66+
def list_time_series_header():
67+
client = monitoring.Client()
6268
metric = 'compute.googleapis.com/instance/cpu/utilization'
63-
print(list(client.query(metric, minutes=5).iter(headers_only=True)))
69+
query_results = client.query(metric, minutes=5).iter(headers_only=True)
70+
for result in query_results:
71+
print result
6472

6573

66-
def list_time_series_aggregate(client):
74+
def list_time_series_aggregate():
75+
client = monitoring.Client()
6776
metric = 'compute.googleapis.com/instance/cpu/utilization'
6877
print(list(client.query(metric, hours=1).align(
69-
Aligner.ALIGN_MEAN, minutes=5)))
78+
monitoring.Aligner.ALIGN_MEAN, minutes=5)))
7079

7180

72-
def list_time_series_reduce(client):
81+
def list_time_series_reduce():
82+
client = monitoring.Client()
7383
metric = 'compute.googleapis.com/instance/cpu/utilization'
7484
print(list(client.query(metric, hours=1).align(
75-
Aligner.ALIGN_MEAN, minutes=5).reduce(
76-
Reducer.REDUCE_MEAN, 'resource.zone')))
85+
monitoring.Aligner.ALIGN_MEAN, minutes=5).reduce(
86+
monitoring.Reducer.REDUCE_MEAN, 'resource.zone')))
7787

7888

79-
def list_metric_descriptors(client):
89+
def list_metric_descriptors():
90+
client = monitoring.Client()
8091
for descriptor in client.list_metric_descriptors():
8192
print(descriptor.type)
8293

8394

84-
def list_monitored_resources(client):
95+
def list_monitored_resources():
96+
client = monitoring.Client()
8597
for descriptor in client.list_resource_descriptors():
8698
print (descriptor.type)
8799

88100

89-
def get_monitored_resource_descriptor(client, type):
101+
def get_monitored_resource_descriptor(type):
102+
client = monitoring.Client()
90103
print(client.fetch_resource_descriptor(type))
91104

92105

93106
if __name__ == '__main__':
94107
parser = argparse.ArgumentParser(
95108
description='Demonstrates Monitoring API operations.')
96-
parser.add_argument('--project_id', help='Your cloud project ID.')
97109

98110
subparsers = parser.add_subparsers(dest='command')
99111

@@ -160,25 +172,24 @@ def get_monitored_resource_descriptor(client, type):
160172
)
161173

162174
args = parser.parse_args()
163-
client = Client(args.project_id)
164175

165176
if args.command == 'create-metric-descriptor':
166-
create_metric_descriptor(client)
177+
create_metric_descriptor()
167178
if args.command == 'list-metric-descriptors':
168-
list_metric_descriptors(client)
179+
list_metric_descriptors()
169180
if args.command == 'delete-metric-descriptor':
170-
delete_metric_descriptor(client, args.metric)
181+
delete_metric_descriptor(args.metric)
171182
if args.command == 'list-resources':
172-
list_monitored_resources(client)
183+
list_monitored_resources()
173184
if args.command == 'get-resource':
174-
get_monitored_resource_descriptor(client, args.resource)
185+
get_monitored_resource_descriptor(args.resource)
175186
if args.command == 'write-time-series':
176-
write_time_series(client)
187+
write_time_series()
177188
if args.command == 'list-time-series':
178-
list_time_series(client)
189+
list_time_series()
179190
if args.command == 'list-time-series-header':
180-
list_time_series_header(client)
191+
list_time_series_header()
181192
if args.command == 'list-time-series-reduce':
182-
list_time_series_reduce(client)
193+
list_time_series_reduce()
183194
if args.command == 'list-time-series-aggregate':
184-
list_time_series_aggregate(client)
195+
list_time_series_aggregate()

monitoring/api/v3/cloud-client/snippets_test.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from google.cloud import monitoring
16-
import pytest
17-
1815
import snippets
1916

2017

21-
@pytest.yield_fixture
22-
def client(cloud_config):
23-
client = monitoring.Client(cloud_config.project)
24-
yield client
25-
26-
27-
def test_create_and_delete_metric_descriptor(capsys, client):
28-
snippets.create_metric_descriptor(client)
29-
snippets.delete_metric_descriptor(client, 'custom.googleapis.com/my_metric')
18+
def test_create_and_delete_metric_descriptor(capsys):
19+
snippets.create_metric_descriptor()
20+
snippets.delete_metric_descriptor('custom.googleapis.com/my_metric')
3021
out, _ = capsys.readouterr()
3122
assert 'Deleted metric' in out
3223

3324

34-
def test_list_metric_descriptors(capsys, client):
35-
snippets.list_metric_descriptors(client)
25+
def test_list_metric_descriptors(capsys):
26+
snippets.list_metric_descriptors()
3627
out, _ = capsys.readouterr()
3728
assert 'logging.googleapis.com/byte_count' in out
3829

0 commit comments

Comments
 (0)