Skip to content

Commit 2f357fd

Browse files
author
Bill Prin
committed
Use Main Project (now whitelisted)
1 parent ee11e64 commit 2f357fd

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

monitoring/api/v3/custom_metric.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def get_now_rfc3339():
5656

5757

5858
def create_custom_metric(client, project_id,
59-
custom_metric_name, metric_kind):
59+
custom_metric_type, metric_kind):
6060
"""Create custom metric descriptor"""
6161
metrics_descriptor = {
6262
"name": "projects/{}/metricDescriptors/{}".format(
63-
project_id, custom_metric_name),
64-
"type": custom_metric_name,
63+
project_id, custom_metric_type),
64+
"type": custom_metric_type,
6565
"labels": [
6666
{
6767
"key": "environment",
@@ -80,11 +80,11 @@ def create_custom_metric(client, project_id,
8080
name=project_id, body=metrics_descriptor).execute()
8181

8282

83-
def get_custom_metric(client, project_id, custom_metric_name):
83+
def get_custom_metric(client, project_id, custom_metric_type):
8484
"""Retrieve the custom metric we created"""
8585
request = client.projects().metricDescriptors().list(
8686
name=project_id,
87-
filter='metric.type=starts_with("{}")'.format(custom_metric_name))
87+
filter='metric.type=starts_with("{}")'.format(custom_metric_type))
8888
response = request.execute()
8989
print('ListCustomMetrics response:')
9090
pprint.pprint(response)
@@ -103,14 +103,14 @@ def get_custom_data_point():
103103

104104

105105
def write_timeseries_value(client, project_resource,
106-
custom_metric_name, instance_id, metric_kind):
106+
custom_metric_type, instance_id, metric_kind):
107107
"""Write the custom metric obtained by get_custom_data_point at a point in
108108
time."""
109109
# Specify a new data point for the time series.
110110
now = get_now_rfc3339()
111111
timeseries_data = {
112112
"metric": {
113-
"type": custom_metric_name,
113+
"type": custom_metric_type,
114114
"labels": {
115115
"environment": "STAGING"
116116
}
@@ -142,7 +142,7 @@ def write_timeseries_value(client, project_resource,
142142
request.execute()
143143

144144

145-
def read_timeseries(client, project_resource, custom_metric_name):
145+
def read_timeseries(client, project_resource, custom_metric_type):
146146
"""Reads all of the CUSTOM_METRICS that we have written between START_TIME
147147
and END_TIME
148148
:param project_resource: Resource of the project to read the timeseries
@@ -151,7 +151,7 @@ def read_timeseries(client, project_resource, custom_metric_name):
151151
"""
152152
request = client.projects().timeSeries().list(
153153
name=project_resource,
154-
filter='metric.type="{0}"'.format(custom_metric_name),
154+
filter='metric.type="{0}"'.format(custom_metric_type),
155155
pageSize=3,
156156
interval_startTime=get_start_time(),
157157
interval_endTime=get_now_rfc3339())
@@ -163,28 +163,28 @@ def main(project_id):
163163
# This is the namespace for all custom metrics
164164
CUSTOM_METRIC_DOMAIN = "custom.googleapis.com"
165165
# This is our specific metric name
166-
CUSTOM_METRIC_NAME = "{}/custom_measurement".format(CUSTOM_METRIC_DOMAIN)
166+
CUSTOM_METRIC_TYPE = "{}/custom_measurement".format(CUSTOM_METRIC_DOMAIN)
167167
INSTANCE_ID = "test_instance"
168168
METRIC_KIND = "GAUGE"
169169

170170
project_resource = "projects/{0}".format(project_id)
171171
client = list_resources.get_client()
172172
create_custom_metric(client, project_resource,
173-
CUSTOM_METRIC_NAME, METRIC_KIND)
173+
CUSTOM_METRIC_TYPE, METRIC_KIND)
174174
custom_metric = None
175175
while not custom_metric:
176176
# wait until it's created
177177
time.sleep(1)
178178
custom_metric = get_custom_metric(
179-
client, project_resource, CUSTOM_METRIC_NAME)
179+
client, project_resource, CUSTOM_METRIC_TYPE)
180180

181181
write_timeseries_value(client, project_resource,
182-
CUSTOM_METRIC_NAME, INSTANCE_ID, METRIC_KIND)
182+
CUSTOM_METRIC_TYPE, INSTANCE_ID, METRIC_KIND)
183183
# Sometimes on new metric descriptors, writes have a delay in being read
184184
# back. 3 seconds should be enough to make sure our read call picks up the
185185
# write
186186
time.sleep(3)
187-
timeseries = read_timeseries(client, project_resource, CUSTOM_METRIC_NAME)
187+
timeseries = read_timeseries(client, project_resource, CUSTOM_METRIC_TYPE)
188188
print('read_timeseries response:\n{}'.format(pprint.pformat(timeseries)))
189189

