def custom_metric(client):
    custom_metric_descriptor = create_custom_metric(
        client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND)

    # wait until metric has been created, use the get call to wait until
    # a response comes back with the new metric with 10 retries.
    custom_metric = None
    retry_count = 0
    while not custom_metric and retry_count < 10:
        time.sleep(1)
        retry_count += 1
        custom_metric = get_custom_metric(
            client, PROJECT_RESOURCE, METRIC_RESOURCE)

    # make sure we get the custom_metric
    assert custom_metric

    yield custom_metric

    # cleanup
    delete_metric_descriptor(client, custom_metric_descriptor['name'])
def test_custom_metric(cloud_config, client):
    PROJECT_RESOURCE = "projects/{}".format(cloud_config.project)
    # Use a constant seed so psuedo random number is known ahead of time
    random.seed(1)
    pseudo_random_value = random.randint(0, 10)
    # Reseed it
    random.seed(1)

    INSTANCE_ID = "test_instance"
    METRIC_KIND = "GAUGE"

    custom_metric_descriptor = create_custom_metric(
        client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND)

    # wait until metric has been created, use the get call to wait until
    # a response comes back with the new metric
    custom_metric = None
    while not custom_metric:
        time.sleep(1)
        custom_metric = get_custom_metric(
            client, PROJECT_RESOURCE, METRIC_RESOURCE)

    write_timeseries_value(client, PROJECT_RESOURCE,
                           METRIC_RESOURCE, INSTANCE_ID,
                           METRIC_KIND)

    # Sometimes on new metric descriptors, writes have a delay in being
    # read back. Use eventually_consistent to account for this.
    @eventually_consistent.call
    def _():
        response = read_timeseries(client, PROJECT_RESOURCE, METRIC_RESOURCE)
        value = int(
            response['timeSeries'][0]['points'][0]['value']['int64Value'])
        # using seed of 1 will create a value of 1
        assert value == pseudo_random_value

    delete_metric_descriptor(client, custom_metric_descriptor['name'])
def test_custom_metric(client):
    PROJECT_RESOURCE = "projects/{}".format(PROJECT)
    # Use a constant seed so psuedo random number is known ahead of time
    random.seed(1)
    pseudo_random_value = random.randint(0, 10)
    # Reseed it
    random.seed(1)

    INSTANCE_ID = "test_instance"
    METRIC_KIND = "GAUGE"

    custom_metric_descriptor = create_custom_metric(client, PROJECT_RESOURCE,
                                                    METRIC_RESOURCE,
                                                    METRIC_KIND)

    # wait until metric has been created, use the get call to wait until
    # a response comes back with the new metric
    custom_metric = None
    while not custom_metric:
        time.sleep(1)
        custom_metric = get_custom_metric(client, PROJECT_RESOURCE,
                                          METRIC_RESOURCE)

    write_timeseries_value(client, PROJECT_RESOURCE, METRIC_RESOURCE,
                           INSTANCE_ID, METRIC_KIND)

    # Sometimes on new metric descriptors, writes have a delay in being
    # read back. Use eventually_consistent to account for this.
    @eventually_consistent.call
    def _():
        response = read_timeseries(client, PROJECT_RESOURCE, METRIC_RESOURCE)
        value = int(
            response['timeSeries'][0]['points'][0]['value']['int64Value'])
        # using seed of 1 will create a value of 1
        assert value == pseudo_random_value

    delete_metric_descriptor(client, custom_metric_descriptor['name'])