Пример #1
0
def run_quickstart(project=""):
    # [START monitoring_quickstart]
    from google.cloud import monitoring_v3

    import time

    client = monitoring_v3.MetricServiceClient()
    # project = 'my-project'  # TODO: Update to your project ID.
    project_name = f"projects/{project}"

    series = monitoring_v3.TimeSeries()
    series.metric.type = "custom.googleapis.com/my_metric"
    series.resource.type = "gce_instance"
    series.resource.labels["instance_id"] = "1234567890123456789"
    series.resource.labels["zone"] = "us-central1-f"
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10**9)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {
            "seconds": seconds,
            "nanos": nanos
        }})
    point = monitoring_v3.Point({
        "interval": interval,
        "value": {
            "double_value": 3.14
        }
    })
    series.points = [point]
    client.create_time_series(request={
        "name": project_name,
        "time_series": [series]
    })
    print("Successfully wrote time series.")
Пример #2
0
def write_time_series(project_id):
    # [START monitoring_write_timeseries]
    client = monitoring_v3.MetricServiceClient()
    project_name = f"projects/{project_id}"

    series = monitoring_v3.TimeSeries()
    series.metric.type = "custom.googleapis.com/my_metric" + str(uuid.uuid4())
    series.resource.type = "gce_instance"
    series.resource.labels["instance_id"] = "1234567890123456789"
    series.resource.labels["zone"] = "us-central1-f"
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10**9)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {
            "seconds": seconds,
            "nanos": nanos
        }})
    point = monitoring_v3.Point({
        "interval": interval,
        "value": {
            "double_value": 3.14
        }
    })
    series.points = [point]
    client.create_time_series(name=project_name, time_series=[series])
Пример #3
0
def send_metric(sales_item, sales_num, report_time):
    client = monitoring_v3.MetricServiceClient()
    project_name = f"projects/{PROJECT_ID}"

    series = monitoring_v3.TimeSeries()
    series.metric.type = f"custom.googleapis.com/{CUSTOM_METRIC_NAME}"
    # Available resource types: https://cloud.google.com/monitoring/api/resources
    series.resource.type = "global"
    series.resource.labels["project_id"] = PROJECT_ID

    # If needed, add more labels for filtering and grouping
    series.metric.labels["item"] = sales_item

    epoch = report_time.timestamp()
    seconds = int(epoch)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {
            "seconds": seconds,
            "nanos": 0
        }})
    point = monitoring_v3.Point({
        "interval": interval,
        "value": {
            "int64_value": sales_num
        }
    })
    series.points = [point]
    client.create_time_series(request={
        "name": project_name,
        "time_series": [series]
    })

    print("Successfully wrote time series.")
def gauge_int_timeseries(resource_type, resource_labels, metric_type,
                         metric_labels, value):
    """Build GAUGE INT timeseries object."""
    series = monitoring_v3.TimeSeries()
    series.metric.type = metric_type
    series.metric.labels.update(metric_labels)
    series.resource.type = resource_type
    series.resource.labels.update(resource_labels)
    series.metric_kind = 'GAUGE'
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10**9)
    interval = monitoring_v3.TimeInterval(
        {'end_time': {
            'seconds': seconds,
            'nanos': nanos
        }})
    point = monitoring_v3.Point({
        'interval':
        interval,
        'value':
        monitoring_v3.TypedValue(int64_value=value)
    })
    series.points = [point]
    return series
Пример #5
0
 def P(timestamp, value):
     interval = monitoring_v3.TimeInterval()
     interval.start_time = datetime_helpers.from_rfc3339(timestamp).replace(
         tzinfo=None)
     interval.end_time = datetime_helpers.from_rfc3339(timestamp).replace(
         tzinfo=None)
     return monitoring_v3.Point(interval=interval,
                                value={"double_value": value})
