python-2.7google-cloud-platformgoogle-api-python-clientgoogle-cloud-pythongoogle-cloud-metrics

Monitoring api in Google gives "By" as response


I am reading monitoring data through Google Timeseries api. The api is working correctly and if give alignment period=3600s it gives me the values for that time series between start and end time for any metric type.

I am calling it through Python like this:

service.projects().timeSeries().list(
        name=api_args["project_name"],
        filter=api_args["metric_filter"],
        aggregation_alignmentPeriod=api_args["aggregation_alignment_period"],
        # aggregation_crossSeriesReducer=api_args["crossSeriesReducer"],
        aggregation_perSeriesAligner=api_args["perSeriesAligner"],
        aggregation_groupByFields=api_args["group_by"],
        interval_endTime=api_args["end_time_str"],
        interval_startTime=api_args["start_time_str"],
        pageSize=config.PAGE_SIZE,
        pageToken=api_args["nextPageToken"]
    ).execute()

and in Postman:

https://monitoring.googleapis.com/v3/projects/my-project/timeSeries?pageSize=500&interval.startTime=2020-07-04T16%3A39%3A37.230000Z&aggregation.alignmentPeriod=3600s&aggregation.perSeriesAligner=ALIGN_SUM&filter=metric.type%3D%22compute.googleapis.com%2Finstance%2Fnetwork%2Freceived_bytes_count%22+&pageToken=&interval.endTime=2020-07-04T17%3A30%3A01.497Z&alt=json&aggregation.groupByFields=metric.labels.key

I face an issue here:

{
      "metric": {
        "labels": {
          "instance_name": "insta-demo1",
          "loadbalanced": "false"
        },
        "type": "compute.googleapis.com/instance/network/received_bytes_count"
      },
      "resource": {
        "type": "gce_instance",
        "labels": {
          "instance_id": "1234343552",
          "zone": "us-central1-f",
          "project_id": "my-project"
        }
      },
      "metricKind": "DELTA",
      "valueType": "INT64",
      "points": [
        {
          "interval": {
            "startTime": "2020-07-04T16:30:01.497Z",
            "endTime": "2020-07-04T17:30:01.497Z"
          },
          "value": {
            "int64Value": "6720271"
          }
        }
      ]
    },
    {
      "metric": {
        "labels": {
          "loadbalanced": "true",
          "instance_name": "insta-demo2"
        },
        "type": "compute.googleapis.com/instance/network/received_bytes_count"
      },
      "resource": {
        "type": "gce_instance",
        "labels": {
          "instance_id": "1234566343",
          "project_id": "my-project",
          "zone": "us-central1-f"
        }
      },
      "metricKind": "DELTA",
      "valueType": "INT64",
      "points": [
        {
          "interval": {
            "startTime": "2020-07-04T16:30:01.497Z",
            "endTime": "2020-07-04T17:30:01.497Z"
          },
          "value": {
            "int64Value": "579187"
          }
        }
      ]
    }
  ],
  "unit": "By". //This "By" is the value which is causing problem, 

I am getting this value like "unit": "By" or "unit":"ms" or something like that at the end, Also if I don't find any data for a range I'm getting this value, as I am evaluating this response in Python I am getting key error as there is not key called "unit"

logMessage: "Key Error: ' '"     
severity: "ERROR"     

As the response is empty I am getting the single key called "unit". Also at the end of any response I am getting this "unit":"ms" or "unit":"by" - is there any way to prevent that unit value coming in the response?

I am new to Google Cloud APIs and Python. What can I try next?


Solution

  • The "unit" field expresses the kind of resource the metric is counting. For bytes, it is "By". Read this. I understand it is always returned, so there is no way of not receiving it; I recommend you to adapt your code to correctly deal with its appearance in the responses.