Writing to custom timeseries with the Google Cloud Monitoring v3 api

Viewed 3299

Google has warned that the v2 monitoring api is now deprecated and will be going away soon. However, migrating to v3 is proving to be a bit difficult. I'm trying to write a custom metric and am getting the following error responses:

Services > Google Monitoring API v3 > monitoring.projects.timeSeries.create

{
    "timeSeries": [{
        "metric": {
            "type": "custom.googleapis.com/test_metric",
            "labels": {
                "payment_type": "Paypal"
            }
        },
        "resource": {
            "type": "custom.googleapis.com/test_metric",
            "labels": {
                "payment_type": "Paypal"
            }
        },
        "metricKind": "GAUGE",
        "valueType": "INT64",
        "points": [{
            "interval": {
                "endTime": "2016-03-20T15:01:23.045123456Z",
                "startTime": "2016-03-20T15:01:23.045123456Z"
            },
            "value": {
                "int64Value": "2"
            }
        }]
    }]
}

{
  "error": {
  "code": 400,
  "message": "Field timeSeries[0].resource.type had an invalid value of \"custom.googleapis.com/test_metric\": Unrecognized resource name.",
  "status": "INVALID_ARGUMENT"
}

The "resource" field is required, and docs say it's the "MonitoredResource"... but I don't see any api for creating one, only for listing. Taking a wild guess and setting it to "global" seems to get me a bit further and gives me this different error:

{
 "error": {
  "code": 400,
  "message": "Field timeSeries[0].resource.labels[0] had an invalid value of \"payment_type\": Unrecognized resource label.",
  "status": "INVALID_ARGUMENT"
 }
}

Listing the metric descriptors shows that payment_type exists:

Services > Google Monitoring API v3 > monitoring.projects.metricDescriptors.list

{
 "name": "projects/gearlaunch-hub-sandbox/metricDescriptors/custom.googleapis.com/test_metric",
 "labels": [
  {
   "key": "payment_type"
  }
 ],
 "metricKind": "GAUGE",
 "valueType": "INT64",
 "description": "Test",
 "type": "custom.googleapis.com/test_metric"
}

I've read through the migration guides and related docs, but am still stymied. Anyone know what I'm missing here?

Update: While it looks to be possible to get this working by removing "resource.labels" from the json, I'm still looking for a way to get this working via the java client api.

Update 2: The accepted (self answered) question shows how to do this with the java api.

2 Answers
Related