Пример #6
0
def record_metric(project_name, metric):
    """Create TS entry for captured metric"""

    global logger
    global now
    global monitoring_client

    series = monitoring_v3.TimeSeries()

    series.resource.type = "global"
    series.metric.type = f"custom.googleapis.com/{metric['Benchmark']}"

    # Required to maintain uniqueness of each permutation
    series.metric.labels["Run"] = metric["Run"]
    series.metric.labels["Iteration"] = metric["Iteration"]

    # Populate dynamic labels, GCP limit is 10 labels per descriptor
    for key in list(metric.keys()):
        if key not in ["Benchmark", "Run", "Iteration", "Score"]:
            series.metric.labels[key] = metric[key]

        if len(series.metric.labels) > GCP_LABEL_LIMIT:
            logger.warn(
                f"Exiting metric label loop, limit of {GCP_LABEL_LIMIT} labels."
            )
            break  # Break out, we have hit limit on labels

    seconds = int(now)
    nanos = int((now - seconds) * 10 ** 9)

    interval = monitoring_v3.TimeInterval(
        {"end_time": {"seconds": seconds, "nanos": nanos}}
    )

    point = monitoring_v3.Point(
        {"interval": interval, "value": {"double_value": float(metric["Score"])}}
    )

    series.points = [point]

    logger.info(
        f"Publishing {series.resource.type}/{series.metric.type}: {metric['Score']}"
    )

    try:
        monitoring_client.create_time_series(name=project_name, time_series=[series])
    except InternalServerError:
        logger.error(
            f"Failed to publish metric {series.metric.type}, this may be because the metric descriptor has been recently created. Will retry on the next run."
        )
def report_metric(value, type, instance_id, zone, project_id):
    series = monitoring_v3.types.TimeSeries()
    series.metric.type = 'custom.googleapis.com/{type}'.format(type=type)
    series.resource.type = 'gce_instance'
    series.resource.labels['instance_id'] = instance_id
    series.resource.labels['zone'] = zone
    series.resource.labels['project_id'] = project_id
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10 ** 9)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {"seconds": seconds, "nanos": nanos}}
    )
    point = monitoring_v3.Point({"interval": interval, "value": {"double_value": 3.14}})
    series.points = [point]
    client.create_time_series(name=project_name, time_series=[series])
Пример #8
0
def write_data_to_metric(config, monitored_project_id, value, metric_name,
                         network_name):
    '''
    Writes data to Cloud Monitoring custom metrics.

      Parameters:
        config (dict): The dict containing config like clients and limits
        monitored_project_id: ID of the project where the resource lives (will be added as a label)
        value (int): Value for the data point of the metric.
        metric_name (string): Name of the metric
        network_name (string): Name of the network (will be added as a label)
      Returns:
        usage (int): Current usage for that network.
        limit (int): Current usage for that network.
  '''
    client = monitoring_v3.MetricServiceClient()

    series = monitoring_v3.TimeSeries()
    series.metric.type = f"custom.googleapis.com/{metric_name}"
    series.resource.type = "global"
    series.metric.labels["network_name"] = network_name
    series.metric.labels["project"] = monitored_project_id

    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10**9)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {
            "seconds": seconds,
            "nanos": nanos
        }})
    point = monitoring_v3.Point({
        "interval": interval,
        "value": {
            "double_value": value
        }
    })
    series.points = [point]

    # TODO: sometimes this cashes with 'DeadlineExceeded: 504 Deadline expired before operation could complete' error
    # Implement exponential backoff retries?
    try:
        client.create_time_series(name=config["monitoring_project_link"],
                                  time_series=[series])
    except Exception as e:
        print(e)
Пример #9
0
def write_time_series(project_id,requested_pets):
    client = monitoring_v3.MetricServiceClient()
    project_name = f"projects/{project_id}"

    series = monitoring_v3.TimeSeries()
    series.metric.type = "custom.googleapis.com/" + CUSTOM_METRIC_NAME_PREFIX + PROJECT_ID
    series.resource.type = "gae_instance"
    series.resource.labels["instance_id"] = os.environ["GAE_INSTANCE"]
    series.resource.labels["location"] = "us-east1"
    series.resource.labels["module_id"] = os.environ["GAE_APPLICATION"]
    series.resource.labels["version_id"] = os.environ["GAE_VERSION"]
    
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10 ** 9)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {"seconds": seconds, "nanos": nanos}}
    )
    point = monitoring_v3.Point({"interval": interval, "value": {"int64_value": requested_pets}})
    series.points = [point]
    client.create_time_series(name=project_name, time_series=[series])
