@@ -56,12 +56,12 @@ def get_now_rfc3339():
56
56
57
57
58
58
def create_custom_metric (client , project_id ,
59
- custom_metric_name , metric_kind ):
59
+ custom_metric_type , metric_kind ):
60
60
"""Create custom metric descriptor"""
61
61
metrics_descriptor = {
62
62
"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 ,
65
65
"labels" : [
66
66
{
67
67
"key" : "environment" ,
@@ -80,11 +80,11 @@ def create_custom_metric(client, project_id,
80
80
name = project_id , body = metrics_descriptor ).execute ()
81
81
82
82
83
- def get_custom_metric (client , project_id , custom_metric_name ):
83
+ def get_custom_metric (client , project_id , custom_metric_type ):
84
84
"""Retrieve the custom metric we created"""
85
85
request = client .projects ().metricDescriptors ().list (
86
86
name = project_id ,
87
- filter = 'metric.type=starts_with("{}")' .format (custom_metric_name ))
87
+ filter = 'metric.type=starts_with("{}")' .format (custom_metric_type ))
88
88
response = request .execute ()
89
89
print ('ListCustomMetrics response:' )
90
90
pprint .pprint (response )
@@ -103,14 +103,14 @@ def get_custom_data_point():
103
103
104
104
105
105
def write_timeseries_value (client , project_resource ,
106
- custom_metric_name , instance_id , metric_kind ):
106
+ custom_metric_type , instance_id , metric_kind ):
107
107
"""Write the custom metric obtained by get_custom_data_point at a point in
108
108
time."""
109
109
# Specify a new data point for the time series.
110
110
now = get_now_rfc3339 ()
111
111
timeseries_data = {
112
112
"metric" : {
113
- "type" : custom_metric_name ,
113
+ "type" : custom_metric_type ,
114
114
"labels" : {
115
115
"environment" : "STAGING"
116
116
}
@@ -142,7 +142,7 @@ def write_timeseries_value(client, project_resource,
142
142
request .execute ()
143
143
144
144
145
- def read_timeseries (client , project_resource , custom_metric_name ):
145
+ def read_timeseries (client , project_resource , custom_metric_type ):
146
146
"""Reads all of the CUSTOM_METRICS that we have written between START_TIME
147
147
and END_TIME
148
148
:param project_resource: Resource of the project to read the timeseries
@@ -151,7 +151,7 @@ def read_timeseries(client, project_resource, custom_metric_name):
151
151
"""
152
152
request = client .projects ().timeSeries ().list (
153
153
name = project_resource ,
154
- filter = 'metric.type="{0}"' .format (custom_metric_name ),
154
+ filter = 'metric.type="{0}"' .format (custom_metric_type ),
155
155
pageSize = 3 ,
156
156
interval_startTime = get_start_time (),
157
157
interval_endTime = get_now_rfc3339 ())
@@ -163,28 +163,28 @@ def main(project_id):
163
163
# This is the namespace for all custom metrics
164
164
CUSTOM_METRIC_DOMAIN = "custom.googleapis.com"
165
165
# 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 )
167
167
INSTANCE_ID = "test_instance"
168
168
METRIC_KIND = "GAUGE"
169
169
170
170
project_resource = "projects/{0}" .format (project_id )
171
171
client = list_resources .get_client ()
172
172
create_custom_metric (client , project_resource ,
173
- CUSTOM_METRIC_NAME , METRIC_KIND )
173
+ CUSTOM_METRIC_TYPE , METRIC_KIND )
174
174
custom_metric = None
175
175
while not custom_metric :
176
176
# wait until it's created
177
177
time .sleep (1 )
178
178
custom_metric = get_custom_metric (
179
- client , project_resource , CUSTOM_METRIC_NAME )
179
+ client , project_resource , CUSTOM_METRIC_TYPE )
180
180
181
181
write_timeseries_value (client , project_resource ,
182
- CUSTOM_METRIC_NAME , INSTANCE_ID , METRIC_KIND )
182
+ CUSTOM_METRIC_TYPE , INSTANCE_ID , METRIC_KIND )
183
183
# Sometimes on new metric descriptors, writes have a delay in being read
184
184
# back. 3 seconds should be enough to make sure our read call picks up the
185
185
# write
186
186
time .sleep (3 )
187
- timeseries = read_timeseries (client , project_resource , CUSTOM_METRIC_NAME )
187
+ timeseries = read_timeseries (client , project_resource , CUSTOM_METRIC_TYPE )
188
188
print ('read_timeseries response:\n {}' .format (pprint .pformat (timeseries )))
189
189
190
190
0 commit comments