Skip to content

Commit

Permalink
Use Main Project (now whitelisted)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Prin committed Mar 28, 2016
1 parent ee11e64 commit 2f357fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
28 changes: 14 additions & 14 deletions monitoring/api/v3/custom_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def get_now_rfc3339():


def create_custom_metric(client, project_id,
custom_metric_name, metric_kind):
custom_metric_type, metric_kind):
"""Create custom metric descriptor"""
metrics_descriptor = {
"name": "projects/{}/metricDescriptors/{}".format(
project_id, custom_metric_name),
"type": custom_metric_name,
project_id, custom_metric_type),
"type": custom_metric_type,
"labels": [
{
"key": "environment",
Expand All @@ -80,11 +80,11 @@ def create_custom_metric(client, project_id,
name=project_id, body=metrics_descriptor).execute()


def get_custom_metric(client, project_id, custom_metric_name):
def get_custom_metric(client, project_id, custom_metric_type):
"""Retrieve the custom metric we created"""
request = client.projects().metricDescriptors().list(
name=project_id,
filter='metric.type=starts_with("{}")'.format(custom_metric_name))
filter='metric.type=starts_with("{}")'.format(custom_metric_type))
response = request.execute()
print('ListCustomMetrics response:')
pprint.pprint(response)
Expand All @@ -103,14 +103,14 @@ def get_custom_data_point():


def write_timeseries_value(client, project_resource,
custom_metric_name, instance_id, metric_kind):
custom_metric_type, instance_id, metric_kind):
"""Write the custom metric obtained by get_custom_data_point at a point in
time."""
# Specify a new data point for the time series.
now = get_now_rfc3339()
timeseries_data = {
"metric": {
"type": custom_metric_name,
"type": custom_metric_type,
"labels": {
"environment": "STAGING"
}
Expand Down Expand Up @@ -142,7 +142,7 @@ def write_timeseries_value(client, project_resource,
request.execute()


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

project_resource = "projects/{0}".format(project_id)
client = list_resources.get_client()
create_custom_metric(client, project_resource,
CUSTOM_METRIC_NAME, METRIC_KIND)
CUSTOM_METRIC_TYPE, METRIC_KIND)
custom_metric = None
while not custom_metric:
# wait until it's created
time.sleep(1)
custom_metric = get_custom_metric(
client, project_resource, CUSTOM_METRIC_NAME)
client, project_resource, CUSTOM_METRIC_TYPE)

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


Expand Down
18 changes: 4 additions & 14 deletions monitoring/api/v3/custom_metric_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,23 @@
"""

import random
import time

import list_resources
from custom_metric import create_custom_metric, get_custom_metric
from custom_metric import read_timeseries, write_timeseries_value
import list_resources

""" Change this to run against other prjoects
GOOGLE_APPLICATION_CREDENTIALS must be the service account for this project
"""

# temporarily hard code to whitelisted project
TEST_PROJECT_ID = 'cloud-monitoring-dev'
# TEST_PROJECT_ID = os.getenv("GCLOUD_PROJECT", 'cloud-monitoring-dev')

""" Custom metric domain for all cusotm metrics"""
""" Custom metric domain for all custom metrics"""
CUSTOM_METRIC_DOMAIN = "custom.googleapis.com"

PROJECT_RESOURCE = "projects/{}".format(TEST_PROJECT_ID)

METRIC = 'compute.googleapis.com/instance/cpu/usage_time'
METRIC_NAME = ''.join(
random.choice('0123456789ABCDEF') for i in range(16))
METRIC_RESOURCE = "{}/{}".format(
CUSTOM_METRIC_DOMAIN, METRIC_NAME)


def test_custom_metric():
def test_custom_metric(cloud_config):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
# Use a constant seed so psuedo random number is known ahead of time
random.seed(1)
Expand Down
13 changes: 6 additions & 7 deletions monitoring/api/v3/list_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@

import list_resources

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


def test_list_monitored_resources(capsys):
def test_list_monitored_resources(cloud_config, capsys):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
list_resources.list_monitored_resource_descriptors(
client, PROJECT_RESOURCE)
Expand All @@ -41,7 +38,8 @@ def test_list_monitored_resources(capsys):
assert regex.search(stdout) is not None


def test_list_metrics(capsys):
def test_list_metrics(cloud_config, capsys):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
list_resources.list_metric_descriptors(
client, PROJECT_RESOURCE, METRIC)
Expand All @@ -51,7 +49,8 @@ def test_list_metrics(capsys):
assert regex.search(stdout) is not None


def test_list_timeseries(capsys):
def test_list_timeseries(cloud_config, capsys):
PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
client = list_resources.get_client()
list_resources.list_timeseries(
client, PROJECT_RESOURCE, METRIC)
Expand Down

0 comments on commit 2f357fd

Please sign in to comment.