Пример #10
0
    def _convert_series(self, metric, ts):
        """Convert an OC timeseries to a SD series."""
        series = monitoring_v3.TimeSeries()
        series.metric.type = self.get_metric_type(metric.descriptor)

        for lk, lv in self.options.default_monitoring_labels.items():
            series.metric.labels[lk.key] = lv.value

        for key, val in zip(metric.descriptor.label_keys, ts.label_values):
            if val.value is not None:
                safe_key = sanitize_label(key.key)
                series.metric.labels[safe_key] = val.value

        set_monitored_resource(series, self.options.resource)

        for point in ts.points:
            sd_point = monitoring_v3.Point()
            # this just modifies points, no return
            self._convert_point(metric, ts, point, sd_point)
            series.points.append(sd_point)
        return series
Пример #11
0
    def build_series(self, prefix, name, labels, details):
        s = monitoring_v3.types.TimeSeries()
        s.metric.type = f'custom.googleapis.com/{self.app_name}/{prefix}/{name}'
        s.resource.type = 'generic_task'
        s.resource.labels.update(labels)

        # setup time for submissions
        now = time.time()
        seconds = int(now)
        nanos = int((now - seconds) * 10 ** 9)
        interval = monitoring_v3.TimeInterval(
            {"end_time": {"seconds": seconds, "nanos": nanos}}
        )

        # create the data point
        p = monitoring_v3.Point({
            'interval': interval,
            'value': {details['kind']: details['value']},
        })
        s.points = [p]

        return s
Пример #12
0
def report_metric(value, metric_type, resource_values):
    """Create time series for report.

    Args:
      value: (int) Report metric value.
      metric_type: (str) Metric type
      resource_values: (dict) Contains resources information
    """
    client = resource_values.get('client')
    project_id = resource_values.get('project_id')
    instance_id = resource_values.get('instance_id')
    zone = resource_values.get('zone')

    project_name = client.common_project_path(project_id)
    # TimeSeries definition.
    series = monitoring_v3.types.TimeSeries()
    series.metric.type = 'custom.googleapis.com/{type}'.format(
        type=metric_type)
    series.resource.type = 'gce_instance'
    series.resource.labels['instance_id'] = instance_id
    series.resource.labels['zone'] = zone
    series.resource.labels['project_id'] = project_id
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10**9)
    interval = monitoring_v3.TimeInterval(
        {"end_time": {
            "seconds": seconds,
            "nanos": nanos
        }})
    point = monitoring_v3.Point({
        "interval": interval,
        "value": {
            "int64_value": value
        }
    })
    series.points = [point]
    client.create_time_series(name=project_name, time_series=[series])
def report_metric(value, t, instance_id, zone, project_id):
    series = monitoring_v3.types.TimeSeries()
    series.metric.type = 'custom.googleapis.com/{type}'.format(type=t)
    series.resource.type = 'gce_instance'
    series.resource.labels['instance_id'] = instance_id
    series.resource.labels['zone'] = zone
    series.resource.labels['project_id'] = project_id
    now = time.time()
    seconds = int(now)
    nanos = int((now - seconds) * 10**9)
    interval = monitoring_v3.TimeInterval(
        {'end_time': {
            'seconds': seconds,
            'nanos': nanos
        }})
    point = monitoring_v3.Point()
    point.value.int64_value = value
    point.interval = interval
    series.points.append(point)
    client.create_time_series(request={
        'name': project_name,
        'time_series': [series]
    })