190190

monitoring/api/v3/custom_metric_test.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,23 @@
2121
"""
2222

2323
import random
24-
import time
2524

25+
import list_resources
2626
from custom_metric import create_custom_metric, get_custom_metric
2727
from custom_metric import read_timeseries, write_timeseries_value
28-
import list_resources
29-
30-
""" Change this to run against other prjoects
31-
GOOGLE_APPLICATION_CREDENTIALS must be the service account for this project
32-
"""
33-
34-
# temporarily hard code to whitelisted project
35-
TEST_PROJECT_ID = 'cloud-monitoring-dev'
36-
# TEST_PROJECT_ID = os.getenv("GCLOUD_PROJECT", 'cloud-monitoring-dev')
3728

38-
""" Custom metric domain for all cusotm metrics"""
29+
""" Custom metric domain for all custom metrics"""
3930
CUSTOM_METRIC_DOMAIN = "custom.googleapis.com"
4031

41-
PROJECT_RESOURCE = "projects/{}".format(TEST_PROJECT_ID)
42-
4332
METRIC = 'compute.googleapis.com/instance/cpu/usage_time'
4433
METRIC_NAME = ''.join(
4534
random.choice('0123456789ABCDEF') for i in range(16))
4635
METRIC_RESOURCE = "{}/{}".format(
4736
CUSTOM_METRIC_DOMAIN, METRIC_NAME)
4837

4938

50-
def test_custom_metric():
39+
def test_custom_metric(cloud_config):
40+
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
5141
client = list_resources.get_client()
5242
# Use a constant seed so psuedo random number is known ahead of time
5343
random.seed(1)

monitoring/api/v3/list_resources_test.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@
2424

2525
import list_resources
2626

27-
# temporarily hard code to whitelisted project
28-
TEST_PROJECT_ID = 'cloud-monitoring-dev'
29-
# TEST_PROJECT_ID = os.getenv("GCLOUD_PROJECT", 'cloud-monitoring-dev')
30-
PROJECT_RESOURCE = "projects/{}".format(TEST_PROJECT_ID)
3127
METRIC = 'compute.googleapis.com/instance/cpu/usage_time'
3228

3329

34-
def test_list_monitored_resources(capsys):
30+
def test_list_monitored_resources(cloud_config, capsys):
31+
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
3532
client = list_resources.get_client()
3633
list_resources.list_monitored_resource_descriptors(
3734
client, PROJECT_RESOURCE)
@@ -41,7 +38,8 @@ def test_list_monitored_resources(capsys):
4138
assert regex.search(stdout) is not None
4239

4340

44-
def test_list_metrics(capsys):
41+
def test_list_metrics(cloud_config, capsys):
42+
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
4543
client = list_resources.get_client()
4644
list_resources.list_metric_descriptors(
4745
client, PROJECT_RESOURCE, METRIC)
@@ -51,7 +49,8 @@ def test_list_metrics(capsys):
5149
assert regex.search(stdout) is not None
5250

5351

54-
def test_list_timeseries(capsys):
52+
def test_list_timeseries(cloud_config, capsys):
53+
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
5554
client = list_resources.get_client()
5655
list_resources.list_timeseries(
5756
client, PROJECT_RESOURCE, METRIC)

0 commit comments

Comments
 (0)