Пример #14
0
    def test_iteration(self):
        T0 = datetime.datetime(2016, 4, 6, 22, 5, 0)
        T1 = datetime.datetime(2016, 4, 6, 22, 10, 0)

        INTERVAL1 = self._make_interval(TS1, TS0)
        INTERVAL2 = self._make_interval(TS2, TS1)

        VALUE1 = 60  # seconds
        VALUE2 = 60.001  # seconds

        # Currently cannot create from a list of dict for repeated fields due to
        # https://github.com/googleapis/proto-plus-python/issues/135
        POINT1 = monitoring_v3.Point({
            "interval": INTERVAL2,
            "value": {
                "double_value": VALUE1
            }
        })
        POINT2 = monitoring_v3.Point({
            "interval": INTERVAL1,
            "value": {
                "double_value": VALUE1
            }
        })
        POINT3 = monitoring_v3.Point({
            "interval": INTERVAL2,
            "value": {
                "double_value": VALUE2
            }
        })
        POINT4 = monitoring_v3.Point({
            "interval": INTERVAL1,
            "value": {
                "double_value": VALUE2
            }
        })
        SERIES1 = monitoring_v3.TimeSeries({
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
            "points": [POINT1, POINT2],
        })
        SERIES2 = monitoring_v3.TimeSeries({
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS2
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS2
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
            "points": [POINT3, POINT4],
        })

        RESPONSE = {"time_series": [SERIES1, SERIES2], "next_page_token": ""}

        channel = ChannelStub(responses=[RESPONSE])
        client = self._create_client(channel)
        query = self._make_one(client, PROJECT, METRIC_TYPE)
        query = query.select_interval(start_time=T0, end_time=T1)
        response = list(query)

        self.assertEqual(len(response), 2)
        series1, series2 = response

        self.assertEqual(series1.metric.labels, METRIC_LABELS)
        self.assertEqual(series2.metric.labels, METRIC_LABELS2)
        self.assertEqual(series1.resource.labels, RESOURCE_LABELS)
        self.assertEqual(series2.resource.labels, RESOURCE_LABELS2)

        self.assertEqual([p.value.double_value for p in series1.points],
                         [VALUE1, VALUE1])
        self.assertEqual([p.value.double_value for p in series2.points],
                         [VALUE2, VALUE2])
        self.assertEqual([p.interval for p in series1.points],
                         [INTERVAL2, INTERVAL1])
        self.assertEqual([p.interval for p in series2.points],
                         [INTERVAL2, INTERVAL1])

        expected_request = monitoring_v3.ListTimeSeriesRequest(
            name="projects/" + PROJECT,
            filter='metric.type = "{type}"'.format(type=METRIC_TYPE),
            interval=self._make_interval(T1, T0),
            view=monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
        )
        request = channel.requests[0][1]
        self.assertEqual(request, expected_request)
Пример #15
0
    def _fixed_list_time_series(self) -> Iterable[TimeSeries]:
        def _make_interval(end_time, start_time):
            interval = monitoring_v3.TimeInterval(end_time=end_time,
                                                  start_time=start_time)
            return interval

        INTERVAL1 = _make_interval(TS1, TS0)
        INTERVAL2 = _make_interval(TS2, TS1)

        VALUE1 = 60  # seconds
        VALUE2 = 42  # seconds

        # Currently cannot create from a list of dict for repeated fields due to
        # https://github.com/googleapis/proto-plus-python/issues/135
        POINT1 = monitoring_v3.Point({
            "interval": INTERVAL2,
            "value": {
                "double_value": VALUE1
            }
        })
        POINT2 = monitoring_v3.Point({
            "interval": INTERVAL1,
            "value": {
                "double_value": VALUE1
            }
        })
        POINT3 = monitoring_v3.Point({
            "interval": INTERVAL2,
            "value": {
                "double_value": VALUE2
            }
        })
        POINT4 = monitoring_v3.Point({
            "interval": INTERVAL1,
            "value": {
                "double_value": VALUE2
            }
        })
        SERIES1 = monitoring_v3.TimeSeries({
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
            "points": [POINT1, POINT2],
        })
        SERIES2 = monitoring_v3.TimeSeries({
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS2
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS2
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
            "points": [POINT3, POINT4],
        })
        yield SERIES1
        yield SERIES2